Add a bit of organization and documentation to the code, make it easier to follow

This commit is contained in:
Jason Perkins 2016-05-26 19:57:27 -04:00
parent 3283b1d141
commit 1339933a01
6 changed files with 137 additions and 97 deletions

View File

@ -1,6 +1,7 @@
return { return {
"self-test.lua", "self-test.lua",
"test_assertions.lua",
"test_declare.lua", "test_declare.lua",
"test_runner.lua", "test_helpers.lua",
"testfx.lua", "test_runner.lua"
} }

View File

@ -17,10 +17,6 @@
dofile("testfx.lua")
newaction { newaction {
trigger = "self-test", trigger = "self-test",
shortname = "Test Premake", shortname = "Test Premake",
@ -88,7 +84,9 @@
dofile("test_assertions.lua")
dofile("test_declare.lua") dofile("test_declare.lua")
dofile("test_helpers.lua")
dofile("test_runner.lua") dofile("test_runner.lua")

View File

@ -1,8 +1,11 @@
---
-- test_assertions.lua
-- --
-- tests/testfx.lua -- Assertion functions for unit tests.
-- Automated test framework for Premake.
-- Copyright (c) 2008-2015 Jason Perkins and the Premake project
-- --
-- Author Jason Perkins
-- Copyright (c) 2008-2016 Jason Perkins and the Premake project.
---
local p = premake local p = premake
@ -12,27 +15,6 @@
--
-- 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
--
-- Assertion functions
--
function m.capture(expected) function m.capture(expected)
local actual = premake.captured() .. premake.eol() local actual = premake.captured() .. premake.eol()
@ -56,6 +38,7 @@
end end
function m.closedfile(expected) function m.closedfile(expected)
if expected and not m.value_closedfile then if expected and not m.value_closedfile then
m.fail("expected file to be closed") m.fail("expected file to be closed")
@ -65,6 +48,7 @@
end end
function m.contains(expected, actual) function m.contains(expected, actual)
if type(expected) == "table" then if type(expected) == "table" then
for i, v in ipairs(expected) do for i, v in ipairs(expected) do
@ -76,6 +60,7 @@
end end
function m.excludes(expected, actual) function m.excludes(expected, actual)
if type(expected) == "table" then if type(expected) == "table" then
for i, v in ipairs(expected) do for i, v in ipairs(expected) do
@ -87,8 +72,8 @@
end end
function m.fail(format, ...)
function m.fail(format, ...)
-- if format is a number then it is the stack depth -- if format is a number then it is the stack depth
local depth = 3 local depth = 3
local arg = {...} local arg = {...}
@ -111,6 +96,7 @@
end end
function m.filecontains(expected, fn) function m.filecontains(expected, fn)
local f = io.open(fn) local f = io.open(fn)
local actual = f:read("*a") local actual = f:read("*a")
@ -121,6 +107,7 @@
end end
function m.hasoutput() function m.hasoutput()
local actual = premake.captured() local actual = premake.captured()
if actual == "" then if actual == "" then
@ -129,6 +116,7 @@
end end
function m.isemptycapture() function m.isemptycapture()
local actual = premake.captured() local actual = premake.captured()
if actual ~= "" then if actual ~= "" then
@ -137,6 +125,7 @@
end end
function m.isequal(expected, actual, depth) function m.isequal(expected, actual, depth)
depth = depth or 0 depth = depth or 0
if type(expected) == "table" then if type(expected) == "table" then
@ -158,6 +147,7 @@
end end
function m.isfalse(value) function m.isfalse(value)
if (value) then if (value) then
m.fail("expected false but was true") m.fail("expected false but was true")
@ -165,6 +155,7 @@
end end
function m.isnil(value) function m.isnil(value)
if (value ~= nil) then if (value ~= nil) then
m.fail("expected nil but was " .. tostring(value)) m.fail("expected nil but was " .. tostring(value))
@ -172,6 +163,7 @@
end end
function m.isnotnil(value) function m.isnotnil(value)
if (value == nil) then if (value == nil) then
m.fail("expected not nil") m.fail("expected not nil")
@ -179,6 +171,7 @@
end end
function m.issame(expected, action) function m.issame(expected, action)
if expected ~= action then if expected ~= action then
m.fail("expected same value") m.fail("expected same value")
@ -186,18 +179,23 @@
end end
function m.istrue(value) function m.istrue(value)
if (not value) then if (not value) then
m.fail("expected true but was false") m.fail("expected true but was false")
end end
end end
function m.missing(value, actual) function m.missing(value, actual)
if table.contains(actual, value) then if table.contains(actual, value) then
m.fail("unexpected value %s found", value) m.fail("unexpected value %s found", value)
end end
end end
function m.openedfile(fname) function m.openedfile(fname)
if fname ~= m.value_openedfilename then if fname ~= m.value_openedfilename then
local msg = "expected to open file '" .. fname .. "'" local msg = "expected to open file '" .. fname .. "'"
@ -209,6 +207,7 @@
end end
function m.success(fn, ...) function m.success(fn, ...)
local ok, err = pcall(fn, ...) local ok, err = pcall(fn, ...)
if not ok then if not ok then
@ -217,80 +216,25 @@
end end
function m.stderr(expected) function m.stderr(expected)
if not expected and stderr_capture then if not expected and m.stderr_capture then
m.fail("Unexpected: " .. stderr_capture) m.fail("Unexpected: " .. m.stderr_capture)
elseif expected then elseif expected then
if not stderr_capture or not stderr_capture:find(expected) then if not m.stderr_capture or not m.stderr_capture:find(expected) then
m.fail(string.format("expected '%s'; got %s", expected, stderr_capture or "(nil)")) m.fail(string.format("expected '%s'; got %s", expected, m.stderr_capture or "(nil)"))
end end
end end
end end
function m.notstderr(expected) function m.notstderr(expected)
if not expected and not stderr_capture then if not expected and not m.stderr_capture then
m.fail("Expected output on stderr; none received") m.fail("Expected output on stderr; none received")
elseif expected then elseif expected then
if stderr_capture and stderr_capture:find(expected) then if m.stderr_capture and m.stderr_capture:find(expected) then
m.fail(string.format("stderr contains '%s'; was %s", expected, stderr_capture)) m.fail(string.format("stderr contains '%s'; was %s", expected, m.stderr_capture))
end end
end end
end end
--
-- Some helper functions
--
function m.createWorkspace()
local wks = workspace("MyWorkspace")
configurations { "Debug", "Release" }
local prj = m.createproject(wks)
return wks, prj
end
-- Eventually we'll want to deprecate this one and move everyone
-- over to createWorkspace() instead (4 Sep 2015).
function m.createsolution()
local wks = workspace("MySolution")
configurations { "Debug", "Release" }
local prj = m.createproject(wks)
return wks, prj
end
function m.createproject(wks)
local n = #wks.projects + 1
if n == 1 then n = "" end
local prj = project ("MyProject" .. n)
language "C++"
kind "ConsoleApp"
return prj
end
function m.getWorkspace(wks)
p.oven.bake()
return p.global.getWorkspace(wks.name)
end
p.alias(m, "getWorkspace", "getsolution")
function m.getproject(wks, i)
wks = m.getWorkspace(wks)
return p.workspace.getproject(wks, i or 1)
end
function m.getconfig(prj, buildcfg, platform)
local wks = m.getWorkspace(prj.workspace)
prj = p.workspace.getproject(wks, prj.name)
return p.project.getconfig(prj, buildcfg, platform)
end
m.print = print

View File

@ -16,8 +16,6 @@
_.suites = {} _.suites = {}
_.suppressed = {} _.suppressed = {}
T = _.suites
--- ---

View File

@ -0,0 +1,79 @@
---
-- test_helpers.lua
--
-- Helper functions for setting up workspaces and projects, etc.
--
-- Author Jason Perkins
-- Copyright (c) 2008-2016 Jason Perkins and the Premake project.
---
local p = premake
local m = p.modules.self_test
function m.createWorkspace()
local wks = workspace("MyWorkspace")
configurations { "Debug", "Release" }
local prj = m.createProject(wks)
return wks, prj
end
-- Eventually we'll want to deprecate this one and move everyone
-- over to createWorkspace() instead (4 Sep 2015).
function m.createsolution()
local wks = workspace("MySolution")
configurations { "Debug", "Release" }
local prj = m.createproject(wks)
return wks, prj
end
function m.createProject(wks)
local n = #wks.projects + 1
if n == 1 then n = "" end
local prj = project ("MyProject" .. n)
language "C++"
kind "ConsoleApp"
return prj
end
function m.getWorkspace(wks)
p.oven.bake()
return p.global.getWorkspace(wks.name)
end
function m.getProject(wks, i)
wks = m.getWorkspace(wks)
return p.workspace.getproject(wks, i or 1)
end
function m.getConfig(prj, buildcfg, platform)
local wks = m.getWorkspace(prj.workspace)
prj = p.workspace.getproject(wks, prj.name)
return p.project.getconfig(prj, buildcfg, platform)
end
m.print = print
p.alias(m, "createProject", "createproject")
p.alias(m, "getConfig", "getconfig")
p.alias(m, "getProject", "getproject")
p.alias(m, "getWorkspace", "getsolution")

View File

@ -117,6 +117,10 @@
hooks.p_utf8 = p.utf8 hooks.p_utf8 = p.utf8
hooks.print = print hooks.print = print
local mt = getmetatable(io.stderr)
_.builtin_write = mt.write
mt.write = _.stub_stderr_write
_OPTIONS = {} _OPTIONS = {}
setmetatable(_OPTIONS, getmetatable(hooks.options)) setmetatable(_OPTIONS, getmetatable(hooks.options))
@ -134,6 +138,7 @@
p.indent("\t") p.indent("\t")
p.api.reset() p.api.reset()
m.stderr_capture = nil
m.value_openedfilename = nil m.value_openedfilename = nil
m.value_openedfilemode = nil m.value_openedfilemode = nil
m.value_closedfile = false m.value_closedfile = false
@ -143,6 +148,7 @@
function _.removeTestingHooks(hooks) function _.removeTestingHooks(hooks)
_ACTION = hooks.action _ACTION = hooks.action
_OPTIONS = hooks.options _OPTIONS = hooks.options
@ -153,6 +159,9 @@
os.writefile_ifnotequal = hooks.os_writefile_ifnotequal os.writefile_ifnotequal = hooks.os_writefile_ifnotequal
p.utf8 = hooks.p_utf8 p.utf8 = hooks.p_utf8
print = hooks.print print = hooks.print
local mt = getmetatable(io.stderr)
mt.write = _.builtin_write
end end
@ -242,5 +251,16 @@
function _.stub_stderr_write(...)
if select(1, ...) == io.stderr then
m.stderr_capture = (m.stderr_capture or "") .. select(2, ...)
else
return _.builtin_write(...)
end
end
function _.stub_utf8() function _.stub_utf8()
end end