main.lua 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. -- load test objs
  2. require('classes.TestSuite')
  3. require('classes.TestModule')
  4. require('classes.TestMethod')
  5. -- create testsuite obj
  6. love.test = TestSuite:new()
  7. -- load test scripts if module is active
  8. -- this is so in future if we have per-module disabling it'll still run
  9. if love ~= nil then require('tests.love') end
  10. if love.audio ~= nil then require('tests.audio') end
  11. if love.data ~= nil then require('tests.data') end
  12. if love.event ~= nil then require('tests.event') end
  13. if love.filesystem ~= nil then require('tests.filesystem') end
  14. if love.font ~= nil then require('tests.font') end
  15. if love.graphics ~= nil then require('tests.graphics') end
  16. if love.image ~= nil then require('tests.image') end
  17. if love.joystick ~= nil then require('tests.joystick') end
  18. if love.keyboard ~= nil then require('tests.keyboard') end
  19. if love.math ~= nil then require('tests.math') end
  20. if love.mouse ~= nil then require('tests.mouse') end
  21. if love.physics ~= nil then require('tests.physics') end
  22. if love.sensor ~= nil then require('tests.sensor') end
  23. if love.sound ~= nil then require('tests.sound') end
  24. if love.system ~= nil then require('tests.system') end
  25. if love.thread ~= nil then require('tests.thread') end
  26. if love.timer ~= nil then require('tests.timer') end
  27. if love.touch ~= nil then require('tests.touch') end
  28. if love.video ~= nil then require('tests.video') end
  29. if love.window ~= nil then require('tests.window') end
  30. -- love.load
  31. -- load given arguments and run the test suite
  32. love.load = function(args)
  33. -- setup basic img to display
  34. if love.window ~= nil then
  35. love.window.updateMode(360, 240, {
  36. fullscreen = false,
  37. resizable = true,
  38. centered = true
  39. })
  40. if love.graphics ~= nil then
  41. love.graphics.setDefaultFilter("nearest", "nearest")
  42. love.graphics.setLineStyle('rough')
  43. love.graphics.setLineWidth(1)
  44. Logo = {
  45. texture = love.graphics.newImage('resources/love.png'),
  46. img = nil
  47. }
  48. Logo.img = love.graphics.newQuad(0, 0, 64, 64, Logo.texture)
  49. Font = love.graphics.newFont('resources/font.ttf', 8, 'normal')
  50. TextCommand = 'Loading...'
  51. TextRun = ''
  52. end
  53. end
  54. -- mount for output later
  55. if love.filesystem.mountFullPath then
  56. love.filesystem.mountFullPath(love.filesystem.getSource() .. "/output", "tempoutput", "readwrite")
  57. end
  58. -- get all args with any comma lists split out as seperate
  59. local arglist = {}
  60. for a=1,#args do
  61. local splits = UtilStringSplit(args[a], '([^,]+)')
  62. for s=1,#splits do
  63. table.insert(arglist, splits[s])
  64. end
  65. end
  66. -- convert args to the cmd to run, modules, method (if any) and disabled
  67. local testcmd = '--runAllTests'
  68. local module = ''
  69. local method = ''
  70. local cmderr = 'Invalid flag used'
  71. local modules = {
  72. 'audio', 'data', 'event', 'filesystem', 'font', 'graphics', 'image',
  73. 'joystick', 'keyboard', 'love', 'math', 'mouse', 'physics', 'sensor',
  74. 'sound', 'system', 'thread', 'timer', 'touch', 'video', 'window'
  75. }
  76. GITHUB_RUNNER = false
  77. for a=1,#arglist do
  78. if testcmd == '--runSpecificMethod' then
  79. if module == '' and (arglist[a] == 'love' or love[ arglist[a] ] ~= nil) then
  80. module = arglist[a]
  81. table.insert(modules, module)
  82. elseif module ~= '' and love[module] ~= nil and method == '' then
  83. if love.test[module][arglist[a]] ~= nil then method = arglist[a] end
  84. end
  85. end
  86. if testcmd == '--runSpecificModules' then
  87. if (arglist[a] == 'love' or love[ arglist[a] ] ~= nil) and arglist[a] ~= '--isRunner' then
  88. table.insert(modules, arglist[a])
  89. end
  90. end
  91. if arglist[a] == '--runSpecificMethod' then
  92. testcmd = arglist[a]
  93. modules = {}
  94. end
  95. if arglist[a] == '--runSpecificModules' then
  96. testcmd = arglist[a]
  97. modules = {}
  98. end
  99. if arglist[a] == '--isRunner' then
  100. GITHUB_RUNNER = true
  101. end
  102. end
  103. -- runSpecificMethod uses the module + method given
  104. if testcmd == '--runSpecificMethod' then
  105. local testmodule = TestModule:new(module, method)
  106. table.insert(love.test.modules, testmodule)
  107. if module ~= '' and method ~= '' then
  108. love.test.module = testmodule
  109. love.test.module:log('grey', '--runSpecificMethod "' .. module .. '" "' .. method .. '"')
  110. love.test.output = 'lovetest_runSpecificMethod_' .. module .. '_' .. method
  111. else
  112. if method == '' then cmderr = 'No valid method specified' end
  113. if module == '' then cmderr = 'No valid module specified' end
  114. end
  115. end
  116. -- runSpecificModules runs all methods for all the modules given
  117. if testcmd == '--runSpecificModules' then
  118. local modulelist = {}
  119. for m=1,#modules do
  120. local testmodule = TestModule:new(modules[m])
  121. table.insert(love.test.modules, testmodule)
  122. table.insert(modulelist, modules[m])
  123. end
  124. if #modulelist > 0 then
  125. love.test.module = love.test.modules[1]
  126. love.test.module:log('grey', '--runSpecificModules "' .. table.concat(modulelist, '" "') .. '"')
  127. love.test.output = 'lovetest_runSpecificModules_' .. table.concat(modulelist, '_')
  128. else
  129. cmderr = 'No modules specified'
  130. end
  131. end
  132. -- otherwise default runs all methods for all modules
  133. if arglist[1] == nil or arglist[1] == '' or arglist[1] == '--runAllTests' then
  134. for m=1,#modules do
  135. local testmodule = TestModule:new(modules[m])
  136. table.insert(love.test.modules, testmodule)
  137. end
  138. love.test.module = love.test.modules[1]
  139. love.test.module:log('grey', '--runAllTests')
  140. love.test.output = 'lovetest_runAllTests'
  141. end
  142. if GITHUB_RUNNER then
  143. love.test.module:log('grey', '--isRunner')
  144. end
  145. -- invalid command
  146. if love.test.module == nil then
  147. print(cmderr)
  148. love.event.quit(0)
  149. else
  150. -- start first module
  151. TextCommand = testcmd
  152. love.test.module:runTests()
  153. end
  154. end
  155. -- love.update
  156. -- run test suite logic
  157. love.update = function(delta)
  158. love.test:runSuite(delta)
  159. end
  160. -- love.draw
  161. -- draw a little logo to the screen
  162. love.draw = function()
  163. local lw = (love.graphics.getWidth() - 128) / 2
  164. local lh = (love.graphics.getHeight() - 128) / 2
  165. love.graphics.draw(Logo.texture, Logo.img, lw, lh, 0, 2, 2)
  166. love.graphics.setFont(Font)
  167. love.graphics.print(TextCommand, 4, 12, 0, 2, 2)
  168. love.graphics.print(TextRun, 4, 32, 0, 2, 2)
  169. end
  170. -- love.quit
  171. -- add a hook to allow test modules to fake quit
  172. love.quit = function()
  173. if love.test.module ~= nil and love.test.module.fakequit then
  174. return true
  175. else
  176. return false
  177. end
  178. end
  179. -- added so bad threads dont fail
  180. function love.threaderror(thread, errorstr) end
  181. -- string split helper
  182. function UtilStringSplit(str, splitter)
  183. local splits = {}
  184. for word in string.gmatch(str, splitter) do
  185. table.insert(splits, word)
  186. end
  187. return splits
  188. end
  189. -- string time formatter
  190. function UtilTimeFormat(seconds)
  191. return string.format("%.3f", tostring(seconds))
  192. end