glyph / automat

Self-service finite-state machines for the programmer on the go.
MIT License
591 stars 65 forks source link

Test helper that asserts all transitions occurred #38

Open markrwilliams opened 7 years ago

markrwilliams commented 7 years ago

Automat should have a test helper that asserts all transitions occurred.

It might go a little something like this:

tracer = TraceAllTransitions()
someMachineInstance.traceWith(tracer)

# testing intensifies

assertHasAllTransitions(tracer, for_=SomeMachine)

...where the tracer stuff would come from #36

markrwilliams commented 7 years ago

(Per @tomprince)

warner commented 7 years ago

At dinner tonight, @markrwilliams and I came up with some ideas:

Then standard set-intersection tests can be used to decide if the right transitions have been exercised. You could merge the traces of multiple context managers to look at combined coverage, or you could use setUp/tearDown to merge multiple test cases and check the results at the very end of the test class.

class Foo(object):
    m = MethodicalMachine()
    ...
f = Foo()
with gather_transitions(f.m) as edges:
    f.trigger_stuff()
assert edges.all_transitions() == Foo.m.all_transitions()

(the difference between a machine instance, like Foo.m, and a transitioner instance, like f.m, is pretty important, both for implementation and for discoverability/learnability/error-reporting. my example probably gets at least one of them wrong)

This also avoids the need to be able to name specific transitions, since the sets contain opaque objects (one per transition). This would let us continue to keep the tracing function private, and these opaque objects would be the strings or tuples that the tracing function provides.

37 would, of course, need a way to name these things.

Other questions: