TestSuite.lua 6.9 KB

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