TestSuite.lua 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. current_os = love.system.getOS(),
  24. -- love modules to test
  25. audio = {},
  26. data = {},
  27. event = {},
  28. filesystem = {},
  29. font = {},
  30. graphics = {},
  31. image = {},
  32. joystick = {},
  33. love = {},
  34. keyboard = {},
  35. math = {},
  36. mouse = {},
  37. physics = {},
  38. sensor = {},
  39. sound = {},
  40. system = {},
  41. thread = {},
  42. timer = {},
  43. touch = {},
  44. video = {},
  45. window = {}
  46. }
  47. setmetatable(test, self)
  48. self.__index = self
  49. return test
  50. end,
  51. -- @method - TestSuite:runSuite()
  52. -- @desc - called in love.update, runs through every method or every module
  53. -- @param {number} delta - delta from love.update to track time elapsed
  54. -- @return {nil}
  55. runSuite = function(self, delta)
  56. -- stagger between tests
  57. if self.module ~= nil then
  58. if self.module.start 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 = 'love.' .. self.module.module .. '.' .. method
  67. self.test.co = coroutine.create(function()
  68. local ok, chunk, err = pcall(
  69. love.test[love.test.module.module][method],
  70. love.test.test
  71. )
  72. if ok == false then
  73. love.test.test['passed'] = false
  74. love.test.test['fatal'] = tostring(chunk) .. tostring(err)
  75. end
  76. end)
  77. -- once called we have a corouting, so just call resume every frame
  78. -- until we have finished
  79. else
  80. -- move onto next yield if any
  81. -- pauses can be set with TestMethod:waitFrames(frames)
  82. coroutine.resume(self.test.co)
  83. -- when wait finished (or no yields)
  84. if coroutine.status(self.test.co) == 'dead' then
  85. -- now we're all done evaluate the test
  86. local ok, chunk, err = pcall(self.test.evaluateTest, self.test)
  87. if ok == false then
  88. self.test.passed = false
  89. self.test.fatal = tostring(chunk) .. tostring(err)
  90. end
  91. -- save having to :release() anything we made in the last test
  92. collectgarbage("collect")
  93. -- move onto the next test
  94. self.module.index = self.module.index + 1
  95. end
  96. end
  97. -- once all tests have run
  98. else
  99. -- print module results and add to output
  100. self.module:printResult()
  101. -- if we have more modules to go run the next one
  102. self.current = self.current + 1
  103. if #self.modules >= self.current then
  104. self.module = self.modules[self.current]
  105. self.module:runTests()
  106. -- otherwise print the final results and export output
  107. else
  108. self:printResult()
  109. love.event.quit(0)
  110. end
  111. end
  112. end
  113. end
  114. end,
  115. -- @method - TestSuite:printResult()
  116. -- @desc - prints the result of the whole test suite as well as writes
  117. -- the MD, XML + HTML of the testsuite output
  118. -- @return {nil}
  119. printResult = function(self)
  120. local finaltime = UtilTimeFormat(self.time)
  121. -- in case we dont have love.graphics loaded, for future module specific disabling
  122. local name = 'NONE'
  123. local version = 'NONE'
  124. local vendor = 'NONE'
  125. local device = 'NONE'
  126. if love.graphics then
  127. name, version, vendor, device = love.graphics.getRendererInfo()
  128. end
  129. local md = '<!-- PASSED ' .. tostring(self.totals[1]) ..
  130. ' || FAILED ' .. tostring(self.totals[2]) ..
  131. ' || SKIPPED ' .. tostring(self.totals[3]) ..
  132. ' || TIME ' .. finaltime .. ' -->\n\n### Info\n' ..
  133. '**' .. tostring(self.totals[1] + self.totals[2] + self.totals[3]) .. '** tests were completed in **' ..
  134. finaltime .. 's** with **' ..
  135. tostring(self.totals[1]) .. '** passed, **' ..
  136. tostring(self.totals[2]) .. '** failed, and **' ..
  137. tostring(self.totals[3]) .. '** skipped\n\n' ..
  138. 'Renderer: ' .. name .. ' | ' .. version .. ' | ' .. vendor .. ' | ' .. device .. '\n\n' ..
  139. '### Report\n' ..
  140. '| Module | Pass | Fail | Skip | Time |\n' ..
  141. '| --------------------- | ------ | ------ | ------- | ------ |\n' ..
  142. self.mdrows .. '\n### Failures\n' .. self.mdfailures
  143. local xml = '<testsuites name="love.test" tests="' .. tostring(self.totals[1]) ..
  144. '" failures="' .. tostring(self.totals[2]) ..
  145. '" skipped="' .. tostring(self.totals[3]) ..
  146. '" time="' .. finaltime .. '">\n'
  147. local status = '🔴'
  148. if self.totals[2] == 0 then status = '🟢' end
  149. 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">'
  150. html = html ..
  151. '<li>🟢&nbsp;' .. tostring(self.totals[1]) .. ' Tests</li>' ..
  152. '<li>🔴&nbsp;' .. tostring(self.totals[2]) .. ' Failures</li>' ..
  153. '<li>🟡&nbsp;' .. tostring(self.totals[3]) .. ' Skipped</li>' ..
  154. '<li>' .. finaltime .. 's</li></ul><br/><br/>'
  155. love.filesystem.write('tempoutput/' .. self.output .. '.xml', xml .. self.xml .. '</testsuites>')
  156. love.filesystem.write('tempoutput/' .. self.output .. '.html', html .. self.html .. '</div></body></html>')
  157. love.filesystem.write('tempoutput/' .. self.output .. '.md', md)
  158. self.module:log('grey', '\nFINISHED - ' .. finaltime .. 's\n')
  159. local failedcol = '\27[31m'
  160. if self.totals[2] == 0 then failedcol = '\27[37m' end
  161. self.module:log('green', tostring(self.totals[1]) .. ' PASSED' .. ' || ' .. failedcol .. tostring(self.totals[2]) .. ' FAILED || \27[37m' .. tostring(self.totals[3]) .. ' SKIPPED')
  162. end
  163. }