Skip to content

Commit 6968ab3

Browse files
committed
exact tests
1 parent 51cfe7c commit 6968ab3

File tree

1 file changed

+18
-36
lines changed

1 file changed

+18
-36
lines changed

tests/test_hypothesis_testing.py

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ def test_adds_derandomize_decorator():
1212
def test_x(x):
1313
assert isinstance(x, int)
1414
"""
15+
expected = """from hypothesis import settings\nfrom hypothesis import given, strategies as st\n\n@given(x=st.integers(min_value=-10000, max_value=10000))\n@settings(derandomize=True)\ndef test_x(x):\n assert isinstance(x, int)"""
1516
out = make_hypothesis_tests_deterministic(src)
16-
assert "@settings(derandomize=True)" in out or "settings(derandomize=True)" in out
17+
assert out == expected
1718

1819

1920
def test_integers_constrained_with_negatives():
@@ -23,11 +24,9 @@ def test_integers_constrained_with_negatives():
2324
def t(x):
2425
pass
2526
"""
27+
expected = """from hypothesis import settings\nfrom hypothesis import given, strategies as st\n\n@given(x=st.integers(min_value=-10000, max_value=10000))\n@settings(derandomize=True)\ndef t(x):\n pass"""
2628
out = make_hypothesis_tests_deterministic(src)
27-
# Remove spaces for easier checking
28-
normalized = out.replace(" ", "").replace("\n", "")
29-
assert "min_value=-10000" in normalized
30-
assert "max_value=10000" in normalized
29+
assert out == expected
3130

3231

3332
def test_floats_constrained_to_finite():
@@ -37,11 +36,9 @@ def test_floats_constrained_to_finite():
3736
def t(x):
3837
pass
3938
"""
39+
expected = """from hypothesis import settings\nfrom hypothesis import given, strategies as st\n\n@given(x=st.floats(min_value=-1000000.0, max_value=1000000.0, allow_nan=False, allow_infinity=False))\n@settings(derandomize=True)\ndef t(x):\n pass"""
4040
out = make_hypothesis_tests_deterministic(src)
41-
normalized = out.replace(" ", "").replace("\n", "")
42-
assert "allow_nan=False" in normalized
43-
assert "allow_infinity=False" in normalized
44-
assert "min_value=" in normalized and "max_value=" in normalized
41+
assert out == expected
4542

4643

4744
def test_existing_constraints_not_overridden():
@@ -53,14 +50,9 @@ def test_existing_constraints_not_overridden():
5350
def t(x):
5451
pass
5552
"""
53+
expected = """from hypothesis import given, strategies as st, settings\n\n@settings(derandomize=True, max_examples=5)\n@given(x=st.integers(min_value=-5, max_value=5))\ndef t(x):\n pass"""
5654
out = make_hypothesis_tests_deterministic(src)
57-
# Should not add duplicate settings decorator
58-
assert out.count("@settings") == 1
59-
# Should preserve original constraints
60-
assert "min_value=-5" in out or "min_value= -5" in out
61-
assert "max_value=5" in out or "max_value= 5" in out
62-
# Should not add the default -10000/10000 bounds
63-
assert "-10000" not in out
55+
assert out == expected
6456

6557

6658
def test_existing_float_constraints_preserved():
@@ -71,11 +63,9 @@ def test_existing_float_constraints_preserved():
7163
def t(y):
7264
pass
7365
"""
66+
expected = """from hypothesis import settings\nfrom hypothesis import given, strategies as st\n\n@given(y=st.floats(min_value=-1.0, max_value=1.0, allow_nan=False, allow_infinity=False))\n@settings(derandomize=True)\ndef t(y):\n pass"""
7467
out = make_hypothesis_tests_deterministic(src)
75-
assert "min_value=-1.0" in out or "min_value= -1.0" in out
76-
assert "max_value=1.0" in out or "max_value= 1.0" in out
77-
# Should not add the default 1e6 bounds
78-
assert "1e6" not in out and "1000000" not in out
68+
assert out == expected
7969

8070

8171
def test_idempotency():
@@ -99,14 +89,9 @@ def test_multiple_strategies_handled():
9989
def test_multi(a, b, c):
10090
pass
10191
"""
92+
expected = """from hypothesis import settings\nfrom hypothesis import given, strategies as st\n\n@given(a=st.integers(min_value=-10000, max_value=10000), b=st.integers(min_value=-10000, max_value=10000), c=st.floats(min_value=-1000000.0, max_value=1000000.0, allow_nan=False, allow_infinity=False))\n@settings(derandomize=True)\ndef test_multi(a, b, c):\n pass"""
10293
out = make_hypothesis_tests_deterministic(src)
103-
normalized = out.replace(" ", "").replace("\n", "")
104-
# All integers should be constrained
105-
assert normalized.count("min_value=-10000") >= 2
106-
assert normalized.count("max_value=10000") >= 2
107-
# Float should be constrained
108-
assert "allow_nan=False" in normalized
109-
assert "allow_infinity=False" in normalized
94+
assert out == expected
11095

11196

11297
def test_settings_import_added_if_missing():
@@ -117,9 +102,9 @@ def test_settings_import_added_if_missing():
117102
def test_x(x):
118103
pass
119104
"""
105+
expected = """from hypothesis import settings\nfrom hypothesis import given, strategies as st\n\n@given(x=st.integers(min_value=-10000, max_value=10000))\n@settings(derandomize=True)\ndef test_x(x):\n pass"""
120106
out = make_hypothesis_tests_deterministic(src)
121-
# Should have settings import or settings in existing import
122-
assert "settings" in out
107+
assert out == expected
123108

124109

125110
def test_partial_constraints_completed():
@@ -130,11 +115,9 @@ def test_partial_constraints_completed():
130115
def test_x(x):
131116
pass
132117
"""
118+
expected = """from hypothesis import settings\nfrom hypothesis import given, strategies as st\n\n@given(x=st.integers(min_value=100))\n@settings(derandomize=True)\ndef test_x(x):\n pass"""
133119
out = make_hypothesis_tests_deterministic(src)
134-
# Should keep the min_value=100 and not override
135-
assert "min_value=100" in out or "min_value= 100" in out
136-
# Should not add default bounds since min_value exists
137-
assert "-10000" not in out
120+
assert out == expected
138121

139122

140123
def test_syntax_error_returns_original():
@@ -152,7 +135,6 @@ def test_no_hypothesis_code_unchanged():
152135
def test_regular():
153136
assert regular_function(2) == 4
154137
"""
138+
expected = """from hypothesis import settings\n\n@settings(derandomize=True)\ndef regular_function(x):\n return x * 2\n\n@settings(derandomize=True)\ndef test_regular():\n assert regular_function(2) == 4"""
155139
out = make_hypothesis_tests_deterministic(src)
156-
# Should still parse and return valid code
157-
assert "def regular_function" in out
158-
assert "def test_regular" in out
140+
assert out == expected

0 commit comments

Comments
 (0)