-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcwtest.test.tl
126 lines (115 loc) · 4.04 KB
/
cwtest.test.tl
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
local cwtest = require "cwtest"
local interface ExtendedTester is cwtest.Tester
last_success: function(self: cwtest.Tester, tpl: string, ...: any):(boolean)
last_failure: function(self: cwtest.Tester, tpl: string, ...: any):(boolean)
end
local T = cwtest.new() as ExtendedTester
local C = cwtest.new() -- tested instance
C.printf = function() end
C.eprintf = function() end
local short_src = debug.getinfo(1).short_src
local exp_got_tpl = "\n expected: %s\n got: %s"
local pw = assert(cwtest.pretty_write)
local _line = function(status: string, before: integer, tpl: string, ...: any): string
assert(
(type(status) == "string") and
(type(before) == "number") and
(type(tpl) == "string")
)
local s = (select('#', ...) == 0) and tpl or string.format(tpl, ...)
return string.format(
"[%s] %s line %s%s", status, short_src,
(debug.getinfo(3).currentline - before), s
)
end
T.last_success = function(self: cwtest.Tester, tpl: string, ...: any): boolean
local l = _line("OK", 1, tpl, ...)
local successes = C:successes()
local s = successes[#successes]
local r: boolean
if s == l then
r = self.pass_tpl(self, exp_got_tpl, l, s)
else
r = self.fail_tpl(self, exp_got_tpl, l, s)
end
return r
end
T.last_failure = function(self: cwtest.Tester, tpl: string, ...: any): boolean
local l = _line("KO", 1, tpl, ...)
local failures = C:failures()
local s = failures[#failures]
local r: boolean
if s == l then
r = self.pass_tpl(self, exp_got_tpl, l, s)
else
r = self.fail_tpl(self, exp_got_tpl, l, s)
end
return r
end
T:start("successes", 26); do
C:start("successes")
T:eq( C:successes(), {} )
T:eq( C:failures(), {} )
T:yes(C:yes( 0 ))
T:last_success( " (assertion)" )
T:yes(C:yes( true, " foo" ))
T:last_success( " foo" )
T:yes(C:yes( true, " %s", "foo" ))
T:last_success( " foo" )
T:yes(C:no( nil ))
T:last_success( " (assertion)" )
T:yes(C:no( false, " %s bar %d", "foo", 42 ))
T:last_success( " foo bar 42" )
T:yes(C:eq( 3*2, 6 ))
T:last_success( exp_got_tpl, 6, 6 )
local _i: any
local _o: any
_i, _o = {1, 2, a = {b = 42}}, {[2] = 2, a = {b = 42}, [1] = 1}
T:yes(C:eq( _i, _o ))
T:last_success( exp_got_tpl, pw(_o), pw(_i) )
_i, _o = {2, {x = 2.5}, 3}, {3, 2, {x = 2.5}}
T:yes(C:neq( _i, _o ))
T:last_success( " (%s != %s)", pw(_i), pw(_o) )
T:yes(C:seq( _i, _o ))
T:last_success( exp_got_tpl, pw(_o), pw(_i) )
T:yes(C:err( function() error("foo", 0) end, "foo" ))
T:last_success( ": error [[foo]] caught" )
T:yes(C:err( function() error("foo", 0) end ))
T:last_success( ": error caught" )
T:eq( C:failures(), {} )
T:eq(C:done(), true)
end; T:done()
T:start("failures", 28); do
C:start("failures")
T:eq( C:successes(), {} )
T:eq( C:failures(), {} )
T:no(C:yes( false ))
T:last_failure( " (assertion)" )
T:no(C:yes( nil, " foo" ))
T:last_failure( " foo" )
T:no(C:yes( false, " %s bar %d", "foo", 42 ))
T:last_failure( " foo bar 42" )
T:no(C:no( "" ))
T:last_failure( " (assertion)" )
T:no(C:no( {s = "e"}, " %d", 6*7 ))
T:last_failure( " 42" )
local _i, _o = {2, {x = 2.5}, 3}, {3, 2, {x = 2.5}}
T:no(C:eq( _i, _o ))
T:last_failure( exp_got_tpl, pw(_o), pw(_i) )
_i, _o = {1}, {nil, 1}
T:no(C:eq( _i, _o ))
T:last_failure( exp_got_tpl, pw(_o), pw(_i) )
T:no(C:seq( _i, _o ))
T:last_failure( exp_got_tpl, pw(_o), pw(_i) )
T:no(C:neq( {nil, nil, nil}, {} ))
T:last_failure( " ({} == {})" )
T:no(C:err( function() error("foo", 0) end, "bar" ))
T:last_failure( "\n expected error: bar\n got error: foo" )
T:no(C:err( function():(integer) return 42 end, "foo" ))
T:last_failure( "\n expected error: foo\n got: %s", pw({42}) )
T:no(C:err( function():(integer) return 42 end ))
T:last_failure( ": expected error, got %s", pw({42}) )
T:eq( C:successes(), {} )
T:eq(C:done(), false)
end; T:done()
T:exit()