2008-10-31 18:38:05 +00:00
|
|
|
--
|
|
|
|
-- tests/testfx.lua
|
|
|
|
-- Automated test framework for Premake.
|
2009-08-12 01:16:15 +00:00
|
|
|
-- Copyright (c) 2008-2009 Jason Perkins and the Premake project
|
2008-10-31 18:38:05 +00:00
|
|
|
--
|
|
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Define a namespace for the testing functions
|
|
|
|
--
|
|
|
|
|
|
|
|
test = { }
|
2009-09-15 21:08:40 +00:00
|
|
|
|
2008-10-31 18:38:05 +00:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Assertion functions
|
|
|
|
--
|
2010-06-10 15:42:45 +00:00
|
|
|
function test.string_contains(buffer, expected)
|
|
|
|
if not string.find(buffer,expected) then
|
|
|
|
test.fail("\n==Fail==: Expected to find :\n%s\nyet it was not found in buffer:\n%s\n", expected,buffer)
|
|
|
|
end
|
|
|
|
end
|
2008-10-31 18:38:05 +00:00
|
|
|
|
2010-06-11 21:07:07 +00:00
|
|
|
function test.string_does_not_contain(buffer, expected)
|
|
|
|
if string.find(buffer,expected) then
|
|
|
|
test.fail("\n==Fail==: Did not expected to find :\n%s\nyet it was found in buffer:\n%s\n", expected,buffer)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-25 23:04:32 +00:00
|
|
|
function test.capture(expected)
|
|
|
|
local actual = io.endcapture()
|
2009-11-25 20:27:57 +00:00
|
|
|
|
2009-03-25 23:04:32 +00:00
|
|
|
local ait = actual:gfind("(.-)" .. io.eol)
|
|
|
|
local eit = expected:gfind("(.-)\n")
|
|
|
|
|
|
|
|
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
|
2009-11-25 20:27:57 +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
|
|
|
|
|
|
|
|
|
|
|
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
|
2009-03-25 23:04:32 +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
|
2009-03-25 23:04:32 +00:00
|
|
|
|
2009-07-14 14:28:26 +00:00
|
|
|
|
2008-10-31 18:38:05 +00:00
|
|
|
function test.fail(format, ...)
|
|
|
|
-- convert nils into something more usefuls
|
|
|
|
for i = 1, arg.n do
|
|
|
|
if (arg[i] == nil) then
|
|
|
|
arg[i] = "(nil)"
|
|
|
|
elseif (type(arg[i]) == "table") then
|
|
|
|
arg[i] = "{" .. table.concat(arg[i], ", ") .. "}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
error(string.format(format, unpack(arg)), 3)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
2008-10-31 18:38:05 +00:00
|
|
|
|
|
|
|
function test.isequal(expected, actual)
|
|
|
|
if (type(expected) == "table") then
|
|
|
|
for k,v in pairs(expected) do
|
2010-02-17 22:11:25 +00:00
|
|
|
if not (test.isequal(expected[k], actual[k])) then
|
2008-10-31 18:38:05 +00:00
|
|
|
test.fail("expected %s but was %s", expected, actual)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if (expected ~= actual) then
|
|
|
|
test.fail("expected %s but was %s", expected, actual)
|
|
|
|
end
|
|
|
|
end
|
2010-02-17 22:11:25 +00:00
|
|
|
return true
|
2008-10-31 18:38:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function test.isfalse(value)
|
|
|
|
if (value) then
|
|
|
|
test.fail("expected false but was true")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function test.isnil(value)
|
|
|
|
if (value ~= nil) then
|
|
|
|
test.fail("expected nil but was " .. tostring(value))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2009-08-18 19:09:17 +00:00
|
|
|
function test.isnotnil(value)
|
|
|
|
if (value == nil) then
|
|
|
|
test.fail("expected not nil")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
-- 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
|
|
|
|
|
|
|
|
local function stub_io_output(f)
|
|
|
|
end
|
|
|
|
|
2009-08-12 15:22:44 +00:00
|
|
|
local function stub_print(s)
|
|
|
|
end
|
2009-08-12 01:16:15 +00:00
|
|
|
|
|
|
|
|
2008-10-31 18:38:05 +00:00
|
|
|
--
|
|
|
|
-- Define a collection for the test suites
|
|
|
|
--
|
|
|
|
|
|
|
|
T = { }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Test execution function
|
|
|
|
--
|
2011-08-21 23:52:49 +00:00
|
|
|
local _OS_host = _OS
|
2009-08-12 01:16:15 +00:00
|
|
|
local function test_setup(suite, fn)
|
|
|
|
-- clear out some important globals
|
|
|
|
_ACTION = "test"
|
|
|
|
_ARGS = { }
|
|
|
|
_OPTIONS = { }
|
2011-08-21 23:52:49 +00:00
|
|
|
_OS = _OS_host
|
2009-11-04 18:44:13 +00:00
|
|
|
premake.solution.list = { }
|
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
|
|
|
|
return pcall(suite.setup)
|
|
|
|
else
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function test_run(suite, fn)
|
2011-02-03 21:38:32 +00:00
|
|
|
io.capture()
|
2009-08-12 01:16:15 +00:00
|
|
|
return pcall(fn)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function test_teardown(suite, fn)
|
|
|
|
if suite.teardown then
|
|
|
|
return pcall(suite.teardown)
|
|
|
|
else
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2008-10-31 18:38:05 +00:00
|
|
|
function test.runall()
|
2009-09-15 21:08:40 +00:00
|
|
|
test.print = print
|
|
|
|
|
|
|
|
print = stub_print
|
|
|
|
io.open = stub_io_open
|
|
|
|
io.output = stub_io_output
|
2009-08-12 01:16:15 +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()
|
2009-08-12 01:16:15 +00:00
|
|
|
for suitename, suitetests in pairs(T) do
|
2008-10-31 18:38:05 +00:00
|
|
|
for testname, testfunc in pairs(suitetests) do
|
2011-04-30 21:29:54 +00:00
|
|
|
|
|
|
|
if suitetests.setup ~= testfunc and suitetests.teardown ~= testfunc then
|
|
|
|
local ok, err = test_setup(suitetests, testfunc)
|
2009-08-12 01:16:15 +00:00
|
|
|
|
2011-04-30 21:29:54 +00:00
|
|
|
if ok then
|
|
|
|
ok, err = test_run(suitetests, testfunc)
|
|
|
|
end
|
2009-08-12 01:16:15 +00:00
|
|
|
|
2011-04-30 21:29:54 +00:00
|
|
|
local tok, terr = test_teardown(suitetests, testfunc)
|
|
|
|
ok = ok and tok
|
|
|
|
err = err or tok
|
2008-10-31 18:38:05 +00:00
|
|
|
|
2011-04-30 21:29:54 +00:00
|
|
|
if (not ok) then
|
|
|
|
test.print(string.format("%s.%s: %s", suitename, testname, err))
|
|
|
|
numfailed = numfailed + 1
|
|
|
|
else
|
|
|
|
numpassed = numpassed + 1
|
|
|
|
end
|
2008-10-31 18:38:05 +00:00
|
|
|
end
|
2011-04-30 21:29:54 +00:00
|
|
|
|
2008-10-31 18:38:05 +00:00
|
|
|
end
|
|
|
|
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
|
2008-10-31 18:38:05 +00:00
|
|
|
return numpassed, numfailed
|
|
|
|
end
|
|
|
|
|