Esempio n. 1
0
class MultipleRulesSameFuncMachine(RuleBasedStateMachine):

    def myfunc(self, data):
        print_unicode(data)

    rule1 = rule(data=just(u"rule1data"))(myfunc)
    rule2 = rule(data=just(u"rule2data"))(myfunc)
Esempio n. 2
0
def _generate_state_machine(rules_object: type) -> type:

    bases = (_BrownieStateMachine, rules_object, sf.RuleBasedStateMachine)
    machine = type("BrownieStateMachine", bases, {})
    strategies = {
        k: v
        for k, v in getmembers(rules_object) if isinstance(v, SearchStrategy)
    }

    for attr, fn in filter(_member_filter, getmembers(machine)):
        varnames = [[i]
                    for i in fn.__code__.co_varnames[1:fn.__code__.co_argcount]
                    ]
        if fn.__defaults__:
            for i in range(-1, -1 - len(fn.__defaults__), -1):
                varnames[i].append(fn.__defaults__[i])

        if _attr_filter(attr, "initialize"):
            wrapped = sf.initialize(
                **{key[0]: strategies[key[-1]]
                   for key in varnames})
            setattr(machine, attr, wrapped(fn))
        elif _attr_filter(attr, "invariant"):
            setattr(machine, attr, sf.invariant()(fn))
        elif _attr_filter(attr, "rule"):
            wrapped = sf.rule(
                **{key[0]: strategies[key[-1]]
                   for key in varnames})
            setattr(machine, attr, wrapped(fn))

    return machine
Esempio n. 3
0
def make_rule(endpoint: "Endpoint", bundle: Bundle, connections: EndpointConnections) -> Rule:
    """Create a rule for an endpoint."""
    previous_strategies = connections.get(endpoint.verbose_name)
    if previous_strategies is not None:
        previous = _combine_strategies(previous_strategies)
    else:
        previous = none()
    return rule(target=bundle, previous=previous, case=endpoint.as_strategy())(APIStateMachine.step)  # type: ignore
Esempio n. 4
0
 def gen_rules(cls, cfg):
     for topic, properties in cfg.subscribers.iteritems():
         rule_name = "pub" + topic.replace("/", "__")
         method = RosRandomTester._gen_publish(topic, rule_name)
         pre_wrapper = precondition(cls._rule_precondition)
         rule_wrapper = rule(msg=properties._strategy())
         method = pre_wrapper(rule_wrapper(method))
         method.__name__ = rule_name
         setattr(cls, rule_name, method)
Esempio n. 5
0
def test_rule_deprecation_targets_and_target():
    k, v = Bundle("k"), Bundle("v")
    rule(targets=(k, ), target=v)
Esempio n. 6
0
def test_rule_non_bundle_target():
    with pytest.raises(InvalidArgument):
        rule(target=integers())
Esempio n. 7
0
def test_rule_non_bundle_target_oneof():
    k, v = Bundle('k'), Bundle('v')
    pattern = r'.+ `one_of(a, b)` or `a | b` .+'
    with pytest.raises(InvalidArgument, match=pattern):
        rule(target=k | v)
Esempio n. 8
0
def test_rule_deprecation_targets_and_target():
    k, v = Bundle('k'), Bundle('v')
    rule(targets=(k,), target=v)
Esempio n. 9
0
def test_rule_deprecation_bundle_by_name():
    Bundle('k')
    rule(target='k')
Esempio n. 10
0
def test_rule_non_bundle_target_oneof():
    k, v = Bundle("k"), Bundle("v")
    pattern = r".+ `one_of(a, b)` or `a | b` .+"
    with pytest.raises(InvalidArgument, match=pattern):
        rule(target=k | v)
Esempio n. 11
0
def test_rule_deprecation_targets_and_target():
    k, v = Bundle('k'), Bundle('v')
    rule(targets=(k, ), target=v)
Esempio n. 12
0
def test_deprecated_target_consumes_bundle():
    # It would be nicer to raise this error at runtime, but the internals make
    # this sadly impractical.  Most InvalidDefinition errors happen at, well,
    # definition-time already anyway, so it's not *worse* than the status quo.
    with validate_deprecation():
        rule(target=consumes(Bundle("b")))
Esempio n. 13
0
 def _make_rule(previous: SearchStrategy) -> Rule:
     decorator = rule(target=bundle,
                      previous=previous,
                      case=operation.as_strategy())  # type: ignore
     return decorator(APIStateMachine._step)
def test_rule_deprecation_bundle_by_name():
    Bundle("k")
    rule(target="k")
Esempio n. 15
0
        @precondition(lambda _: False)
        @invariant()
        def another_invariant(self):
            raise ValueError()

        @rule()
        def do_stuff(self):
            pass

    run_state_machine_as_test(Invariant)


@pytest.mark.parametrize(
    "decorators",
    [
        (invariant(), rule()),
        (rule(), invariant()),
        (invariant(), initialize()),
        (initialize(), invariant()),
        (invariant(), precondition(lambda self: True), rule()),
        (rule(), precondition(lambda self: True), invariant()),
        (precondition(lambda self: True), invariant(), rule()),
        (precondition(lambda self: True), rule(), invariant()),
    ],
    ids=lambda x: "-".join(f.__qualname__.split(".")[0] for f in x),
)
def test_invariant_and_rule_are_incompatible(decorators):
    """It's an error to apply @invariant and @rule to the same method."""

    def method(self):
        pass
def test_rule_deprecation_targets_and_target():
    k, v = Bundle("k"), Bundle("v")
    rule(targets=(k,), target=v)
Esempio n. 17
0
class ReturningInvariantMachine(RuleBasedStateMachine):
    _ = rule()(lambda self: None)

    @invariant(check_during_init=True)
    def r(self):
        return "any non-None value"
Esempio n. 18
0
def test_rule_deprecation_bundle_by_name():
    Bundle('k')
    rule(target='k')
Esempio n. 19
0
def test_rule_deprecation_bundle_by_name():
    Bundle("k")
    rule(target="k")
Esempio n. 20
0
def test_rule_deprecation_bundle_by_name():
    Bundle("k")
    with pytest.raises(InvalidArgument):
        rule(target="k")
Esempio n. 21
0
def test_rule_non_bundle_target():
    with pytest.raises(InvalidArgument):
        rule(target=integers())
Esempio n. 22
0
class ReturningInitializeMachine(RuleBasedStateMachine):
    _ = rule()(lambda self: None)

    @initialize()
    def r(self):
        return "any non-None value"
Esempio n. 23
0
def test_rule_deprecation_targets_and_target():
    k, v = Bundle("k"), Bundle("v")
    with pytest.raises(InvalidArgument):
        rule(targets=(k,), target=v)
Esempio n. 24
0
def srulep(precond=lambda s: True, **kwargs):
    return compose(precondition(precond), rule(**kwargs), stateful)