forked from Yonaba/Jumper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid_specs.lua
501 lines (426 loc) · 12.3 KB
/
grid_specs.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
context('Module Grid', function()
local Grid, Node
before(function()
Grid = require ('jumper.grid')
Node = require ('jumper.core.node')
end)
context('Grid:new() or Grid() returns a new Grid object', function()
test('Grid:new() or Grid() returns a new Grid object', function()
assert_equal(getmetatable(getmetatable(Grid:new({{0}}))),Grid)
assert_equal(getmetatable(getmetatable(Grid({{0}}))),Grid)
end)
test('Grid:new() requires a collision map upon initialization', function()
local map = {{0,0},{0,0}}
assert_not_nil(Grid:new(map))
end)
test('The passed-in map can be a string', function()
local map = '00\n00'
assert_not_nil(Grid:new(map))
end)
test('passing nil to Grid:new() or Grid() causes an error', function()
assert_error(pcall(Grid, Grid))
assert_error(pcall(Grid.new, Grid))
end)
test('Grid and map should have the same width', function()
local map = '00\n00'
local map = {{0,0},{0,0}}
local grid = Grid(map)
local grid2 = Grid(map)
assert_equal(grid:getWidth(), 2)
assert_equal(grid2:getWidth(), 2)
end)
test('Grid and map should have the same height', function()
local map = '00\n00\n00'
local map2 = {{0,0},{0,0},{0,0},{0,0}}
local grid = Grid(map)
local grid2 = Grid(map2)
assert_equal(grid:getHeight(), 3)
assert_equal(grid2:getHeight(), 4)
end)
test('Grid:getBounds() returns the grid corners coordinates', function()
local map = {{0,0,0},{0,0,0},{0,0,0},{0,0,0}}
local grid = Grid(map)
local lx,ly,rx,ry = grid:getBounds()
assert_equal(lx,1)
assert_equal(ly,1)
assert_equal(rx,3)
assert_equal(ry,4)
local map = {}
for y = 0,2 do map[y] = {}
for x = 0,2 do map[y][x] = 0 end
end
grid = Grid(map)
local lx,ly,rx,ry = grid:getBounds()
assert_equal(lx,0)
assert_equal(ly,0)
assert_equal(rx,2)
assert_equal(ry,2)
end)
test('Each value on the map matches a node on the grid', function()
local map = {{0,0},{0,0},{0,0},{0,0}}
local grid = Grid(map)
for y in pairs(map) do
for x in pairs(map[y]) do
local node = grid:getNodeAt(x,y)
assert_not_nil(node)
assert_equal(getmetatable(node), Node)
end
end
end)
test('the passed-in map should have a width greather than 0',function()
assert_error(pcall(Grid, Grid, {{},{}}))
assert_error(pcall(Grid, Grid, '\n\n'))
end)
test('the passed-in map should have a height greather than 0',function()
assert_error(pcall(Grid, Grid, {}))
assert_error(pcall(Grid, Grid, ''))
end)
test('the passed-in map should have rows with the same width',function()
assert_error(pcall(Grid, Grid, {{0},{0,0}}))
assert_error(pcall(Grid, Grid, '00\n000'))
end)
test('values in the map should only be integers or strings',function()
assert_error(pcall(Grid, Grid, {{0.1,0,0},{0,0,0}}))
assert_error(pcall(Grid, Grid, {{0,function() end,0},{0,0,0}}))
end)
end)
context('Grid types', function()
test('passing a 2nd arg to Grid:new() or Grid() returns a safe-memory grid', function()
local grid = Grid({{0}})
local pgrid = Grid({{0}},true)
assert_not_equal(getmetatable(grid), getmetatable(pgrid))
assert_equal(getmetatable(getmetatable(grid)), getmetatable(getmetatable(pgrid)))
end)
test('those grids are memory safe, as nodes are cached on purpose', function()
local map = {{0,0,0},{0,0,0},{0,0,0}}
local pgrid = Grid(map, true)
assert_equal(#pgrid:getNodes(), 0)
local count = 0
for node in pgrid:iter() do
assert_equal(getmetatable(node), Node)
count = count+1
end
assert_equal(count, pgrid:getWidth()*pgrid:getHeight())
end)
end)
context('Grid:isWalkablkeAt', function()
test('returns whether or not a node is walkable',function()
local map = {{0,0},{0,0},{0,1},{0,0}}
local grid = Grid(map)
local walkable = 1
for y in pairs(map) do
for x in pairs(map[y]) do
if map[y][x] == walkable then
assert_true(grid:isWalkableAt(x,y,walkable))
else
assert_false(grid:isWalkableAt(x,y,walkable))
end
end
end
map = 'WXW\nWWW\n'
grid = Grid(map)
walkable = 'W'
for y = 1,2 do
for x = 1,3 do
if x==2 and y==1 then
assert_false(grid:isWalkableAt(x,y,walkable))
else
assert_true(grid:isWalkableAt(x,y,walkable))
end
end
end
end)
test('All nodes are walkable when no walkability rule was set', function()
local map = {{0,0},{0,0}}
local grid = Grid(map)
for y in pairs(map) do
for x in pairs(map[y]) do
assert_true(grid:isWalkableAt(x,y,walkable))
end
end
end)
end)
context('Grid:getMap()', function()
test('returns the collision map',function()
local map = {{0,0},{0,0}}
local grid = Grid(map)
assert_equal(grid:getMap(), map)
end)
test('returns the array parsed from a given string',function()
local map = '00\n00'
local grid = Grid(map)
assert_equal(type(grid:getMap()), 'table')
assert_not_equal(grid:getMap(), map)
end)
end)
context('Grid:getNodeAt()', function()
local map, grid
before(function()
map = {
{0,0,0,0},
{0,0,0,0},
}
grid = Grid(map)
end)
test('returns the node at a given position', function()
local node = grid:getNodeAt(1,1)
assert_equal(getmetatable(node),Node)
assert_equal(node._x,1)
assert_equal(node._y,1)
end)
test('returns nil if the node does not exist', function()
assert_nil(grid:getNodeAt(0,0))
assert_nil(grid:getNodeAt(5,1))
end)
test('returns nil if one of its args is missing', function()
assert_nil(grid:getNodeAt(0))
assert_nil(grid:getNodeAt())
end)
end)
context('Grid:getNodes()', function()
test('returns the array of nodes', function()
local map = {{0,0},{0,0}}
local grid = Grid(map)
local nodes = grid:getNodes()
assert_equal(type(nodes), 'table')
for y in pairs(nodes) do
for x in pairs(nodes[y]) do
assert_equal(getmetatable(nodes[y][x]),Node)
end
end
end)
end)
context('grid:getNeighbours()', function()
test('returns neighbours of a given node', function()
local map = {{0,0},{0,0},}
local grid = Grid(map)
local walkable = 0
local node = grid:getNodeAt(1,1)
local nb = grid:getNeighbours(node, walkable)
assert_equal(type(nb), 'table')
assert_equal(#nb, 2)
assert_equal(nb[1], grid:getNodeAt(2,1))
assert_equal(nb[2], grid:getNodeAt(1,2))
end)
test('passing true as a third arg includes ajacent nodes', function()
local map = {{0,0},{0,0},}
local grid = Grid(map)
local walkable, allowDiag = 0, true
local node = grid:getNodeAt(1,1)
local nb = grid:getNeighbours(node, walkable, allowDiag)
assert_equal(type(nb), 'table')
assert_equal(#nb, 3)
assert_equal(nb[1], grid:getNodeAt(2,1))
assert_equal(nb[2], grid:getNodeAt(1,2))
assert_equal(nb[3], grid:getNodeAt(2,2))
end)
test('passing arg tunnel includes adjacent nodes that cannot be reached diagonnally', function()
local map = {{0,0,0},{1,0,0},{0,2,0}}
local grid = Grid(map)
local walkable, allowDiag, tunnel = 0, true, true
local node = grid:getNodeAt(1,3)
local nb = grid:getNeighbours(node, walkable, allowDiag, tunnel)
assert_equal(type(nb), 'table')
assert_equal(#nb, 1)
assert_equal(nb[1], grid:getNodeAt(2,2))
end)
end)
context('Grid:iter()', function()
test('iterates on all nodes in a grid', function()
local map = {
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
}
local grid = Grid(map)
local record = {}
for n in grid:iter() do
assert_equal(getmetatable(n), Node)
assert_not_nil(map[n._y] and map[n._y][n._x])
assert_nil(record[n])
record[n] = true
end
end)
test('can iterate only on a rectangle of nodes', function()
local map = {
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
}
local grid = Grid(map)
local record = {}
for n in grid:iter(2,2,3,3) do
assert_equal(getmetatable(n), Node)
assert_not_nil(map[n._y] and map[n._y][n._x])
assert_gte(n._x, 2)
assert_gte(n._y, 2)
assert_lte(n._x, 3)
assert_lte(n._y, 3)
assert_nil(record[n])
record[n] = true
end
end)
end)
context('Grid:around()', function()
test('iterates on nodes following a square outline pattern', function()
local map = {
{0,0,0},
{0,0,0},
{0,0,0}
}
local m = {{1,1},{2,1},{3,1},{3,2},{3,3},{2,3},{1,3},{1,2}}
local grid = Grid(map)
local record = {}
local i = 0
for n in grid:around(Node(2,2),1) do
i = i + 1
assert_equal(getmetatable(n), Node)
assert_not_nil(map[n._y] and map[n._y][n._x])
assert_equal(n._x, m[i][1])
assert_equal(n._y, m[i][2])
assert_nil(record[n])
record[n] = true
end
end)
test('arg spacing defaults to 1 when not given', function()
local map = {
{0,0,0},
{0,0,0},
{0,0,0}
}
local m = {{1,1},{2,1},{3,1},{3,2},{3,3},{2,3},{1,3},{1,2}}
local grid = Grid(map)
local record = {}
local i = 0
for n in grid:around(Node(2,2)) do
i = i + 1
assert_equal(getmetatable(n), Node)
assert_not_nil(map[n._y] and map[n._y][n._x])
assert_equal(n._x, m[i][1])
assert_equal(n._y, m[i][2])
assert_nil(record[n])
record[n] = true
end
end)
test('skips unexisting nodes', function()
local map = {
{0,0,0},
{0,0,0},
{0,0,0}
}
local m = {{2,1},{2,2},{1,2}}
local grid = Grid(map)
local record = {}
local i = 0
for n in grid:around(Node(1,1)) do
i = i + 1
assert_equal(getmetatable(n), Node)
assert_not_nil(map[n._y] and map[n._y][n._x])
assert_equal(n._x, m[i][1])
assert_equal(n._y, m[i][2])
assert_nil(record[n])
record[n] = true
end
end)
end)
context('Grid:each()', function()
test('calls a given function on each node in a grid', function()
local map = {
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
}
local grid = Grid(map)
local record = {}
local function f(node,i) node.value = i end
grid:each(f, 3)
for n in grid:iter() do
assert_equal(getmetatable(n), Node)
assert_not_nil(map[n._y] and map[n._y][n._x])
assert_equal(n.value,3)
assert_nil(record[n])
record[n] = true
end
end)
end)
context('Grid:eachRange()', function()
test('calls a given function on each node in a range', function()
local map = {
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
}
local grid = Grid(map)
local record = {}
local function f(node,i) node.value = i end
grid:eachRange(1,1,2,2,f,3)
for n in grid:iter() do
if n._x <= 2 and n._y <= 2 then
assert_equal(n.value,3)
else
assert_nil(n.value)
end
assert_equal(getmetatable(n), Node)
assert_not_nil(map[n._y] and map[n._y][n._x])
assert_nil(record[n])
record[n] = true
end
end)
end)
context('Grid:imap()', function()
test('Maps a given function on each node in a grid', function()
local map = {
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
}
local grid = Grid(map)
local record = {}
local function f(node,i)
node.v = i
return node
end
grid:imap(f, 5)
for n in grid:iter() do
assert_equal(getmetatable(n), Node)
assert_not_nil(map[n._y] and map[n._y][n._x])
assert_equal(n.v,5)
assert_nil(record[n])
record[n] = true
end
end)
end)
context('Grid:imapRange()', function()
test('calls a given function on each node in a range', function()
local map = {
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
}
local grid = Grid(map)
local record = {}
local function f(node,i)
node.v = i
return node
end
grid:imapRange(3,3,4,4,f,7)
for n in grid:iter() do
if n._x >= 3 and n._y >= 3 then
assert_equal(n.v,7)
else
assert_nil(n.v)
end
assert_equal(getmetatable(n), Node)
assert_not_nil(map[n._y] and map[n._y][n._x])
assert_nil(record[n])
record[n] = true
end
end)
end)
end)