-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
NTest_NTest.lua
563 lines (483 loc) · 15.8 KB
/
NTest_NTest.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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
local N = require('NTest')("selftest")
local orig_node = node
local metatest
local async
local expected = {}
local failed, passed = 0,0
local load_tests2
local function load_tests()
--
-- Basic tests
--
metatest('simple assert passes', function()
ok(2 == 2, '2==2')
end, {'2==2'}, {})
metatest('simple negative assert fails', function()
nok(2 == 2, '2==2')
nok(false, 'unreachable code')
end, {}, {'2==2'})
metatest('simple assert fails', function()
ok(1 == 2, '1~=2')
ok(true, 'unreachable code')
end, {}, {'1~=2'})
metatest('simple negative assert passes', function()
nok(1 == 2, '1~=2')
end, {'1~=2'}, {})
metatest('ok without a message', function()
ok(1 == 1)
ok(1 == 2)
end, {'NTest_test.lua:31'}, {'NTest_test.lua:32'})
metatest('nok without a message', function()
nok(1 == "")
nok(1 == 1)
end, {'NTest_test.lua:36'}, {'NTest_test.lua:37'})
--
-- Equality tests
--
metatest('eq nil', function()
ok(eq(nil, nil), 'nil == nil')
nok(eq("", nil), '"" != nil')
nok(eq(nil, ""), 'nil != ""')
end, {'nil == nil', '"" != nil', 'nil != ""'}, {})
metatest('eq primitives', function()
ok(eq('foo', 'foo'), 'str == str')
nok(eq('foo', 'bar'), 'str != str')
ok(eq(123, 123), 'num == num')
nok(eq(123, 12), 'num != num')
end, {'str == str', 'str != str', 'num == num', 'num != num'}, {})
metatest('eq arrays', function()
ok(eq({}, {}), 'empty')
ok(eq({1, 2}, {1, 2}), 'equal')
nok(eq({1, 2}, {2, 1}), 'swapped')
nok(eq({1, 2, 3}, {1, 2}), 'longer')
nok(eq({1, 2}, {1, 2, 3}), 'shorter')
end, {'empty', 'equal', 'swapped', 'longer', 'shorter'}, {})
metatest('eq objects', function()
ok(eq({}, {}), 'empty')
ok(eq({a=1,b=2}, {a=1,b=2}), 'equal')
ok(eq({b=2,a=1}, {a=1,b=2}), 'swapped')
nok(eq({a=1,b=2}, {a=1,b=3}), 'not equal')
end, {'empty', 'equal', 'swapped', 'not equal'}, {})
metatest('eq nested objects', function()
ok(eq({
['1'] = { name = 'mhc', age = 28 },
['2'] = { name = 'arb', age = 26 }
}, {
['1'] = { name = 'mhc', age = 28 },
['2'] = { name = 'arb', age = 26 }
}), 'equal')
ok(eq({
['1'] = { name = 'mhc', age = 28 },
['2'] = { name = 'arb', age = 26 }
}, {
['1'] = { name = 'mhc', age = 28 },
['2'] = { name = 'arb', age = 27 }
}), 'not equal')
end, {'equal'}, {'not equal', 'different numbers expected 26 vs. 27'})
metatest('eq functions', function()
ok(eq(function(x) return x end, function(x) return x end), 'equal')
nok(eq(function(z) return x + z end, function(z) return y + z end), 'wrong variable') -- luacheck: ignore
nok(eq(function(x) return x*2 end, function(x) return x+2 end), 'wrong code')
end, {'equal', 'wrong variable', 'wrong code'}, {})
metatest('eq different types', function()
local eqos = eq({a=1,b=2}, "text")
ok(eq(eqos.msg, "type 1 is table, type 2 is string"), 'object/string')
local eqfn = eq(function(x) return x end, 12)
ok(eq(eqfn.msg, "type 1 is function, type 2 is number"), 'function/int')
nok(eq(12, "Hallo"), 'int/string')
end, {"object/string", "function/int", 'int/string'}, {})
--
-- Spies
--
metatest('spy called', function()
local f = spy()
ok(not f.called or #f.called == 0, 'not called')
f()
ok(f.called, 'called')
ok(#f.called == 1, 'called once')
f()
ok(#f.called == 2, 'called twice')
end, {'not called', 'called', 'called once', 'called twice'}, {})
metatest('spy with arguments', function()
local x = 0
local function setX(n) x = n end
local f = spy(setX)
f(1)
ok(x == 1, 'x == 1')
ok(eq(f.called, {{1}}), 'called with 1')
f(42)
ok(x == 42, 'x == 42')
ok(eq(f.called, {{1}, {42}}), 'called with 42')
end, {'x == 1', 'called with 1', 'x == 42', 'called with 42'}, {})
metatest('spy with nils', function()
local function nils(a, _, b) return a, nil, b, nil end
local f = spy(nils)
local r1, r2, r3, r4 = f(1, nil, 2)
ok(eq(f.called, {{1, nil, 2}}), 'nil in args')
ok(r1 == 1 and r2 == nil and r3 == 2 and r4 == nil, 'nil in returns')
end, {'nil in args', 'nil in returns'}, {})
metatest('spy with exception', function()
local function throwSomething(s)
if s ~= 'nopanic' then error('panic: '..s) end
end
local f = spy(throwSomething)
f('nopanic')
ok(f.errors == nil, 'no errors yet')
f('foo')
ok(eq(f.called, {{'nopanic'}, {'foo'}}), 'args ok')
ok(f.errors[1] == nil and f.errors[2] ~= nil, 'thrown ok')
end, {'no errors yet', 'args ok', 'thrown ok'}, {})
metatest('another spy with exception', function()
local f = spy(function() local a = unknownVariable + 1 end) -- luacheck: ignore
f()
ok(f.errors[1], 'exception thrown')
end, {'exception thrown'}, {})
metatest('spy should return a value', function()
local f = spy(function() return 5 end)
ok(f() == 5, 'spy returns a value')
local g = spy()
ok(g() == nil, 'default spy returns undefined')
end, {'spy returns a value', 'default spy returns undefined'}, {})
--
-- fail tests
--
metatest('fail with correct errormessage', function()
fail(function() error("my error") end, "my error", "Failed with correct error")
ok(true, 'reachable code')
end, {'Failed with correct error', 'reachable code'}, {})
metatest('fail with incorrect errormessage', function()
fail(function() error("my error") end, "different error", "Failed with incorrect error")
ok(true, 'unreachable code')
end, {}, {'Failed with incorrect error',
'expected errormessage "NTest_test.lua:169: my error" to contain "different error"'})
metatest('fail with incorrect errormessage default message', function()
fail(function() error("my error") end, "different error")
ok(true, 'unreachable code')
end, {}, {'NTest_test.lua:175',
'expected errormessage "NTest_test.lua:175: my error" to contain "different error"'})
metatest('fail with not failing code', function()
fail(function() end, "my error", "did not fail")
ok(true, 'unreachable code')
end, {}, {"did not fail", 'Expected to fail with Error containing "my error"'})
metatest('fail with failing code', function()
fail(function() error("my error") end, nil, "Failed as expected")
ok(true, 'reachable code')
end, {'Failed as expected', 'reachable code'}, {})
metatest('fail with not failing code', function()
fail(function() end, nil , "did not fail")
ok(true, 'unreachable code')
end, {}, {"did not fail", 'Expected to fail with Error'})
metatest('fail with not failing code default message', function()
fail(function() end)
ok(true, 'unreachable code')
end, {}, {"NTest_test.lua:196", 'Expected to fail with Error'})
metatest('=== load more tests ===', function()
load_tests2()
end, {}, {})
end
load_tests2 = function()
--
-- except tests
--
metatest('error should panic', function()
error("lua error")
ok(true, 'unreachable code')
end, {}, {}, {'NTest_test.lua:211: lua error'})
--
-- called function except
--
local function subfuncerror()
error("lua error")
end
metatest('subroutine error should panic', function()
subfuncerror()
ok(true, 'unreachable code')
end, {}, {}, {'NTest_test.lua:220: lua error'})
local function subfuncok()
ok(false)
end
metatest('subroutine ok should fail', function()
subfuncok()
ok(true, 'unreachable code')
end, {}, {'NTest_test.lua:229'})
--drain_post_queue()
--
-- Async tests
--
metatest('async test', function(next)
async(function()
ok(true, 'bar')
async(function()
ok(true, 'baz')
next()
end)
end)
ok(true, 'foo')
end, {'foo', 'bar', 'baz'}, {}, {}, true)
metatest('async test without actually async', function(next)
ok(true, 'bar')
next()
end, {'bar'}, {}, {}, true)
metatest('async fail in main', function(--[[ next ]])
ok(false, "async fail")
ok(true, 'unreachable code')
end, {}, {'async fail'}, {}, true)
--drain_post_queue()
metatest('another async test after async queue drained', function(next)
async(function()
ok(true, 'bar')
next()
end)
ok(true, 'foo')
end, {'foo', 'bar'}, {}, {}, true)
--
-- except tests async
--
metatest('async except in main', function(--[[ next ]])
error("async except")
ok(true, 'unreachable code')
end, {}, {}, {'NTest_test.lua:277: async except'}, true)
metatest('async fail in callback', function(next)
async(function()
ok(false, "async fail")
next()
end)
ok(true, 'foo')
end, {'foo'}, {'async fail'}, {}, true)
metatest('async except in callback', function(next)
async(function()
error("async Lua error")
next()
end)
ok(true, 'foo')
end, {'foo'}, {}, {'NTest_test.lua:291: async Lua error'}, true)
--
-- sync after async test
--
local marker = 0
metatest('set marker async', function(next)
async(function()
marker = "marked"
ok(true, 'async bar')
next()
end)
ok(true, 'foo')
end, {'foo', 'async bar'}, {}, {}, true)
metatest('check marker async', function()
ok(eq(marker, "marked"), "marker check")
end, {"marker check"}, {})
--
-- coroutine async tests
--
metatest('coroutine pass', function(--[[ getCB, waitCB ]])
ok(true, "passing")
end, {"passing"}, {}, {}, "co")
metatest('coroutine fail in main', function(--[[ getCB, waitCB ]])
ok(false, "coroutine fail")
ok(true, 'unreachable code')
end, {}, {'coroutine fail'}, {}, "co")
metatest('coroutine fail in main', function(--[[ getCB, waitCB ]])
nok(true, "coroutine fail")
nok(false, 'unreachable code')
end, {}, {'coroutine fail'}, {}, "co")
metatest('coroutine fail error', function(--[[ getCB, waitCB ]])
fail(function() error("my error") end, "my error", "Failed with correct error")
fail(function() error("my error") end, "other error", "Failed with other error")
ok(true, 'unreachable code')
end, {'Failed with correct error'}, {'Failed with other error',
'expected errormessage "NTest_test.lua:333: my error" to contain "other error"'}, {}, "co")
metatest('coroutine except in main', function(--[[ getCB, waitCB ]])
error("coroutine except")
ok(true, 'unreachable code')
end, {}, {}, {'NTest_test.lua:339: coroutine except'}, "co")
--local function coasync(f) table.insert(post_queue, 1, f) end
local function coasync(f, p1, p2) node.task.post(node.task.MEDIUM_PRIORITY, function() f(p1,p2) end) end
metatest('coroutine with callback', function(getCB, waitCB)
coasync(getCB("testCb"))
local name = waitCB()
ok(eq(name, "testCb"), "cb")
end, {"cb"}, {}, {}, "co")
metatest('coroutine with callback with values', function(getCB, waitCB)
coasync(getCB("testCb"), "p1", 2)
local name, p1, p2 = waitCB()
ok(eq(name, "testCb"), "cb")
ok(eq(p1, "p1"), "p1")
ok(eq(p2, 2), "p2")
end, {"cb", "p1", "p2"}, {}, {}, "co")
metatest('coroutine fail after callback', function(getCB, waitCB)
coasync(getCB("testCb"))
local name = waitCB()
ok(eq(name, "testCb"), "cb")
ok(false, "fail")
ok(true, 'unreachable code')
end, {"cb"}, {"fail"}, {}, "co")
metatest('coroutine except after callback', function(getCB, waitCB)
coasync(getCB("testCb"))
local name = waitCB()
ok(eq(name, "testCb"), "cb")
error("error")
ok(true, 'unreachable code')
end, {"cb"}, {}, {"NTest_test.lua:372: error"}, "co")
--- detect stray callbacks after the test has finished
local strayCb
local function rewrap()
coasync(strayCb)
end
metatest('leave stray callback', function(getCB--[[ , waitCB ]])
strayCb = getCB("testa")
coasync(rewrap)
end, {}, {}, {}, "co")
metatest('test after stray cb', function(--[[ getCB, waitCB ]])
end, {}, {"Found stray Callback 'testa' from test 'leave stray callback'"}, {}, "co")
--
-- Finalize: check test results
--
metatest("finishing up pending tests", function()
for i = 1,#expected -1 do
print("--- FAIL "..expected[i].name..' (pending)')
failed = failed + 1
end
print("failed: "..failed, "passed: "..passed)
node = orig_node -- luacheck: ignore 121 (setting read-only global variable)
end, {}, {})
end -- load_tests()
local cbWrap = function(cb) return cb end
-- implement pseudo task handling for on host testing
local drain_post_queue = function() end
if not node then
local post_queue = {{},{},{}}
drain_post_queue = function()
while #post_queue[1] + #post_queue[2] + #post_queue[3] > 0 do
for i = 3, 1, -1 do
if #post_queue[i] > 0 then
local f = table.remove(post_queue[i], 1)
if f then
f()
end
break
end
end
end
end
-- luacheck: push ignore 121 122 (setting read-only global variable)
node = {}
node.task = {LOW_PRIORITY = 1, MEDIUM_PRIORITY = 2, HIGH_PRIORITY = 3}
node.task.post = function (p, f)
table.insert(post_queue[p], f)
end
local errorfunc
node.setonerror = function(fn) errorfunc = fn end
-- luacheck: pop
cbWrap = function(cb)
return function(...)
local ok, p1,p2,p3,p4 = pcall(cb, ...)
if not ok then
if errorfunc then
errorfunc(p1)
else
print(p1, "::::::::::::: reboot :::::::::::::")
end
else
return p1,p2,p3,p4
end
end
end
end
-- Helper function to print arrays
local function stringify(t)
local s = ''
for i=1,#(t or {}) do
s = s .. '"' .. t[i] .. '"' .. ', '
end
return s:gsub('..$', '')
end
local pass
-- Set meta test handler
N.report(function(e, test, msg, errormsg)
local function consumemsg(msg, area) -- luacheck: ignore
if not expected[1][area][1] then
print("--- FAIL "..expected[1].name..' ('..area..'ed): unexpected "'..
msg..'"')
pass = false
return
end
if msg ~= expected[1][area][1] then
print("--- FAIL "..expected[1].name..' ('..area..'ed): expected "'..
expected[1][area][1]..'" vs "'..
msg..'"')
pass = false
end
table.remove(expected[1][area], 1)
end
local function consumeremainder(area)
if #expected[1][area] > 0 then
print("--- FAIL "..expected[1].name..' ('..area..'ed): expected ['..
stringify(expected[1][area])..']')
pass = false
end
end
if e == 'begin' then
pass = true
elseif e == 'end' then
consumeremainder("pass")
consumeremainder("fail")
consumeremainder("except")
if pass then
print("+++ Pass "..expected[1].name)
passed = passed + 1
else
failed = failed + 1
end
table.remove(expected, 1)
elseif e == 'pass' then
consumemsg(msg, "pass")
if errormsg then consumemsg(errormsg, "pass") end
elseif e == 'fail' then
consumemsg(msg, "fail")
if errormsg then consumemsg(errormsg, "fail") end
elseif e == 'except' then
consumemsg(msg, "except")
if errormsg then consumemsg(errormsg, "except") end
elseif e == 'start' or e == 'finish' then -- luacheck: ignore
-- ignore
else
print("Extra output: ", e, test, msg, errormsg)
end
end)
local async_queue = {}
async = function(f) table.insert(async_queue, cbWrap(f)) end
local function async_next()
local f = table.remove(async_queue, 1)
if f then
f()
end
end
local function drain_async_queue()
while #async_queue > 0 do
async_next()
end
end
metatest = function(name, f, expectedPassed, expectedFailed, expectedExcept, asyncMode)
local ff = f
if asyncMode then
ff = function(...)
f(...)
drain_async_queue()
end
if (asyncMode == "co") then
N.testco(name,ff)
else
N.testasync(name, ff)
end
else
N.test(name, ff)
end
table.insert(expected, {
name = name,
pass = expectedPassed,
fail = expectedFailed,
except = expectedExcept or {}
})
end
load_tests()
drain_post_queue()