Belle II Software  release-08-01-10
test_caf_machine.py
1 
8 import unittest
9 from unittest import TestCase
10 
11 from caf.state_machines import ConditionError, Machine, MachineError
12 
13 
14 class Test_Machine(TestCase):
15  """
16  Set of tests for checking Machine Base class
17  """
18 
19  def setUp(self):
20  """
21  """
22 
23  self.mm = Machine()
24  self.mm.add_state("standing")
25  self.mm.add_state("walking")
26  self.mm.add_transition("walk", "standing", "walking")
27  self.mm.add_transition("stop", "walking", "standing")
28  self.mm.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.mm.state = "walking"
44  self.assertEqual(self.mm.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.mm.state = "fake"
52 
53  def test_walk(self):
54  """
55  Checks that machine can transition to a state
56  """
57  self.mm.state = "standing"
58  self.mm.walk()
59  self.assertEqual(self.mm.state.name, "walking")
60 
61  def test_walk_stop(self):
62  """
63  Checks that machine can traverse several states
64  """
65  self.mm.state = "standing"
66  self.mm.walk()
67  self.mm.stop()
68  self.assertEqual(self.mm.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.mm.add_state("airborne")
76 
77  def can_jump():
78  return True
79 
80  self.mm.add_transition("jump", "standing", "airborne", conditions=can_jump)
81  self.mm.state = "standing"
82  self.mm.jump()
83  self.assertEqual(self.mm.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.mm.add_state("airborne")
91 
92  def can_jump():
93  return False
94 
95  self.mm.add_transition("jump", "standing", "airborne", conditions=can_jump)
96  self.mm.state = "standing"
97  with self.assertRaises(ConditionError):
98  self.mm.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.mm.add_state("airborne")
106 
107  def can_jump():
108  return True
109 
110  def eaten_lunch():
111  return True
112  self.mm.add_transition("jump", "standing", "airborne", conditions=[can_jump, eaten_lunch])
113  self.mm.state = "standing"
114  self.mm.jump()
115  self.assertEqual(self.mm.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.mm.add_state("airborne")
123 
124  def can_jump():
125  return True
126 
127  def eaten_lunch():
128  return False
129  self.mm.add_transition("jump", "standing", "airborne", conditions=[can_jump, eaten_lunch])
130  self.mm.state = "standing"
131  with self.assertRaises(ConditionError):
132  self.mm.jump()
133 
134 
135 def main():
136  unittest.main()
137 
138 
139 if __name__ == '__main__':
140  main()
Definition: main.py:1
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:91