forked from bluebird75/luaunit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_with_luaunit.lua
executable file
·145 lines (119 loc) · 3.45 KB
/
example_with_luaunit.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
#!/usr/bin/env lua
local lu = require('luaunit')
TestToto = {} --class
function TestToto:setUp()
-- set up tests
self.a = 1
self.s = 'hop'
self.t1 = {1,2,3}
self.t2 = {one=1,two=2,three=3}
self.t3 = {1,2,three=3}
end
function TestToto:test1_withFailure()
-- print( "some stuff test 1" )
lu.assertEquals( self.a , 1 )
-- will fail
lu.assertEquals( self.a , 2 )
lu.assertEquals( self.a , 2 )
end
function TestToto:test2_withFailure()
-- print( "some stuff test 2" )
lu.assertEquals( self.a , 1 )
lu.assertEquals( self.s , 'hop' )
-- will fail
lu.assertEquals( self.s , 'bof' )
lu.assertEquals( self.s , 'bof' )
end
function TestToto:test3()
-- print( "some stuff test 3" )
lu.assertEquals( self.a , 1 )
lu.assertEquals( self.s , 'hop' )
lu.assertEquals( type(self.a), 'number' )
end
function TestToto:test4()
-- print( "some stuff test 4" )
lu.assertNotEquals( self.a , 1 )
end
function TestToto:test5()
-- print( "some stuff test 5" )
lu.assertEvalToTrue( self.a )
lu.assertEvalToFalse( self.a ) -- will trigger the failure
end
function TestToto:test6()
-- print( "some stuff test 6" )
lu.assertTrue( true )
lu.assertFalse( false )
lu.assertEvalToFalse( nil )
lu.assertFalse( nil ) -- trigger the failure assertFalse is strict
end
function TestToto:test7()
-- assertEquals( {1,2}, self.t1 )
-- assertEquals( {1,2}, self.t2 )
lu.assertEquals( {1,2}, self.t3 )
end
function TestToto:test8a()
-- failure occurs in a submethod
self:funcWithError()
end
function TestToto:test8b()
-- failure occurs in a submethod
self:funcWithFuncWithError()
end
function TestToto:funcWithFuncWithError()
self:funcWithError()
end
function TestToto:funcWithError()
error('Bouhouhoum error!')
end
-- class TestTiti
TestTiti = {} --class
function TestTiti:setUp()
-- set up tests
self.a = 1
self.s = 'hop'
-- print( 'TestTiti:setUp' )
end
function TestTiti:tearDown()
-- some tearDown() code if necessary
-- print( 'TestTiti:tearDown' )
end
function TestTiti:test1_withFailure()
-- print( "some stuff test 1" )
lu.assertEquals( self.a , 1 )
-- will fail
lu.assertEquals( self.a , 2 )
lu.assertEquals( self.a , 2 )
end
function TestTiti:test2_withFailure()
-- print( "some stuff test 2" )
lu.assertEquals( self.a , 1 )
lu.assertEquals( self.s , 'hop' )
-- will fail
lu.assertEquals( self.s , 'bof' )
lu.assertEquals( self.s , 'bof' )
end
function TestTiti:test3()
-- print( "some stuff test 3" )
lu.assertEquals( self.a , 1 )
lu.assertEquals( self.s , 'hop' )
end
-- class TestTiti
-- simple test functions that were written previously can be integrated
-- in luaunit too
function test1_withFailure()
assert( 1 == 1)
-- will fail
assert( 1 == 2)
end
function test2_withFailure()
assert( 'a' == 'a')
-- will fail
assert( 'a' == 'b')
end
function test3()
assert( 1 == 1)
assert( 'a' == 'a')
end
local runner = lu.LuaUnit.new()
runner:setOutputType("tap")
os.exit( runner:runSuite() )