Repository navigation
Expand file tree
/
Copy pathsequence_spec.js
More file actions
143 lines (127 loc) · 3.87 KB
/
Copy pathsequence_spec.js
File metadata and controls
143 lines (127 loc) · 3.87 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
describe('Sequence', function() {
var sequence;
describe('can be constructed with', function() {
describe("an object containing it's title", function() {
beforeEach(function() {
sequence = new BehaviorTree.Sequence({
title: 'runAll'
});
});
it('and the title is saved on the instance', function() {
expect(sequence.title).toBe('runAll');
});
});
});
describe('instance', function() {
beforeEach(function() {
sequence = new BehaviorTree.Sequence({
title: 'sequence'
});
});
it('has the same methods like a BranchNode instance', function() {
var branchNode = new BehaviorTree.BranchNode({});
for (var method in branchNode) {
expect(sequence[method]).toBeTruthy();
}
});
});
describe('when having a Sequence with two nodes', function() {
var node, hasRun1, beSuccess1, startCalled1, endCalled1,
hasRun2, startCalled2, endCalled2;
beforeEach(function() {
beSuccess1 = true;
hasRun1 = startCalled1 = endCalled1 = false;
hasRun2 = startCalled2 = endCalled2 = false;
node = new BehaviorTree.Node({
run: function() {
hasRun1 = true;
if (beSuccess1) {
this.success();
} else {
this.fail();
}
},
start: function() { startCalled1 = true; },
end: function() { endCalled1 = true; }
});
selector = new BehaviorTree.Sequence({
title: 'a sequence',
nodes: [
node,
new BehaviorTree.Node({
run: function() { hasRun2 = true; this.success(); },
start: function() { startCalled2 = true; },
end: function() { endCalled2 = true; }
})
]
});
selector.setControl({
success: function() {},
fail: function() {}
});
});
describe('if success is called by first task', function() {
beforeEach(function() {
beSuccess1 = true;
selector.run();
});
it("calls start of the next task in line", function() {
expect(startCalled2).toBeTruthy();
});
it("calls run of the next task in line", function() {
expect(hasRun2).toBeTruthy();
});
});
describe('if fail is called by first task', function() {
beforeEach(function() {
beSuccess1 = false;
selector.run();
});
it("does not call start of the next task in line", function() {
expect(startCalled2).toBeFalsy();
});
it("does not call run of the next task in line", function() {
expect(hasRun2).toBeFalsy();
});
});
});
describe('when in Sequence with two nodes', function() {
var node, parentSelector, beSuccess, parentSuccessCalled, parentFailCalled;
beforeEach(function() {
beSuccess = true;
parentSuccessCalled = parentFailCalled = false;
selector = new BehaviorTree.Sequence({
title: 'a sequence',
nodes: [
new BehaviorTree.Node({
run: function() { if (beSuccess) { this.success(); } else { this.fail(); } }
})
]
});
parentSelector = new BehaviorTree.Sequence({
title: 'parent',
nodes: [ selector ],
success: function() {
parentSuccessCalled = true;
},
fail: function() {
parentFailCalled = true;
}
});
});
describe('all task result in success', function() {
it('calls success also in parent node', function() {
beSuccess = true;
parentSelector.run();
expect(parentSuccessCalled).toBeTruthy();
});
});
describe('a task results in failure', function() {
it('calls fail also in parent node', function() {
beSuccess = false;
parentSelector.run();
expect(parentFailCalled).toBeTruthy();
});
});
});
});