TestModule.lua 4.8 KB

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