-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcwtest.lua
More file actions
538 lines (412 loc) · 11.7 KB
/
cwtest.lua
File metadata and controls
538 lines (412 loc) · 11.7 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
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
local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local assert = _tl_compat and _tl_compat.assert or assert; local debug = _tl_compat and _tl_compat.debug or debug; local io = _tl_compat and _tl_compat.io or io; local math = _tl_compat and _tl_compat.math or math; local os = _tl_compat and _tl_compat.os or os; local pairs = _tl_compat and _tl_compat.pairs or pairs; local pcall = _tl_compat and _tl_compat.pcall or pcall; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table; local VERSION = "3.0"
local deepcompare
local function deepcompare_tables(t1, t2)
local mt = getmetatable(t1)
if mt and mt.__eq then return t1 == t2 end
for k1 in pairs(t1) do
if t2[k1] == nil then return false end
end
for k2 in pairs(t2) do
if t1[k2] == nil then return false end
end
for k1, v1 in pairs(t1) do
local v2 = t2[k1]
if not deepcompare(v1, v2) then return false end
end
return true
end
deepcompare = function(t1, t2)
local ty1 = type(t1)
local ty2 = type(t2)
if ty1 ~= ty2 then return false end
if ty1 ~= "table" then return t1 == t2 end
return deepcompare_tables(t1, t2)
end
local function compare_no_order_tables(
t1, t2, cmp)
if #t1 ~= #t2 then return false end
local visited = {}
for i = 1, #t1 do
local val = t1[i]
local gotcha = 0
for j = 1, #t2 do
if not visited[j] then
if cmp(val, t2[j]) then
gotcha = j
break
end
end
end
if not gotcha then return false end
visited[gotcha] = true
end
return true
end
local function compare_no_order(
t1, t2, cmp)
cmp = cmp or deepcompare
if (type(t1) ~= "table") or (type(t2) ~= "table") then return false end
return compare_no_order_tables(t1, t2, cmp)
end
local function pretty_write(t)
local function quote(s)
if type(s) == "string" then
return string.format("%q", tostring(s))
else
return tostring(s)
end
end
if type(t) == "table" then
local r = { "{" }
for k, v in pairs(t) do
r[#r + 1] = "["
r[#r + 1] = quote(k)
r[#r + 1] = "]="
r[#r + 1] = pretty_write(v)
r[#r + 1] = ","
end
r[#r + 1] = "}"
return table.concat(r)
else
return quote(t)
end
end
local function printf(p, ...)
io.stdout:write(string.format(p, ...)); io.stdout:flush()
end
local function eprintf(p, ...)
io.stderr:write(string.format(p, ...))
end
local function running(self)
return not not self._suite
end
local function m_successes(self)
assert(self:running(), "tester must be running")
assert(self._successes)
return self._successes
end
local function m_failures(self)
assert(self:running(), "tester must be running")
assert(self._failures)
return self._failures
end
local function m_suite(self)
assert(self:running(), "tester must be running")
assert(self._suite)
return self._suite
end
local function elapsed(self)
return #self:successes() + #self:failures()
end
local function log_success(self, tpl, ...)
assert(type(tpl) == "string")
local s = (select('#', ...) == 0) and tpl or string.format(tpl, ...)
local successes = self:successes()
successes[#successes + 1] = s
if self.tap then
self.printf(
" ok %d - %s %d\n",
self:elapsed(), self:suite().name, self:elapsed())
elseif self.verbosity == 2 then
self.printf("\n%s\n", s)
else
self.printf(".")
end
end
local function log_failure(self, tpl, ...)
assert(type(tpl) == "string")
local s = (select('#', ...) == 0) and tpl or string.format(tpl, ...)
local failures = self:failures()
failures[#failures + 1] = s
if self.tap then
self.printf(
" not ok %d - %s %d\n",
self:elapsed(), self:suite().name, self:elapsed())
elseif self.verbosity > 0 then
self.eprintf("\n%s\n", s)
else
self.printf("x")
end
end
local function pass_tpl(self, tpl, ...)
assert(type(tpl) == "string")
local info = debug.getinfo(3)
self:log_success(
"[OK] %s line %d%s",
info.short_src,
info.currentline,
(select('#', ...) == 0) and tpl or string.format(tpl, ...))
return true
end
local function fail_tpl(self, tpl, ...)
assert(type(tpl) == "string")
local info = debug.getinfo(3)
self:log_failure(
"[KO] %s line %d%s",
info.short_src,
info.currentline,
(select('#', ...) == 0) and tpl or string.format(tpl, ...))
return false
end
local function pass_assertion(self)
local info = debug.getinfo(3)
self:log_success(
"[OK] %s line %d (assertion)",
info.short_src,
info.currentline)
return true
end
local function fail_assertion(self)
local info = debug.getinfo(3)
self:log_failure(
"[KO] %s line %d (assertion)",
info.short_src,
info.currentline)
return false
end
local function pass_eq(self, x, y)
local info = debug.getinfo(3)
self:log_success(
"[OK] %s line %d\n expected: %s\n got: %s",
info.short_src,
info.currentline,
pretty_write(y),
pretty_write(x))
return true
end
local function fail_eq(self, x, y)
local info = debug.getinfo(3)
self:log_failure(
"[KO] %s line %d\n expected: %s\n got: %s",
info.short_src,
info.currentline,
pretty_write(y),
pretty_write(x))
return false
end
local function start(self, s, n)
assert((not (self._failures or self._successes)), "test already started")
assert(s, "no name given to test suite")
local suite = { name = s }
if type(n) == "number" then suite.plan = n end
self._suite, self._failures, self._successes = suite, {}, {}
if self.tap then
local tap = self.tap
if not tap.started then
tap.started = 0
if tap.plan then
self.printf("1..%d\n", tap.plan)
end
end
tap.started = tap.started + 1
if suite.plan then
self.printf(" 1..%d\n", suite.plan)
end
elseif self.verbosity > 0 then
self.printf("\n=== %s ===\n", s)
else
self.printf("%s ", s)
end
end
local function done(self)
local f, s = self:failures(), self:successes()
assert((f and s), "call start before done")
local suite = self:suite()
local bad_plan = suite.plan and (suite.plan ~= self:elapsed())
local failed = #f > 0 or bad_plan
local plan = string.format("%d OK, %d KO", #s, #f)
if suite.plan then
plan = string.format("%s, %d total", plan, suite.plan)
end
if self.tap then
local started = (self.tap).started
if not suite.plan then
self.printf(" 1..%d\n", #f + #s)
end
if failed then
self.printf("not ok %d - %s (%s)\n", started, suite.name, plan)
else
self.printf("ok %d - %s (%s)\n", started, suite.name, plan)
end
elseif failed then
if self.verbosity > 0 then
self.printf("\n=== FAILED (%s) ===\n", plan)
else
self.printf(" FAILED (%s)\n", plan)
for i = 1, #f do self.eprintf("\n%s\n", f[i]) end
self.printf("\n")
end
else
if self.verbosity > 0 then
self.printf("\n=== OK (%s) ===\n", plan)
else
self.printf(" OK (%s)\n", plan)
end
end
self._failures, self._successes, self._suite = nil, nil, nil
if failed then self.tainted = true end
return (not failed)
end
local function eq(self, x, y)
local ok = (x == y) or deepcompare(x, y)
local r = (ok and pass_eq or fail_eq)(self, x, y)
return r
end
local function neq(self, x, y)
local sx, sy = pretty_write(x), pretty_write(y)
local r
if deepcompare(x, y) then
r = fail_tpl(self, " (%s == %s)", sx, sy)
else
r = pass_tpl(self, " (%s != %s)", sx, sy)
end
return r
end
local function seq(self, x, y)
local ok = compare_no_order(x, y)
local r = (ok and pass_eq or fail_eq)(self, x, y)
return r
end
local function _assert_fun(x, ...)
if (select('#', ...) == 0) then
return (x and pass_assertion or fail_assertion)
else
return (x and pass_tpl or fail_tpl)
end
end
local function is_true(self, x, ...)
local r = _assert_fun(x, ...)(self, ...)
return r
end
local function is_false(self, x, ...)
local r = _assert_fun((not x), ...)(self, ...)
return r
end
local function err(self, f, e)
local r = { pcall(f) }
local res = false
if e == nil then
if r[1] then
table.remove(r, 1)
res = fail_tpl(
self,
": expected error, got %s",
pretty_write(r))
else
res = pass_tpl(self, ": error caught")
end
elseif type(e) == "string" then
if r[1] then
table.remove(r, 1)
res = fail_tpl(
self,
"\n expected error: %s\n got: %s",
e, pretty_write(r))
elseif r[2] ~= e then
res = fail_tpl(
self,
"\n expected error: %s\n got error: %s",
e, r[2])
else
res = pass_tpl(self, ": error [[%s]] caught", e)
end
else
assert(type(e) == "table")
local pattern = (e).matching
assert(type(pattern) == "string")
if r[1] then
table.remove(r, 1)
res = fail_tpl(
self,
"\n expected error, got: %s",
e, pretty_write(r))
else
assert(type(r[2]) == "string")
if not (r[2]):match(pattern) then
res = fail_tpl(
self,
"\n expected error matching: %q\n got error: %s",
pattern, r[2])
else
res = pass_tpl(self, ": error [[%s]] caught", e)
end
end
end
return res
end
local function exit(self)
if self.tap then
local tap = self.tap
if tap.started and not tap.plan then
self.printf("1..%d\n", tap.started)
end
end
os.exit(self.tainted and 1 or 0)
end
local methods = {
running = running,
successes = m_successes,
failures = m_failures,
suite = m_suite,
start = start,
done = done,
eq = eq,
neq = neq,
seq = seq,
yes = is_true,
no = is_false,
err = err,
exit = exit,
elapsed = elapsed,
log_success = log_success,
log_failure = log_failure,
pass_eq = pass_eq,
fail_eq = fail_eq,
pass_assertion = pass_assertion,
fail_assertion = fail_assertion,
pass_tpl = pass_tpl,
fail_tpl = fail_tpl,
}
local function new(args)
if not args then args = {} end
args = { verbosity = args.verbosity, tap = args.tap, env = args.env }
if args.env ~= false then
local _p = type(args.env) == "string" and (args.env) or "CWTEST_"
local v = math.tointeger(os.getenv(_p .. "VERBOSITY"))
if v then args.verbosity = v end
local v2 = os.getenv(_p .. "TAP")
if v2 == "" then
args.tap = nil
elseif v2 then
args.tap = math.tointeger(v2) or {}
end
end
if not (args.verbosity) then args.verbosity = 0 end
if type(args.verbosity) ~= "number" then args.verbosity = 1 end
assert(
(math.floor(args.verbosity) == args.verbosity) and
(args.verbosity >= 0) and (args.verbosity < 3))
if args.tap then
if type(args.tap) == "number" then
args.tap = { plan = args.tap }
elseif type(args.tap) ~= "table" then
args.tap = {}
end
end
local r = {
verbosity = args.verbosity,
tap = args.tap,
printf = printf,
eprintf = eprintf,
tainted = false,
}
return setmetatable(r, { __index = methods })
end
return {
new = new,
pretty_write = pretty_write,
deepcompare = deepcompare,
compare_no_order = compare_no_order,
Tester = Tester,
_VERSION = VERSION,
}