Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Nov 20, 2020. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlunit.lua
More file actions
Latest commit
670 lines (542 loc) · 17.1 KB
/
Copy pathlunit.lua
File metadata and controls
670 lines (542 loc) · 17.1 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
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
--[[--------------------------------------------------------------------------
This file is part of lunit 0.5.
For Details about lunit look at: http://www.mroth.net/lunit/
Author: Michael Roth <mroth@nessie.de>
Copyright (c) 2004, 2006-2009 Michael Roth <mroth@nessie.de>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--]]--------------------------------------------------------------------------
localorig_assert=assert
localpairs=pairs
localipairs=ipairs
localnext=next
localtype=type
localerror=error
localtostring=tostring
localstring_sub=string.sub
localstring_format=string.format
module("lunit", package.seeall) -- FIXME: Remove package.seeall
locallunit=_M
local__failure__= {} -- Type tag for failed assertions
localtypenames= { "nil", "boolean", "number", "string", "table", "function", "thread", "userdata" }
localtraceback_hide-- Traceback function which hides lunit internals
localmypcall-- Protected call to a function with own traceback
do
local_tb_hide=setmetatable( {}, {__mode="k"} )
functiontraceback_hide(func)
_tb_hide[func] =true
end
localfunctionmy_traceback(errobj)
ifis_table(errobj) anderrobj.type==__failure__then
localinfo=debug.getinfo(5, "Sl") -- FIXME: Hardcoded integers are bad...
errobj.where=string_format( "%s:%d", info.short_src, info.currentline)
else
errobj= { msg=tostring(errobj) }
errobj.tb= {}
locali=2
whiletruedo
localinfo=debug.getinfo(i, "Snlf")
ifnotis_table(info) then
break
end
ifnot_tb_hide[info.func] then
localline= {} -- Ripped from ldblib.c...
line[#line+1] =string_format("%s:", info.short_src)
ifinfo.currentline>0then
line[#line+1] =string_format("%d:", info.currentline)
end
ifinfo.namewhat~="" then
line[#line+1] =string_format(" in function '%s'", info.name)
else
ifinfo.what=="main" then
line[#line+1] =" in main chunk"
elseifinfo.what=="C" orinfo.what=="tail" then
line[#line+1] =" ?"
else
line[#line+1] =string_format(" in function <%s:%d>", info.short_src, info.linedefined)
end
end
errobj.tb[#errobj.tb+1] =table.concat(line)
end
i=i+1
end
end
returnerrobj
end
functionmypcall(func)
orig_assert( is_function(func) )
localok, errobj=xpcall(func, my_traceback)
ifnotokthen
returnerrobj
end
end
traceback_hide(mypcall)
end
-- Type check functions
for_, typenameinipairs(typenames) do
lunit["is_"..typename] =function(x)
returntype(x) ==typename
end
end
localis_nil=is_nil
localis_boolean=is_boolean
localis_number=is_number
localis_string=is_string
localis_table=is_table
localis_function=is_function
localis_thread=is_thread
localis_userdata=is_userdata
localfunctionfailure(name, usermsg, defaultmsg, ...)
localerrobj= {
type=__failure__,
name=name,
msg=string_format(defaultmsg,...),
usermsg=usermsg
}
error(errobj, 0)
end
traceback_hide( failure )
localfunctionformat_arg(arg)
localargtype=type(arg)
ifargtype=="string" then
return"'"..arg.."'"
elseifargtype=="number" orargtype=="boolean" orargtype=="nil" then
returntostring(arg)
else
return"["..tostring(arg).."]"
end
end
functionfail(msg)
stats.assertions=stats.assertions+1
failure( "fail", msg, "failure" )
end
traceback_hide( fail )
functionassert(assertion, msg)
stats.assertions=stats.assertions+1
ifnotassertionthen
failure( "assert", msg, "assertion failed" )
end
returnassertion
end
traceback_hide( assert )
functionassert_true(actual, msg)
stats.assertions=stats.assertions+1
localactualtype=type(actual)
ifactualtype~="boolean" then
failure( "assert_true", msg, "true expected but was a "..actualtype )
end
ifactual~=truethen
failure( "assert_true", msg, "true expected but was false" )
end
returnactual
end
traceback_hide( assert_true )
functionassert_false(actual, msg)
stats.assertions=stats.assertions+1
localactualtype=type(actual)
ifactualtype~="boolean" then
failure( "assert_false", msg, "false expected but was a "..actualtype )
end
ifactual~=falsethen
failure( "assert_false", msg, "false expected but was true" )
end
returnactual
end
traceback_hide( assert_false )
functionassert_equal(expected, actual, msg)
stats.assertions=stats.assertions+1
ifexpected~=actualthen
failure( "assert_equal", msg, "expected %s but was %s", format_arg(expected), format_arg(actual) )
end
returnactual
end
traceback_hide( assert_equal )
functionassert_not_equal(unexpected, actual, msg)
stats.assertions=stats.assertions+1
ifunexpected==actualthen
failure( "assert_not_equal", msg, "%s not expected but was one", format_arg(unexpected) )
end
returnactual
end
traceback_hide( assert_not_equal )
functionassert_match(pattern, actual, msg)
stats.assertions=stats.assertions+1
localpatterntype=type(pattern)
ifpatterntype~="string" then
failure( "assert_match", msg, "expected the pattern as a string but was a "..patterntype )
end
localactualtype=type(actual)
ifactualtype~="string" then
failure( "assert_match", msg, "expected a string to match pattern '%s' but was a %s", pattern, actualtype )
end
ifnotstring.find(actual, pattern) then
failure( "assert_match", msg, "expected '%s' to match pattern '%s' but doesn't", actual, pattern )
end
returnactual
end
traceback_hide( assert_match )
functionassert_not_match(pattern, actual, msg)
stats.assertions=stats.assertions+1
localpatterntype=type(pattern)
ifpatterntype~="string" then
failure( "assert_not_match", msg, "expected the pattern as a string but was a "..patterntype )
end
localactualtype=type(actual)
ifactualtype~="string" then
failure( "assert_not_match", msg, "expected a string to not match pattern '%s' but was a %s", pattern, actualtype )
end
ifstring.find(actual, pattern) then
failure( "assert_not_match", msg, "expected '%s' to not match pattern '%s' but it does", actual, pattern )
end
returnactual
end
traceback_hide( assert_not_match )
functionassert_error(msg, func)
stats.assertions=stats.assertions+1
iffunc==nilthen
func, msg=msg, nil
end
localfunctype=type(func)
iffunctype~="function" then
failure( "assert_error", msg, "expected a function as last argument but was a "..functype )
end
localok, errmsg=pcall(func)
ifokthen
failure( "assert_error", msg, "error expected but no error occurred" )
end
end
traceback_hide( assert_error )
functionassert_error_match(msg, pattern, func)
stats.assertions=stats.assertions+1
iffunc==nilthen
msg, pattern, func=nil, msg, pattern
end
localpatterntype=type(pattern)
ifpatterntype~="string" then
failure( "assert_error_match", msg, "expected the pattern as a string but was a "..patterntype )
end
localfunctype=type(func)
iffunctype~="function" then
failure( "assert_error_match", msg, "expected a function as last argument but was a "..functype )
end
localok, errmsg=pcall(func)
ifokthen
failure( "assert_error_match", msg, "error expected but no error occurred" )
end
localerrmsgtype=type(errmsg)
iferrmsgtype~="string" then
failure( "assert_error_match", msg, "error as string expected but was a "..errmsgtype )
end
ifnotstring.find(errmsg, pattern) then
failure( "assert_error_match", msg, "expected error '%s' to match pattern '%s' but doesn't", errmsg, pattern )
end
end
traceback_hide( assert_error_match )
functionassert_pass(msg, func)
stats.assertions=stats.assertions+1
iffunc==nilthen
func, msg=msg, nil
end
localfunctype=type(func)
iffunctype~="function" then
failure( "assert_pass", msg, "expected a function as last argument but was a %s", functype )
end
localok, errmsg=pcall(func)
ifnotokthen
failure( "assert_pass", msg, "no error expected but error was: '%s'", errmsg )
end
end
traceback_hide( assert_pass )
-- lunit.assert_typename functions
for_, typenameinipairs(typenames) do
localassert_typename="assert_"..typename
lunit[assert_typename] =function(actual, msg)
stats.assertions=stats.assertions+1
localactualtype=type(actual)
ifactualtype~=typenamethen
failure( assert_typename, msg, typename.." expected but was a "..actualtype )
end
returnactual
end
traceback_hide( lunit[assert_typename] )
end
-- lunit.assert_not_typename functions
for_, typenameinipairs(typenames) do
localassert_not_typename="assert_not_"..typename
lunit[assert_not_typename] =function(actual, msg)
stats.assertions=stats.assertions+1
iftype(actual) ==typenamethen
failure( assert_not_typename, msg, typename.." not expected but was one" )
end
end
traceback_hide( lunit[assert_not_typename] )
end
functionlunit.clearstats()
stats= {
assertions=0;
passed=0;
failed=0;
errors=0;
}
end
localreport, reporterrobj
do
localtestrunner
functionlunit.setrunner(newrunner)
ifnot ( is_table(newrunner) oris_nil(newrunner) ) then
returnerror("lunit.setrunner: Invalid argument", 0)
end
localoldrunner=testrunner
testrunner=newrunner
returnoldrunner
end
functionlunit.loadrunner(name)
ifnotis_string(name) then
returnerror("lunit.loadrunner: Invalid argument", 0)
end
localok, runner=pcall( require, name )
ifnotokthen
returnerror("lunit.loadrunner: Can't load test runner: "..runner, 0)
end
returnsetrunner(runner)
end
functionreport(event, ...)
localf=testrunnerandtestrunner[event]
ifis_function(f) then
pcall(f, ...)
end
end
functionreporterrobj(context, tcname, testname, errobj)
localfullname=tcname.."." ..testname
ifcontext=="setup" then
fullname=fullname..":" ..setupname(tcname, testname)
elseifcontext=="teardown" then
fullname=fullname..":" ..teardownname(tcname, testname)
end
iferrobj.type==__failure__then
stats.failed=stats.failed+1
report("fail", fullname, errobj.where, errobj.msg, errobj.usermsg)
else
stats.errors=stats.errors+1
report("err", fullname, errobj.msg, errobj.tb)
end
end
end
localfunctionkey_iter(t, k)
return (next(t,k))
end
localtestcase
do
-- Array with all registered testcases
local_testcases= {}
-- Marks a module as a testcase.
-- Applied over a module from module("xyz", lunit.testcase).
functionlunit.testcase(m)
orig_assert( is_table(m) )
--orig_assert( m._M == m )
orig_assert( is_string(m._NAME) )
--orig_assert( is_string(m._PACKAGE) )
-- Register the module as a testcase
_testcases[m._NAME] =m
-- Import lunit, fail, assert* and is_* function to the module/testcase
m.lunit=lunit
m.fail=lunit.fail
forfuncname, funcinpairs(lunit) do
if"assert" ==string_sub(funcname, 1, 6) or"is_" ==string_sub(funcname, 1, 3) then
m[funcname] =func
end
end
end
-- Iterator (testcasename) over all Testcases
functionlunit.testcases()
-- Make a copy of testcases to prevent confusing the iterator when
-- new testcase are defined
local_testcases2= {}
fork,vinpairs(_testcases) do
_testcases2[k] =true
end
returnkey_iter, _testcases2, nil
end
functiontestcase(tcname)
return_testcases[tcname]
end
end
do
-- Finds a function in a testcase case insensitive
localfunctionfindfuncname(tcname, name)
forkey, valueinpairs(testcase(tcname)) do
ifis_string(key) andis_function(value) andstring.lower(key) ==namethen
returnkey
end
end
end
functionlunit.setupname(tcname)
returnfindfuncname(tcname, "setup")
end
functionlunit.teardownname(tcname)
returnfindfuncname(tcname, "teardown")
end
-- Iterator over all test names in a testcase.
-- Have to collect the names first in case one of the test
-- functions creates a new global and throws off the iteration.
functionlunit.tests(tcname)
localtestnames= {}
forkey, valueinpairs(testcase(tcname)) do
ifis_string(key) andis_function(value) then
locallfn=string.lower(key)
ifstring.sub(lfn, 1, 4) =="test" orstring.sub(lfn, -4) =="test" then
testnames[key] =true
end
end
end
returnkey_iter, testnames, nil
end
end
functionlunit.runtest(tcname, testname)
orig_assert( is_string(tcname) )
orig_assert( is_string(testname) )
localfunctioncallit(context, func)
iffuncthen
localerr=mypcall(func)
iferrthen
reporterrobj(context, tcname, testname, err)
returnfalse
end
end
returntrue
end
traceback_hide(callit)
report("run", tcname, testname)
localtc=testcase(tcname)
localsetup=tc[setupname(tcname)]
localtest=tc[testname]
localteardown=tc[teardownname(tcname)]
localsetup_ok=callit( "setup", setup )
localtest_ok=setup_okandcallit( "test", test )
localteardown_ok=setup_okandcallit( "teardown", teardown )
ifsetup_okandtest_okandteardown_okthen
stats.passed=stats.passed+1
report("pass", tcname, testname)
end
end
traceback_hide(runtest)
functionlunit.run()
clearstats()
report("begin")
fortestcasenameinlunit.testcases() do
-- Run tests in the testcases
fortestnameinlunit.tests(testcasename) do
runtest(testcasename, testname)
end
end
report("done")
returnstats
end
traceback_hide(run)
functionlunit.loadonly()
clearstats()
report("begin")
report("done")
returnstats
end
locallunitpat2luapat
do
localconv= {
["^"] ="%^",
["$"] ="%$",
["("] ="%(",
[")"] ="%)",
["%"] ="%%",
["."] ="%.",
["["] ="%[",
["]"] ="%]",
["+"] ="%+",
["-"] ="%-",
["?"] =".",
["*"] =".*"
}
functionlunitpat2luapat(str)
return"^" ..string.gsub(str, "%W", conv) .."$"
end
end
localfunctionin_patternmap(map, name)
ifmap[name] ==truethen
returntrue
else
for_, patinipairs(map) do
ifstring.find(name, pat) then
returntrue
end
end
end
returnfalse
end
-- Called from 'lunit' shell script.
functionmain(argv)
argv=argvor {}
-- FIXME: Error handling and error messages aren't nice.
localfunctioncheckarg(optname, arg)
ifnotis_string(arg) then
returnerror("lunit.main: option "..optname..": argument missing.", 0)
end
end
localfunctionloadtestcase(filename)
ifnotis_string(filename) then
returnerror("lunit.main: invalid argument")
end
localchunk, err=loadfile(filename)
iferrthen
returnerror(err)
else
chunk()
end
end
localtestpatterns=nil
localdoloadonly=false
localrunner=nil
locali=0
whilei<#argvdo
i=i+1
localarg=argv[i]
ifarg=="--loadonly" then
doloadonly=true
elseifarg=="--runner" orarg=="-r" then
localoptname=arg; i=i+1; arg=argv[i]
checkarg(optname, arg)
runner=arg
elseifarg=="--test" orarg=="-t" then
localoptname=arg; i=i+1; arg=argv[i]
checkarg(optname, arg)
testpatterns=testpatternsor {}
testpatterns[#testpatterns+1] =arg
elseifarg=="--" then
whilei<#argvdo
i=i+1; arg=argv[i]
loadtestcase(arg)
end
else
loadtestcase(arg)
end
end
loadrunner(runneror"lunit-console")
ifdoloadonlythen
returnloadonly()
else
returnrun(testpatterns)
end
end
clearstats()