main.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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(256, 256, {
  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. end
  46. end
  47. -- get all args with any comma lists split out as seperate
  48. local arglist = {}
  49. for a=1,#args do
  50. local splits = UtilStringSplit(args[a], '([^,]+)')
  51. for s=1,#splits do
  52. table.insert(arglist, splits[s])
  53. end
  54. end
  55. -- convert args to the cmd to run, modules, method (if any) and disabled
  56. local testcmd = '--runAllTests'
  57. local module = ''
  58. local method = ''
  59. local modules = {
  60. 'audio', 'data', 'event', 'filesystem', 'font', 'graphics',
  61. 'image', 'math', 'objects', 'physics', 'sound', 'system',
  62. 'thread', 'timer', 'video', 'window'
  63. }
  64. for a=1,#arglist do
  65. if testcmd == '--runSpecificMethod' then
  66. if module == '' and love[ arglist[a] ] ~= nil then
  67. module = arglist[a]
  68. table.insert(modules, module)
  69. end
  70. if module ~= '' and love[module][ arglist[a] ] ~= nil and method == '' then
  71. method = arglist[a]
  72. end
  73. end
  74. if testcmd == '--runSpecificModules' then
  75. if love[ arglist[a] ] ~= nil or arglist[a] == 'objects' then
  76. table.insert(modules, arglist[a])
  77. end
  78. end
  79. if arglist[a] == '--runSpecificMethod' then
  80. testcmd = arglist[a]
  81. modules = {}
  82. end
  83. if arglist[a] == '--runSpecificModules' then
  84. testcmd = arglist[a]
  85. modules = {}
  86. end
  87. end
  88. -- runSpecificMethod uses the module + method given
  89. if testcmd == '--runSpecificMethod' then
  90. local testmodule = TestModule:new(module, method)
  91. table.insert(love.test.modules, testmodule)
  92. love.test.module = testmodule
  93. love.test.module:log('grey', '--runSpecificMethod "' .. module .. '" "' .. method .. '"')
  94. love.test.output = 'lovetest_runSpecificMethod_' .. module .. '_' .. method
  95. end
  96. -- runSpecificModules runs all methods for all the modules given
  97. if testcmd == '--runSpecificModules' then
  98. local modulelist = {}
  99. for m=1,#modules do
  100. local testmodule = TestModule:new(modules[m])
  101. table.insert(love.test.modules, testmodule)
  102. table.insert(modulelist, modules[m])
  103. end
  104. love.test.module = love.test.modules[1]
  105. love.test.module:log('grey', '--runSpecificModules "' .. table.concat(modulelist, '" "') .. '"')
  106. love.test.output = 'lovetest_runSpecificModules_' .. table.concat(modulelist, '_')
  107. end
  108. -- otherwise default runs all methods for all modules
  109. if arglist[1] == nil or arglist[1] == '' or arglist[1] == '--runAllTests' then
  110. for m=1,#modules do
  111. local testmodule = TestModule:new(modules[m])
  112. table.insert(love.test.modules, testmodule)
  113. end
  114. love.test.module = love.test.modules[1]
  115. love.test.module:log('grey', '--runAllTests')
  116. love.test.output = 'lovetest_runAllTests'
  117. end
  118. -- invalid command
  119. if love.test.module == nil then
  120. print("Wrong flags used")
  121. end
  122. -- start first module
  123. love.test.module:runTests()
  124. end
  125. -- love.update
  126. -- run test suite logic
  127. love.update = function(delta)
  128. love.test:runSuite(delta)
  129. end
  130. -- love.draw
  131. -- draw a little logo to the screen
  132. love.draw = function()
  133. love.graphics.draw(Logo.texture, Logo.img, 64, 64, 0, 2, 2)
  134. end
  135. -- love.quit
  136. -- add a hook to allow test modules to fake quit
  137. love.quit = function()
  138. if love.test.module ~= nil and love.test.module.fakequit == true then
  139. return true
  140. else
  141. return false
  142. end
  143. end
  144. -- string split helper
  145. function UtilStringSplit(str, splitter)
  146. local splits = {}
  147. for word in string.gmatch(str, splitter) do
  148. table.insert(splits, word)
  149. end
  150. return splits
  151. end
  152. -- string time formatter
  153. function UtilTimeFormat(seconds)
  154. return string.format("%.3f", tostring(seconds))
  155. end