2016-05-26 23:57:27 +00:00
|
|
|
---
|
|
|
|
-- test_assertions.lua
|
2008-10-31 18:38:05 +00:00
|
|
|
--
|
2016-05-26 23:57:27 +00:00
|
|
|
-- Assertion functions for unit tests.
|
2008-10-31 18:38:05 +00:00
|
|
|
--
|
2016-05-26 23:57:27 +00:00
|
|
|
-- Author Jason Perkins
|
|
|
|
-- Copyright (c) 2008-2016 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
|
|
|
|
|
|
|
|
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-06-22 15:36:14 +00:00
|
|
|
m.fail("(%d) expected:\n%s\n...but was:\n%s\nfulltext:\n%s", linenum, etxt, atxt, actual)
|
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-26 23:57:27 +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-26 23:57:27 +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-26 23:57:27 +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
|
|
|
|
|
|
|
|
|
2013-02-01 19:54:14 +00:00
|
|
|
|
2016-05-26 23:57:27 +00:00
|
|
|
function m.fail(format, ...)
|
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-26 23:57:27 +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-26 23:57:27 +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-26 23:57:27 +00:00
|
|
|
|
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-26 23:57:27 +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-26 23:57:27 +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-26 23:57:27 +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-26 23:57:27 +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-26 23:57:27 +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-26 23:57:27 +00:00
|
|
|
|
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-26 23:57:27 +00:00
|
|
|
|
|
|
|
|
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-26 23:57:27 +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-26 23:57:27 +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-26 23:57:27 +00:00
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.stderr(expected)
|
2016-05-26 23:57:27 +00:00
|
|
|
if not expected and m.stderr_capture then
|
|
|
|
m.fail("Unexpected: " .. m.stderr_capture)
|
2013-02-01 19:54:14 +00:00
|
|
|
elseif expected then
|
2016-05-26 23:57:27 +00:00
|
|
|
if not m.stderr_capture or not m.stderr_capture:find(expected) then
|
|
|
|
m.fail(string.format("expected '%s'; got %s", expected, m.stderr_capture or "(nil)"))
|
2013-02-01 19:54:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2016-05-26 23:57:27 +00:00
|
|
|
|
2016-05-20 23:14:08 +00:00
|
|
|
function m.notstderr(expected)
|
2016-05-26 23:57:27 +00:00
|
|
|
if not expected and not m.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
|
2016-05-26 23:57:27 +00:00
|
|
|
if m.stderr_capture and m.stderr_capture:find(expected) then
|
|
|
|
m.fail(string.format("stderr contains '%s'; was %s", expected, m.stderr_capture))
|
2013-02-01 19:54:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|