Improved error handling in pretty.
This commit is contained in:
parent
08e659fc4a
commit
4e7a1a9839
|
@ -191,6 +191,7 @@ end
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
local function analyze_structure (root)
|
local function analyze_structure (root)
|
||||||
|
if type(root) ~= 'table' then return {} end
|
||||||
assert(type(root) == 'table')
|
assert(type(root) == 'table')
|
||||||
|
|
||||||
local info, visited, next_mark, depth = { root = root }, {}, 1, { [root] = 0 }
|
local info, visited, next_mark, depth = { root = root }, {}, 1, { [root] = 0 }
|
||||||
|
|
43
pretty.lua
43
pretty.lua
|
@ -394,6 +394,7 @@ end
|
||||||
|
|
||||||
local function format_string (str, depth, l)
|
local function format_string (str, depth, l)
|
||||||
-- TODO: Add option for escaping unicode characters.
|
-- TODO: Add option for escaping unicode characters.
|
||||||
|
-- TODO: Improve cutstring argument.
|
||||||
|
|
||||||
-- Error checking
|
-- Error checking
|
||||||
assert( type(str) == 'string' )
|
assert( type(str) == 'string' )
|
||||||
|
@ -494,41 +495,49 @@ setmetatable(StringBuilder, {
|
||||||
local DEBUG_OPTIONS = { _all_function_info = true, _table_addr_comment = true }
|
local DEBUG_OPTIONS = { _all_function_info = true, _table_addr_comment = true }
|
||||||
|
|
||||||
local KNOWN_OPTIONS = {
|
local KNOWN_OPTIONS = {
|
||||||
_table_addr_comment = 'boolean',
|
_table_addr_comment = { type = 'boolean', default = false },
|
||||||
_all_function_info = 'boolean',
|
_all_function_info = { type = 'boolean', default = false },
|
||||||
cut_strings = 'boolean',
|
cut_strings = { type = 'boolean', default = false },
|
||||||
include_closure = 'boolean',
|
include_closure = { type = 'boolean', default = false },
|
||||||
indent = 'string',
|
indent = { type = 'string', default = '\t' }, -- TODO: Change default to ' '.
|
||||||
math_shorthand = 'boolean',
|
math_shorthand = { type = 'boolean', default = false },
|
||||||
max_depth = 'number',
|
max_depth = { type = 'number', default = math.huge },
|
||||||
more_function_info = 'boolean',
|
more_function_info = { type = 'boolean', default = false },
|
||||||
recursion = 'string',
|
recursion = { type = 'string', default = 'ignore', accepted = {['ignore'] = true, ['marked'] = true} },
|
||||||
short_builtins = 'boolean',
|
short_builtins = { type = 'boolean', default = false },
|
||||||
}
|
}
|
||||||
|
|
||||||
local function ensure_that_all_options_are_known (options)
|
local function ensure_that_all_options_are_known (options)
|
||||||
assert(type(options) == 'table')
|
assert(type(options) == 'table')
|
||||||
|
-- Error check options
|
||||||
for option_name, option_value in pairs(options) do
|
for option_name, option_value in pairs(options) do
|
||||||
if not KNOWN_OPTIONS[option_name] then
|
if not KNOWN_OPTIONS[option_name] then
|
||||||
error(('[pretty]: Unknown option: %s. Was given value %s (%s)'):format(option_name, option_value, type(option_value)), 2)
|
error(('[pretty]: Unknown option: %s. Was given value %s (%s)'):format(option_name, option_value, type(option_value)), 2)
|
||||||
elseif type(option_value) ~= KNOWN_OPTIONS[option_name] then
|
elseif type(option_value) ~= KNOWN_OPTIONS[option_name].type then
|
||||||
error(('[pretty]: Bad value given to option %s: %s (%s). Expected value of type %s'):format(option_name, option_value, type(option_value), KNOWN_OPTIONS[option_name]), 2)
|
error(('[pretty]: Bad value given to option %s: %s (%s). Expected value of type %s'):format(option_name, option_value, type(option_value), KNOWN_OPTIONS[option_name].type), 2)
|
||||||
|
elseif KNOWN_OPTIONS[option_name].accepted and not KNOWN_OPTIONS[option_name].accepted[option_value] then
|
||||||
|
error(('[pretty]: Bad value given to option %s: %s (%s). Expected one of: %s'):format(option_name, option_value, type(option_value), table.concat(KNOWN_OPTIONS[option_name].accepted, ', ')), 2)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
-- Assign default values
|
||||||
|
for option_name, option_info in pairs(KNOWN_OPTIONS) do
|
||||||
|
if not options[option_name] then
|
||||||
|
options[option_name] = option_info.default
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- Returns options
|
||||||
|
return options
|
||||||
end
|
end
|
||||||
|
|
||||||
local function pretty_format (value, options)
|
local function pretty_format (value, options)
|
||||||
-- Error checking
|
-- Error checking
|
||||||
options = options or {}
|
local options = ensure_that_all_options_are_known(options or {})
|
||||||
options.max_depth = options.max_depth or math.huge
|
|
||||||
options.indent = options.indent or '\t'
|
|
||||||
ensure_that_all_options_are_known(options)
|
|
||||||
|
|
||||||
-- Setup StringBuilder
|
-- Setup StringBuilder
|
||||||
local l = StringBuilder()
|
local l = StringBuilder()
|
||||||
l.visited = { next_mark = 1 }
|
l.visited = { next_mark = 1 }
|
||||||
l.options = options
|
l.options = options
|
||||||
l.info = (type(value) == 'table') and analyze_structure(value) or {}
|
l.info = analyze_structure(value)
|
||||||
|
|
||||||
-- Format value.
|
-- Format value.
|
||||||
format_value(value, 0, l)
|
format_value(value, 0, l)
|
||||||
|
|
|
@ -41,6 +41,11 @@ SUITE:addTest('Dont allow bad types in options', function ()
|
||||||
assert(error_msg)
|
assert(error_msg)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
SUITE:addTest('Dont allow bad values in recursion', function ()
|
||||||
|
local error_msg = bad_call(pretty, 'Hello World', { recursion = "hello world" })
|
||||||
|
assert(error_msg)
|
||||||
|
end)
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- Survive not loading libs
|
-- Survive not loading libs
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user