1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
--
-- tests/testfx.lua
-- Automated test framework for Premake.
-- Copyright (c) 2008-2009 Jason Perkins and the Premake project
--
--
-- Define a namespace for the testing functions
--
test = { }
--
-- Assertion functions
--
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
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
function test.capture(expected)
local actual = io.endcapture()
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
linenum = linenum + 1
atxt = ait()
etxt = eit()
end
end
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
function test.contains(value, expected)
if not table.contains(value, expected) then
test.fail("expected value %s not found", expected)
end
end
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
function test.isequal(expected, actual)
if (type(expected) == "table") then
for k,v in pairs(expected) do
if not (test.isequal(expected[k], actual[k])) then
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
return true
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
function test.isnotnil(value)
if (value == nil) then
test.fail("expected not nil")
end
end
function test.istrue(value)
if (not value) then
test.fail("expected true but was false")
end
end
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
function test.success(fn, ...)
local ok, err = pcall(fn, unpack(arg))
if not ok then
test.fail("call failed: " .. err)
end
end
--
-- 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
local function stub_print(s)
end
--
-- Define a collection for the test suites
--
T = { }
--
-- Test execution function
--
local function test_setup(suite, fn)
-- clear out some important globals
_ACTION = "test"
_ARGS = { }
_OPTIONS = { }
premake.solution.list = { }
-- reset captured I/O values
test.value_openedfilename = nil
test.value_openedfilemode = nil
test.value_closedfile = false
if suite.setup then
return pcall(suite.setup)
else
return true
end
end
local function test_run(suite, fn)
return pcall(fn)
end
local function test_teardown(suite, fn)
if suite.teardown then
return pcall(suite.teardown)
else
return true
end
end
function test.runall()
test.print = print
print = stub_print
io.open = stub_io_open
io.output = stub_io_output
local numpassed = 0
local numfailed = 0
for suitename, suitetests in pairs(T) do
for testname, testfunc in pairs(suitetests) do
local ok, err = test_setup(suitetests, testfunc)
if ok then
ok, err = test_run(suitetests, testfunc)
end
local tok, terr = test_teardown(suitetests, testfunc)
ok = ok and tok
err = err or tok
if (not ok) then
test.print(string.format("%s.%s: %s", suitename, testname, err))
numfailed = numfailed + 1
else
numpassed = numpassed + 1
end
end
end
print = test.print
return numpassed, numfailed
end
|