main.lua 6.3 KB

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