-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mock.lua
285 lines (255 loc) · 8.68 KB
/
Mock.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
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
---
-- Created by zhao guangyue ([email protected])
-- DateTime: 2020/3/1 9:45
-- Copyright (c) 2015 ostack. http://www.ostack.cn
-- This source code is licensed under BSD 3-Clause License (the "License").
-- You may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- https://opensource.org/licenses/BSD-3-Clause
--/
-------------------common use function---------------
local function isEq(expect, actual)
if expect ~= nil and actual ~= nil then
if 'table' == type(expect) then
return isTableEq(expect, actual)
else
return expect == actual
end
end
return false
end
local function isTableContains(tab, element)
if tab ~= nil and 'table' == type(tab) then
for key, val in pairs(tab) do
if type(val) == type(element) then
if isEq(element, val) then
return true
end
end
end
return true
end
return false
end
local function isTableEq(expect, actual)
if expect ~= nil and actual ~= nil then
if 'table' == type(expect) and 'table' == type(actual) and #expect == #actual then
for key, val in pairs(expect) do
if not isTableContains(actual, val) then
return false
end
end
return true
end
end
return false;
end
local function log(msg)
print("[LUA MOCK] " .. msg)
end
-------------------class for Error-------------------
local Error = {}
function Error:new(msg, code)
local err = { _l_err_code_ = code or 65535, _l_err_msg_ = msg or "" }
setmetatable(err, self)
self.__index = self;
return err
end
function Error:throw()
error(self._l_err_msg_, 0)
end
-------------------class for times--------------------
local Times = {}
function Times:new(times)
local times = { _l_times_num_ = times or 0 }
setmetatable(times, self)
self.__index = self
return times
end
function Times:set(times)
self._l_times_num_ = times
end
function Times:get()
return self._l_times_num_
end
function Times:increase(step)
self._l_times_num_ = self._l_times_num_ + step
end
function Times:equal(other)
local selfTimes = self._l_times_num_;
local otherTimes = other._l_times_num_;
return selfTimes ~= nil and otherTimes ~= nil and selfTimes == otherTimes
end
-------------------class for args--------------------
local Args = {}
function Args:new(...)
local args = { _l_args_val_ = { ... } }
setmetatable(args, self)
self.__index = self
return args
end
function Args:equal(other)
local selfArgs = self._l_args_val_
local otherArgs = other._l_args_val_
return isTableEq(selfArgs, otherArgs)
end
function Args:toString()
local argStr = ""
for key, val in pairs(self._l_args_val_) do
if argStr ~= "" then
argStr = argStr .. ","
end
argStr = argStr .. tostring(val)
end
return "(" .. argStr .. ")"
end
-------------------class for Method--------------------
local Method = {}
function Method:new(className, methodName)
local method = { _l_method_class_name = className, _l_method_name_ = methodName, _l_method_expect_calls_ = {}, _l_method_unexpect_calls_ = {}, _l_current_arg_ = {} }
method.mock = self.mock
method.with = self.with
method.result = self.result
method.times = self.times
method.willRtn = self.willRtn
method.verify = self.verify
return method
end
function Method:times(times)
self._l_current_arg_._l_method_expect_times:set(times);
return self
end
function Method:with(...)
self:mock(nil,0,...)
return self
end
function Method:willRtn(result)
self._l_current_arg_._l_method_rlt_ = result;
return self
end
function Method:mock(result, times, ...)
local args = Args:new(...)
local currentMethodArgs = self._l_current_arg_._l_method_args_
if currentMethodArgs ~= args then
local expectArgs = Method:createExpectCallArgs(result, times, args)
table.insert(self._l_method_expect_calls_, expectArgs)
self._l_current_arg_ = expectArgs;
else
self._l_current_arg_._l_method_rlt_ = result
self._l_current_arg_._l_method_expect_times = Times:new(1)
self._l_current_arg_._l_method_actual_times_ = Times:new(0)
end
return self
end
function Method:result(...)
local actualArgs = Args:new(...)
local expectArgs = self._l_method_expect_calls_
for key, val in pairs(expectArgs) do
if val._l_method_args_:equal(actualArgs) then
val._l_method_actual_times_:increase(1)
return val._l_method_rlt_
else
log("actualArgs:"..actualArgs:toString()..",mockArgs:"..val._l_method_args_:toString())
end
end
return Method:unExpectCalls(self, 1, ...)
end
function Method:verify()
local msg = Method:checkExpectCalls(self)
msg = msg .. Method:checkUnExpectCalls(self)
return msg
end
function Method:getName()
return self._l_method_class_name .. "." .. self._l_method_name_
end
------------------------private functions for method---------------------------------
function Method:checkExpectCalls(mockMethod)
local msg = ""
local expectCalls = mockMethod._l_method_expect_calls_
for key, val in pairs(expectCalls) do
if not val._l_method_expect_times:equal(val._l_method_actual_times_) then
msg = msg .. Method:buildErrMsg(mockMethod._l_method_class_name, mockMethod._l_method_name_, val)
end
end
return msg
end
function Method:checkUnExpectCalls(mockMethod)
local msg = ""
local unExpectCalls = mockMethod._l_method_unexpect_calls_
for key, val in pairs(unExpectCalls) do
msg = msg .. Method:buildErrMsg(mockMethod._l_method_class_name, mockMethod._l_method_name_, val)
end
return msg
end
function Method:buildErrMsg(className, functionName, mockMethod)
local tabSpace = " "
local msg = ""..tabSpace .. className .. "." .. functionName .. mockMethod._l_method_args_:toString() .. " Call times not as expect\n"
msg = msg .. tabSpace .. "Expect:" .. tostring(mockMethod._l_method_expect_times:get()) .. "\n"
msg = msg .. tabSpace .. "Actual:" .. tostring(mockMethod._l_method_actual_times_:get())
return msg
end
function Method:createExpectCallArgs(result, times, args)
local actualTimes = Times:new(0)
local expectTimes = Times:new(times)
return { _l_method_args_ = args, _l_method_expect_times = expectTimes, _l_method_actual_times_ = actualTimes, _l_method_rlt_ = result }
end
function Method:unExpectCalls(self, times, ...)
local args = Args:new(...)
local actualTimes = Times:new(times)
local expectTimes = Times:new(0)
local unExpectCalls = self._l_method_unexpect_calls_
for key, val in pairs(unExpectCalls) do
if args:equal(val._l_method_args_) then
val._l_method_actual_times_:increase(1)
return nil
end
end
local unExpectCall = { _l_method_args_ = args, _l_method_expect_times = expectTimes, _l_method_actual_times_ = actualTimes, _l_method_rlt_ = result }
table.insert(self._l_method_unexpect_calls_, unExpectCall)
return nil
end
-------------------class for Mock--------------------
local Mock = {}
function Mock:new(className)
local mock = { _l_mock_class_name_ = className, _l_mock_methods_ = {} }
setmetatable(mock, self)
self.__index = self;
return mock
end
function Mock:expectCall(functionName)
local mockMethodKey = self._l_mock_class_name_ .. "-" .. functionName
if self._l_mock_methods_[mockMethodKey] == nil then
local mockMethod = Method:new(self._l_mock_class_name_, functionName)
self[functionName] = function(...)
return mockMethod:result(...)
end
self._l_mock_methods_.mockMethodKey = mockMethod
end
return self._l_mock_methods_.mockMethodKey
end
function Mock:mock(functionName, result, times, ...)
local mockMethodKey = self._l_mock_class_name_ .. "-" .. functionName
if self._l_mock_methods_[mockMethodKey] == nil then
local mockMethod = Method:new(self._l_mock_class_name_, functionName):mock(result, times, ...)
self[functionName] = function(...)
return mockMethod:result(...)
end
self._l_mock_methods_.mockMethodKey = mockMethod
else
self._l_mock_methods_[mockMethodKey]:mock(result, times, ...)
end
return self._l_mock_methods_.mockMethodKey
end
function Mock:verify()
local msg = ""
local allMockMethods = self._l_mock_methods_
for key, val in pairs(allMockMethods) do
msg = msg .. val:verify()
end
self._l_mock_methods_ = {}
if msg ~= "" then
Error:new(msg):throw()
end
end
return Mock