##// END OF EJS Templates
tests: adjust for code move in Hypothesis 2.0.0...
Yuya Nishihara -
r28362:b7e19224 stable
parent child Browse files
Show More
@@ -1,62 +1,69 b''
1 # Helper module to use the Hypothesis tool in tests
1 # Helper module to use the Hypothesis tool in tests
2 #
2 #
3 # Copyright 2015 David R. MacIver
3 # Copyright 2015 David R. MacIver
4 #
4 #
5 # For details see http://hypothesis.readthedocs.org
5 # For details see http://hypothesis.readthedocs.org
6
6
7 import os
7 import os
8 import sys
8 import sys
9 import traceback
9 import traceback
10
10
11 from hypothesis.settings import set_hypothesis_home_dir
11 try:
12 # hypothesis 2.x
13 from hypothesis.configuration import set_hypothesis_home_dir
14 from hypothesis import settings
15 except ImportError:
16 # hypothesis 1.x
17 from hypothesis.settings import set_hypothesis_home_dir
18 from hypothesis import Settings as settings
12 import hypothesis.strategies as st
19 import hypothesis.strategies as st
13 from hypothesis import given, Settings
20 from hypothesis import given
14
21
15 # hypothesis store data regarding generate example and code
22 # hypothesis store data regarding generate example and code
16 set_hypothesis_home_dir(os.path.join(
23 set_hypothesis_home_dir(os.path.join(
17 os.getenv('TESTTMP'), ".hypothesis"
24 os.getenv('TESTTMP'), ".hypothesis"
18 ))
25 ))
19
26
20 def check(*args, **kwargs):
27 def check(*args, **kwargs):
21 """decorator to make a function a hypothesis test
28 """decorator to make a function a hypothesis test
22
29
23 Decorated function are run immediately (to be used doctest style)"""
30 Decorated function are run immediately (to be used doctest style)"""
24 def accept(f):
31 def accept(f):
25 # Workaround for https://github.com/DRMacIver/hypothesis/issues/206
32 # Workaround for https://github.com/DRMacIver/hypothesis/issues/206
26 # Fixed in version 1.13 (released 2015 october 29th)
33 # Fixed in version 1.13 (released 2015 october 29th)
27 f.__module__ = '__anon__'
34 f.__module__ = '__anon__'
28 try:
35 try:
29 given(*args, settings=Settings(max_examples=2000), **kwargs)(f)()
36 given(*args, settings=settings(max_examples=2000), **kwargs)(f)()
30 except Exception:
37 except Exception:
31 traceback.print_exc(file=sys.stdout)
38 traceback.print_exc(file=sys.stdout)
32 sys.exit(1)
39 sys.exit(1)
33 return accept
40 return accept
34
41
35
42
36 def roundtrips(data, decode, encode):
43 def roundtrips(data, decode, encode):
37 """helper to tests function that must do proper encode/decode roundtripping
44 """helper to tests function that must do proper encode/decode roundtripping
38 """
45 """
39 @given(data)
46 @given(data)
40 def testroundtrips(value):
47 def testroundtrips(value):
41 encoded = encode(value)
48 encoded = encode(value)
42 decoded = decode(encoded)
49 decoded = decode(encoded)
43 if decoded != value:
50 if decoded != value:
44 raise ValueError(
51 raise ValueError(
45 "Round trip failed: %s(%r) -> %s(%r) -> %r" % (
52 "Round trip failed: %s(%r) -> %s(%r) -> %r" % (
46 encode.__name__, value, decode.__name__, encoded,
53 encode.__name__, value, decode.__name__, encoded,
47 decoded
54 decoded
48 ))
55 ))
49 try:
56 try:
50 testroundtrips()
57 testroundtrips()
51 except Exception:
58 except Exception:
52 # heredoc swallow traceback, we work around it
59 # heredoc swallow traceback, we work around it
53 traceback.print_exc(file=sys.stdout)
60 traceback.print_exc(file=sys.stdout)
54 raise
61 raise
55 print("Round trip OK")
62 print("Round trip OK")
56
63
57
64
58 # strategy for generating bytestring that might be an issue for Mercurial
65 # strategy for generating bytestring that might be an issue for Mercurial
59 bytestrings = (
66 bytestrings = (
60 st.builds(lambda s, e: s.encode(e), st.text(), st.sampled_from([
67 st.builds(lambda s, e: s.encode(e), st.text(), st.sampled_from([
61 'utf-8', 'utf-16',
68 'utf-8', 'utf-16',
62 ]))) | st.binary()
69 ]))) | st.binary()
General Comments 0
You need to be logged in to leave comments. Login now