TestSuite.lua 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. TestSuite = {
  2. -- @method - TestSuite:new()
  3. -- @desc - creates a new TestSuite object that handles all the tests
  4. -- @return {table} - returns the new TestSuite object
  5. new = function(self)
  6. local test = {
  7. -- testsuite internals
  8. modules = {},
  9. module = nil,
  10. test = nil,
  11. testcanvas = nil,
  12. current = 1,
  13. output = '',
  14. totals = {0, 0, 0},
  15. time = 0,
  16. xml = '',
  17. html = '',
  18. mdrows = '',
  19. mdfailures = '',
  20. delayed = nil,
  21. fakequit = false,
  22. windowmode = true,
  23. -- love modules to test
  24. audio = {},
  25. data = {},
  26. event = {},
  27. filesystem = {},
  28. font = {},
  29. graphics = {},
  30. image = {},
  31. joystick = {},
  32. math = {},
  33. mouse = {},
  34. objects = {}, -- special for all object class contructor tests
  35. physics = {},
  36. sound = {},
  37. system = {},
  38. thread = {},
  39. timer = {},
  40. touch = {},
  41. video = {},
  42. window = {}
  43. }
  44. setmetatable(test, self)
  45. self.__index = self
  46. return test
  47. end,
  48. -- @method - TestSuite:runSuite()
  49. -- @desc - called in love.update, runs through every method or every module
  50. -- @param {number} delta - delta from love.update to track time elapsed
  51. -- @return {nil}
  52. runSuite = function(self, delta)
  53. -- stagger between tests
  54. if self.module ~= nil then
  55. self.module.timer = self.module.timer + delta
  56. if self.module.timer >= self.module.delay then
  57. self.module.timer = self.module.timer - self.module.delay
  58. if self.module.start == true then
  59. -- work through each test method 1 by 1
  60. if self.module.index <= #self.module.running then
  61. -- run method once
  62. if self.module.called[self.module.index] == nil then
  63. self.module.called[self.module.index] = true
  64. local method = self.module.running[self.module.index]
  65. self.test = TestMethod:new(method, self.module)
  66. TextRun:set('love.' .. self.module.module .. '.' .. method)
  67. -- check method exists in love first
  68. if self.module.module ~= 'objects' and (love[self.module.module] == nil or love[self.module.module][method] == nil) then
  69. local tested = 'love.' .. self.module.module .. '.' .. method .. '()'
  70. local matching = string.sub(self.module.spacer, string.len(tested), 40)
  71. self.module:log(self.module.colors['FAIL'],
  72. tested .. matching,
  73. ' ==> FAIL (0/0) - call failed - method does not exist'
  74. )
  75. -- otherwise run the test method
  76. else
  77. local ok, chunk, err = pcall(self[self.module.module][method], self.test)
  78. if ok == false then
  79. print("FATAL", chunk, err)
  80. self.test.fatal = tostring(chunk) .. tostring(err)
  81. end
  82. end
  83. -- once we've run check delay + eval
  84. else
  85. -- @TODO use coroutines?
  86. -- if we have a test method that needs a delay
  87. -- we wait for the delay to run out first
  88. if self.delayed ~= nil then
  89. self.delayed.delay = self.delayed.delay - 1
  90. -- re-run the test method again when delay ends
  91. -- its up to the test to handle the :isDelayed() property
  92. if self.delayed.delay <= 0 then
  93. local ok, chunk, err = pcall(self[self.module.module][self.delayed.method], self.test)
  94. if ok == false then
  95. print("FATAL", chunk, err)
  96. self.test.fatal = tostring(chunk) .. tostring(err)
  97. end
  98. self.delayed = nil
  99. end
  100. else
  101. -- now we're all done evaluate the test
  102. local ok, chunk, err = pcall(self.test.evaluateTest, self.test)
  103. if ok == false then
  104. print("FATAL", chunk, err)
  105. self.test.fatal = tostring(chunk) .. tostring(err)
  106. end
  107. -- save having to :release() anything we made in the last test
  108. -- 7251ms > 7543ms
  109. collectgarbage("collect")
  110. -- move onto the next test
  111. self.module.index = self.module.index + 1
  112. end
  113. end
  114. -- once all tests have run
  115. else
  116. -- print module results and add to output
  117. self.module:printResult()
  118. -- if we have more modules to go run the next one
  119. self.current = self.current + 1
  120. if #self.modules >= self.current then
  121. self.module = self.modules[self.current]
  122. self.module:runTests()
  123. -- otherwise print the final results and export output
  124. else
  125. self:printResult()
  126. love.event.quit(0)
  127. end
  128. end
  129. end
  130. end
  131. end
  132. end,
  133. -- @method - TestSuite:printResult()
  134. -- @desc - prints the result of the whole test suite as well as writes
  135. -- the MD, XML + HTML of the testsuite output
  136. -- @return {nil}
  137. printResult = function(self)
  138. local finaltime = UtilTimeFormat(self.time)
  139. local md = '<!-- PASSED ' .. tostring(self.totals[1]) ..
  140. ' || FAILED ' .. tostring(self.totals[2]) ..
  141. ' || SKIPPED ' .. tostring(self.totals[3]) ..
  142. ' || TIME ' .. finaltime .. ' -->\n\n' ..
  143. '**' .. tostring(self.totals[1] + self.totals[2] + self.totals[3]) .. '** tests were completed in **' ..
  144. finaltime .. 's** with **' ..
  145. tostring(self.totals[1]) .. '** passed, **' ..
  146. tostring(self.totals[2]) .. '** failed, and **' ..
  147. tostring(self.totals[3]) .. '** skipped\n\n### Report\n' ..
  148. '| Module | Passed | Failed | Skipped | Time |\n' ..
  149. '| --------------------- | ------ | ------ | ------- | ------ |\n' ..
  150. self.mdrows .. '\n\n### Failures\n' .. self.mdfailures
  151. local xml = '<testsuites name="love.test" tests="' .. tostring(self.totals[1]) ..
  152. '" failures="' .. tostring(self.totals[2]) ..
  153. '" skipped="' .. tostring(self.totals[3]) ..
  154. '" time="' .. finaltime .. '">\n'
  155. local status = '🔴'
  156. if self.totals[2] == 0 then status = '🟢' end
  157. local html = '<html><head><style>* { font-family: monospace; margin: 0; font-size: 11px; padding: 0; } body { margin: 50px; } h1 { padding-bottom: 10px; font-size: 13px; } h2 { padding: 20px 0 10px 0; font-size: 12px; } .summary { list-style: none; margin: 0; padding: 0; } .summary li { float: left; background: #eee; padding: 5px; margin-right: 10px; } table { background: #eee; margin-top: 10px; width: 100%; max-width: 800px; border-collapse: collapse } table thead { background: #ddd; } table th, table td { padding: 2px; } tr.red { color: red } .wrap { max-width: 800px; margin: auto; } .preview { width: 64px; height: 80px; float: left; margin-right: 10px; } .preview img { width: 100% } .preview p { text-align: center; }</style></head><body><div class="wrap"><h1>' .. status .. '&nbsp;love.test</h1><ul class="summary">'
  158. html = html ..
  159. '<li>🟢&nbsp;' .. tostring(self.totals[1]) .. ' Tests</li>' ..
  160. '<li>🔴&nbsp;' .. tostring(self.totals[2]) .. ' Failures</li>' ..
  161. '<li>🟡&nbsp;' .. tostring(self.totals[3]) .. ' Skipped</li>' ..
  162. '<li>' .. finaltime .. 's</li></ul><br/><br/>'
  163. -- @TODO use mountFullPath to write output to src?
  164. love.filesystem.mountFullPath(love.filesystem.getSource() .. "/output", "tempoutput", "readwrite")
  165. love.filesystem.write('tempoutput/' .. self.output .. '.xml', xml .. self.xml .. '</testsuites>')
  166. love.filesystem.write('tempoutput/' .. self.output .. '.html', html .. self.html .. '</div></body></html>')
  167. love.filesystem.write('tempoutput/' .. self.output .. '.md', md)
  168. self.module:log('grey', '\nFINISHED - ' .. finaltime .. 's\n')
  169. local failedcol = '\27[31m'
  170. if self.totals[2] == 0 then failedcol = '\27[37m' end
  171. self.module:log('green', tostring(self.totals[1]) .. ' PASSED' .. ' || ' .. failedcol .. tostring(self.totals[2]) .. ' FAILED || \27[37m' .. tostring(self.totals[3]) .. ' SKIPPED')
  172. end
  173. }