Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
reaganjlee committed Oct 17, 2024
1 parent 90f4360 commit 0194f90
Showing 1 changed file with 44 additions and 9 deletions.
53 changes: 44 additions & 9 deletions hypothesis-python/tests/cover/test_stateful.py
Original file line number Diff line number Diff line change
Expand Up @@ -1434,25 +1434,26 @@ def use_flatmap(self, bun):

@rule(
target=buns,
bun=buns.flatmap(lambda x: just(x + 1)).filter(lambda x: x > 0),
bun=buns.flatmap(lambda x: just(-x)).filter(lambda x: x < -1),
)
def use_flatmap_filtered(self, bun):
assert isinstance(bun, int)
assert bun > 0
return bun
assert bun < -1
return -bun

@rule(
target=buns,
bun=buns.flatmap(lambda x: just(x + 1)).map(lambda x: -(x + 1)),
bun=buns.flatmap(lambda x: just(x + 1)).map(lambda x: -x),
)
def use_flatmap_mapped(self, bun):
assert isinstance(bun, int)
assert bun < 0
return bun
return -bun

@rule(bun=buns)
def use_directly(self, bun):
assert isinstance(bun, int)
assert bun >= 0

Machine.TestCase.settings = Settings(stateful_step_count=5, max_examples=10)
run_state_machine_as_test(Machine)
Expand All @@ -1479,7 +1480,7 @@ def use_flatmap_filtered(self, bun):
assert bun < -1

@rule(
bun=buns.map(lambda x: -x).flatmap(lambda x: just(abs(x))),
bun=buns.map(lambda x: -x).flatmap(lambda x: just(abs(x) + 1)),
)
def use_flatmap_mapped(self, bun):
assert isinstance(bun, int)
Expand All @@ -1500,7 +1501,7 @@ class Machine(RuleBasedStateMachine):

@initialize(target=buns)
def create_bun(self):
return multiple(0, 1, 2)
return multiple(0, -1, -2)

@rule(bun=buns.filter(lambda x: x > 0))
def use_filter_base(self, bun):
Expand All @@ -1515,15 +1516,49 @@ def use_filter_flatmapped(self, bun):
assert bun < 0

@rule(
bun=buns.filter(lambda x: x > 0).map(lambda x: -x),
bun=buns.filter(lambda x: x < 0).map(lambda x: -x),
)
def use_flatmap_mapped(self, bun):
assert isinstance(bun, int)
assert bun < 0
assert bun > 0

@rule(bun=buns)
def use_directly(self, bun):
assert isinstance(bun, int)

Machine.TestCase.settings = Settings(stateful_step_count=5, max_examples=10)
run_state_machine_as_test(Machine)


def test_mapped_values_assigned_properly():
class Machine(RuleBasedStateMachine):
a = Bundle("a")

@initialize(target=a)
def initialize(self):
return multiple("ret1", "ret2")

@rule(
a1=a,
a2=a.map(lambda x: x + x),
a3=consumes(a).map(lambda x: x + x),
a4=a,
)
def fail_fast(self, a1, a2, a3, a4):
raise AssertionError

Machine.TestCase.settings = NO_BLOB_SETTINGS
with pytest.raises(AssertionError) as err:
run_state_machine_as_test(Machine)

result = "\n".join(err.value.__notes__)
assert (
result
== """
Falsifying example:
state = Machine()
a_0, a_1 = state.initialize()
state.fail_fast(a1=a_1, a2=a_1, a3=a_1, a4=a_0)
state.teardown()
""".strip()
)

0 comments on commit 0194f90

Please sign in to comment.