-
Notifications
You must be signed in to change notification settings - Fork 0
/
LuaUnit.lua
261 lines (223 loc) · 6.86 KB
/
LuaUnit.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
---
-- 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
--
------------------------class for Test 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
function Error:show()
print(self._l_err_msg_)
end
------------------------class for Test Assert-----------------
---
---
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 isEq(expect, actual)
-- return expect ~= nil and actual ~= nil and type(expect) == type(actual)
-- and expect == actual
-- end
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 getNotEqMsg(expect, actual, needTrace)
local errMsg = " Value not equal. Expect:" .. tostring(expect) .. ", Actual:" .. tostring(actual);
if needTrace then
errMsg = errMsg .. "\n" .. debug.traceback()
end
return errMsg
end
Assert = { _l_assert_need_trace_ = false }
function Assert:equal(expect, actual)
if not isEq(expect, actual) then
Error:new(getNotEqMsg(expect, actual, self._l_assert_need_trace_)):throw()
end
end
function Assert:isTrue(isTrue)
if isTrue == false then
Error:new(getNotEqMsg(true, isTrue, self._l_assert_need_trace_)):throw()
end
end
function Assert:isFalse(isFalse)
if isFalse == true then
Error:new(getNotEqMsg(false, isFalse, self._l_assert_need_trace_)):throw()
end
end
function Assert:needTrace(isNeedTrace)
self._l_assert_need_trace_ = isNeedTrace;
end
------------------------class for Test Result-----------------
local TestResult = {}
function TestResult:new(testClassName)
local testResult = { _l_unit_case_result_ = {},
_l_total_case_num_ = 0,
_l_succ_case_num_ = 0,
_l_unit_class_name_ = testClassName or "TestClassNotKnown",
new = self.new,
record = self.record,
display = self. display
}
setmetatable(testResult, self)
self.__index = self
return testResult
end
function TestResult:record(testCaseName, isSucc, error)
self:updateTotalNum();
self:updateSuccNum(isSucc);
self:recordRlt(testCaseName, isSucc, error);
end
function TestResult:display()
local caseRlt = self._l_unit_case_result_;
for key, val in pairs(caseRlt) do
self:printRlt(val)
self:printErrMsg(val)
end
self:printStat()
end
function TestResult:updateSuccNum(isSucc)
if self:isSucc(isSucc) then
self._l_succ_case_num_ = self._l_succ_case_num_ + 1
end
end
function TestResult:updateTotalNum(isSucc)
self._l_total_case_num_ = self._l_total_case_num_ + 1
end
function TestResult:recordRlt(testCaseName, isSucc, errMsg)
table.insert(self._l_unit_case_result_, { testCaseName, isSucc, errMsg })
end
function TestResult:isSucc(isSuccess)
return nil ~= isSuccess and isSuccess == true
end
function TestResult:getCaseRltStr(isSuccess)
if self:isSucc(isSuccess) then
return "[SUCC] "
else
return "[FAIL] "
end
end
function TestResult:printRlt(val)
local testCaseNameName = val[1];
local testResult = val[2];
print(self:getCaseRltStr(testResult) .. self._l_unit_class_name_ .. "." .. testCaseNameName);
end
function TestResult:printErrMsg(val)
local errMsg = val[3];
if errMsg then
errMsg:show();
end
end
function TestResult:printStat()
local succPercent = 100 * self._l_succ_case_num_ / self._l_total_case_num_;
print("=========Succ:" .. self._l_succ_case_num_ .. ", Total:" .. self._l_total_case_num_ .. ", Percent:" .. succPercent .. "%=========")
end
-------------------------------------------------------------
local LuaUnit = {}
function LuaUnit:derive(classNames)
local luaUnit = { _l_unit_class_name_ = classNames,
run = run
}
setmetatable(luaUnit, LuaUnit)
self.__index = self;
return luaUnit;
end
function LuaUnit:run(...)
self:init()
self:runSetUp()
self:runTestCase()--todo filter test case by input paras
self:runTearDown()
self:finish()
end
function LuaUnit:init()
self._l_unit_result_ = TestResult:new(self._l_unit_class_name_)
end
function LuaUnit:finish()
self._l_unit_result_:display()
end
function LuaUnit:runSetUp()
if self:isFunction(self.setUp) then
self:setUp()
end
end
function LuaUnit:runTestCase()
for key, val in pairs(self) do
if string.sub(key,1,4) == 'test' then
---call caseSetUp if user have
local isSucc, errMsg = self:safeCallFunc(self.caseSetUp)
---call test case
if(isSucc and errMsg == nil) then
isSucc, errMsg = self:safeCallFunc(val)
end
---call casetearDown if user have
if(isSucc and errMsg == nil) then
isSucc, errMsg = self:safeCallFunc(self.caseTearDown)
end
self:recordTestCaseResult(key, isSucc, errMsg)
end
end
end
function LuaUnit:runTearDown()
if self:isFunction(self.tearDown) then
self:tearDown()
end
end
function LuaUnit:isFunction(key)
return key ~= nil and type(key) == 'function'
end
function LuaUnit:safeCallFunc(func)
if(func~=nil and self:isFunction(func))then
local isSucc, errMsg = xpcall(func, function(e)
return Error:new(e)
end)
return isSucc,errMsg
end
return true;
end
function LuaUnit:recordTestCaseResult(testCaseName, isSucc, error)
self._l_unit_result_:record(testCaseName, isSucc, error)
end
return LuaUnit