Added new array API value type, and started support for new key-value handling

This commit is contained in:
Jason Perkins 2012-04-05 20:02:53 -04:00
parent 22266a995d
commit a68aa47e5f
5 changed files with 164 additions and 27 deletions

View File

@ -20,9 +20,9 @@
-- for usage examples. -- for usage examples.
-- --
function api.register(description) function api.register(field)
-- verify the name -- verify the name
local name = description.name local name = field.name
if not name then if not name then
error("missing name", 2) error("missing name", 2)
end end
@ -32,12 +32,17 @@
end end
-- make sure there is a handler available for this kind of value -- make sure there is a handler available for this kind of value
if not api["set" .. description.kind] then local kind = field.kind
error("invalid kind", 2) if kind:startswith("key-") then
kind = kind:sub(5)
end
if not api["set" .. kind] then
error("invalid kind '" .. kind .. "'", 2)
end end
_G[name] = function(value) _G[name] = function(value)
return api.callback(description, value) return api.callback(field, value)
end end
end end
@ -60,9 +65,51 @@
error("no " .. field.scope .. " in scope", 3) error("no " .. field.scope .. " in scope", 3)
end end
-- find and call the setter for this field's value kind -- A keyed value is a table containing key-value pairs, where the
local setter = api["set" .. field.kind] -- type of the value is defined by the field.
setter(target, field, value) if field.kind:startswith("key-") then
target[field.name] = target[field.name] or {}
api.setkeyvalue(target[field.name], field, value)
-- Otherwise, it is a "simple" value defined by the field
else
local setter = api["set" .. field.kind]
setter(target, field.name, field, value)
end
end
--
-- Update a keyed value. Iterate over the keys in the new value, and use
-- the corresponding values to update the target object.
--
function api.setkeyvalue(target, field, values)
if type(values) ~= "table" then
error("value must be a table of key-value pairs", 4)
end
local kind = field.kind:sub(5)
local setter = api["set" .. kind]
for key, value in pairs(values) do
setter(target, key, field, value)
end
end
--
-- Set a new array value. Arrays are lists of values stored by "value",
-- in that new values overwrite old ones, rather than merging like lists.
--
function api.setarray(target, name, field, value)
-- put simple values in an array
if type(value) ~= "table" then
value = { value }
end
-- store it, overwriting any existing value
target[field.name] = value
end end
@ -70,7 +117,8 @@
-- Set a new string value on an API field. -- Set a new string value on an API field.
-- --
function api.setstring(target, field, value) function api.setstring(target, name, field, value)
error("setstring is not yet implemented")
end end

View File

@ -0,0 +1,44 @@
--
-- tests/api/test_array_kind.lua
-- Tests the array API value type.
-- Copyright (c) 2012 Jason Perkins and the Premake project
--
T.api_array_kind = {}
local suite = T.api_array_kind
local api = premake.api
--
-- Setup and teardown
--
function suite.setup()
api.register { name = "testapi", kind = "array", scope = "project" }
test.createsolution()
end
function suite.teardown()
testapi = nil
end
--
-- Array values should be stored as-is.
--
function suite.storesTable_onArrayValue()
testapi { "one", "two" }
test.isequal({ "one", "two" }, api.scope.project.testapi)
end
--
-- String values should be converted into a table.
--
function suite.storesTable_onStringValue()
testapi "myvalue"
test.isequal({ "myvalue" }, api.scope.project.testapi)
end

View File

@ -14,9 +14,10 @@
-- --
function suite.setup() function suite.setup()
api.settest = function(target, field, value) api.settest = function(target, name, field, value)
test_args = { test_args = {
["target"] = target, ["target"] = target,
["name"] = name,
["field"] = field, ["field"] = field,
["value"] = value ["value"] = value
} }
@ -25,7 +26,7 @@
function suite.teardown() function suite.teardown()
_G["testapi"] = nil testapi = nil
test_args = nil test_args = nil
api.settest = nil api.settest = nil
end end
@ -46,6 +47,18 @@
end end
--
-- Verify that the target field name is getting passed to the setter.
--
function suite.setterGetsFieldName()
api.register { name = "testapi", kind = "test", scope = "project" }
solution "MySolution"
testapi "test"
test.isequal("testapi", test_args.name)
end
-- --
-- Verify that the field description is passed along to the setter. -- Verify that the field description is passed along to the setter.
-- --
@ -137,3 +150,43 @@
testapi "test" testapi "test"
test.istrue(cfg == test_args.target) test.istrue(cfg == test_args.target)
end end
--
-- On key-value APIs, the keyed object value should be the target.
--
function suite.keyObjectTarget_onKeyValue()
api.register { name = "testapi", kind = "key-test", scope = "project" }
local sln = solution "MySolution"
testapi { key = "test" }
test.istrue(sln.testapi == test_args.target)
end
--
-- On key-value APIs, the field name should be the key value from the supplied table.
--
function suite.keyObjectName_onKeyValue()
api.register { name = "testapi", kind = "key-test", scope = "project" }
local sln = solution "MySolution"
testapi { key = "test" }
test.isequal("key", test_args.name)
end
--
-- Raise an error is a simple value is passed to a key-value API.
--
function suite.keyValueRaisesError_onSimpleValue()
api.register { name = "testapi", kind = "key-test", scope = "project" }
local sln = solution "MySolution"
ok, err = pcall(function ()
testapi "test"
end)
test.isfalse(ok)
end

View File

@ -13,18 +13,8 @@
-- Setup and teardown -- Setup and teardown
-- --
local callback_args
function suite.setup()
suite.callback = api.callback
api.callback = function(...) callback_args = arg end
end
function suite.teardown() function suite.teardown()
_G["testapi"] = nil testapi = nil
api.callback = suite.callback
callback_args = nil
end end
@ -87,11 +77,12 @@
-- --
-- Verify that the central API callback is invoked by the registered function. -- Verify that key-value forms are accepted.
-- --
function suite.callbackInvoked_onApiCall() function suite.succeeds_onKeyValueForm()
api.register { name = "testapi", kind = "testkind", scope = "project" } ok, err = pcall(function ()
testapi "testvalue" api.register { name = "testapi", kind = "key-string", scope = "project" }
test.isnotnil(callback_args) end)
test.istrue(ok)
end end

View File

@ -76,6 +76,7 @@
dofile("config/test_targetinfo.lua") dofile("config/test_targetinfo.lua")
-- API tests -- API tests
dofile("api/test_array_kind.lua")
dofile("api/test_callback.lua") dofile("api/test_callback.lua")
dofile("api/test_register.lua") dofile("api/test_register.lua")