Skip to content

Commit f9a28f0

Browse files
committed
split misalignment test for more reliable coverage
1 parent bd12c7d commit f9a28f0

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

hypothesis-python/src/hypothesis/internal/conjecture/data.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2186,7 +2186,7 @@ def _pop_ir_tree_node(self, ir_type: IRTypeName, kwargs: IRKWargsType) -> IRNode
21862186
# that is allowed by the expected kwargs, then we can coerce this node
21872187
# into an aligned one by using its value. It's unclear how useful this is.
21882188
if not ir_value_permitted(node.value, node.ir_type, kwargs):
2189-
self.mark_invalid() # pragma: no cover # FIXME @tybug
2189+
self.mark_invalid()
21902190

21912191
return node
21922192

hypothesis-python/tests/conjecture/common.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,18 @@ def draw_boolean_kwargs(draw, *, use_forced=False):
277277
return {"p": p, "forced": forced}
278278

279279

280+
def kwargs_strategy(ir_type):
281+
return {
282+
"boolean": draw_boolean_kwargs(),
283+
"integer": draw_integer_kwargs(),
284+
"float": draw_float_kwargs(),
285+
"bytes": draw_bytes_kwargs(),
286+
"string": draw_string_kwargs(),
287+
}[ir_type]
288+
289+
280290
def ir_types_and_kwargs():
281-
options = [
282-
("boolean", draw_boolean_kwargs()),
283-
("integer", draw_integer_kwargs()),
284-
("float", draw_float_kwargs()),
285-
("bytes", draw_bytes_kwargs()),
286-
("string", draw_string_kwargs()),
287-
]
288-
return st.one_of(st.tuples(st.just(name), kws) for name, kws in options)
291+
options = ["boolean", "integer", "float", "bytes", "string"]
292+
return st.one_of(
293+
st.tuples(st.just(name), kwargs_strategy(name)) for name in options
294+
)

hypothesis-python/tests/conjecture/test_ir.py

+22-8
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from hypothesis.internal.floats import SMALLEST_SUBNORMAL, next_down, next_up
3131
from hypothesis.internal.intervalsets import IntervalSet
3232

33-
from tests.conjecture.common import fresh_data, ir_types_and_kwargs
33+
from tests.conjecture.common import fresh_data, ir_types_and_kwargs, kwargs_strategy
3434

3535

3636
def draw_value(ir_type, kwargs):
@@ -366,19 +366,33 @@ def test_data_with_empty_ir_tree_is_overrun():
366366

367367

368368
@given(st.data())
369-
def test_data_with_misaligned_ir_tree_is_invalid(data):
369+
@settings(suppress_health_check=[HealthCheck.too_slow])
370+
def test_node_with_different_ir_type_is_invalid(data):
370371
node = data.draw(ir_nodes())
371372
(ir_type, kwargs) = data.draw(ir_types_and_kwargs())
372373

374+
# drawing a node with a different ir type should cause a misalignment.
375+
assume(ir_type != node.ir_type)
376+
373377
data = ConjectureData.for_ir_tree([node])
374378
draw_func = getattr(data, f"draw_{ir_type}")
375-
# a misalignment occurs when we try and draw a node with a different ir
376-
# type, or with the same ir type but a non-compatible value.
377-
assume(
378-
ir_type != node.ir_type
379-
or not ir_value_permitted(node.value, node.ir_type, kwargs)
380-
)
379+
with pytest.raises(StopTest):
380+
draw_func(**kwargs)
381+
382+
assert data.status is Status.INVALID
381383

384+
385+
@given(st.data())
386+
def test_node_with_same_ir_type_but_different_value_is_invalid(data):
387+
node = data.draw(ir_nodes())
388+
kwargs = data.draw(kwargs_strategy(node.ir_type))
389+
390+
# drawing a node with the same ir type, but a non-compatible value, should
391+
# also cause a misalignment.
392+
assume(not ir_value_permitted(node.value, node.ir_type, kwargs))
393+
394+
data = ConjectureData.for_ir_tree([node])
395+
draw_func = getattr(data, f"draw_{node.ir_type}")
382396
with pytest.raises(StopTest):
383397
draw_func(**kwargs)
384398

0 commit comments

Comments
 (0)