main.lua 6.8 KB

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