-
Notifications
You must be signed in to change notification settings - Fork 0
/
DOCUMENTATION
245 lines (152 loc) · 6.48 KB
/
DOCUMENTATION
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
To use the lunit unit testing framework copy these files to your
lua search path or include it to your package:
lunit
lunit.lua
lunit-console.lua
To write a testcase, open the framework using require. The "lunit" shell script
works hard to find the "lunit.lua" and "lunit-console.lua" in all cases.
require "lunit"
Lunit uses the lua-5.1 module system. A testcase is a arbitrarily named module
marked with "lunit.testcase" as a testcase. An example:
require "lunit"
module( "my_testcase", lunit.testcase, package.seeall )
The tests itself in a testcase are functions whose names must begin
or end with 'test'. The function names are case insensitive. Example:
require "lunit"
module( "my_testcase", lunit.testcase, package.seeall )
function FirstTest()
-- Test code goes here
end
function test_something()
-- Test code goes here
end
Inside the test functions you use asserts to test your code or package.
Lunit defines 26 assert functions:
fail( [msg] )
Always fails.
assert( assertion, [msg] )
Fails, if 'assertion' is false or nil.
assert_true( actual, [msg] )
Fails, if 'actual' isn't true.
assert_false( actual, [msg] )
Fails, if 'actual' isn't false. (Even fails if 'actual' is
a nil value!)
assert_equal( expected, actual, [msg] )
Fails, if 'actual' is different from 'expected'. Make sure
that you don't mix 'expected' and 'actual' because they are
used to build a nice error message.
assert_not_equal( unexpected, actual, [msg] )
Fails, if 'actual' and 'unexpected' are equal.
assert_match( pattern, actual, [msg] )
Fails, if the string 'actual' doesn't match 'pattern'.
assert_not_match( pattern, actual, [msg] )
Fails, if the string 'actual' match 'pattern'.
assert_nil( actual, [msg] )
Fails, if 'actual' isn't a nil value.
assert_not_nil( actual, [msg] )
Fails, if 'actual' is a nil value.
assert_boolean( actual, [msg] )
Fails, if 'actual' isn't true or false.
assert_not_boolean( actual, [msg] )
Fails, if 'actual' is true or false.
assert_number( actual, [msg] )
Fails, if 'actual' isn't a number.
assert_not_number( actual, [msg] )
Fails, if 'actual' is a number.
assert_string( actual, [msg] )
Fails, if 'actual' isn't a string.
assert_not_string( actual, [msg] )
Fails, if 'actual' is a string.
assert_table( actual, [msg] )
Fails, if 'actual' isn't a table.
assert_not_table( actual, [msg] )
Fails, if 'actual' is a table.
assert_function( actual, [msg] )
Fails, if 'actual' isn't a function.
assert_not_function( actual, [msg] )
Fails, if 'actual' is a function.
assert_thread( actual, [msg] )
Fails, if 'actual' isn't a thread (created by
coroutine.create or coroutine.wrap).
assert_not_thread( actual, [msg] )
Fails, if 'actual' is a thread.
assert_userdata( actual, [msg] )
Fails, if 'actual' isn't userdata.
assert_not_userdata( actual, [msg] )
Fails, if 'actual' is userdata.
assert_error( [msg], func )
Fails, if 'func' doesn't raises an error (using error()).
assert_pass( [msg], func )
Fails, if 'func' raises an error.
All assert functions take an optional message as the last argument. Only
assert_pass() and assert_error() require the optional message as the first
argument. The last argument of these two are functions.
There are also useful functions to test for the type of a value:
is_nil( actual )
is_boolean( actual )
is_number( actual )
is_string( actual )
is_table( actual )
is_function( actual )
is_thread( actual )
is_userdata( actual )
These all returns true if 'actual' is of correct type, otherwise false.
You use the assert functions and the is_type functions in your tests to check
your code or package. Example:
require "lunit"
module( "my_testcase", lunit.testcase, package.seeall )
function FirstTest()
local result = compute_some_value()
assert_string( result )
assert_equal("foobar", result)
end
function test_something()
local result = flip_coin() -- flip_coin returns at random 0 or 1
assert_number(result)
if result == 0 then
-- ok
elseif result == 1 then
-- ok
else
fail("flip_coin: invalid number: "..tostring(result))
end
end
You can define the functions setup() and teardown() if you have to allocate
some resources or obtain some handles for your tests.
The setup() function is called before every test and teardown() is
called after every test. Example:
require "lunit"
module( "resource_testcase", lunit.testcase, package.seeall )
local orig_content, handle
function setup()
orig_content = { "row 1", "row 2", "row 3" }
handle = database_open("test.db")
database_create_table(handle, ...)
database_fill_table(handle, orig_content, ...)
end
function teardown()
database_drop_table(handle, ...)
database_close(handle)
handle = nil
orig_content = nil
delete_file("test.db")
end
function test_select()
local content = database_select(handle, ...)
assert_table( content )
assert_equal( orig_content, content )
end
function test_insert()
database_insert(handle, "row 4", ...)
local content = database_select(handle, ...)
assert_table( content )
assert_equal( { "row 1", "row 2", "row 3", "row 4" }, content )
end
function test_delete()
database_delete(handle, "row 2", ...)
local content = database_select(handle, ...)
assert_table( content )
assert_equal( { "row 1", "row 3" }, content )
end
To run your testcases, simply use the shell script "lunit". Example:
# ./lunit my_testcase.lua