예제 #1
0
def test_single_rule(SMTestBase):
    class StateMachine(SMTestBase):
        def rule(self):
            assert False

    with pytest.raises(AssertionError):
        state_machine(StateMachine, settings={"max_examples": 5})
예제 #2
0
def test_teardown_final_called(SMTestBase):
    class StateMachine(SMTestBase):
        def teardown_final(self):
            raise AssertionError("I am a potato")

    with pytest.raises(AssertionError, match="I am a potato"):
        state_machine(StateMachine, settings={"max_examples": 5})
예제 #3
0
def test_teardown_final_only_called_once(SMTestBase):
    class StateMachine(SMTestBase):
        def teardown_final(self):
            assert not hasattr(self, "foo")
            self.foo = "foo"

    state_machine(StateMachine, settings={"max_examples": 5})
예제 #4
0
def test_rpc_reverts_between_runs(SMTestBase, accounts, web3):
    class StateMachine(SMTestBase):
        def initialize_one(self):
            assert web3.eth.blockNumber == 1
            accounts[0].transfer(accounts[1], 100)

    accounts[0].transfer(accounts[1], 100)
    state_machine(StateMachine, settings={"max_examples": 5})
예제 #5
0
def test_init_receives_args_and_kwargs(SMTestBase):
    class StateMachine(SMTestBase):
        def __init__(self, foo, bar=42, baz=31337):
            assert foo == 11
            assert bar in (32, 42)
            assert baz in (69, 31337)

    state_machine(StateMachine, 11, baz=69, settings={"max_examples": 2})
예제 #6
0
def test_misc_methods(SMTestBase):
    class StateMachine(SMTestBase):
        def foo(self):
            pass

        def bar(self):
            pass

    state_machine(StateMachine, settings={"max_examples": 5})
예제 #7
0
def test_strategy_injection_fails(SMTestBase):
    class StateMachine(SMTestBase):
        st_int = strategy("uint8")

        def invariant_one(self, st_int):
            pass

    with pytest.raises(TypeError):
        state_machine(StateMachine, settings={"max_examples": 5})
예제 #8
0
def test_init_only_called_once(SMTestBase):
    class StateMachine(SMTestBase):
        count = 0

        def __init__(self):
            assert not self.count
            self.count += 1

    state_machine(StateMachine, settings={"max_examples": 5})
예제 #9
0
def test_existing_decorators(SMTestBase):
    class StateMachine(SMTestBase):
        @sf.invariant()
        def invariant_one(self):
            pass

        @sf.invariant()
        def initialize_horrible_name_for_invariant(self):
            pass

    state_machine(StateMachine, settings={"max_examples": 5})
예제 #10
0
def test_existing_decorators(SMTestBase):
    class StateMachine(SMTestBase):
        @sf.rule(st_int=strategy("uint8"))
        def rule_one(self, st_int):
            pass

        @sf.rule()
        def invariant_horrible_name_for_a_rule(self):
            pass

    state_machine(StateMachine, settings={"max_examples": 5})
예제 #11
0
def test_single_invariant(SMTestBase):
    class StateMachine(SMTestBase):
        foo = "foo"

    def invariant(self):
        self.foo = "bar"

    def teardown(self):
        assert self.foo == "bar"

    state_machine(StateMachine, settings={"max_examples": 5})
예제 #12
0
def test_setup_called_on_each_run(SMTestBase):
    class StateMachine(SMTestBase):
        foo = "foo"

        def setup(self):
            assert self.foo == "foo"
            self.foo = "bar"

        def teardown(self):
            assert self.foo == "bar"

    state_machine(StateMachine, settings={"max_examples": 5})
예제 #13
0
def test_teardown_called_on_each_run(SMTestBase):
    class StateMachine(SMTestBase):
        setup_count = 0
        teardown_count = 0

        def setup(self):
            type(self).setup_count += 1
            assert self.teardown_count == self.setup_count - 1

        def teardown(self):
            type(self).teardown_count += 1

    state_machine(StateMachine, settings={"max_examples": 5})
예제 #14
0
def test_rules_run(SMTestBase):
    class StateMachine(SMTestBase):
        def __init__(self, value):
            self.value = value

        def rule_one(self):
            assert self.value

        def rule_two(self):
            assert not self.value

    with pytest.raises(AssertionError):
        state_machine(StateMachine, True, settings={"max_examples": 5})

    with pytest.raises(AssertionError):
        state_machine(StateMachine, False, settings={"max_examples": 5})
예제 #15
0
def test_invariants_always_run(SMTestBase):
    class StateMachine(SMTestBase):
        def __init__(self):
            self.counts = [0, 0]

    def invariant_one(self):
        self.counts[0] += 1

    def invariant_two(self):
        self.counts[1] += 1

    def teardown(self):
        assert self.counts[0] > 0
        assert self.counts[0] == self.counts[1]

    state_machine(StateMachine, settings={"max_examples": 5})
예제 #16
0
def test_strategy_injection(SMTestBase):
    class StateMachine(SMTestBase):
        st_int = strategy("uint8")
        st_bool = strategy("bool")
        foobar = strategy("bytes4")

        def rule_one(self, st_int):
            assert type(st_int) is int
            assert 0 <= st_int <= 255

        def rule_two(self, st_bool, foobar):
            assert type(st_bool) is bool
            assert type(foobar) is bytes

        def rule_three(self, foo="st_bool"):
            assert type(foo) is bool

    state_machine(StateMachine, settings={"max_examples": 5})
예제 #17
0
def test_base_class_isolated(SMTestBase):
    # changes to class attributes while running a state machine
    # should not affect the original class
    class StateMachine(SMTestBase):
        value = 42

        def __init__(self):
            self.value = 31337

        def rule_one(self):
            type(self).value = 31337

        def rule_two(self):
            type(self).value = 31337

        def teardown(self):
            assert type(self).value == 31337

    assert StateMachine.value == 42
    state_machine(StateMachine, settings={"max_examples": 5})
    assert StateMachine.value == 42
예제 #18
0
def test_init_runs_on_class(SMTestBase):
    class StateMachine(SMTestBase):
        def __init__(self):
            assert type(self) is StateMachineMeta

    state_machine(StateMachine, settings={"max_examples": 2})
예제 #19
0
def test_decoration(SMTestBase):
    class StateMachine(SMTestBase):
        rule_trap = "potato"
        rule_othertrap = strategy("bool")

    state_machine(StateMachine, settings={"max_examples": 5})
예제 #20
0
def test_without_init(SMTestBase):

    state_machine(SMTestBase, settings={"max_examples": 2})