-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtest.lua
221 lines (181 loc) · 5.95 KB
/
test.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
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
pcall(require, "luacov") --measure code coverage, if luacov is present
local lunatest = require "lunatest"
local assert_true, assert_false = lunatest.assert_true, lunatest.assert_false
local assert_diffvars = lunatest.assert_diffvars
local assert_boolean, assert_not_boolean = lunatest.assert_boolean, lunatest.assert_not_boolean
local assert_len, assert_not_len = lunatest.assert_len, lunatest.assert_not_len
local assert_match, assert_not_match = lunatest.assert_match, lunatest.assert_not_match
local assert_error = lunatest.assert_error
local assert_lt, assert_lte = lunatest.assert_lt, lunatest.assert_lte
local assert_gt, assert_gte = lunatest.assert_gt, lunatest.assert_gte
local assert_nil, assert_not_nil = lunatest.assert_nil, lunatest.assert_not_nil
local assert_equal, assert_not_equal = lunatest.assert_equal, lunatest.assert_not_equal
local assert_string, assert_not_string = lunatest.assert_string, lunatest.assert_not_string
local assert_metatable, assert_not_metatable = lunatest.assert_metatable, lunatest.assert_not_metatable
local assert_userdata, assert_not_userdata = lunatest.assert_userdata, lunatest.assert_not_userdata
local assert_thread, assert_not_thread = lunatest.assert_thread, lunatest.assert_not_thread
local assert_function, assert_not_function = lunatest.assert_function, lunatest.assert_not_function
local assert_table, assert_not_table = lunatest.assert_table, lunatest.assert_not_table
local assert_number, assert_not_number = lunatest.assert_number, lunatest.assert_not_number
local skip, fail = lunatest.skip, lunatest.fail
print '=============================='
print('To test just the random suite, add "-s random" to the command line')
print('To run just some tests, add "-t [pattern]"')
print '=============================='
lunatest.suite("suite-with-random-tests")
lunatest.suite("suite-hooks")
lunatest.suite("suite-hooks-fail")
function test_fail()
-- the true here is so the test run as a whole still succeeds.
fail("This one *should* fail.", true)
end
function test_assert()
assert_true(true)
end
function test_skip()
skip("(reason why this test was skipped)")
end
function test_assert_false()
assert_false(false)
end
function test_assert_nil()
assert_nil(nil)
end
function test_assert_not_nil()
assert_not_nil("foo")
end
function test_assert_equal()
assert_equal(4, 4)
end
function test_assert_equal_tolerance()
assert_equal(4, 4.0001, 0.0001, "Should approximately match")
end
function test_assert_not_equal()
assert_not_equal("perl", "quality")
end
function test_assert_gt()
assert_gt(8, 400)
end
function test_assert_gte()
assert_gte(8, 400)
assert_gte(8, 8)
end
function test_assert_lt()
assert_lt(8, -2)
end
function test_assert_lte()
assert_lte(8, -2)
assert_lte(8, 8)
end
function test_assert_len()
assert_len(3, { "foo", "bar", "baz" })
end
function test_assert_not_len()
assert_not_len(23, { "foo", "bar", "baz" })
end
function test_assert_match()
assert_match("oo", "string with foo in it")
end
function test_assert_not_match()
assert_not_match("abba zabba", "foo")
end
function test_assert_boolean()
assert_boolean(true)
assert_boolean(false)
end
function test_assert_not_boolean()
assert_not_boolean("cheesecake")
end
function test_assert_number()
assert_number(47)
assert_number(0)
assert_number(math.huge)
assert_number(-math.huge)
end
function test_assert_not_number()
assert_not_number(_G)
assert_not_number("abc")
assert_not_number({1, 2, 3})
assert_not_number(false)
assert_not_number(function () return 3 end)
end
function test_assert_string()
assert_string("yarn")
assert_string("")
end
function test_assert_not_string()
assert_not_string(23)
assert_not_string(true)
assert_not_string(false)
assert_not_string({"1", "2", "3"})
end
function test_assert_table()
assert_table({})
assert_table({"1", "2", "3"})
assert_table({ foo=true, bar=true, baz=true })
end
function test_assert_not_table()
assert_not_table(nil)
assert_not_table(23)
assert_not_table("lapdesk")
assert_not_table(false)
assert_not_table(function () return 3 end)
end
function test_assert_function()
assert_function(function() return "*splat*" end)
assert_function(string.format)
end
function test_assert_not_function()
assert_not_function(nil)
assert_not_function(23)
assert_not_function("lapdesk")
assert_not_function(false)
assert_not_function(coroutine.create(function () return 3 end))
assert_not_function({"1", "2", "3"})
assert_not_function({ foo=true, bar=true, baz=true })
end
function test_assert_thread()
assert_thread(coroutine.create(function () return 3 end))
end
function test_assert_not_thread()
assert_not_thread(nil)
assert_not_thread(23)
assert_not_thread("lapdesk")
assert_not_thread(false)
assert_not_thread(function () return 3 end)
assert_not_thread({"1", "2", "3"})
assert_not_thread({ foo=true, bar=true, baz=true })
end
function test_assert_userdata()
assert_userdata(io.open("test.lua", "r"))
end
function test_assert_not_userdata()
assert_not_userdata(nil)
assert_not_userdata(23)
assert_not_userdata("lapdesk")
assert_not_userdata(false)
assert_not_userdata(function () return 3 end)
assert_not_userdata({"1", "2", "3"})
assert_not_userdata({ foo=true, bar=true, baz=true })
end
function test_assert_metatable()
assert_metatable(getmetatable("any string"), "foo")
local t = { __index=string }
local val = setmetatable( { 1 }, t)
assert_metatable(t, val)
end
function test_assert_not_metatable()
assert_not_metatable(getmetatable("any string"), 23)
end
function test_assert_error()
assert_error(function ()
error("*crash!*")
end)
end
-- This caused a crash when matching a string with invalid % escapes.
-- Thanks to Diab Jerius for the bugfix.
function test_failure_formatting()
local inv_esc = "str with invalid escape %( in it"
assert_match(inv_esc, inv_esc, "Should fail but not crash")
end
lunatest.run()