main.lua 6.7 KB

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