forked from collaborative-robotics/behavior3py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_memsequence.py
138 lines (112 loc) · 4.59 KB
/
test_memsequence.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import b3
import unittest
#from common import *
from tests.common import *
def get_node(status):
stub = NodeStub();
stub._execute.return_value = status
return stub
class TestMemSequence(unittest.TestCase):
def test_category(self):
self.assertEqual(b3.MemSequence.category, b3.COMPOSITE)
def test_initialization(self):
node = b3.MemSequence()
self.assertIsNotNone(node.id)
self.assertEqual(node.name, 'MemSequence')
self.assertEqual(node.title, 'MemSequence')
self.assertIsNotNone(node.description)
def test_success(self):
node1 = get_node(b3.SUCCESS)
node2 = get_node(b3.SUCCESS)
node3 = get_node(b3.SUCCESS)
memsequence = b3.MemSequence(children=[node1, node2, node3])
tick = TickStub()
tick.blackboard.get.return_value = 0
status = memsequence.tick(tick)
self.assertEqual(status, b3.SUCCESS)
self.assertEqual(node1._execute.call_count, 1)
self.assertEqual(node2._execute.call_count, 1)
self.assertEqual(node3._execute.call_count, 1)
def test_failure(self):
node1 = get_node(b3.SUCCESS)
node2 = get_node(b3.SUCCESS)
node3 = get_node(b3.FAILURE)
node4 = get_node(b3.SUCCESS)
memsequence = b3.MemSequence(children=[node1, node2, node3])
tick = TickStub()
tick.blackboard.get.return_value = 0
status = memsequence.tick(tick)
self.assertEqual(status, b3.FAILURE)
self.assertEqual(node1._execute.call_count, 1)
self.assertEqual(node2._execute.call_count, 1)
self.assertEqual(node3._execute.call_count, 1)
self.assertEqual(node4._execute.call_count, 0)
def test_running(self):
node1 = get_node(b3.SUCCESS)
node2 = get_node(b3.SUCCESS)
node3 = get_node(b3.RUNNING)
node4 = get_node(b3.SUCCESS)
memsequence = b3.MemSequence(children=[node1, node2, node3, node4])
tick = TickStub()
tick.blackboard.get.return_value = 0
status = memsequence.tick(tick)
self.assertEqual(status, b3.RUNNING)
self.assertEqual(node1._execute.call_count, 1)
self.assertEqual(node2._execute.call_count, 1)
self.assertEqual(node3._execute.call_count, 1)
self.assertEqual(node4._execute.call_count, 0)
def test_error(self):
node1 = get_node(b3.SUCCESS)
node2 = get_node(b3.SUCCESS)
node3 = get_node(b3.ERROR)
node4 = get_node(b3.SUCCESS)
memsequence = b3.MemSequence(children=[node1, node2, node3, node4])
tick = TickStub()
tick.blackboard.get.return_value = 0
status = memsequence.tick(tick)
self.assertEqual(status, b3.ERROR)
self.assertEqual(node1._execute.call_count, 1)
self.assertEqual(node2._execute.call_count, 1)
self.assertEqual(node3._execute.call_count, 1)
self.assertEqual(node4._execute.call_count, 0)
def test_memory(self):
node1 = get_node(b3.SUCCESS)
node2 = get_node(b3.SUCCESS)
node3 = get_node(b3.RUNNING)
node4 = get_node(b3.FAILURE)
node5 = get_node(b3.SUCCESS)
memsequence = b3.MemSequence(children=[node1, node2, node3, node4, node5])
memsequence.id = 'node1';
tick = TickStub()
tick.blackboard.get.return_value = 0
status = memsequence._execute(tick)
expected = [
mock.call('is_open', True, 'tree1', 'node1'),
mock.call('running_child', 0, 'tree1', 'node1'),
mock.call('running_child', 2, 'tree1', 'node1')
]
result = tick.blackboard.set.mock_calls
self.assertListEqual(result, expected)
def test_memory_continue(self):
node1 = get_node(b3.SUCCESS)
node2 = get_node(b3.SUCCESS)
node3 = get_node(b3.SUCCESS)
node4 = get_node(b3.FAILURE)
node5 = get_node(b3.SUCCESS)
memsequence = b3.MemSequence(children=[node1, node2, node3, node4, node5])
memsequence.id = 'node1';
tick = TickStub()
tick.blackboard.get.return_value = 2
status = memsequence._execute(tick)
expected = [
mock.call('is_open', False, 'tree1', 'node1'),
]
result = tick.blackboard.set.mock_calls
self.assertListEqual(result, expected)
self.assertEqual(node1._execute.call_count, 0)
self.assertEqual(node2._execute.call_count, 0)
self.assertEqual(node3._execute.call_count, 1)
self.assertEqual(node4._execute.call_count, 1)
self.assertEqual(node5._execute.call_count, 0)
if __name__ == '__main__':
unittest.main()