1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Copyright the Hypothesis Authors.
# Individual contributors are listed in AUTHORS.rst and the git log.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.
from collections import namedtuple
import pytest
from hypothesis import settings as Settings
from hypothesis.stateful import Bundle, RuleBasedStateMachine, precondition, rule
from hypothesis.strategies import booleans, integers, lists
Leaf = namedtuple("Leaf", ("label",))
Split = namedtuple("Split", ("left", "right"))
class BalancedTrees(RuleBasedStateMachine):
trees = Bundle("BinaryTree")
@rule(target=trees, x=booleans())
def leaf(self, x):
return Leaf(x)
@rule(target=trees, left=trees, right=trees)
def split(self, left, right):
return Split(left, right)
@rule(tree=trees)
def test_is_balanced(self, tree):
if isinstance(tree, Leaf):
return
else:
assert abs(self.size(tree.left) - self.size(tree.right)) <= 1
self.test_is_balanced(tree.left)
self.test_is_balanced(tree.right)
def size(self, tree):
if isinstance(tree, Leaf):
return 1
else:
return 1 + self.size(tree.left) + self.size(tree.right)
class DepthCharge:
def __init__(self, value):
if value is None:
self.depth = 0
else:
self.depth = value.depth + 1
class DepthMachine(RuleBasedStateMachine):
charges = Bundle("charges")
@rule(targets=(charges,), child=charges)
def charge(self, child):
return DepthCharge(child)
@rule(targets=(charges,))
def none_charge(self):
return DepthCharge(None)
@rule(check=charges)
def is_not_too_deep(self, check):
assert check.depth < 3
class RoseTreeStateMachine(RuleBasedStateMachine):
nodes = Bundle("nodes")
@rule(target=nodes, source=lists(nodes))
def bunch(self, source):
return source
@rule(source=nodes)
def shallow(self, source):
def d(ls):
if not ls:
return 0
else:
return 1 + max(map(d, ls))
assert d(source) <= 5
class NotTheLastMachine(RuleBasedStateMachine):
stuff = Bundle("stuff")
def __init__(self):
super().__init__()
self.last = None
self.bye_called = False
@rule(target=stuff)
def hi(self):
result = object()
self.last = result
return result
@precondition(lambda self: not self.bye_called)
@rule(v=stuff)
def bye(self, v):
assert v == self.last
self.bye_called = True
class PopulateMultipleTargets(RuleBasedStateMachine):
b1 = Bundle("b1")
b2 = Bundle("b2")
@rule(targets=(b1, b2))
def populate(self):
return 1
@rule(x=b1, y=b2)
def fail(self, x, y):
raise AssertionError
class CanSwarm(RuleBasedStateMachine):
"""This test will essentially never pass if you choose rules uniformly at
random, because every time the snake rule fires we return to the beginning,
so we will tend to undo progress well before we make enough progress for
the test to fail.
This tests our swarm testing functionality in stateful testing by ensuring
that we can sometimes generate long runs of steps which exclude a
particular rule.
"""
def __init__(self):
super().__init__()
self.seen = set()
# The reason this rule takes a parameter is that it ensures that we do not
# achieve "swarming" by by just restricting the alphabet for single byte
# decisions, which is a thing the underlying conjecture engine will
# happily do on its own without knowledge of the rule structure.
@rule(move=integers(0, 255))
def ladder(self, move):
self.seen.add(move)
assert len(self.seen) <= 15
@rule()
def snake(self):
self.seen.clear()
bad_machines = (
BalancedTrees,
DepthMachine,
RoseTreeStateMachine,
NotTheLastMachine,
PopulateMultipleTargets,
CanSwarm,
)
for m in bad_machines:
m.TestCase.settings = Settings(m.TestCase.settings, max_examples=1000)
cheap_bad_machines = list(bad_machines)
cheap_bad_machines.remove(BalancedTrees)
with_cheap_bad_machines = pytest.mark.parametrize(
"machine", cheap_bad_machines, ids=[t.__name__ for t in cheap_bad_machines]
)
@pytest.mark.parametrize(
"machine", bad_machines, ids=[t.__name__ for t in bad_machines]
)
def test_bad_machines_fail(machine):
test_class = machine.TestCase
try:
test_class().runTest()
raise RuntimeError("Expected an assertion error")
except AssertionError as err:
notes = err.__notes__
steps = [l for l in notes if "Step " in l or "state." in l]
assert 1 <= len(steps) <= 50
|