~divmod-dev/divmod.org/1304710-storeless-adapter

« back to all changes in this revision

Viewing changes to Vertex/vertex/test/test_statemachine.py

  • Committer: cyli
  • Date: 2013-06-27 06:02:46 UTC
  • mto: This revision was merged to the branch mainline in revision 2702.
  • Revision ID: cyli-20130627060246-ciict8hwvjuy9d81
Move Vertex out of the Divmod.org repository

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
from twisted.trial import unittest
3
 
 
4
 
from vertex import statemachine
5
 
 
6
 
S_1, S_2 = 'S_1', 'S_2'
7
 
I_1, I_2 = 'I_1', 'I_2'
8
 
O_1, O_2 = 'O_1', 'O_2'
9
 
 
10
 
class TestMachine(statemachine.StateMachine):
11
 
    states = {
12
 
        S_1: {
13
 
            I_1: (O_1, S_1),
14
 
            I_2: (O_1, S_2),
15
 
            },
16
 
        S_2: {
17
 
            I_1: (O_2, S_2),
18
 
            I_2: (O_1, S_2),
19
 
            },
20
 
        }
21
 
 
22
 
    initialState = S_1
23
 
 
24
 
    def _event(self, name):
25
 
        def method():
26
 
            self.events.append(name)
27
 
        return method
28
 
 
29
 
    def __getattr__(self, name):
30
 
        if (name.startswith('exit_') or name.startswith('enter_') or
31
 
            name.startswith('transition_') or name.startswith('output_')):
32
 
            return self._event(name)
33
 
        raise AttributeError(name)
34
 
 
35
 
class Transitions(unittest.TestCase):
36
 
    def testOutput(self):
37
 
        m = TestMachine()
38
 
 
39
 
        self.assertEquals(m.state, S_1)
40
 
 
41
 
        m.events = []
42
 
        m.input(I_1)
43
 
        self.assertEquals(
44
 
            m.events,
45
 
            ['output_O_1'])
46
 
        self.assertEquals(m.state, S_1)
47
 
 
48
 
        m.events = []
49
 
        m.input(I_2)
50
 
        self.assertEquals(
51
 
            m.events,
52
 
            ['exit_S_1', 'enter_S_2', 'transition_S_1_to_S_2', 'output_O_1'])
53
 
        self.assertEquals(m.state, S_2)
54
 
 
55
 
        m.events = []
56
 
        m.input(I_1)
57
 
        self.assertEquals(
58
 
            m.events,
59
 
            ['output_O_2'])
60
 
        self.assertEquals(m.state, S_2)
61
 
 
62
 
        m.events = []
63
 
        m.input(I_2)
64
 
        self.assertEquals(
65
 
            m.events,
66
 
            ['output_O_1'])
67
 
        self.assertEquals(m.state, S_2)