TestMethod.lua 16 KB

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