premake/tests/base/test_detoken.lua

122 lines
2.8 KiB
Lua

--
-- tests/base/test_detoken.lua
-- Test suite for the token expansion API.
-- Copyright (c) 2011-2014 Jason Perkins and the Premake project
--
local suite = test.declare("detoken")
local detoken = premake.detoken
--
-- Setup
--
local x, action
local environ = {}
function suite.setup()
action = premake.action.get("test")
end
function suite.teardown()
action.pathVars = nil
end
--
-- The contents of the token should be executed and the results returned.
--
function suite.executesTokenContents()
x = detoken.expand("MyProject%{1+1}", environ)
test.isequal("MyProject2", x)
end
--
-- If the value contains more than one token, then should all be expanded.
--
function suite.expandsMultipleTokens()
x = detoken.expand("MyProject%{'X'}and%{'Y'}and%{'Z'}", environ)
test.isequal("MyProjectXandYandZ", x)
end
--
-- If the token replacement values contain tokens themselves, those
-- should also get expanded.
--
function suite.expandsNestedTokens()
environ.sln = { name="MySolution%{'X'}" }
x = detoken.expand("%{sln.name}", environ)
test.isequal("MySolutionX", x)
end
--
-- Verify that the global namespace is still accessible.
--
function suite.canUseGlobalFunctions()
x = detoken.expand("%{iif(true, 'a', 'b')}", environ)
test.isequal("a", x)
end
--
-- If a path field contains a token, and if that token expands to an
-- absolute path itself, that should be returned as the new value.
--
function suite.canExpandToAbsPath()
environ.cfg = { basedir = os.getcwd() }
x = detoken.expand("bin/debug/%{cfg.basedir}", environ, {paths=true})
test.isequal(os.getcwd(), x)
end
--
-- If a non-path field contains a token that expands to a path, that
-- path should be converted to a relative value.
--
function suite.canExpandToRelPath()
local cwd = os.getcwd()
environ.cfg = { basedir = path.getdirectory(cwd) }
x = detoken.expand("cd %{cfg.basedir}", environ, {}, cwd)
test.isequal("cd ..", x)
end
--
-- If the value being expanded is a table, iterate over all of its values.
--
function suite.expandsAllItemsInList()
x = detoken.expand({ "A%{1}", "B%{2}", "C%{3}" }, environ)
test.isequal({ "A1", "B2", "C3" }, x)
end
--
-- If the field being expanded supports path variable mapping, and the
-- action provides a map, replace tokens with the mapped values.
--
function suite.replacesToken_onSupportedAndMapped()
action.pathVars = { ["cfg.objdir"] = "$(IntDir)" }
x = detoken.expand("cmd %{cfg.objdir}/file", environ, {pathVars=true})
test.isequal("cmd $(IntDir)/file", x)
end
function suite.replacesToken_onSupportedAndMapped_inAbsPath()
action.pathVars = { ["cfg.objdir"] = "$(IntDir)" }
x = detoken.expand(os.getcwd() .. "/%{cfg.objdir}/file", environ, {paths=true,pathVars=true})
test.isequal("$(IntDir)/file", x)
end