From 51ab0947e809f8dbbc423c7d6b2c563e6a020b33 Mon Sep 17 00:00:00 2001 From: Jason Perkins Date: Tue, 25 Feb 2014 13:17:04 -0500 Subject: [PATCH] Make _OPTIONS table key lookups case insensitive --- src/base/option.lua | 45 ++++++++++++++++++++++++++++++++------ tests/base/test_option.lua | 32 +++++++++++++++++++++++++++ tests/premake5.lua | 1 + tests/testfx.lua | 6 +++-- 4 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 tests/base/test_option.lua diff --git a/src/base/option.lua b/src/base/option.lua index 8dd9d050..9d38cf1d 100644 --- a/src/base/option.lua +++ b/src/base/option.lua @@ -1,17 +1,45 @@ -- -- option.lua -- Work with the list of registered options. --- Copyright (c) 2002-2013 Jason Perkins and the Premake project +-- Copyright (c) 2002-2014 Jason Perkins and the Premake project -- premake.option = {} + local m = premake.option + -- --- The list of registered options. +-- We can't control how people will type in the command line arguments, or how +-- project scripts will define their custom options, so case becomes an issue. +-- To mimimize issues, set up the _OPTIONS table to always use lowercase keys. -- - premake.option.list = {} + local _OPTIONS_metatable = { + __index = function(tbl, key) + if type(key) == "string" then + key = key:lower() + end + return rawget(tbl, key) + end, + __newindex = function(tbl, key, value) + if type(key) == "string" then + key = key:lower() + end + rawset(tbl, key, value) + end + } + + setmetatable(_OPTIONS, _OPTIONS_metatable) + + + +-- +-- The list of registered options. Calls to newoption() will add +-- new entries here. +-- + + m.list = {} -- @@ -21,7 +49,7 @@ -- The new option object. -- - function premake.option.add(opt) + function m.add(opt) -- some sanity checking local missing for _, field in ipairs({ "description", "trigger" }) do @@ -39,6 +67,7 @@ end + -- -- Retrieve an option by name. -- @@ -48,16 +77,17 @@ -- The requested option, or nil if the option does not exist. -- - function premake.option.get(name) + function m.get(name) return premake.option.list[name] end + -- -- Iterator for the list of options. -- - function premake.option.each() + function m.each() -- sort the list by trigger local keys = { } for _, option in pairs(premake.option.list) do @@ -73,6 +103,7 @@ end + -- -- Validate a list of user supplied key/value pairs against the list of registered options. -- @@ -82,7 +113,7 @@ --- True if the list of pairs are valid, false and an error message otherwise. -- - function premake.option.validate(values) + function m.validate(values) for key, value in pairs(values) do -- does this option exist local opt = premake.option.get(key) diff --git a/tests/base/test_option.lua b/tests/base/test_option.lua new file mode 100644 index 00000000..e8e9d9b4 --- /dev/null +++ b/tests/base/test_option.lua @@ -0,0 +1,32 @@ +-- +-- tests/base/test_option.lua +-- Verify the handling of command line options and the _OPTIONS table. +-- Copyright (c) 2014 Jason Perkins and the Premake project +-- + + local suite = test.declare("base_option") + + +-- +-- Setup and teardown. +-- + + function suite.setup() + _LOGGING = true + _OPTIONS["testopt"] = "testopt" + end + + function suite.teardown() + _OPTIONS["testopt"] = nil + _LOGGING = false + end + + +-- +-- Because we can't control how the user will type in options on the +-- command line, all key lookups should be case insensitive. +-- + + function suite.returnsCorrectOption_onMixedCase() + test.isnotnil(_OPTIONS["TestOpt"]) + end diff --git a/tests/premake5.lua b/tests/premake5.lua index 2447ba8e..2ba81e48 100644 --- a/tests/premake5.lua +++ b/tests/premake5.lua @@ -53,6 +53,7 @@ dofile("base/test_criteria.lua") dofile("base/test_detoken.lua") dofile("base/test_include.lua") + dofile("base/test_option.lua") dofile("base/test_os.lua") dofile("base/test_override.lua") dofile("base/test_path.lua") diff --git a/tests/testfx.lua b/tests/testfx.lua index 966c0600..7e258cdd 100644 --- a/tests/testfx.lua +++ b/tests/testfx.lua @@ -248,6 +248,7 @@ -- Test execution function -- local _OS_host = _OS + local _OPTIONS_host = _OPTIONS local function error_handler(err) local msg = err @@ -275,10 +276,11 @@ local function test_setup(suite, fn) _ACTION = "test" - _ARGS = { } - _OPTIONS = { } _OS = _OS_host + _OPTIONS = {} + setmetatable(_OPTIONS, getmetatable(_OPTIONS_host)) + stderr_capture = nil premake.solution.list = { }