Пример #1
0
class StarStateMachine(unittest.TestCase):
    def setUp(self):
        self.states = ('one', 'two', 'three')
        self.transitions = (
            ('one', '*'),
            ('two', 'three'),
            ('*', 'one'),
        )
        self.initial = 'one'
        self.sm = StateMachine(states=self.states,
                               initial=self.initial,
                               transitions=self.transitions)

    def test_transition_star_same(self):
        self.sm.transition_to('one')
        self.sm.transition_to('one')

    def test_transition_star_to(self):
        self.sm.transition_to('three')

    def test_transition_star_from(self):
        self.sm._state = 'three'
        self.sm.transition_to('one')

    def test_transition_star_invalid(self):
        self.assertRaises(ValueError, self.sm.transition_to, 'unknown')
Пример #2
0
class ValidStateMachineTest(unittest.TestCase):
    def setUp(self):
        self.states = ('one', 'two', 'three')
        self.transitions = (
            ('one', 'two'),
            ('two', 'three'),
            ('three', 'one'),
        )
        self.initial = 'two'
        self.sm = StateMachine(states=self.states,
                               initial=self.initial,
                               transitions=self.transitions)

    def test_initial(self):
        self.assertEqual(self.sm.initial, self.initial)

    def test_states(self):
        self.assertEqual(self.sm.states, self.states)

    def test_state(self):
        self.assertEqual(self.sm.state, self.initial)

    def test_transition_same(self):
        self.assertRaises(ValueError, self.sm.transition_to, self.sm.state)

    def test_transitions_exist(self):
        for transition in self.transitions:
            self.assertTrue(transition in self.sm.transitions)

    def test_transition_valid(self):
        self.sm.transition_to('three')
        self.assertEqual(self.sm.state, 'three')
        self.sm.transition_to('one')
        self.assertEqual(self.sm.state, 'one')

    def test_transition_valid_fromto(self):
        self.sm.transition_fromto('two', 'three')
        self.assertEqual(self.sm.state, 'three')
        self.sm.transition_fromto('three', 'one')
        self.assertEqual(self.sm.state, 'one')

    def test_transition_invalid_fromto_invalidtransition(self):
        self.assertRaises(ValueError, self.sm.transition_fromto, 'one',
                          'three')

    def test_transition_invalid_fromto_validtransition(self):
        self.assertRaises(ValueError, self.sm.transition_fromto, 'one', 'two')

    def test_transition_invalid_fromto_unknownstate(self):
        self.assertRaises(ValueError, self.sm.transition_fromto, 'two',
                          'unknown')

    def test_transition_valid_setter(self):
        self.sm.state = 'three'
        self.assertEqual(self.sm.state, 'three')
        self.sm.state = 'one'
        self.assertEqual(self.sm.state, 'one')

    def test_transition_invalid(self):
        self.assertRaises(ValueError, self.sm.transition_to, 'one')

    def test_reset(self):
        self.sm.state = 'three'
        self.assertEqual(self.sm.state, 'three')
        self.sm.reset()
        self.assertEqual(self.sm.state, 'two')