1
0

What is an is not an identifier is now automatically determined by is_identifier.

This commit is contained in:
Jon Michael Aanes 2017-07-20 18:58:06 +02:00
parent 8f8e5b9c7f
commit 33fb88fdd7
2 changed files with 27 additions and 4 deletions

View File

@ -48,13 +48,18 @@ local MINIMUM_NUMBER_OF_SET_ELEMENTS = 2
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
local function is_identifier(str) local function is_identifier(s)
-- An identier is defined in the lua reference guide -- Predicate: Is the given string usable as an identifier in this version of
-- Lua?
return str:match('^[_%a][_%w]*$') and not RESERVED_LUA_WORDS[str] assert(type(s) == 'string')
--
return not not loadstring(s..'=0') and not s:find '%.' and not RESERVED_LUA_WORDS[s]
end end
local function get_key_types (t) local function get_key_types (t)
assert(type(t) == 'table')
--
local types = { nr_types = -1 } local types = { nr_types = -1 }
for key, _ in pairs(t) do for key, _ in pairs(t) do
types[type(key)] = true types[type(key)] = true
@ -67,6 +72,8 @@ local function get_key_types (t)
end end
local function get_value_types (t) local function get_value_types (t)
assert(type(t) == 'table')
--
local types = { nr_types = -1 } local types = { nr_types = -1 }
for _, value in pairs(t) do for _, value in pairs(t) do
types[type(value)] = true types[type(value)] = true
@ -81,6 +88,8 @@ end
local function largest_number_index (t) local function largest_number_index (t)
-- Returns the largest number index in t. -- Returns the largest number index in t.
assert(type(t) == 'table')
--
local max_index = 0 local max_index = 0
for k,v in pairs(t) do for k,v in pairs(t) do
if type(k) == 'number' then if type(k) == 'number' then
@ -91,6 +100,8 @@ local function largest_number_index (t)
end end
local function nr_elements_in_table (t) local function nr_elements_in_table (t)
assert(type(t) == 'table')
--
local k, count = nil, -1 local k, count = nil, -1
repeat repeat
k, count = next(t, k), count + 1 k, count = next(t, k), count + 1

View File

@ -96,7 +96,6 @@ format_test {
input = '\000', input = '\000',
expect = '\'\\000\'', expect = '\'\\000\'',
} }
format_test { format_test {
input = '\a\b\v\r\f', input = '\a\b\v\r\f',
expect = '\'\\a\\b\\v\\r\\f\'', expect = '\'\\a\\b\\v\\r\\f\'',
@ -107,6 +106,13 @@ format_test {
expect = '\'ø\'', expect = '\'ø\'',
} }
format_test {
name = 'Malformed Unicode is escaped',
input = '\000\001\003\012\169\003\000\030',
expect = '\'\\000\\000\\001\\003\\012\\169\\003\\000\\030\'',
}
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Primitive types -- Primitive types
@ -414,6 +420,12 @@ SUITE:addTest('UseCase: Can load function from file that is shortly deleted', fu
assert(true) assert(true)
end) end)
SUITE:addTest('UseCase: Can use pretty on loadstring block', function ()
-- TODO: Move to more appropriate test module.
format(loadstring 'hi = 0')
assert(true)
end)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
return SUITE return SUITE