Repository navigation
Expand file tree
/
Copy pathbranch_node_spec.js
More file actions
160 lines (143 loc) · 3.84 KB
/
Copy pathbranch_node_spec.js
File metadata and controls
160 lines (143 loc) · 3.84 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
describe('BranchNode', function() {
var selector;
describe('can be constructed with', function() {
describe("an object containing it's title", function() {
beforeEach(function() {
selector = new BehaviorTree.BranchNode({
title: 'choose'
});
});
it('and the title is saved on the instance', function() {
expect(selector.title).toBe('choose');
});
});
});
describe('instance', function() {
beforeEach(function() {
selector = new BehaviorTree.BranchNode({
title: 'selector'
});
});
it('has the same methods like a Node instance', function() {
var node = new BehaviorTree.Node({});
for (var method in node) {
expect(selector[method]).toBeTruthy();
}
});
});
describe('the run method', function() {
var node, hasRun, beSuccess, startCalled, endCalled, canObj, runObj;
beforeEach(function() {
hasRun = false;
startCalled = endCalled = 0;
node = new BehaviorTree.Node({
run: function(arg) {
runObj = arg;
hasRun = true;
if (beSuccess) {
this.success();
} else {
this.fail();
}
},
start: function() { ++startCalled; },
end: function() { ++endCalled; }
});
selector = new BehaviorTree.BranchNode({
title: 'call it selector',
nodes: [
node
]
});
});
it("run of task gets the object as argument we passed in", function() {
var testObj = 23;
selector.run(testObj);
expect(runObj).toBe(testObj);
});
it('calls first the start method of next node before calling the run method', function() {
selector.run();
expect(startCalled).toBe(1);
});
describe('if success is called by task', function() {
it("calls the end method of task", function() {
beSuccess = true;
selector.run();
expect(endCalled).toBe(1);
});
});
describe('if fail is called by task', function() {
it("it still calls the end method of task", function() {
beSuccess = false;
selector.run();
expect(endCalled).toBe(1);
});
});
});
describe('the start method', function() {
var node, runObj, testObj;
beforeEach(function() {
testObj = 123;
node = new BehaviorTree.Node({
run: function() {
this.success();
},
start: function(arg) {
runObj = arg;
}
});
selector = new BehaviorTree.BranchNode({
title: 'test me',
nodes: [
node
]
});
selector.run(testObj);
});
it("gets the object as argument we passed in", function() {
expect(runObj).toBe(testObj);
});
});
describe('the end method', function() {
var node, runObj, testObj, beSuccess;
beforeEach(function() {
testObj = 123;
node = new BehaviorTree.Node({
run: function() {
if (beSuccess) {
this.success();
} else {
this.fail();
}
},
end: function(arg) {
runObj = arg;
}
});
selector = new BehaviorTree.BranchNode({
title: 'test me twice',
nodes: [
node
]
});
});
describe('if success is called by task', function() {
beforeEach(function() {
beSuccess = true;
selector.run(testObj);
});
it("gets the object as argument we passed in", function() {
expect(runObj).toBe(testObj);
});
});
describe('if fail is called by task', function() {
beforeEach(function() {
beSuccess = false;
selector.run(testObj);
});
it("gets the object as argument we passed in", function() {
expect(runObj).toBe(testObj);
});
});
});
});