[PATCH 2 of 2] test-encoding: rewrite utf8b fuzz testing as python inlines

Yuya Nishihara yuya at tcha.org
Mon Nov 9 08:41:20 CST 2015


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1447076944 -32400
#      Mon Nov 09 22:49:04 2015 +0900
# Node ID 67bc69088f4fd921e9bc2353524ec0b2c61eb4bd
# Parent  a7e586cc8587d3e4a0bbf607d5b902a988cb5bc8
test-encoding: rewrite utf8b fuzz testing as python inlines

We generally prefer putting a test code and its result in the same file.

diff --git a/tests/hypothesishelpers.py b/tests/hypothesishelpers.py
--- a/tests/hypothesishelpers.py
+++ b/tests/hypothesishelpers.py
@@ -32,29 +32,6 @@ def check(*args, **kwargs):
             sys.exit(1)
     return accept
 
-
-def roundtrips(data, decode, encode):
-    """helper to tests function that must do proper encode/decode roundtripping
-    """
-    @given(data)
-    def testroundtrips(value):
-        encoded = encode(value)
-        decoded = decode(encoded)
-        if decoded != value:
-            raise ValueError(
-                "Round trip failed: %s(%r) -> %s(%r) -> %r" % (
-                    encode.__name__, value, decode.__name__, encoded,
-                    decoded
-                ))
-    try:
-        testroundtrips()
-    except Exception:
-        # heredoc swallow traceback, we work around it
-        traceback.print_exc(file=sys.stdout)
-        raise
-    print("Round trip OK")
-
-
 # strategy for generating bytestring that might be an issue for Mercurial
 bytestrings = (
     st.builds(lambda s, e: s.encode(e), st.text(), st.sampled_from([
diff --git a/tests/test-encoding.t b/tests/test-encoding.t
--- a/tests/test-encoding.t
+++ b/tests/test-encoding.t
@@ -279,7 +279,11 @@ Test roundtrip encoding/decoding of utf8
 
   >>> from hypothesishelpers import *
   >>> from mercurial import encoding
-  >>> roundtrips(st.binary(), encoding.fromutf8b, encoding.toutf8b)
-  Round trip OK
+  >>> @check(st.binary())
+  ... def roundtrips(val):
+  ...     enc = encoding.toutf8b(val)
+  ...     dec = encoding.fromutf8b(enc)
+  ...     assert dec == val, ('round trip failed: %r -> %r -> %r'
+  ...                         % (val, enc, dec))
 
 #endif


More information about the Mercurial-devel mailing list