Skip to content

Commit

Permalink
split misalignment test for more reliable coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tybug committed Mar 18, 2024
1 parent bd12c7d commit f9a28f0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2186,7 +2186,7 @@ def _pop_ir_tree_node(self, ir_type: IRTypeName, kwargs: IRKWargsType) -> IRNode
# that is allowed by the expected kwargs, then we can coerce this node
# into an aligned one by using its value. It's unclear how useful this is.
if not ir_value_permitted(node.value, node.ir_type, kwargs):
self.mark_invalid() # pragma: no cover # FIXME @tybug
self.mark_invalid()

return node

Expand Down
22 changes: 14 additions & 8 deletions hypothesis-python/tests/conjecture/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,18 @@ def draw_boolean_kwargs(draw, *, use_forced=False):
return {"p": p, "forced": forced}


def kwargs_strategy(ir_type):
return {
"boolean": draw_boolean_kwargs(),
"integer": draw_integer_kwargs(),
"float": draw_float_kwargs(),
"bytes": draw_bytes_kwargs(),
"string": draw_string_kwargs(),
}[ir_type]


def ir_types_and_kwargs():
options = [
("boolean", draw_boolean_kwargs()),
("integer", draw_integer_kwargs()),
("float", draw_float_kwargs()),
("bytes", draw_bytes_kwargs()),
("string", draw_string_kwargs()),
]
return st.one_of(st.tuples(st.just(name), kws) for name, kws in options)
options = ["boolean", "integer", "float", "bytes", "string"]
return st.one_of(
st.tuples(st.just(name), kwargs_strategy(name)) for name in options
)
30 changes: 22 additions & 8 deletions hypothesis-python/tests/conjecture/test_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from hypothesis.internal.floats import SMALLEST_SUBNORMAL, next_down, next_up
from hypothesis.internal.intervalsets import IntervalSet

from tests.conjecture.common import fresh_data, ir_types_and_kwargs
from tests.conjecture.common import fresh_data, ir_types_and_kwargs, kwargs_strategy


def draw_value(ir_type, kwargs):
Expand Down Expand Up @@ -366,19 +366,33 @@ def test_data_with_empty_ir_tree_is_overrun():


@given(st.data())
def test_data_with_misaligned_ir_tree_is_invalid(data):
@settings(suppress_health_check=[HealthCheck.too_slow])
def test_node_with_different_ir_type_is_invalid(data):
node = data.draw(ir_nodes())
(ir_type, kwargs) = data.draw(ir_types_and_kwargs())

# drawing a node with a different ir type should cause a misalignment.
assume(ir_type != node.ir_type)

data = ConjectureData.for_ir_tree([node])
draw_func = getattr(data, f"draw_{ir_type}")
# a misalignment occurs when we try and draw a node with a different ir
# type, or with the same ir type but a non-compatible value.
assume(
ir_type != node.ir_type
or not ir_value_permitted(node.value, node.ir_type, kwargs)
)
with pytest.raises(StopTest):
draw_func(**kwargs)

assert data.status is Status.INVALID


@given(st.data())
def test_node_with_same_ir_type_but_different_value_is_invalid(data):
node = data.draw(ir_nodes())
kwargs = data.draw(kwargs_strategy(node.ir_type))

# drawing a node with the same ir type, but a non-compatible value, should
# also cause a misalignment.
assume(not ir_value_permitted(node.value, node.ir_type, kwargs))

data = ConjectureData.for_ir_tree([node])
draw_func = getattr(data, f"draw_{node.ir_type}")
with pytest.raises(StopTest):
draw_func(**kwargs)

Expand Down

0 comments on commit f9a28f0

Please sign in to comment.