Mercurial > hg
comparison tests/hypothesishelpers.py @ 43076:2372284d9457
formatting: blacken the codebase
This is using my patch to black
(https://github.com/psf/black/pull/826) so we don't un-wrap collection
literals.
Done with:
hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S
# skip-blame mass-reformatting only
# no-check-commit reformats foo_bar functions
Differential Revision: https://phab.mercurial-scm.org/D6971
author | Augie Fackler <augie@google.com> |
---|---|
date | Sun, 06 Oct 2019 09:45:02 -0400 |
parents | 8699c89f3ae9 |
children | 89a2afe31e82 |
comparison
equal
deleted
inserted
replaced
43075:57875cf423c9 | 43076:2372284d9457 |
---|---|
19 from hypothesis import Settings as settings | 19 from hypothesis import Settings as settings |
20 import hypothesis.strategies as st | 20 import hypothesis.strategies as st |
21 from hypothesis import given | 21 from hypothesis import given |
22 | 22 |
23 # hypothesis store data regarding generate example and code | 23 # hypothesis store data regarding generate example and code |
24 set_hypothesis_home_dir(os.path.join( | 24 set_hypothesis_home_dir(os.path.join(os.getenv('TESTTMP'), ".hypothesis")) |
25 os.getenv('TESTTMP'), ".hypothesis" | 25 |
26 )) | |
27 | 26 |
28 def check(*args, **kwargs): | 27 def check(*args, **kwargs): |
29 """decorator to make a function a hypothesis test | 28 """decorator to make a function a hypothesis test |
30 | 29 |
31 Decorated function are run immediately (to be used doctest style)""" | 30 Decorated function are run immediately (to be used doctest style)""" |
31 | |
32 def accept(f): | 32 def accept(f): |
33 # Workaround for https://github.com/DRMacIver/hypothesis/issues/206 | 33 # Workaround for https://github.com/DRMacIver/hypothesis/issues/206 |
34 # Fixed in version 1.13 (released 2015 october 29th) | 34 # Fixed in version 1.13 (released 2015 october 29th) |
35 f.__module__ = '__anon__' | 35 f.__module__ = '__anon__' |
36 try: | 36 try: |
37 with settings(max_examples=2000): | 37 with settings(max_examples=2000): |
38 given(*args, **kwargs)(f)() | 38 given(*args, **kwargs)(f)() |
39 except Exception: | 39 except Exception: |
40 traceback.print_exc(file=sys.stdout) | 40 traceback.print_exc(file=sys.stdout) |
41 sys.exit(1) | 41 sys.exit(1) |
42 | |
42 return accept | 43 return accept |
43 | 44 |
44 | 45 |
45 def roundtrips(data, decode, encode): | 46 def roundtrips(data, decode, encode): |
46 """helper to tests function that must do proper encode/decode roundtripping | 47 """helper to tests function that must do proper encode/decode roundtripping |
47 """ | 48 """ |
49 | |
48 @given(data) | 50 @given(data) |
49 def testroundtrips(value): | 51 def testroundtrips(value): |
50 encoded = encode(value) | 52 encoded = encode(value) |
51 decoded = decode(encoded) | 53 decoded = decode(encoded) |
52 if decoded != value: | 54 if decoded != value: |
53 raise ValueError( | 55 raise ValueError( |
54 "Round trip failed: %s(%r) -> %s(%r) -> %r" % ( | 56 "Round trip failed: %s(%r) -> %s(%r) -> %r" |
55 encode.__name__, value, decode.__name__, encoded, | 57 % (encode.__name__, value, decode.__name__, encoded, decoded) |
56 decoded | 58 ) |
57 )) | 59 |
58 try: | 60 try: |
59 testroundtrips() | 61 testroundtrips() |
60 except Exception: | 62 except Exception: |
61 # heredoc swallow traceback, we work around it | 63 # heredoc swallow traceback, we work around it |
62 traceback.print_exc(file=sys.stdout) | 64 traceback.print_exc(file=sys.stdout) |
64 print("Round trip OK") | 66 print("Round trip OK") |
65 | 67 |
66 | 68 |
67 # strategy for generating bytestring that might be an issue for Mercurial | 69 # strategy for generating bytestring that might be an issue for Mercurial |
68 bytestrings = ( | 70 bytestrings = ( |
69 st.builds(lambda s, e: s.encode(e), st.text(), st.sampled_from([ | 71 st.builds( |
70 'utf-8', 'utf-16', | 72 lambda s, e: s.encode(e), |
71 ]))) | st.binary() | 73 st.text(), |
74 st.sampled_from(['utf-8', 'utf-16',]), | |
75 ) | |
76 ) | st.binary() |