def test_addTransitionsDoesNotMutate(self): """ L{TransitionTable.addTransitions} does not change the L{TransitionTable} it is called on. """ table = TransitionTable({"foo": {"bar": Transition("baz", "quux")}}) table.addTransitions("apple", {"banana": ("clementine", "date")}) self.assertEqual({"foo": { "bar": Transition("baz", "quux") }}, table.table)
def test_repr(self): """ The other string representation of a L{Transition} includes the output and next state it represents. """ self.assertEqual("<Transition output='a' nextState='b'>", repr(Transition("a", "b")))
def test_extraTransitionInput(self): """ L{ExtraTransitionInput} is raised if there are any keys in any of the values of C{transitions} that are not defined by C{input}. """ extra = object() exc = self.assertRaises( ExtraTransitionInput, constructFiniteStateMachine, Input, Output, State, TransitionTable({ State.amber: { Input.apple: Transition(Output.aardvark, State.amber), extra: Transition(Output.aardvark, State.amber) } }), State.amber, [], {}, NULL_WORLD) self.assertEqual(({extra}, ), exc.args)
def test_initial(self): """ When constructed with a transition table as an argument, L{TransitionTable} contains exactly that table. """ expected = {"foo": {"bar": Transition("baz", "quux")}} table = TransitionTable(expected) self.assertIs(expected, table.table)
def test_addTransitions(self): """ L{TransitionTable.addTransitions} accepts a state and a mapping from inputs to output, next state pairs and adds all of those transitions to the given state to a new L{TransitionTable} which it returns. """ table = TransitionTable() more = table.addTransitions("apple", { "banana": ("clementine", "date"), "eggplant": ("fig", "grape") }) self.assertEqual( { "apple": { "banana": Transition("clementine", "date"), "eggplant": Transition("fig", "grape") } }, more.table)
def test_addTransition(self): """ L{TransitionTable.addTransition} accepts a state, an input, an output, and a next state and adds the transition defined by those four values to a new L{TransitionTable} which it returns. """ table = TransitionTable() more = table.addTransition("foo", "bar", "baz", "quux") self.assertEqual({"foo": { "bar": Transition("baz", "quux") }}, more.table)
def test_missingTransitionOutput(self): """ L{MissingTransitionOutput} is raised if any of the values defined by C{output} does not appear as an output value defined by C{transitions}. """ exc = self.assertRaises( MissingTransitionOutput, constructFiniteStateMachine, Input, Output, State, TransitionTable({State.amber: { Input.apple: Transition([], None) }}), State.amber, [], {}, NULL_WORLD) self.assertEqual(({Output.aardvark}, ), exc.args)
class Output(Names): START = NamedConstant() STOP = NamedConstant() class State(Names): IDLE = NamedConstant() STARTING = NamedConstant() START_CANCELLED = NamedConstant() ACTIVE = NamedConstant() STOPPING = NamedConstant() STOP_CANCELLED = NamedConstant() table = TransitionTable({ State.IDLE: { Input.REQUEST_START: Transition([Output.START], State.STARTING), Input.REQUEST_STOP: Transition([], State.IDLE), }, State.STARTING: { Input.REQUEST_START: Transition([], State.STARTING), Input.REQUEST_STOP: Transition([], State.START_CANCELLED), Input.INSTANCE_STARTED: Transition([], State.ACTIVE), Input.START_FAILED: Transition([Output.START], State.STARTING), }, State.START_CANCELLED: { Input.REQUEST_START: Transition([], State.STARTING), Input.REQUEST_STOP: Transition([], State.START_CANCELLED), Input.INSTANCE_STARTED: Transition([Output.STOP], State.STOPPING), Input.START_FAILED: Transition([], State.IDLE), }, State.ACTIVE: {
def test_equal(self): """ Two L{Transition} instances are equal to each other if their attributes have the same values. """ self.assertTrue(Transition("a", "b") == Transition("a", "b"))