TestSuite.lua 6.7 KB

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