TestModule.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. -- @class - TestModule
  2. -- @desc - used to run tests for a given module, each test method will spawn
  3. -- a love.test.Test object
  4. TestModule = {
  5. -- @method - TestModule:new()
  6. -- @desc - create a new Suite object
  7. -- @param {string} module - string of love module the suite is for
  8. -- @return {table} - returns the new Suite object
  9. new = function(self, module, method)
  10. local testmodule = {
  11. timer = 0,
  12. time = 0,
  13. delay = 0.01,
  14. spacer = ' ',
  15. colors = {
  16. PASS = 'green', FAIL = 'red', SKIP = 'grey'
  17. },
  18. colormap = {
  19. grey = '\27[37m',
  20. green = '\27[32m',
  21. red = '\27[31m',
  22. yellow = '\27[33m'
  23. },
  24. xml = '',
  25. html = '',
  26. tests = {},
  27. running = {},
  28. called = {},
  29. passed = 0,
  30. failed = 0,
  31. skipped = 0,
  32. module = module,
  33. method = method,
  34. index = 1,
  35. start = false,
  36. }
  37. setmetatable(testmodule, self)
  38. self.__index = self
  39. return testmodule
  40. end,
  41. -- @method - TestModule:log()
  42. -- @desc - log to console with specific colors, split out to make it easier
  43. -- to adjust all console output across the tests
  44. -- @param {string} color - color key to use for the log
  45. -- @param {string} line - main message to write (LHS)
  46. -- @param {string} result - result message to write (RHS)
  47. -- @return {nil}
  48. log = function(self, color, line, result)
  49. if result == nil then result = '' end
  50. print(self.colormap[color] .. line .. result)
  51. end,
  52. -- @method - TestModule:runTests()
  53. -- @desc - starts the running of tests and sets up the list of methods to test
  54. -- @param {string} module - module to set for the test suite
  55. -- @param {string} method - specific method to test, if nil all methods tested
  56. -- @return {nil}
  57. runTests = function(self)
  58. self.running = {}
  59. self.passed = 0
  60. self.failed = 0
  61. if self.method ~= nil then
  62. table.insert(self.running, self.method)
  63. else
  64. for i,_ in pairs(love.test[self.module]) do
  65. table.insert(self.running, i)
  66. end
  67. table.sort(self.running)
  68. end
  69. self.index = 1
  70. self.start = true
  71. self:log('yellow', '\nlove.' .. self.module .. '.testmodule.start')
  72. end,
  73. -- @method - TestModule:printResult()
  74. -- @desc - prints the result of the module to the console as well as appends
  75. -- the XML + HTML for the test to the testsuite output
  76. -- @return {nil}
  77. printResult = function(self)
  78. local finaltime = UtilTimeFormat(self.time)
  79. local status = '🔴'
  80. if self.failed == 0 then status = '🟢' end
  81. -- add md row to main output
  82. love.test.mdrows = love.test.mdrows .. '| ' .. status ..
  83. ' ' .. self.module ..
  84. ' | ' .. tostring(self.passed) ..
  85. ' | ' .. tostring(self.failed) ..
  86. ' | ' .. tostring(self.skipped) ..
  87. ' | ' .. finaltime .. 's |' .. '\n'
  88. -- add xml to main output
  89. love.test.xml = love.test.xml .. '\t<testsuite name="love.' .. self.module ..
  90. '" tests="' .. tostring(self.passed) ..
  91. '" failures="' .. tostring(self.failed) ..
  92. '" skipped="' .. tostring(self.skipped) ..
  93. '" time="' .. finaltime .. '">\n' .. self.xml .. '\t</testsuite>\n'
  94. -- add html to main output
  95. love.test.html = love.test.html .. '<h2>' .. status .. '&nbsp;love.' .. self.module .. '</h2><ul class="summary">' ..
  96. '<li>🟢&nbsp;' .. tostring(self.passed) .. ' Tests</li>' ..
  97. '<li>🔴&nbsp;' .. tostring(self.failed) .. ' Failures</li>' ..
  98. '<li>🟡&nbsp;' .. tostring(self.skipped) .. ' Skipped</li>' ..
  99. '<li>' .. finaltime .. 's</li>' .. '<ul><br/><br/>' ..
  100. '<table><thead><tr><td width="20px"></td><td width="100px">Method</td><td width="60px">Time</td><td>Details</td></tr></thead><tbody>' ..
  101. self.html .. '</tbody></table>'
  102. -- print module results to console
  103. self:log('yellow', 'love.' .. self.module .. '.testmodule.end')
  104. local failedcol = '\27[31m'
  105. if self.failed == 0 then failedcol = '\27[37m' end
  106. self:log('green', tostring(self.passed) .. ' PASSED' .. ' || ' ..
  107. failedcol .. tostring(self.failed) .. ' FAILED || \27[37m' ..
  108. tostring(self.skipped) .. ' SKIPPED || ' .. finaltime .. 's')
  109. self.start = false
  110. self.fakequit = false
  111. end
  112. }