about summary refs log tree commit diff stats
path: root/app.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2023-01-20 22:13:31 -0800
committerKartik K. Agaram <vc@akkartik.com>2023-01-20 22:13:31 -0800
commiteb26b9ab5e4a95e608c5fb0cde8fb653c260d110 (patch)
treea24842dd071e44e348c2c8e28882a9245b9d6021 /app.lua
parent9f0c797d19af4d4afe76a2a00882b4126ca038d4 (diff)
parent486afb1c6887cef1b89757e6a75fc14c5cc78dd2 (diff)
downloadview.love-eb26b9ab5e4a95e608c5fb0cde8fb653c260d110.tar.gz
Merge text.love
Diffstat (limited to 'app.lua')
-rw-r--r--app.lua20
1 files changed, 16 insertions, 4 deletions
diff --git a/app.lua b/app.lua
index e717220..cf6de60 100644
--- a/app.lua
+++ b/app.lua
@@ -1,7 +1,7 @@
 -- love.run: main entrypoint function for LÖVE
 --
--- Most apps can just use the default, but we need to override it to
--- install a test harness.
+-- Most apps can just use the default shown in https://love2d.org/wiki/love.run,
+-- but we need to override it to install a test harness.
 --
 -- A test harness needs to check what the 'real' code did.
 -- To do this it needs to hook into primitive operations performed by code.
@@ -134,7 +134,11 @@ end
 
 function App.run_tests_and_initialize()
   App.load()
+  Test_errors = {}
   App.run_tests()
+  if #Test_errors > 0 then
+    error('There were test failures:\n\n'..table.concat(Test_errors))
+  end
   App.disable_tests()
   App.initialize_globals()
   App.initialize(love.arg.parseGameArguments(arg), arg)
@@ -365,15 +369,23 @@ function App.run_tests()
   table.sort(sorted_names)
   for _,name in ipairs(sorted_names) do
     App.initialize_for_test()
-    _G[name]()
+    xpcall(_G[name], function(err) prepend_debug_info_to_test_failure(name, err) end)
   end
-  print()
   -- clean up all test methods
   for _,name in ipairs(sorted_names) do
     _G[name] = nil
   end
 end
 
+-- prepend file/line/test
+function prepend_debug_info_to_test_failure(test_name, err)
+  local err_without_line_number = err:gsub('^[^:]*:[^:]*: ', '')
+  local stack_trace = debug.traceback('', --[[stack frame]]5)
+  local file_and_line_number = stack_trace:gsub('stack traceback:\n', ''):gsub(': .*', '')
+  local full_error = file_and_line_number..':'..test_name..' -- '..err_without_line_number
+  table.insert(Test_errors, full_error)
+end
+
 -- call this once all tests are run
 -- can't run any tests after this
 function App.disable_tests()