consistently save/restore state when running tests
It's not obvious, but this is a refactoring. There should be no behavior changes.
This commit is contained in:
parent
20a586dc1f
commit
7168ed5372
45
app.lua
45
app.lua
|
@ -428,22 +428,55 @@ function App.run_tests(record_error_fn)
|
|||
end
|
||||
end
|
||||
table.sort(sorted_names)
|
||||
local saved_app = App
|
||||
local globals = App.shallow_copy_all_globals()
|
||||
App = App_for_tests
|
||||
local saved_font = love.graphics.getFont()
|
||||
love.graphics.setFont(Love_snapshot.initial_font)
|
||||
for _,name in ipairs(sorted_names) do
|
||||
print(name)
|
||||
App.initialize_for_test()
|
||||
xpcall(_G[name], function(err) record_error_fn(name, err) end)
|
||||
end
|
||||
App = saved_app
|
||||
love.graphics.setFont(saved_font)
|
||||
-- restore all global state except Test_errors
|
||||
local test_errors = Test_errors
|
||||
App.restore_all_globals(globals)
|
||||
Test_errors = test_errors
|
||||
end
|
||||
|
||||
function App.run_test(test, record_error_fn)
|
||||
local saved_app = App
|
||||
local globals = App.shallow_copy_all_globals()
|
||||
App = App_for_tests
|
||||
App.initialize_for_test()
|
||||
xpcall(test, function(err) record_error_fn('', err) end)
|
||||
App = saved_app
|
||||
local saved_font = love.graphics.getFont()
|
||||
love.graphics.setFont(Love_snapshot.initial_font)
|
||||
App.initialize_for_test()
|
||||
xpcall(test, function(err) record_error_fn('', err) end)
|
||||
love.graphics.setFont(saved_font)
|
||||
-- restore all global state except Test_errors
|
||||
local test_errors = Test_errors
|
||||
App.restore_all_globals(globals)
|
||||
Test_errors = test_errors
|
||||
end
|
||||
|
||||
function App.shallow_copy_all_globals()
|
||||
local result = {}
|
||||
for k,v in pairs(_G) do
|
||||
result[k] = v
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
function App.restore_all_globals(x)
|
||||
-- delete extra bindings
|
||||
for k,v in pairs(_G) do
|
||||
if x[k] == nil then
|
||||
_G[k] = nil
|
||||
end
|
||||
end
|
||||
-- restore previous bindings
|
||||
for k,v in pairs(x) do
|
||||
_G[k] = v
|
||||
end
|
||||
end
|
||||
|
||||
-- Test_errors will be an array
|
||||
|
|
26
live.lua
26
live.lua
|
@ -279,35 +279,9 @@ function live.run(buf)
|
|||
return
|
||||
end
|
||||
-- run all tests
|
||||
local globals = live.shallow_copy_all_globals()
|
||||
local current_font = love.graphics.getFont()
|
||||
love.graphics.setFont(Love_snapshot.initial_font)
|
||||
Test_errors = {}
|
||||
App.run_tests(record_error_by_test)
|
||||
live.send_to_driver(json.encode(Test_errors))
|
||||
love.graphics.setFont(current_font)
|
||||
live.restore_all_globals(globals)
|
||||
end
|
||||
end
|
||||
|
||||
function live.shallow_copy_all_globals()
|
||||
local result = {}
|
||||
for k,v in pairs(_G) do
|
||||
result[k] = v
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
function live.restore_all_globals(x)
|
||||
-- delete extra bindings
|
||||
for k,v in pairs(_G) do
|
||||
if x[k] == nil then
|
||||
_G[k] = nil
|
||||
end
|
||||
end
|
||||
-- restore previous bindings
|
||||
for k,v in pairs(x) do
|
||||
_G[k] = v
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue