2008-10-31 18:38:05 +00:00
|
|
|
--
|
|
|
|
-- tests/testfx.lua
|
|
|
|
-- Automated test framework for Premake.
|
2012-12-04 16:45:26 +00:00
|
|
|
-- Copyright (c) 2008-2012 Jason Perkins and the Premake project
|
2008-10-31 18:38:05 +00:00
|
|
|
--
|
|
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Define a namespace for the testing functions
|
|
|
|
--
|
|
|
|
|
2012-12-04 16:45:26 +00:00
|
|
|
test = {}
|
2009-09-15 21:08:40 +00:00
|
|
|
|
2008-10-31 18:38:05 +00:00
|
|
|
|
2013-02-01 19:54:14 +00:00
|
|
|
--
|
|
|
|
-- Capture stderr for testing.
|
|
|
|
--
|
|
|
|
|
|
|
|
local stderr_capture = nil
|
|
|
|
|
|
|
|
local mt = getmetatable(io.stderr)
|
|
|
|
local builtin_write = mt.write
|
|
|
|
mt.write = function(...)
|
|
|
|
if select(1,...) == io.stderr then
|
|
|
|
stderr_capture = (stderr_capture or "") .. select(2,...)
|
|
|
|
else
|
|
|
|
return builtin_write(...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2008-10-31 18:38:05 +00:00
|
|
|
--
|
|
|
|
-- Assertion functions
|
|
|
|
--
|
2012-12-04 16:45:26 +00:00
|
|
|
|
2009-03-25 23:04:32 +00:00
|
|
|
function test.capture(expected)
|
|
|
|
local actual = io.endcapture()
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2011-12-04 19:47:59 +00:00
|
|
|
-- create line-by-line iterators for both values
|
2009-03-25 23:04:32 +00:00
|
|
|
local ait = actual:gfind("(.-)" .. io.eol)
|
|
|
|
local eit = expected:gfind("(.-)\n")
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2011-12-04 19:47:59 +00:00
|
|
|
-- compare each value line by line
|
2009-03-25 23:04:32 +00:00
|
|
|
local linenum = 1
|
|
|
|
local atxt = ait()
|
|
|
|
local etxt = eit()
|
|
|
|
while etxt do
|
|
|
|
if (etxt ~= atxt) then
|
|
|
|
test.fail("(%d) expected:\n%s\n...but was:\n%s", linenum, etxt, atxt)
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2009-03-25 23:04:32 +00:00
|
|
|
linenum = linenum + 1
|
|
|
|
atxt = ait()
|
|
|
|
etxt = eit()
|
|
|
|
end
|
|
|
|
end
|
2009-08-12 01:16:15 +00:00
|
|
|
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2009-08-12 01:16:15 +00:00
|
|
|
function test.closedfile(expected)
|
|
|
|
if expected and not test.value_closedfile then
|
|
|
|
test.fail("expected file to be closed")
|
|
|
|
elseif not expected and test.value_closedfile then
|
|
|
|
test.fail("expected file to remain open")
|
|
|
|
end
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2009-07-14 14:28:26 +00:00
|
|
|
|
|
|
|
function test.contains(value, expected)
|
|
|
|
if not table.contains(value, expected) then
|
|
|
|
test.fail("expected value %s not found", expected)
|
|
|
|
end
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
|
|
|
|
2008-10-31 18:38:05 +00:00
|
|
|
function test.fail(format, ...)
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2012-11-04 14:21:40 +00:00
|
|
|
-- if format is a number then it is the stack depth
|
|
|
|
local depth = 3
|
|
|
|
if type(format) == "number" then
|
|
|
|
depth = depth + format
|
|
|
|
format = table.remove(arg, 1)
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2008-10-31 18:38:05 +00:00
|
|
|
-- convert nils into something more usefuls
|
|
|
|
for i = 1, arg.n do
|
2013-02-01 19:54:14 +00:00
|
|
|
if (arg[i] == nil) then
|
2008-10-31 18:38:05 +00:00
|
|
|
arg[i] = "(nil)"
|
|
|
|
elseif (type(arg[i]) == "table") then
|
|
|
|
arg[i] = "{" .. table.concat(arg[i], ", ") .. "}"
|
|
|
|
end
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2012-10-31 23:34:04 +00:00
|
|
|
local msg = string.format(format, unpack(arg))
|
2012-11-04 14:21:40 +00:00
|
|
|
error(debug.traceback(msg, depth), depth)
|
2008-10-31 18:38:05 +00:00
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
|
|
|
|
2008-10-31 18:38:05 +00:00
|
|
|
function test.filecontains(expected, fn)
|
|
|
|
local f = io.open(fn)
|
|
|
|
local actual = f:read("*a")
|
|
|
|
f:close()
|
|
|
|
if (expected ~= actual) then
|
|
|
|
test.fail("expected %s but was %s", expected, actual)
|
|
|
|
end
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2011-07-01 19:05:16 +00:00
|
|
|
|
|
|
|
function test.isemptycapture()
|
|
|
|
local actual = io.endcapture()
|
|
|
|
if actual ~= "" then
|
|
|
|
test.fail("expected empty capture, but was %s", actual);
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2012-11-04 14:21:40 +00:00
|
|
|
function test.isequal(expected, actual, depth)
|
|
|
|
depth = depth or 0
|
2012-02-17 00:51:14 +00:00
|
|
|
if type(expected) == "table" then
|
2012-10-11 14:59:15 +00:00
|
|
|
if expected and not actual then
|
2012-11-04 14:21:40 +00:00
|
|
|
test.fail(depth, "expected table, got nil")
|
2012-10-11 14:59:15 +00:00
|
|
|
end
|
2012-02-17 00:51:14 +00:00
|
|
|
if #expected < #actual then
|
2012-11-04 14:21:40 +00:00
|
|
|
test.fail(depth, "expected %d items, got %d", #expected, #actual)
|
2012-02-17 00:51:14 +00:00
|
|
|
end
|
2008-10-31 18:38:05 +00:00
|
|
|
for k,v in pairs(expected) do
|
2012-11-04 14:21:40 +00:00
|
|
|
test.isequal(expected[k], actual[k], depth + 1)
|
2008-10-31 18:38:05 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
if (expected ~= actual) then
|
2012-11-04 14:21:40 +00:00
|
|
|
test.fail(depth, "expected %s but was %s", expected, actual)
|
2008-10-31 18:38:05 +00:00
|
|
|
end
|
|
|
|
end
|
2010-02-17 22:11:25 +00:00
|
|
|
return true
|
2008-10-31 18:38:05 +00:00
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
|
|
|
|
2008-10-31 18:38:05 +00:00
|
|
|
function test.isfalse(value)
|
|
|
|
if (value) then
|
|
|
|
test.fail("expected false but was true")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2008-10-31 18:38:05 +00:00
|
|
|
function test.isnil(value)
|
|
|
|
if (value ~= nil) then
|
|
|
|
test.fail("expected nil but was " .. tostring(value))
|
|
|
|
end
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
|
|
|
|
2009-08-18 19:09:17 +00:00
|
|
|
function test.isnotnil(value)
|
|
|
|
if (value == nil) then
|
|
|
|
test.fail("expected not nil")
|
|
|
|
end
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
|
|
|
|
2008-10-31 18:38:05 +00:00
|
|
|
function test.istrue(value)
|
|
|
|
if (not value) then
|
|
|
|
test.fail("expected true but was false")
|
|
|
|
end
|
2009-07-16 14:29:08 +00:00
|
|
|
end
|
|
|
|
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2009-08-12 01:16:15 +00:00
|
|
|
function test.openedfile(fname)
|
|
|
|
if fname ~= test.value_openedfilename then
|
|
|
|
local msg = "expected to open file '" .. fname .. "'"
|
|
|
|
if test.value_openedfilename then
|
|
|
|
msg = msg .. ", got '" .. test.value_openedfilename .. "'"
|
|
|
|
end
|
|
|
|
test.fail(msg)
|
|
|
|
end
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
|
|
|
|
2009-07-16 14:29:08 +00:00
|
|
|
function test.success(fn, ...)
|
|
|
|
local ok, err = pcall(fn, unpack(arg))
|
|
|
|
if not ok then
|
|
|
|
test.fail("call failed: " .. err)
|
|
|
|
end
|
2008-10-31 18:38:05 +00:00
|
|
|
end
|
|
|
|
|
2009-08-12 01:16:15 +00:00
|
|
|
|
2013-02-01 19:54:14 +00:00
|
|
|
function test.stderr(expected)
|
|
|
|
if not expected and stderr_capture then
|
|
|
|
test.fail("Unexpected: " .. stderr_capture)
|
|
|
|
elseif expected then
|
|
|
|
if not stderr_capture or not stderr_capture:find(expected) then
|
|
|
|
test.fail(string.format("expected '%s'; got %s", expected, stderr_capture or "(nil)"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function test.notstderr(expected)
|
|
|
|
if not expected and not stderr_capture then
|
|
|
|
test.fail("Expected output on stderr; none received")
|
|
|
|
elseif expected then
|
|
|
|
if stderr_capture and stderr_capture:find(expected) then
|
|
|
|
test.fail(string.format("stderr contains '%s'; was %s", expected, stderr_capture))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-12 01:16:15 +00:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Test stubs
|
|
|
|
--
|
|
|
|
|
|
|
|
local function stub_io_open(fname, mode)
|
|
|
|
test.value_openedfilename = fname
|
|
|
|
test.value_openedfilemode = mode
|
|
|
|
return {
|
|
|
|
close = function()
|
|
|
|
test.value_closedfile = true
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2009-08-12 01:16:15 +00:00
|
|
|
local function stub_io_output(f)
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2009-08-12 15:22:44 +00:00
|
|
|
local function stub_print(s)
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
|
|
|
|
2008-10-31 18:38:05 +00:00
|
|
|
--
|
|
|
|
-- Define a collection for the test suites
|
|
|
|
--
|
|
|
|
|
2012-12-04 16:45:26 +00:00
|
|
|
T = {}
|
2008-10-31 18:38:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Test execution function
|
|
|
|
--
|
2011-08-21 23:52:49 +00:00
|
|
|
local _OS_host = _OS
|
2012-04-04 20:21:22 +00:00
|
|
|
|
2012-10-24 15:31:30 +00:00
|
|
|
local function error_handler(err)
|
2012-10-31 23:34:04 +00:00
|
|
|
local msg = err
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2012-10-31 23:34:04 +00:00
|
|
|
-- if the error doesn't include a stack trace, add one
|
|
|
|
if not msg:find("stack traceback:", 1, true) then
|
|
|
|
msg = debug.traceback(err, 2)
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2012-10-31 23:34:04 +00:00
|
|
|
-- trim of the trailing context of the originating xpcall
|
|
|
|
local i = msg:find("[C]: in function 'xpcall'", 1, true)
|
|
|
|
if i then
|
|
|
|
msg = msg:sub(1, i - 3)
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2012-10-31 23:34:04 +00:00
|
|
|
-- if the resulting stack trace is only one level deep, ignore it
|
|
|
|
local n = select(2, msg:gsub('\n', '\n'))
|
|
|
|
if n == 2 then
|
|
|
|
msg = msg:sub(1, msg:find('\n', 1, true) - 1)
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2012-10-24 15:31:30 +00:00
|
|
|
return msg
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
|
|
|
|
2009-08-12 01:16:15 +00:00
|
|
|
local function test_setup(suite, fn)
|
|
|
|
_ACTION = "test"
|
|
|
|
_ARGS = { }
|
|
|
|
_OPTIONS = { }
|
2011-08-21 23:52:49 +00:00
|
|
|
_OS = _OS_host
|
2012-04-04 20:21:22 +00:00
|
|
|
|
2013-02-01 19:54:14 +00:00
|
|
|
stderr_capture = nil
|
|
|
|
|
2009-11-04 18:44:13 +00:00
|
|
|
premake.solution.list = { }
|
2012-11-28 14:55:25 +00:00
|
|
|
premake.api.reset()
|
2012-04-04 20:21:22 +00:00
|
|
|
|
2011-05-12 20:00:07 +00:00
|
|
|
io.indent = nil
|
2011-09-15 20:45:48 +00:00
|
|
|
io.eol = "\n"
|
2009-08-12 01:16:15 +00:00
|
|
|
|
2009-09-15 21:08:40 +00:00
|
|
|
-- reset captured I/O values
|
2009-08-12 01:16:15 +00:00
|
|
|
test.value_openedfilename = nil
|
|
|
|
test.value_openedfilemode = nil
|
2011-05-12 20:00:07 +00:00
|
|
|
test.value_closedfile = false
|
2009-08-12 01:16:15 +00:00
|
|
|
|
|
|
|
if suite.setup then
|
2012-10-24 15:31:30 +00:00
|
|
|
return xpcall(suite.setup, error_handler)
|
2009-08-12 01:16:15 +00:00
|
|
|
else
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2009-08-12 01:16:15 +00:00
|
|
|
|
|
|
|
local function test_run(suite, fn)
|
2011-02-03 21:38:32 +00:00
|
|
|
io.capture()
|
2012-10-24 15:31:30 +00:00
|
|
|
return xpcall(fn, error_handler)
|
2009-08-12 01:16:15 +00:00
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2009-08-12 01:16:15 +00:00
|
|
|
|
|
|
|
local function test_teardown(suite, fn)
|
|
|
|
if suite.teardown then
|
2012-10-24 15:31:30 +00:00
|
|
|
return xpcall(suite.teardown, error_handler)
|
2009-08-12 01:16:15 +00:00
|
|
|
else
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-12-29 18:26:41 +00:00
|
|
|
function test.declare(id)
|
|
|
|
if T[id] then
|
|
|
|
error("Duplicate test suite " .. id)
|
|
|
|
end
|
|
|
|
T[id] = {}
|
|
|
|
return T[id]
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-04-18 19:14:31 +00:00
|
|
|
function test.runall(suitename, testname)
|
2009-09-15 21:08:40 +00:00
|
|
|
test.print = print
|
|
|
|
|
|
|
|
print = stub_print
|
|
|
|
io.open = stub_io_open
|
|
|
|
io.output = stub_io_output
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2008-10-31 18:38:05 +00:00
|
|
|
local numpassed = 0
|
|
|
|
local numfailed = 0
|
2011-04-30 21:29:54 +00:00
|
|
|
local start_time = os.clock()
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2012-04-18 19:14:31 +00:00
|
|
|
function runtest(suitename, suitetests, testname, testfunc)
|
|
|
|
if suitetests.setup ~= testfunc and suitetests.teardown ~= testfunc then
|
|
|
|
local ok, err = test_setup(suitetests, testfunc)
|
2009-08-12 01:16:15 +00:00
|
|
|
|
2012-04-18 19:14:31 +00:00
|
|
|
if ok then
|
|
|
|
ok, err = test_run(suitetests, testfunc)
|
|
|
|
end
|
2009-08-12 01:16:15 +00:00
|
|
|
|
2012-04-18 19:14:31 +00:00
|
|
|
local tok, terr = test_teardown(suitetests, testfunc)
|
|
|
|
ok = ok and tok
|
2012-10-31 23:34:04 +00:00
|
|
|
err = err or terr
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2012-04-18 19:14:31 +00:00
|
|
|
if (not ok) then
|
|
|
|
test.print(string.format("%s.%s: %s", suitename, testname, err))
|
|
|
|
numfailed = numfailed + 1
|
|
|
|
else
|
|
|
|
numpassed = numpassed + 1
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
end
|
2012-04-18 19:14:31 +00:00
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2012-04-18 19:14:31 +00:00
|
|
|
function runsuite(suitename, suitetests, testname)
|
|
|
|
if testname then
|
|
|
|
runtest(suitename, suitetests, testname, suitetests[testname])
|
|
|
|
else
|
|
|
|
for testname, testfunc in pairs(suitetests) do
|
|
|
|
runtest(suitename, suitetests, testname, testfunc)
|
2008-10-31 18:38:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2012-04-18 19:14:31 +00:00
|
|
|
if suitename then
|
|
|
|
runsuite(suitename, T[suitename], testname)
|
|
|
|
else
|
|
|
|
for suitename, suitetests in pairs(T) do
|
|
|
|
runsuite(suitename, suitetests, testname)
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
end
|
|
|
|
|
2011-04-30 21:29:54 +00:00
|
|
|
io.write('running time : ', os.clock() - start_time,'\n')
|
2009-09-15 21:08:40 +00:00
|
|
|
print = test.print
|
2013-02-01 19:54:14 +00:00
|
|
|
return numpassed, numfailed
|
2008-10-31 18:38:05 +00:00
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|