TestSuite.lua 7.5 KB

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