TestMethod.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. -- @class - TestMethod
  2. -- @desc - used to run a specific method from a module's /test/ suite
  3. -- each assertion is tracked and then printed to output
  4. TestMethod = {
  5. -- @method - TestMethod:new()
  6. -- @desc - create a new TestMethod object
  7. -- @param {string} method - string of method name to run
  8. -- @param {TestMethod} testmethod - parent testmethod this test belongs to
  9. -- @return {table} - returns the new Test object
  10. new = function(self, method, testmodule)
  11. local test = {
  12. testmodule = testmodule,
  13. method = method,
  14. asserts = {},
  15. start = love.timer.getTime(),
  16. finish = 0,
  17. count = 0,
  18. passed = false,
  19. skipped = false,
  20. skipreason = '',
  21. fatal = '',
  22. message = nil,
  23. result = {},
  24. colors = {
  25. red = {1, 0, 0, 1},
  26. redpale = {1, 0.5, 0.5, 1},
  27. red07 = {0.7, 0, 0, 1},
  28. green = {0, 1, 0, 1},
  29. greenhalf = {0, 0.5, 0, 1},
  30. greenfade = {0, 1, 0, 0.5},
  31. blue = {0, 0, 1, 1},
  32. bluefade = {0, 0, 1, 0.5},
  33. yellow = {1, 1, 0, 1},
  34. pink = {1, 0, 1, 1},
  35. black = {0, 0, 0, 1},
  36. white = {1, 1, 1, 1},
  37. lovepink = {214/255, 86/255, 151/255, 1},
  38. loveblue = {83/255, 168/255, 220/255, 1}
  39. },
  40. imgs = 1,
  41. delay = 0,
  42. delayed = false,
  43. store = {},
  44. co = nil
  45. }
  46. setmetatable(test, self)
  47. self.__index = self
  48. return test
  49. end,
  50. -- @method - TestMethod:assertEquals()
  51. -- @desc - used to assert two values are equals
  52. -- @param {any} expected - expected value of the test
  53. -- @param {any} actual - actual value of the test
  54. -- @param {string} label - label for this test to use in exports
  55. -- @return {nil}
  56. assertEquals = function(self, expected, actual, label)
  57. self.count = self.count + 1
  58. table.insert(self.asserts, {
  59. key = 'assert ' .. tostring(self.count),
  60. passed = expected == actual,
  61. message = 'expected \'' .. tostring(expected) .. '\' got \'' ..
  62. tostring(actual) .. '\'',
  63. test = label or 'no label given'
  64. })
  65. end,
  66. -- @method - TestMethod:assertPixels()
  67. -- @desc - checks a list of coloured pixels agaisnt given imgdata
  68. -- @param {ImageData} imgdata - image data to check
  69. -- @param {table} pixels - map of colors to list of pixel coords, i.e.
  70. -- { blue = { {1, 1}, {2, 2}, {3, 4} } }
  71. -- @return {nil}
  72. assertPixels = function(self, imgdata, pixels, label)
  73. for i, v in pairs(pixels) do
  74. local col = self.colors[i]
  75. local pixels = v
  76. for p=1,#pixels do
  77. local coord = pixels[p]
  78. local tr, tg, tb, ta = imgdata:getPixel(coord[1], coord[2])
  79. local compare_id = tostring(coord[1]) .. ',' .. tostring(coord[2])
  80. -- prevent us getting stuff like 0.501960785 for 0.5 red
  81. tr = math.floor((tr*10)+0.5)/10
  82. tg = math.floor((tg*10)+0.5)/10
  83. tb = math.floor((tb*10)+0.5)/10
  84. ta = math.floor((ta*10)+0.5)/10
  85. col[1] = math.floor((col[1]*10)+0.5)/10
  86. col[2] = math.floor((col[2]*10)+0.5)/10
  87. col[3] = math.floor((col[3]*10)+0.5)/10
  88. col[4] = math.floor((col[4]*10)+0.5)/10
  89. -- @TODO add some sort pixel tolerance to the coords
  90. self:assertEquals(col[1], tr, 'check pixel r for ' .. i .. ' at ' .. compare_id .. '(' .. label .. ')')
  91. self:assertEquals(col[2], tg, 'check pixel g for ' .. i .. ' at ' .. compare_id .. '(' .. label .. ')')
  92. self:assertEquals(col[3], tb, 'check pixel b for ' .. i .. ' at ' .. compare_id .. '(' .. label .. ')')
  93. self:assertEquals(col[4], ta, 'check pixel a for ' .. i .. ' at ' .. compare_id .. '(' .. label .. ')')
  94. end
  95. end
  96. end,
  97. -- @method - TestMethod:assertNotEquals()
  98. -- @desc - used to assert two values are not equal
  99. -- @param {any} expected - expected value of the test
  100. -- @param {any} actual - actual value of the test
  101. -- @param {string} label - label for this test to use in exports
  102. -- @return {nil}
  103. assertNotEquals = function(self, expected, actual, label)
  104. self.count = self.count + 1
  105. table.insert(self.asserts, {
  106. key = 'assert ' .. tostring(self.count),
  107. passed = expected ~= actual,
  108. message = 'avoiding \'' .. tostring(expected) .. '\' got \'' ..
  109. tostring(actual) .. '\'',
  110. test = label or 'no label given'
  111. })
  112. end,
  113. -- @method - TestMethod:assertRange()
  114. -- @desc - used to check a value is within an expected range
  115. -- @param {number} actual - actual value of the test
  116. -- @param {number} min - minimum value the actual should be >= to
  117. -- @param {number} max - maximum value the actual should be <= to
  118. -- @param {string} label - label for this test to use in exports
  119. -- @return {nil}
  120. assertRange = function(self, actual, min, max, label)
  121. self.count = self.count + 1
  122. table.insert(self.asserts, {
  123. key = 'assert ' .. tostring(self.count),
  124. passed = actual >= min and actual <= max,
  125. message = 'value \'' .. tostring(actual) .. '\' out of range \'' ..
  126. tostring(min) .. '-' .. tostring(max) .. '\'',
  127. test = label or 'no label given'
  128. })
  129. end,
  130. -- @method - TestMethod:assertMatch()
  131. -- @desc - used to check a value is within a list of values
  132. -- @param {number} list - list of valid values for the test
  133. -- @param {number} actual - actual value of the test to check is in the list
  134. -- @param {string} label - label for this test to use in exports
  135. -- @return {nil}
  136. assertMatch = function(self, list, actual, label)
  137. self.count = self.count + 1
  138. local found = false
  139. for l=1,#list do
  140. if list[l] == actual then found = true end;
  141. end
  142. table.insert(self.asserts, {
  143. key = 'assert ' .. tostring(self.count),
  144. passed = found == true,
  145. message = 'value \'' .. tostring(actual) .. '\' not found in \'' ..
  146. table.concat(list, ',') .. '\'',
  147. test = label or 'no label given'
  148. })
  149. end,
  150. -- @method - TestMethod:assertGreaterEqual()
  151. -- @desc - used to check a value is >= than a certain target value
  152. -- @param {any} target - value to check the test agaisnt
  153. -- @param {any} actual - actual value of the test
  154. -- @param {string} label - label for this test to use in exports
  155. -- @return {nil}
  156. assertGreaterEqual = function(self, target, actual, label)
  157. self.count = self.count + 1
  158. local passing = false
  159. if target ~= nil and actual ~= nil then
  160. passing = actual >= target
  161. end
  162. table.insert(self.asserts, {
  163. key = 'assert ' .. tostring(self.count),
  164. passed = passing,
  165. message = 'value \'' .. tostring(actual) .. '\' not >= \'' ..
  166. tostring(target) .. '\'',
  167. test = label or 'no label given'
  168. })
  169. end,
  170. -- @method - TestMethod:assertLessEqual()
  171. -- @desc - used to check a value is <= than a certain target value
  172. -- @param {any} target - value to check the test agaisnt
  173. -- @param {any} actual - actual value of the test
  174. -- @param {string} label - label for this test to use in exports
  175. -- @return {nil}
  176. assertLessEqual = function(self, target, actual, label)
  177. self.count = self.count + 1
  178. local passing = false
  179. if target ~= nil and actual ~= nil then
  180. passing = actual <= target
  181. end
  182. table.insert(self.asserts, {
  183. key = 'assert ' .. tostring(self.count),
  184. passed = passing,
  185. message = 'value \'' .. tostring(actual) .. '\' not <= \'' ..
  186. tostring(target) .. '\'',
  187. test = label or 'no label given'
  188. })
  189. end,
  190. -- @method - TestMethod:assertObject()
  191. -- @desc - used to check a table is a love object, this runs 3 seperate
  192. -- tests to check table has the basic properties of an object
  193. -- @note - actual object functionality tests have their own methods
  194. -- @param {table} obj - table to check is a valid love object
  195. -- @return {nil}
  196. assertObject = function(self, obj)
  197. self:assertNotNil(obj)
  198. self:assertEquals('userdata', type(obj), 'check is userdata')
  199. if obj ~= nil then
  200. self:assertNotEquals(nil, obj:type(), 'check has :type()')
  201. end
  202. end,
  203. -- @method - TestMethod:assertCoords()
  204. -- @desc - used to check a pair of values (usually coordinates)
  205. -- @param {table} obj - table to check is a valid love object
  206. -- @return {nil}
  207. assertCoords = function(self, expected, actual, label)
  208. self.count = self.count + 1
  209. local passing = false
  210. if expected ~= nil and actual ~= nil then
  211. if expected[1] == actual[1] and expected[2] == actual[2] then
  212. passing = true
  213. end
  214. end
  215. table.insert(self.asserts, {
  216. key = 'assert ' .. tostring(self.count),
  217. passed = passing,
  218. message = 'expected \'' .. tostring(expected[1]) .. 'x,' ..
  219. tostring(expected[2]) .. 'y\' got \'' ..
  220. tostring(actual[1]) .. 'x,' .. tostring(actual[2]) .. 'y\'',
  221. test = label or 'no label given'
  222. })
  223. end,
  224. -- @method - TestMethod:assertNotNil()
  225. -- @desc - quick assert for value not nil
  226. -- @param {any} value - value to check not nil
  227. -- @return {nil}
  228. assertNotNil = function (self, value, err)
  229. self:assertNotEquals(nil, value, 'check not nil')
  230. if err ~= nil then
  231. table.insert(self.asserts, {
  232. key = 'assert ' .. tostring(self.count),
  233. passed = false,
  234. message = err,
  235. test = 'assert not nil catch'
  236. })
  237. end
  238. end,
  239. -- @method - TestMethod:exportImg()
  240. -- @desc - used to export actual test img results to compare to the expected
  241. -- @param {table} imgdata - imgdata to save as a png
  242. -- @return {nil}
  243. exportImg = function(self, imgdata)
  244. local path = 'tempoutput/actual/love.test.graphics.' ..
  245. self.method .. '-' .. tostring(self.imgs) .. '.png'
  246. imgdata:encode('png', path)
  247. self.imgs = self.imgs + 1
  248. end,
  249. -- @method - TestMethod:skipTest()
  250. -- @desc - used to mark this test as skipped for a specific reason
  251. -- @param {string} reason - reason why method is being skipped
  252. -- @return {nil}
  253. skipTest = function(self, reason)
  254. self.skipped = true
  255. self.skipreason = reason
  256. end,
  257. waitFrames = function(self, frames)
  258. for i=1,frames do coroutine.yield() end
  259. end,
  260. -- @method - TestMethod:evaluateTest()
  261. -- @desc - evaluates the results of all assertions for a final restult
  262. -- @return {nil}
  263. evaluateTest = function(self)
  264. local failure = ''
  265. local failures = 0
  266. for a=1,#self.asserts do
  267. -- @TODO just return first failed assertion msg? or all?
  268. -- currently just shows the first assert that failed
  269. if self.asserts[a].passed == false and self.skipped == false then
  270. if failure == '' then failure = self.asserts[a] end
  271. failures = failures + 1
  272. end
  273. end
  274. if self.fatal ~= '' then failure = self.fatal end
  275. local passed = tostring(#self.asserts - failures)
  276. local total = '(' .. passed .. '/' .. tostring(#self.asserts) .. ')'
  277. if self.skipped == true then
  278. self.testmodule.skipped = self.testmodule.skipped + 1
  279. love.test.totals[3] = love.test.totals[3] + 1
  280. self.result = {
  281. total = '',
  282. result = "SKIP",
  283. passed = false,
  284. message = '(0/0) - method skipped [' .. self.skipreason .. ']'
  285. }
  286. else
  287. if failure == '' and #self.asserts > 0 then
  288. self.passed = true
  289. self.testmodule.passed = self.testmodule.passed + 1
  290. love.test.totals[1] = love.test.totals[1] + 1
  291. self.result = {
  292. total = total,
  293. result = 'PASS',
  294. passed = true,
  295. message = nil
  296. }
  297. else
  298. self.passed = false
  299. self.testmodule.failed = self.testmodule.failed + 1
  300. love.test.totals[2] = love.test.totals[2] + 1
  301. if #self.asserts == 0 then
  302. local msg = 'no asserts defined'
  303. if self.fatal ~= '' then msg = self.fatal end
  304. self.result = {
  305. total = total,
  306. result = 'FAIL',
  307. passed = false,
  308. key = 'test',
  309. message = msg
  310. }
  311. else
  312. local key = failure['key']
  313. if failure['test'] ~= nil then
  314. key = key .. ' [' .. failure['test'] .. ']'
  315. end
  316. local msg = failure['message']
  317. if self.fatal ~= '' then
  318. key = 'code'
  319. msg = self.fatal
  320. end
  321. self.result = {
  322. total = total,
  323. result = 'FAIL',
  324. passed = false,
  325. key = key,
  326. message = msg
  327. }
  328. end
  329. end
  330. end
  331. self:printResult()
  332. end,
  333. -- @method - TestMethod:printResult()
  334. -- @desc - prints the result of the test to the console as well as appends
  335. -- the XML + HTML for the test to the testsuite output
  336. -- @return {nil}
  337. printResult = function(self)
  338. -- get total timestamp
  339. -- @TODO make nicer, just need a 3DP ms value
  340. self.finish = love.timer.getTime() - self.start
  341. love.test.time = love.test.time + self.finish
  342. self.testmodule.time = self.testmodule.time + self.finish
  343. local endtime = UtilTimeFormat(love.timer.getTime() - self.start)
  344. -- get failure/skip message for output (if any)
  345. local failure = ''
  346. local output = ''
  347. if self.passed == false and self.skipped == false then
  348. failure = '\t\t\t<failure message="' .. self.result.key .. ' ' ..
  349. self.result.message .. '">' .. self.result.key .. ' ' .. self.result.message .. '</failure>\n'
  350. output = self.result.key .. ' ' .. self.result.message
  351. -- append failures if any to report md
  352. love.test.mdfailures = love.test.mdfailures .. '> 🔴 ' .. self.method .. ' \n' ..
  353. '> ' .. output .. ' \n\n'
  354. end
  355. if output == '' and self.skipped == true then
  356. failure = '\t\t\t<skipped message="' .. self.skipreason .. '" />\n'
  357. output = self.skipreason
  358. end
  359. -- append XML for the test class result
  360. self.testmodule.xml = self.testmodule.xml .. '\t\t<testcase classname="' ..
  361. self.method .. '" name="' .. self.method .. '" assertions="' .. tostring(#self.asserts) ..
  362. '" time="' .. endtime .. '">\n' ..
  363. failure .. '\t\t</testcase>\n'
  364. -- unused currently, adds a preview image for certain graphics methods to the output
  365. local preview = ''
  366. if self.testmodule.module == 'graphics' then
  367. local filename = 'love.test.graphics.' .. self.method
  368. if love.filesystem.openFile('tempoutput/actual/' .. filename .. '-1.png', 'r') then
  369. preview = '<div class="preview">' .. '<img src="expected/' .. filename .. '-1.png"/><p>Expected</p></div>' ..
  370. '<div class="preview">' .. '<img src="actual/' .. filename .. '-1.png"/><p>Actual</p></div>'
  371. end
  372. if love.filesystem.openFile('tempoutput/actual/' .. filename .. '-2.png', 'r') then
  373. preview = preview .. '<div class="preview">' .. '<img src="expected/' .. filename .. '-2.png"/><p>Expected</p></div>' ..
  374. '<div class="preview">' .. '<img src="actual/' .. filename .. '-2.png"/><p>Actual</p></div>'
  375. end
  376. if love.filesystem.openFile('tempoutput/actual/' .. filename .. '-3.png', 'r') then
  377. preview = preview .. '<div class="preview">' .. '<img src="expected/' .. filename .. '-3.png"/><p>Expected</p></div>' ..
  378. '<div class="preview">' .. '<img src="actual/' .. filename .. '-3.png"/><p>Actual</p></div>'
  379. end
  380. end
  381. -- append HTML for the test class result
  382. local status = '🔴'
  383. local cls = 'red'
  384. if self.passed == true then status = '🟢'; cls = '' end
  385. if self.skipped == true then status = '🟡'; cls = '' end
  386. self.testmodule.html = self.testmodule.html ..
  387. '<tr class=" ' .. cls .. '">' ..
  388. '<td>' .. status .. '</td>' ..
  389. '<td>' .. self.method .. '</td>' ..
  390. '<td>' .. endtime .. 's</td>' ..
  391. '<td>' .. output .. preview .. '</td>' ..
  392. '</tr>'
  393. -- add message if assert failed
  394. local msg = ''
  395. if self.result.message ~= nil and self.skipped == false then
  396. msg = ' - ' .. self.result.key ..
  397. ' failed - (' .. self.result.message .. ')'
  398. end
  399. if self.skipped == true then
  400. msg = self.result.message
  401. end
  402. -- log final test result to console
  403. -- i know its hacky but its neat soz
  404. local tested = 'love.' .. self.testmodule.module .. '.' .. self.method .. '()'
  405. local matching = string.sub(self.testmodule.spacer, string.len(tested), 40)
  406. self.testmodule:log(
  407. self.testmodule.colors[self.result.result],
  408. ' ' .. tested .. matching,
  409. ' ==> ' .. self.result.result .. ' - ' .. endtime .. 's ' ..
  410. self.result.total .. msg
  411. )
  412. end
  413. }