-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathhelpers.lua
More file actions
320 lines (254 loc) · 8.62 KB
/
helpers.lua
File metadata and controls
320 lines (254 loc) · 8.62 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
local Helpers = {}
local expect = include( "gluatest/expectations/expect.lua" )
local stubMaker = include( "gluatest/stubs/stubMaker.lua" )
------------------
-- Cleanup stuff--
------------------
local makeHookTable = function()
local trackedHooks = {}
local hook_Add = function( event, name, func, ... )
if not trackedHooks[event] then trackedHooks[event] = {} end
table.insert( trackedHooks[event], name )
if not isfunction( func ) and func.IsStub then
local givenStub = func
func = function( ... )
givenStub( ... )
end
end
return _G.hook.Add( event, name, func, ... )
end
local function cleanup()
for event, names in pairs( trackedHooks ) do
for _, name in ipairs( names ) do
_G.hook.Remove( event, name )
end
end
end
local newHookTable = setmetatable( {}, {
__index = function( _, key )
if key == "Add" then
return hook_Add
end
return rawget( _G.hook, key )
end,
__newindex = function( _, key, value )
rawset( _G.hook, key, value )
end
} )
return newHookTable, cleanup
end
local timerCount = 0
local function makeTimerTable()
local timerNames = {}
local timer_Create = function( identifier, delay, reps, func, ... )
table.insert( timerNames, identifier )
return timer.Create( identifier, delay, reps, func, ... )
end
local timer_Simple = function( delay, func )
local name = "simple_timer_" .. timerCount
timerCount = timerCount + 1
timer_Create( name, delay, 1, func )
end
local function cleanup()
for _, name in ipairs( timerNames ) do
timer.Remove( name )
end
end
return table.Inherit( { Create = timer_Create, Simple = timer_Simple }, timer ), cleanup
end
local function makeTestLibStubs()
local testHook, hookCleanup = makeHookTable()
local testTimer, timerCleanup = makeTimerTable()
local testEnv = {
hook = testHook,
timer = testTimer
}
local function cleanup()
hookCleanup()
timerCleanup()
end
return testEnv, cleanup
end
local function makeTestTools()
local stub, stubCleanup = stubMaker()
local tools = {
stub = stub,
expect = expect,
}
local function cleanup()
stubCleanup()
end
return tools, cleanup
end
local function makeTestEnv()
local testEnv, envCleanup = makeTestLibStubs()
local testTools, toolsCleanup = makeTestTools()
local function cleanup()
envCleanup()
toolsCleanup()
end
local env = setmetatable(
testTools,
{
__index = function( _, idx )
return testEnv[idx] or _G[idx]
end,
}
)
hook.Run( "GLuaTest_EnvCreated", env )
return env, cleanup
end
local function getLocals( thread, level )
local locals = {}
local i = 1
while true do
local name, value = debug.getlocal( thread, level, i )
if name == nil then break end
if name ~= "(*temporary)" then
table.insert( locals, { name, value == nil and "nil" or value } )
end
i = i + 1
end
return locals
end
-- OLD: FIXME: There has to be a better way to do this
-- NEW: Fixed by srlion :)
local function findStackInfo( thread, caseFunc, reason )
-- Step through the stack to find the first non-C function call. If no stack is found for the called function, it will point to case function. This case will only happen
-- when the function is tail called, and the error is thrown from the tail called function.
local lastInfoLevel, lastInfo
for level = 0, 20 do
local info = debug.getinfo( thread, level, "nSl" )
if info and info.short_src ~= "[C]" and not string.match( info.short_src, "/lua/gluatest/" ) then
lastInfoLevel, lastInfo = level, info
break
end
end
local locals
if not lastInfoLevel then
ErrorNoHalt(
"Failed to get a stack, probably returning a function that errored! " ..
"For example, 'return error('!')'\n"
)
lastInfo = debug.getinfo( caseFunc, "nSl" )
lastInfo.currentline = lastInfo.linedefined -- currentline will be -1, so we will point it to the line where the function was defined
locals = {} -- We can't get locals from a function that has tail call returns
else
-- We got info about the error, but if the error was thrown from calling a nil value 'thisdoesntexist()', we can't get the currentline (executing line) as it was a nil value!
-- Thankfully, the error message will contain the line number, so we can extract it from there.
if lastInfo.currentline == -1 then
local line = string.match( reason, ":(%d+):" )
if line then
lastInfo.currentline = tonumber( line )
end
end
locals = getLocals( thread, lastInfoLevel )
end
return lastInfo, locals
end
function Helpers.FailCallback( thread, caseFunc, reason )
if reason == "" then
ErrorNoHaltWithStack( "Received empty error reason in failCallback- ignoring " )
return
end
-- root/file/name.lua:420: Expectation Failed: Failure reason
-- root/file/name.lua:420: attempt to index nil value 'blah'
local reasonSpl = string.Split( reason, ": " )
if reasonSpl[2] == "Expectation Failed" then
table.remove( reasonSpl, 2 )
else
table.insert( reasonSpl, 2, "Unhandled" )
end
local cleanReason = table.concat( reasonSpl, ": ", 2, #reasonSpl )
local info, locals = findStackInfo( thread, caseFunc, reason )
return {
reason = cleanReason,
sourceFile = info.short_src,
lineNumber = info.currentline,
locals = locals,
thread = thread
}
end
function Helpers.MakeAsyncEnv( done, fail, onFailedExpectation )
-- TODO: How can we make Stubs safer in Async environments?
local stub, stubCleanup = stubMaker()
local testEnv, envCleanup = makeTestLibStubs()
local function cleanup()
envCleanup()
stubCleanup()
end
local env = setmetatable(
{
-- We manually catch expectation errors here in case
-- they're called in an async function
expect = function( subject )
local built = expect( subject )
local expected = built.to.expected
local recordedFailure = false
-- Wrap the error-throwing function
-- and handle the error with the correct context
built.to.expected = function( ... )
if recordedFailure then return end
local _, errInfo = Helpers.SafeRunFunction( expected, ... )
onFailedExpectation( errInfo )
recordedFailure = true
end
return built
end,
done = done,
fail = fail,
stub = stub,
},
{
__index = function( _, idx )
return testEnv[idx] or _G[idx]
end
}
)
hook.Run( "GLuaTest_AsyncEnvCreated", env )
return env, cleanup
end
function Helpers.SafeRunWithEnv( defaultEnv, before, func, state )
local testEnv, cleanup = makeTestEnv()
local ranExpect = false
local ogExpect = testEnv.expect
testEnv.expect = function( ... )
ranExpect = true
testEnv.expect = ogExpect
return ogExpect( ... )
end
setfenv( before, testEnv )
before( state )
setfenv( before, defaultEnv )
setfenv( func, testEnv )
local success, errInfo = Helpers.SafeRunFunction( func, state )
setfenv( func, defaultEnv )
cleanup()
-- If it succeeded but never ran `expect`, it's an empty test
if success and not ranExpect then
return nil, nil
end
return success, errInfo
end
function Helpers.SafeRunFunction( func, ... )
local co = coroutine.create( func )
local success, err = coroutine.resume( co, ... )
local errInfo
if not success then
errInfo = Helpers.FailCallback( co, func, err )
end
return success, errInfo
end
function Helpers.CreateCaseState( testGroupState )
return setmetatable( {}, {
__index = function( self, idx )
if testGroupState[idx] ~= nil then
return testGroupState[idx]
end
if rawget( self, idx ) ~= nil then
return rawget( self, idx )
end
end
} )
end
return Helpers