-
Notifications
You must be signed in to change notification settings - Fork 1
/
spec.lua
233 lines (209 loc) · 6.29 KB
/
spec.lua
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
require 'busted'
local Ygg = require 'ygg'
describe('Ygg:', function()
it('Basic functionality', function()
local isHungry = Ygg('isHungry', function(this)
return this.hunger >= 50
end)
local isSleepy = Ygg('isSleepy', function(this)
return this.tiredness >= 100
end)
local eat = Ygg('eat', function(this)
this.state = 'eating'
this.hunger = math.max(0, this.hunger - 25)
return (this.hunger == 0) and true or nil
end)
local sleep = Ygg('sleep', function(this)
this.state = 'sleeping'
this.tiredness = math.max(0, this.tiredness - 30)
return (this.tiredness == 0) and true or nil
end)
local idle = Ygg('idle', function(this)
this.state = 'idle'
this.hunger = this.hunger + 10
this.tiredness = this.tiredness + 10
return true
end)
local tree = Ygg.selector('root')
:add(
Ygg.sequence('hunger sequence')
:add(isHungry)
:add(eat)
)
:add(
Ygg.sequence('sleep sequence')
:add(isSleepy)
:add(sleep)
)
:add(idle)
local runner = Ygg.run(tree)
local entity = {
state = '<unset>',
hunger = 30,
tiredness = 70,
}
local expectedResults = {
{state = 'idle', hunger = 40, tiredness = 80},
{state = 'idle', hunger = 50, tiredness = 90},
{state = 'eating', hunger = 25, tiredness = 90},
{state = 'eating', hunger = 0, tiredness = 90},
{state = 'idle', hunger = 10, tiredness = 100},
{state = 'sleeping', hunger = 10, tiredness = 70},
{state = 'sleeping', hunger = 10, tiredness = 40},
{state = 'sleeping', hunger = 10, tiredness = 10},
{state = 'sleeping', hunger = 10, tiredness = 0},
{state = 'idle', hunger = 20, tiredness = 10},
}
for _, expected in ipairs(expectedResults) do
runner:update(entity)
-- print(("State: %8s, Hunger: %3d, Tired: %3d"):format(entity.state, entity.hunger, entity.tiredness))
assert.same(expected, entity)
end
end)
it('Should be able to have a Action in Selector return nil (although this is unusual, maybe?)', function()
local tryEat = Ygg('tryEat', function(this)
this.state = 'eating'
if this.hunger == 0 then
return false
end
this.hunger = math.max(0, this.hunger - 25)
return nil
end)
local idle = Ygg('idle', function(this)
this.state = 'idle'
return true
end)
local tree = Ygg.selector('root')
:add(tryEat)
:add(idle)
local runner = Ygg.run(tree)
local entity = {
state = 'idle',
hunger = 60,
}
local expectedResults = {
{state = 'eating', hunger = 35},
{state = 'eating', hunger = 10},
{state = 'eating', hunger = 0},
{state = 'idle', hunger = 0},
}
for _, expected in ipairs(expectedResults) do
runner:update(entity)
-- print(("State: %6s, Hunger: %2d"):format(entity.state, entity.hunger))
assert.same(expected, entity)
end
end)
it('Selector should restart if any sub-nodes succeed, including sequence.', function()
local willAlwaysSucceedOne = Ygg('addIdol', function(this)
this.expected = this.expected + 1
return true
end)
local willAlwaysSucceedTen = Ygg('addIdol', function(this)
this.expected = this.expected + 10
return true
end)
local shouldNotBeExecuted = Ygg('willFail', function(this)
this.unexpected = this.unexpected + 1
return true
end)
local tree = Ygg.selector('root')
:add(
Ygg.sequence('success seq')
:add(willAlwaysSucceedOne)
:add(willAlwaysSucceedTen)
)
:add(shouldNotBeExecuted)
local runner = Ygg.run(tree)
local entity = {
expected = 0,
unexpected = 0,
}
local expectedResults = {
{expected = 11, unexpected = 0},
{expected = 22, unexpected = 0},
}
for _, expected in ipairs(expectedResults) do
runner:update(entity)
assert.same(expected, entity)
end
shouldNotBeExecuted.func({unexpected = -1}) -- Test coverage lololol
end)
it('Should reset to first index after Selector succeeds', function()
local hasPlate = Ygg('hasPlate', function(this)
return this.plates >= 1
end)
local getFood = Ygg('getFood', function(this)
this.food = this.food + 1
return true
end)
local getPlate = Ygg('getPlate', function(this)
this.plates = this.plates + 1
return true
end)
local tree = Ygg.selector('root')
:add(
Ygg.sequence('food seq')
:add(hasPlate)
:add(getFood)
)
:add(
Ygg.sequence('plate seq')
:add(getPlate)
)
local runner = Ygg.run(tree)
local entity = {
food = 0,
plates = 0,
}
local expectedResults = {
{food = 0, plates = 1},
{food = 1, plates = 1},
{food = 2, plates = 1},
{food = 3, plates = 1},
}
for _, expected in ipairs(expectedResults) do
runner:update(entity)
-- print(("Food: %d, Plates: %d"):format(entity.food, entity.plates))
assert.same(expected, entity)
end
end)
it('Should reset to first index if Selector within Sequence fails', function()
local addIdol = Ygg('addIdol', function(this)
this.idols = this.idols + 1
return true
end)
local willFail = Ygg('willFail', function()
return false
end)
local tree = Ygg.sequence('root')
:add(addIdol)
:add(
Ygg.selector('fail selector')
:add(willFail)
:add(willFail)
:add(willFail)
)
local runner = Ygg.run(tree)
local entity = {
idols = 0,
}
local expectedResults = {
{idols = 1},
{idols = 2},
{idols = 3},
}
for _, expected in ipairs(expectedResults) do
runner:update(entity)
-- print(("Idols: %d"):format(entity.idols))
assert.same(expected, entity)
end
end)
it('Should be able to create actions without a name', function()
local action = Ygg(function() end)
assert.same('<Action>', action.name)
end)
it('Invalid scripts should relay error message', function()
local expectedError = "action function must be a function, got: nil"
assert.has_error(function() Ygg(11) end, expectedError)
end)
end)