TestSuite.lua 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. testcanvas = nil,
  11. current = 1,
  12. output = '',
  13. totals = {0, 0, 0},
  14. time = 0,
  15. xml = '',
  16. html = '',
  17. fakequit = false,
  18. windowmode = true,
  19. -- love modules to test
  20. audio = {},
  21. data = {},
  22. event = {},
  23. filesystem = {},
  24. font = {},
  25. graphics = {},
  26. image = {},
  27. joystick = {},
  28. math = {},
  29. mouse = {},
  30. objects = {}, -- special for all object class contructor tests
  31. physics = {},
  32. sound = {},
  33. system = {},
  34. thread = {},
  35. timer = {},
  36. touch = {},
  37. video = {},
  38. window = {}
  39. }
  40. setmetatable(test, self)
  41. self.__index = self
  42. return test
  43. end,
  44. -- @method - TestSuite:runSuite()
  45. -- @desc - called in love.update, runs through every method or every module
  46. -- @param {number} delta - delta from love.update to track time elapsed
  47. -- @return {nil}
  48. runSuite = function(self, delta)
  49. -- stagger 0.1s between tests
  50. if self.module ~= nil then
  51. self.module.timer = self.module.timer + delta
  52. if self.module.timer >= self.module.delay then
  53. self.module.timer = self.module.timer - self.module.delay
  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. local test = TestMethod:new(method, self.module)
  62. -- check method exists in love first
  63. if self.module.module ~= 'objects' and (love[self.module.module] == nil or love[self.module.module][method] == nil) then
  64. local tested = 'love.' .. self.module.module .. '.' .. method .. '()'
  65. local matching = string.sub(self.module.spacer, string.len(tested), 40)
  66. self.module:log(self.module.colors['FAIL'],
  67. tested .. matching,
  68. ' ==> FAIL (0/0) - call failed - method does not exist'
  69. )
  70. -- otherwise run the test method then eval the asserts
  71. else
  72. local ok, chunk, err = pcall(self[self.module.module][method], test)
  73. if ok == false then
  74. print("FATAL", chunk, err)
  75. test.fatal = tostring(chunk) .. tostring(err)
  76. end
  77. local ok, chunk, err = pcall(test.evaluateTest, test)
  78. if ok == false then
  79. print("FATAL", chunk, err)
  80. test.fatal = tostring(chunk) .. tostring(err)
  81. end
  82. end
  83. -- save having to :release() anything we made in the last test
  84. -- 7251ms > 7543ms
  85. collectgarbage("collect")
  86. -- move onto the next test
  87. self.module.index = self.module.index + 1
  88. end
  89. else
  90. -- print module results and add to output
  91. self.module:printResult()
  92. -- if we have more modules to go run the next one
  93. self.current = self.current + 1
  94. if #self.modules >= self.current then
  95. self.module = self.modules[self.current]
  96. self.module:runTests()
  97. -- otherwise print the final results and export output
  98. else
  99. self:printResult()
  100. love.event.quit(0)
  101. end
  102. end
  103. end
  104. end
  105. end
  106. end,
  107. -- @method - TestSuite:printResult()
  108. -- @desc - prints the result of the whole test suite as well as writes
  109. -- the XML + HTML of the testsuite output
  110. -- @return {nil}
  111. printResult = function(self)
  112. local finaltime = UtilTimeFormat(self.time)
  113. local xml = '<testsuites name="love.test" tests="' .. tostring(self.totals[1]) ..
  114. '" failures="' .. tostring(self.totals[2]) ..
  115. '" skipped="' .. tostring(self.totals[3]) ..
  116. '" time="' .. finaltime .. '">\n'
  117. local status = '🔴'
  118. if self.totals[2] == 0 then status = '🟢' end
  119. 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">'
  120. html = html ..
  121. '<li>🟢&nbsp;' .. tostring(self.totals[1]) .. ' Tests</li>' ..
  122. '<li>🔴&nbsp;' .. tostring(self.totals[2]) .. ' Failures</li>' ..
  123. '<li>🟡&nbsp;' .. tostring(self.totals[3]) .. ' Skipped</li>' ..
  124. '<li>' .. finaltime .. 's</li></ul><br/><br/>'
  125. -- @TODO use mountFullPath to write output to src?
  126. love.filesystem.createDirectory('output')
  127. love.filesystem.write('output/' .. self.output .. '.xml', xml .. self.xml .. '</testsuites>')
  128. love.filesystem.write('output/' .. self.output .. '.html', html .. self.html .. '</div></body></html>')
  129. self.module:log('grey', '\nFINISHED - ' .. finaltime .. 's\n')
  130. local failedcol = '\27[31m'
  131. if self.totals[2] == 0 then failedcol = '\27[37m' end
  132. self.module:log('green', tostring(self.totals[1]) .. ' PASSED' .. ' || ' .. failedcol .. tostring(self.totals[2]) .. ' FAILED || \27[37m' .. tostring(self.totals[3]) .. ' SKIPPED')
  133. end
  134. }