1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-10-24 18:37:38 +00:00

Add a test utility for capturing program output

- Make mcfly's stubbing system a little more fault-tolerant.
 - Add a small utility function which redirects print, printError and
   write to capture their output, rather than printing to the terminal.
   This can then be matched against in order to determine a program's
   output.
   It's a little flakey - you can't use it multiple times in an it
   block, etc... but it's a nice feature.
 - Add a small couple of tests to delete as a proof-of-concept.
This commit is contained in:
SquidDev
2019-06-03 20:33:09 +01:00
parent 6f1b740c8f
commit ffa4cc241b
3 changed files with 67 additions and 4 deletions

View File

@@ -34,10 +34,12 @@ local active_stubs = {}
-- @tparam string var The variable to stub
-- @param value The value to stub it with
local function stub(tbl, var, value)
table.insert(active_stubs, { tbl = tbl, var = var, value = tbl[var] })
_G[var] = value
end
check('stub', 1, 'table', tbl)
check('stub', 2, 'string', var)
table.insert(active_stubs, { tbl = tbl, var = var, value = tbl[var] })
rawset(tbl, var, value)
end
--- Capture the current global state of the computer
local function push_state()
@@ -55,7 +57,7 @@ end
local function pop_state(state)
for i = #active_stubs, 1, -1 do
local stub = active_stubs[i]
stub.tbl[stub.var] = stub.value
rawset(stub.tbl, stub.var, stub.value)
end
active_stubs = state.stubs
@@ -353,6 +355,9 @@ if not fs.isDir(root_dir) then
error()
end
-- Ensure the test folder is also on the package path
package.path = ("/%s/?.lua;/%s/?/init.lua;%s"):format(root_dir, root_dir, package.path)
do
-- Load in the tests from all our files
local env = setmetatable({}, { __index = _ENV })