main.lua 6.8 KB

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