Belle II Software development
test_caf_machine.py
1
8import unittest
9from unittest import TestCase
10
11from caf.state_machines import ConditionError, Machine, MachineError
12
13
14class Test_Machine(TestCase):
15 """
16 Set of tests for checking Machine Base class
17 """
18
19 def setUp(self):
20 """
21 """
22
23 self.m = Machine()
24 self.m.add_state("standing")
25 self.m.add_state("walking")
26 self.m.add_transition("walk", "standing", "walking")
27 self.m.add_transition("stop", "walking", "standing")
28 self.m.initial_state = "standing"
29
30 def test_initial(self):
31 """
32 Checks that a new machine sets an initial state correctly
33 """
34 states = ["walking", "standing"]
35 initial = "walking"
36 m = Machine(states, initial)
37 self.assertEqual(m.state.name, initial)
38
39 def test_set(self):
40 """
41 Checks that machine sets at a state
42 """
43 self.m.state = "walking"
44 self.assertEqual(self.m.state.name, "walking")
45
46 def test_bad_set(self):
47 """
48 Checks that machine won't set a state not in the list
49 """
50 with self.assertRaises(MachineError):
51 self.m.state = "fake"
52
53 def test_walk(self):
54 """
55 Checks that machine can transition to a state
56 """
57 self.m.state = "standing"
58 self.m.walk()
59 self.assertEqual(self.m.state.name, "walking")
60
61 def test_walk_stop(self):
62 """
63 Checks that machine can traverse several states
64 """
65 self.m.state = "standing"
66 self.m.walk()
67 self.m.stop()
68 self.assertEqual(self.m.state.name, "standing")
69
71 """
72 Check that when a condition for a transition evaulates true it will transition
73 to the correct state
74 """
75 self.m.add_state("airborne")
76
77 def can_jump():
78 return True
79
80 self.m.add_transition("jump", "standing", "airborne", conditions=can_jump)
81 self.m.state = "standing"
82 self.m.jump()
83 self.assertEqual(self.m.state.name, "airborne")
84
86 """
87 Check that when a condition for a transition evaulates false it will raise
88 the correct error.
89 """
90 self.m.add_state("airborne")
91
92 def can_jump():
93 return False
94
95 self.m.add_transition("jump", "standing", "airborne", conditions=can_jump)
96 self.m.state = "standing"
97 with self.assertRaises(ConditionError):
98 self.m.jump()
99
101 """
102 Check that when multiple conditions for a transition all evaulate true it will transition
103 to the correct state
104 """
105 self.m.add_state("airborne")
106
107 def can_jump():
108 return True
109
110 def eaten_lunch():
111 return True
112 self.m.add_transition("jump", "standing", "airborne", conditions=[can_jump, eaten_lunch])
113 self.m.state = "standing"
114 self.m.jump()
115 self.assertEqual(self.m.state.name, "airborne")
116
118 """
119 Check that when a single condition out of many for a transition evaulates true it will
120 fail to move to the correct state
121 """
122 self.m.add_state("airborne")
123
124 def can_jump():
125 return True
126
127 def eaten_lunch():
128 return False
129 self.m.add_transition("jump", "standing", "airborne", conditions=[can_jump, eaten_lunch])
130 self.m.state = "standing"
131 with self.assertRaises(ConditionError):
132 self.m.jump()
133
134
135def main():
136 unittest.main()
137
138
139if __name__ == '__main__':
140 main()
Definition: main.py:1