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