2008-10-31 18:38:05 +00:00
|
|
|
--
|
|
|
|
-- tests/testfx.lua
|
|
|
|
-- Automated test framework for Premake.
|
2015-07-28 23:08:28 +00:00
|
|
|
-- Copyright (c) 2008-2015 Jason Perkins and the Premake project
|
2008-10-31 18:38:05 +00:00
|
|
|
--
|
|
|
|
|
2015-07-28 23:08:28 +00:00
|
|
|
local p = premake
|
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
local m = p.modules.self_test
|
2016-05-16 20:27:14 +00:00
|
|
|
|
2016-05-21 00:13:04 +00:00
|
|
|
local _ = {}
|
|
|
|
|
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
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.capture(expected)
|
2014-02-08 15:44:57 +00:00
|
|
|
local actual = premake.captured() .. premake.eol()
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2011-12-04 19:47:59 +00:00
|
|
|
-- create line-by-line iterators for both values
|
2015-09-02 21:01:10 +00:00
|
|
|
local ait = actual:gmatch("(.-)" .. premake.eol())
|
|
|
|
local eit = expected:gmatch("(.-)\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
|
2016-05-20 23:14:08 +00:00
|
|
|
m.fail("(%d) expected:\n%s\n...but was:\n%s", linenum, etxt, atxt)
|
2009-03-25 23:04:32 +00:00
|
|
|
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
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.closedfile(expected)
|
|
|
|
if expected and not m.value_closedfile then
|
|
|
|
m.fail("expected file to be closed")
|
|
|
|
elseif not expected and m.value_closedfile then
|
|
|
|
m.fail("expected file to remain open")
|
2009-08-12 01:16:15 +00:00
|
|
|
end
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2009-07-14 14:28:26 +00:00
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.contains(expected, actual)
|
2014-07-23 17:10:41 +00:00
|
|
|
if type(expected) == "table" then
|
|
|
|
for i, v in ipairs(expected) do
|
2016-05-20 23:14:08 +00:00
|
|
|
m.contains(v, actual)
|
2014-07-23 17:10:41 +00:00
|
|
|
end
|
|
|
|
elseif not table.contains(actual, expected) then
|
2016-05-20 23:14:08 +00:00
|
|
|
m.fail("expected value %s not found", expected)
|
2009-07-14 14:28:26 +00:00
|
|
|
end
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.excludes(expected, actual)
|
2014-09-17 23:19:47 +00:00
|
|
|
if type(expected) == "table" then
|
|
|
|
for i, v in ipairs(expected) do
|
2016-05-20 23:14:08 +00:00
|
|
|
m.excludes(v, actual)
|
2014-09-17 23:19:47 +00:00
|
|
|
end
|
|
|
|
elseif table.contains(actual, expected) then
|
2016-05-20 23:14:08 +00:00
|
|
|
m.fail("excluded value %s found", expected)
|
2014-09-17 23:19:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.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
|
2015-03-24 23:15:39 +00:00
|
|
|
local arg = {...}
|
2012-11-04 14:21:40 +00:00
|
|
|
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
|
2015-03-24 23:15:39 +00:00
|
|
|
for i = 1, #arg 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
|
|
|
|
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.filecontains(expected, fn)
|
2008-10-31 18:38:05 +00:00
|
|
|
local f = io.open(fn)
|
|
|
|
local actual = f:read("*a")
|
|
|
|
f:close()
|
|
|
|
if (expected ~= actual) then
|
2016-05-20 23:14:08 +00:00
|
|
|
m.fail("expected %s but was %s", expected, actual)
|
2008-10-31 18:38:05 +00:00
|
|
|
end
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2011-07-01 19:05:16 +00:00
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.hasoutput()
|
2015-03-11 18:40:12 +00:00
|
|
|
local actual = premake.captured()
|
|
|
|
if actual == "" then
|
2016-05-20 23:14:08 +00:00
|
|
|
m.fail("expected output, received none");
|
2015-03-11 18:40:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.isemptycapture()
|
2014-02-08 15:44:57 +00:00
|
|
|
local actual = premake.captured()
|
2011-07-01 19:05:16 +00:00
|
|
|
if actual ~= "" then
|
2016-05-20 23:14:08 +00:00
|
|
|
m.fail("expected empty capture, but was %s", actual);
|
2011-07-01 19:05:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.isequal(expected, actual, depth)
|
2012-11-04 14:21:40 +00:00
|
|
|
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
|
2016-05-20 23:14:08 +00:00
|
|
|
m.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
|
2016-05-20 23:14:08 +00:00
|
|
|
m.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
|
2016-05-20 23:14:08 +00:00
|
|
|
m.isequal(expected[k], actual[k], depth + 1)
|
2008-10-31 18:38:05 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
if (expected ~= actual) then
|
2016-05-20 23:14:08 +00:00
|
|
|
m.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
|
|
|
|
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.isfalse(value)
|
2008-10-31 18:38:05 +00:00
|
|
|
if (value) then
|
2016-05-20 23:14:08 +00:00
|
|
|
m.fail("expected false but was true")
|
2008-10-31 18:38:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.isnil(value)
|
2008-10-31 18:38:05 +00:00
|
|
|
if (value ~= nil) then
|
2016-05-20 23:14:08 +00:00
|
|
|
m.fail("expected nil but was " .. tostring(value))
|
2008-10-31 18:38:05 +00:00
|
|
|
end
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.isnotnil(value)
|
2009-08-18 19:09:17 +00:00
|
|
|
if (value == nil) then
|
2016-05-20 23:14:08 +00:00
|
|
|
m.fail("expected not nil")
|
2009-08-18 19:09:17 +00:00
|
|
|
end
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.issame(expected, action)
|
2013-11-20 18:26:52 +00:00
|
|
|
if expected ~= action then
|
2016-05-20 23:14:08 +00:00
|
|
|
m.fail("expected same value")
|
2013-11-20 18:26:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.istrue(value)
|
2008-10-31 18:38:05 +00:00
|
|
|
if (not value) then
|
2016-05-20 23:14:08 +00:00
|
|
|
m.fail("expected true but was false")
|
2008-10-31 18:38:05 +00:00
|
|
|
end
|
2009-07-16 14:29:08 +00:00
|
|
|
end
|
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.missing(value, actual)
|
2013-11-22 19:06:41 +00:00
|
|
|
if table.contains(actual, value) then
|
2016-05-20 23:14:08 +00:00
|
|
|
m.fail("unexpected value %s found", value)
|
2013-11-22 19:06:41 +00:00
|
|
|
end
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.openedfile(fname)
|
|
|
|
if fname ~= m.value_openedfilename then
|
2009-08-12 01:16:15 +00:00
|
|
|
local msg = "expected to open file '" .. fname .. "'"
|
2016-05-20 23:14:08 +00:00
|
|
|
if m.value_openedfilename then
|
|
|
|
msg = msg .. ", got '" .. m.value_openedfilename .. "'"
|
2009-08-12 01:16:15 +00:00
|
|
|
end
|
2016-05-20 23:14:08 +00:00
|
|
|
m.fail(msg)
|
2009-08-12 01:16:15 +00:00
|
|
|
end
|
|
|
|
end
|
2013-02-01 19:54:14 +00:00
|
|
|
|
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.success(fn, ...)
|
2015-03-24 23:15:39 +00:00
|
|
|
local ok, err = pcall(fn, ...)
|
2009-07-16 14:29:08 +00:00
|
|
|
if not ok then
|
2016-05-20 23:14:08 +00:00
|
|
|
m.fail("call failed: " .. err)
|
2009-07-16 14:29:08 +00:00
|
|
|
end
|
2008-10-31 18:38:05 +00:00
|
|
|
end
|
|
|
|
|
2009-08-12 01:16:15 +00:00
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.stderr(expected)
|
2013-02-01 19:54:14 +00:00
|
|
|
if not expected and stderr_capture then
|
2016-05-20 23:14:08 +00:00
|
|
|
m.fail("Unexpected: " .. stderr_capture)
|
2013-02-01 19:54:14 +00:00
|
|
|
elseif expected then
|
|
|
|
if not stderr_capture or not stderr_capture:find(expected) then
|
2016-05-20 23:14:08 +00:00
|
|
|
m.fail(string.format("expected '%s'; got %s", expected, stderr_capture or "(nil)"))
|
2013-02-01 19:54:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.notstderr(expected)
|
2013-02-01 19:54:14 +00:00
|
|
|
if not expected and not stderr_capture then
|
2016-05-20 23:14:08 +00:00
|
|
|
m.fail("Expected output on stderr; none received")
|
2013-02-01 19:54:14 +00:00
|
|
|
elseif expected then
|
|
|
|
if stderr_capture and stderr_capture:find(expected) then
|
2016-05-20 23:14:08 +00:00
|
|
|
m.fail(string.format("stderr contains '%s'; was %s", expected, stderr_capture))
|
2013-02-01 19:54:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-12 01:16:15 +00:00
|
|
|
|
2014-07-07 21:03:14 +00:00
|
|
|
--
|
|
|
|
-- Some helper functions
|
|
|
|
--
|
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.createWorkspace()
|
2015-09-03 22:03:09 +00:00
|
|
|
local wks = workspace("MyWorkspace")
|
2014-07-07 21:03:14 +00:00
|
|
|
configurations { "Debug", "Release" }
|
2016-05-20 23:14:08 +00:00
|
|
|
local prj = m.createproject(wks)
|
2015-07-30 19:30:41 +00:00
|
|
|
return wks, prj
|
2014-07-07 21:03:14 +00:00
|
|
|
end
|
|
|
|
|
2015-09-04 19:40:12 +00:00
|
|
|
-- Eventually we'll want to deprecate this one and move everyone
|
|
|
|
-- over to createWorkspace() instead (4 Sep 2015).
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.createsolution()
|
2015-09-04 19:40:12 +00:00
|
|
|
local wks = workspace("MySolution")
|
|
|
|
configurations { "Debug", "Release" }
|
2016-05-20 23:14:08 +00:00
|
|
|
local prj = m.createproject(wks)
|
2015-09-04 19:40:12 +00:00
|
|
|
return wks, prj
|
|
|
|
end
|
2015-07-28 23:08:28 +00:00
|
|
|
|
2014-07-07 21:03:14 +00:00
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.createproject(wks)
|
2015-08-28 20:16:14 +00:00
|
|
|
local n = #wks.projects + 1
|
2014-07-07 21:03:14 +00:00
|
|
|
if n == 1 then n = "" end
|
|
|
|
|
|
|
|
local prj = project ("MyProject" .. n)
|
|
|
|
language "C++"
|
|
|
|
kind "ConsoleApp"
|
|
|
|
return prj
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.getWorkspace(wks)
|
2015-08-10 17:05:26 +00:00
|
|
|
p.oven.bake()
|
2015-08-28 20:16:14 +00:00
|
|
|
return p.global.getWorkspace(wks.name)
|
2015-03-13 20:48:06 +00:00
|
|
|
end
|
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
p.alias(m, "getWorkspace", "getsolution")
|
2015-03-13 20:48:06 +00:00
|
|
|
|
2015-08-28 20:16:14 +00:00
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.getproject(wks, i)
|
|
|
|
wks = m.getWorkspace(wks)
|
2015-08-28 20:16:14 +00:00
|
|
|
return p.workspace.getproject(wks, i or 1)
|
2014-07-07 21:03:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.getconfig(prj, buildcfg, platform)
|
|
|
|
local wks = m.getWorkspace(prj.workspace)
|
2015-08-28 20:16:14 +00:00
|
|
|
prj = p.workspace.getproject(wks, prj.name)
|
|
|
|
return p.project.getconfig(prj, buildcfg, platform)
|
2014-07-07 21:03:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2008-10-31 18:38:05 +00:00
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
m.print = print
|