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