9from unittest
import TestCase
11from caf.state_machines
import ConditionError, Machine, MachineError
16 Set of tests for checking Machine Base
class
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"
32 Checks that a new machine sets an initial state correctly
34 states = ["walking",
"standing"]
36 m = Machine(states, initial)
37 self.assertEqual(m.state.name, initial)
41 Checks that machine sets at a state
43 self.m.state = "walking"
44 self.assertEqual(self.
m.state.name,
"walking")
48 Checks that machine won't set a state not in the list
50 with self.assertRaises(MachineError):
55 Checks that machine can transition to a state
57 self.m.state = "standing"
59 self.assertEqual(self.
m.state.name,
"walking")
63 Checks that machine can traverse several states
65 self.m.state = "standing"
68 self.assertEqual(self.
m.state.name,
"standing")
72 Check that when a condition for a transition evaluates true it will transition
75 self.m.add_state("airborne")
80 self.
m.add_transition(
"jump",
"standing",
"airborne", conditions=can_jump)
81 self.
m.state =
"standing"
83 self.assertEqual(self.
m.state.name,
"airborne")
87 Check that when a condition for a transition evaluates false it will
raise
90 self.m.add_state("airborne")
95 self.
m.add_transition(
"jump",
"standing",
"airborne", conditions=can_jump)
96 self.
m.state =
"standing"
97 with self.assertRaises(ConditionError):
102 Check that when multiple conditions for a transition all evaluate true it will transition
105 self.m.add_state("airborne")
112 self.
m.add_transition(
"jump",
"standing",
"airborne", conditions=[can_jump, eaten_lunch])
113 self.
m.state =
"standing"
115 self.assertEqual(self.
m.state.name,
"airborne")
119 Check that when a single condition out of many for a transition evaluates true it will
120 fail to move to the correct state
122 self.m.add_state("airborne")
129 self.
m.add_transition(
"jump",
"standing",
"airborne", conditions=[can_jump, eaten_lunch])
130 self.
m.state =
"standing"
131 with self.assertRaises(ConditionError):
139if __name__ ==
'__main__':
def test_true_condition(self)
def test_false_condition(self)
def test_true_conditions(self)
def test_halftrue_conditions(self)