Debian Codegraph @

extensions/Scribunto/tests/phpunit/Engines/LuaCommon/TestFramework.lua

raw symbols SCIP
1 local testframework = testframework or {}
2
3 -- Return a string represetation of a value, including the deep structure of a table
4 local function deepToString( val, indent, done )
5 done = done or {}
6 indent = indent or 0
7
8 local tp = type( val )
9 if tp == 'string' then
10 return string.format( "%q", val )
11 elseif tp == 'table' then
12 if done[val] then return '{ ... }' end
13 done[val] = true
14 local sb = { '{\n' }
15 local donekeys = {}
16 for key, value in ipairs( val ) do
17 donekeys[key] = true
18 sb[#sb + 1] = string.rep( " ", indent + 2 )
19 sb[#sb + 1] = deepToString( value, indent + 2, done )
20 sb[#sb + 1] = ",\n"
21 end
22 local keys = {}
23 for key in pairs( val ) do
24 if not donekeys[key] then
25 keys[#keys + 1] = key
26 end
27 end
28 table.sort( keys )
29 for i = 1, #keys do
30 local key = keys[i]
31 sb[#sb + 1] = string.rep( " ", indent + 2 )
32 if type( key ) == 'table' then
33 sb[#sb + 1] = '[{ ... }] = '
34 else
35 sb[#sb + 1] = '['
36 sb[#sb + 1] = deepToString( key, indent + 3, done )
37 sb[#sb + 1] = '] = '
38 end
39 sb[#sb + 1] = deepToString( val[key], indent + 2, done )
40 sb[#sb + 1] = ",\n"
41 end
42 sb[#sb + 1] = string.rep( " ", indent )
43 sb[#sb + 1] = "}"
44 return table.concat( sb )
45 else
46 return tostring( val )
47 end
48 end
49 testframework.deepToString = deepToString
50
51 -- Test whether two objects are equal, including the deep structure of a table.
52 -- Returns 4 values:
53 -- boolean equal?
54 -- list key path to first inequality
55 -- mixed value from 'a' for key path
56 -- mixed value from 'b' for key path
57 local function deepEquals( a, b, keypath, done )
58 -- Simple equality
59 if a == b then
60 return true
61 end
62
63 keypath = keypath or {}
64 done = done or {}
65
66 -- Must be equal types to be equal
67 local tp = type( a )
68 if type( b ) ~= tp then
69 return false, keypath, a, b
70 end
71
72 -- Special tests for certain types
73
74 if tp == 'number' then
75 -- For test framework purposes, NaNs are equivalent. Lua has no
76 -- standard "isNaN" function, but only NaN will return true for
77 -- "x ~= x".
78 if a ~= a and b ~= b then
79 return true
80 end
81
82 return false, keypath, a, b
83 end
84
85 if tp == 'table' then
86 -- To avoid recursion, see if we've seen this pair of tables before. If
87 -- so, they must be equal or the test would have failed the first time we saw them.
88 done[a] = done[a] or {}
89 done[b] = done[b] or {}
90 if done[a][b] or done[b][a] then
91 return true
92 end
93
94 -- Not seen before, record them and compare key by key.
95 done[a][b] = true
96
97 local n = #keypath + 1
98 -- First, check if the values for all keys in 'a' are equal in 'b'.
99 for k in pairs( a ) do
100 keypath[n] = k
101 local ok, kp, aa, bb = deepEquals( a[k], b[k], keypath, done )
102 if not ok then
103 return false, kp, aa, bb
104 end
105 end
106 keypath[n] = nil
107
108 -- Then check if there are any keys in 'b' that don't exist in 'a'.
109 for k, v in pairs( b ) do
110 if a[k] == nil then
111 keypath[n] = k
112 return false, keypath, nil, v
113 end
114 end
115
116 -- Ok, all keys equal so it must match.
117 return true
118 end
119
120 -- Ok, they're not equal
121 return false, keypath, a, b
122 end
123 testframework.deepEquals = deepEquals
124
125 -- Skip a test (throws an error)
126 function testframework.markTestSkipped( message )
127 error( 'SKIP: ' .. message, 0 )
128 end
129
130 ---- Test types available ---
131 -- Each type has a formatter and an executor:
132 -- Formatters take 1 arg: expected return value from the function.
133 -- Executors take 2 args: function and arguments.
134 -- Both return a string. The test passes if the two strings match.
135 testframework.types = testframework.types or {}
136
137 -- Execute a function and assert expected results
138 -- Expected value is a list of return values, or a string error message
139 testframework.types.Normal = {
140 format = function ( expect )
141 if type( expect ) == 'string' then
142 return 'ERROR: ' .. expect
143 else
144 return deepToString( expect )
145 end
146 end,
147 exec = function ( func, args )
148 local got = { pcall( func, unpack( args ) ) }
149 if table.remove( got, 1 ) then
150 return deepToString( got )
151 else
152 if string.sub( got[1], 1, 6 ) == 'SKIP: ' then
153 error( got[1], 0 )
154 end
155 got = string.gsub( got[1], '^%S+:%d+: ', '' )
156 return 'ERROR: ' .. got
157 end
158 end
159 }
160
161 -- Execute an iterator-returning function and assert expected results from each
162 -- iteration.
163 -- Expected value is a list of return value lists.
164 testframework.types.Iterator = {
165 format = function ( expect )
166 local sb = {}
167 for i = 1, #expect do
168 sb[i] = '[iteration ' .. i .. ']:\n' .. deepToString( expect[i] )
169 end
170 return table.concat( sb, '\n\n' )
171 end,
172 exec = function ( func, args )
173 local sb = {}
174 local i = 0
175 local f, s, var = func( unpack( args ) )
176 while true do
177 local got = { f( s, var ) }
178 var = got[1]
179 if var == nil then break end
180 i = i + 1
181 sb[i] = '[iteration ' .. i .. ']:\n' .. deepToString( got )
182 end
183 return table.concat( sb, '\n\n' )
184 end
185 }
186
187 -- Execute a function and assert expected results
188 -- Expected value is a list of return values, or a string error message
189 testframework.types.ToString = {
190 format = function ( expect )
191 if type( expect ) == 'string' then
192 return 'ERROR: ' .. expect
193 else
194 local ret = {}
195 for k, v in pairs( expect ) do
196 ret[k] = tostring( v )
197 end
198 return deepToString( ret )
199 end
200 end,
201 exec = function ( func, args )
202 local got = { pcall( func, unpack( args ) ) }
203 if table.remove( got, 1 ) then
204 for k, v in pairs( got ) do
205 got[k] = tostring( v )
206 end
207 return deepToString( got )
208 else
209 if string.sub( got[1], 1, 6 ) == 'SKIP: ' then
210 error( got[1], 0 )
211 end
212 got = string.gsub( got[1], '^%S+:%d+: ', '' )
213 return 'ERROR: ' .. got
214 end
215 end
216 }
217
218 -- This takes a list of tests to run, and returns the object used by PHP to
219 -- call them.
220 --
221 -- Each test is a table with the following keys:
222 -- name: Name of the test
223 -- expect: Table of results expected
224 -- func: Function to execute
225 -- args: (optional) Table of args to be unpacked and passed to the function
226 -- type: (optional) Formatter/Executor name, default "Normal"
227 function testframework.getTestProvider( tests )
228 return {
229 count = #tests,
230
231 provide = function ( n )
232 local t = tests[n]
233 return n, t.name, testframework.types[t.type or 'Normal'].format( t.expect )
234 end,
235
236 run = function ( n )
237 local t = tests[n]
238 if not t then
239 return 'Test ' .. name .. ' does not exist'
240 end
241 return testframework.types[t.type or 'Normal'].exec( t.func, t.args or {} )
242 end,
243 }
244 end
245
246 return testframework