vanilla-wow-addons – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | --[[ |
2 | luaunit.lua |
||
3 | |||
4 | Description: A unit testing framework |
||
5 | Initial author: Ryu, Gwang (http://www.gpgstudy.com/gpgiki/LuaUnit) |
||
6 | improvements by Philippe Fremy <phil@freehackers.org> |
||
7 | Version: 1.0 |
||
8 | |||
9 | TODO: |
||
10 | - try to display the failing line |
||
11 | - use xpcall to reveal the calling stack |
||
12 | - isolate output rendering into a separate class ? |
||
13 | - isolate result into a separate class ? |
||
14 | - customised output for the failing line ? |
||
15 | ]]-- |
||
16 | |||
17 | argv = arg |
||
18 | |||
19 | function assertEquals(expected, actual) |
||
20 | if actual ~= expected then |
||
21 | local function wrapValue( v ) |
||
22 | if type(v) == 'string' then return "'"..v.."'" end |
||
23 | return tostring(v) |
||
24 | end |
||
25 | errorMsg = "expected: "..wrapValue(expected)..", actual: "..wrapValue(actual) |
||
26 | error( errorMsg, 2 ) |
||
27 | end |
||
28 | end |
||
29 | |||
30 | function isFunction(aObject) |
||
31 | if 'function' == type(aObject) then |
||
32 | return true |
||
33 | else |
||
34 | return false |
||
35 | end |
||
36 | end |
||
37 | |||
38 | luaUnit = { |
||
39 | FailedCount = 0, |
||
40 | TestCount = 0, |
||
41 | Errors = {} |
||
42 | } |
||
43 | |||
44 | function luaUnit:displayClassName( aClassName ) |
||
45 | print( '>>>>>> '..aClassName ) |
||
46 | end |
||
47 | |||
48 | function luaUnit:displayTestName( testName ) |
||
49 | print( ">>> "..testName ) |
||
50 | end |
||
51 | |||
52 | function luaUnit:displayFailure( errorMsg ) |
||
53 | print( errorMsg ) |
||
54 | print( 'Failed' ) |
||
55 | end |
||
56 | |||
57 | function luaUnit:displaySuccess() |
||
58 | print ("Ok" ) |
||
59 | end |
||
60 | |||
61 | function luaUnit:displayResult() |
||
62 | if self.TestCount == 0 then |
||
63 | failurePercent = 0 |
||
64 | else |
||
65 | failurePercent = 100.0 * self.FailedCount / self.TestCount |
||
66 | end |
||
67 | successCount = self.TestCount - self.FailedCount |
||
68 | print( string.format("Success : %d%% - %d / %d", |
||
69 | 100-math.ceil(failurePercent), successCount, self.TestCount) ) |
||
70 | end |
||
71 | |||
72 | function luaUnit:analyseErrorLine( errorMsg ) |
||
73 | mb, me, filename, line = string.find(errorMsg, "(%w+):(%d+)" ) |
||
74 | if filename and line then |
||
75 | -- check that file exists |
||
76 | -- read it, read the line |
||
77 | -- display the line |
||
78 | end |
||
79 | end |
||
80 | |||
81 | function luaUnit:runTestMethod(aName, aClassInstance, aMethod) |
||
82 | -- example: runTestMethod( 'TestToto:test1', TestToto, TestToto.testToto(self) ) |
||
83 | luaUnit:displayTestName(aName) |
||
84 | self.TestCount = self.TestCount + 1 |
||
85 | |||
86 | -- run setUp first(if any) |
||
87 | if isFunction( aClassInstance.setUp) then |
||
88 | aClassInstance:setUp() |
||
89 | end |
||
90 | |||
91 | -- run testMethod() |
||
92 | ok, errorMsg = pcall( aMethod ) |
||
93 | if not ok then |
||
94 | self.FailedCount = self.FailedCount + 1 |
||
95 | table.insert( self.Errors, errorMsg ) |
||
96 | luaUnit:analyseErrorLine( errorMsg ) |
||
97 | luaUnit:displayFailure( errorMsg ) |
||
98 | else |
||
99 | luaUnit:displaySuccess() |
||
100 | end |
||
101 | |||
102 | -- lastly, run tearDown(if any) |
||
103 | if isFunction(aClassInstance.tearDown) then |
||
104 | aClassInstance:tearDown() |
||
105 | end |
||
106 | end |
||
107 | |||
108 | function luaUnit:runTestMethodName( methodName, classInstance ) |
||
109 | -- example: runTestMethodName( 'TestToto:testToto', TestToto ) |
||
110 | methodInstance = loadstring(methodName .. '()') |
||
111 | luaUnit:runTestMethod(methodName, classInstance, methodInstance) |
||
112 | end |
||
113 | |||
114 | function luaUnit:runTestClassByName( aClassName ) |
||
115 | -- example: runTestMethodName( 'TestToto' ) |
||
116 | hasMethod = string.find(aClassName, ':' ) |
||
117 | if hasMethod then |
||
118 | methodName = string.sub(aClassName, hasMethod+1) |
||
119 | aClassName = string.sub(aClassName,1,hasMethod-1) |
||
120 | end |
||
121 | classInstance = _G[aClassName] |
||
122 | if not classInstance then |
||
123 | error( "No such class: "..aClassName ) |
||
124 | end |
||
125 | luaUnit:displayClassName( aClassName ) |
||
126 | |||
127 | if hasMethod then |
||
128 | if not classInstance[ methodName ] then |
||
129 | error( "No such method: "..methodName ) |
||
130 | end |
||
131 | luaUnit:runTestMethodName( aClassName..':'.. methodName, classInstance ) |
||
132 | else |
||
133 | -- run all test methods of the class |
||
134 | for methodName, method in classInstance do |
||
135 | if isFunction(method) and string.sub(methodName, 1, 4) == "test" then |
||
136 | luaUnit:runTestMethodName( aClassName..':'.. methodName, classInstance ) |
||
137 | end |
||
138 | end |
||
139 | end |
||
140 | print() |
||
141 | end |
||
142 | |||
143 | function luaUnit:run(...) |
||
144 | -- Run some specific test classes. |
||
145 | -- If no arguments are passed, run the class names specified on the |
||
146 | -- command line. If no class name is specified on the command line |
||
147 | -- run all classes whose name starts with 'Test' |
||
148 | -- |
||
149 | -- If arguments are passed, they must be strings of the class names |
||
150 | -- that you want to run |
||
151 | if arg.n > 0 then |
||
152 | table.foreachi( arg, luaUnit.runTestClassByName ) |
||
153 | else |
||
154 | if argv.n > 0 then |
||
155 | table.foreachi(argv, luaUnit.runTestClassByName ) |
||
156 | else |
||
157 | for var, value in _G do |
||
158 | if string.sub(var,1,4) == 'Test' then |
||
159 | luaUnit:runTestClassByName(var) |
||
160 | end |
||
161 | end |
||
162 | end |
||
163 | end |
||
164 | luaUnit:displayResult() |
||
165 | end |
||
166 | -- class luaUnit |
||
167 | |||
168 | function wrapFunctions(...) |
||
169 | testClass = {} |
||
170 | local function storeAsMethod(idx, testName) |
||
171 | testFunction = _G[testName] |
||
172 | testClass[testName] = testFunction |
||
173 | end |
||
174 | table.foreachi( arg, storeAsMethod ) |
||
175 | return testClass |
||
176 | end |