From 33fb88fdd7d1bd6aa5f01cb5c6312a72988543ea Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Thu, 20 Jul 2017 18:58:06 +0200 Subject: [PATCH] What is an is not an identifier is now automatically determined by `is_identifier`. --- analyze_structure.lua | 17 ++++++++++++++--- test/test_pretty.lua | 14 +++++++++++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/analyze_structure.lua b/analyze_structure.lua index df9113e..62c7e81 100644 --- a/analyze_structure.lua +++ b/analyze_structure.lua @@ -48,13 +48,18 @@ local MINIMUM_NUMBER_OF_SET_ELEMENTS = 2 -------------------------------------------------------------------------------- -local function is_identifier(str) - -- An identier is defined in the lua reference guide +local function is_identifier(s) + -- 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 local function get_key_types (t) + assert(type(t) == 'table') + -- local types = { nr_types = -1 } for key, _ in pairs(t) do types[type(key)] = true @@ -67,6 +72,8 @@ local function get_key_types (t) end local function get_value_types (t) + assert(type(t) == 'table') + -- local types = { nr_types = -1 } for _, value in pairs(t) do types[type(value)] = true @@ -81,6 +88,8 @@ end local function largest_number_index (t) -- Returns the largest number index in t. + assert(type(t) == 'table') + -- local max_index = 0 for k,v in pairs(t) do if type(k) == 'number' then @@ -91,6 +100,8 @@ local function largest_number_index (t) end local function nr_elements_in_table (t) + assert(type(t) == 'table') + -- local k, count = nil, -1 repeat k, count = next(t, k), count + 1 diff --git a/test/test_pretty.lua b/test/test_pretty.lua index 2d3acb8..6753ab1 100644 --- a/test/test_pretty.lua +++ b/test/test_pretty.lua @@ -96,7 +96,6 @@ format_test { input = '\000', expect = '\'\\000\'', } - format_test { input = '\a\b\v\r\f', expect = '\'\\a\\b\\v\\r\\f\'', @@ -107,6 +106,13 @@ format_test { 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 @@ -414,6 +420,12 @@ SUITE:addTest('UseCase: Can load function from file that is shortly deleted', fu assert(true) 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