Skip to content

Commit 5d98fa8

Browse files
committed
Made mock functions and methods into functables -- this will make it easier to look up the name of a function/method
1 parent 1a2f5a0 commit 5d98fa8

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

mock.lua

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@ local Mock = {}
22

33

44

5-
local callbacks
5+
local subscriber
66

77
function mockHandle(callback, thunk)
8-
callbacks[callback] = true
8+
subscriber = callback
99
thunk()
10-
callbacks[callback] = nil
10+
subscriber = nil
1111
end
1212

1313
function mockCalled(m, name, args)
14-
for callback in callbacks do
15-
callback(m, name, args)
16-
end
14+
return subscriber(m, name, args)
1715
end
1816

1917

@@ -136,25 +134,28 @@ end
136134

137135
function Mock:mockFunction(name)
138136
name = name or '<anonymous>'
139-
local f
137+
local f = {}
140138

141-
function f(...)
139+
function fCall(_, ...)
142140
return mockCalled(f, name, table.pack(...))
143141
end
144142

143+
setmetatable(f, {__call = fCall})
144+
145145
return f
146146
end
147147

148148
function Mock:mockMethod(name)
149149
name = name or '<anonymous>'
150-
local m
150+
local m = {}
151151

152-
function m(...)
152+
function mCall(_, _, ...)
153153
local args = table.pack(...)
154-
table.remove(args, 1)
155154
return mockCalled(m, name, args)
156155
end
157156

157+
setmetatable(m, {__call = mCall})
158+
158159
return m
159160
end
160161

0 commit comments

Comments
 (0)