1
0

Now avoids analysing tables deeper than max_depth.

This commit is contained in:
Jon Michael Aanes 2017-06-15 19:01:41 +02:00
parent 288d9e4de8
commit b37a02c850
4 changed files with 28 additions and 4 deletions

View File

@ -211,9 +211,11 @@ end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
local function analyze_structure (root) local function analyze_structure (root, max_depth)
if type(root) ~= 'table' then return {} end if type(root) ~= 'table' then return {} end
assert(type(root) == 'table') assert(type(root) == 'table')
local max_depth = max_depth or math.huge
assert(type(max_depth) == 'number')
local info, visited, next_mark, depth = { root = root }, {}, 1, { [root] = 0 } local info, visited, next_mark, depth = { root = root }, {}, 1, { [root] = 0 }
local queue = { root, bottom = 1, top = 2 } local queue = { root, bottom = 1, top = 2 }
@ -227,7 +229,7 @@ local function analyze_structure (root)
info[node].marker, next_mark = next_mark, next_mark + 1 info[node].marker, next_mark = next_mark, next_mark + 1
end end
-- Get table info & visit children. -- Get table info & visit children.
if visited[node] < 2 then if visited[node] < 2 and depth[node] < max_depth then
for k, v in pairs(node) do for k, v in pairs(node) do
if type(k) == 'table' then queue[queue.top], queue.top, depth[k] = k, queue.top + 1, math.min(depth[k] or math.huge, depth[node] + 1) end if type(k) == 'table' then queue[queue.top], queue.top, depth[k] = k, queue.top + 1, math.min(depth[k] or math.huge, depth[node] + 1) end
if type(v) == 'table' then queue[queue.top], queue.top, depth[v] = v, queue.top + 1, math.min(depth[v] or math.huge, depth[node] + 1) end if type(v) == 'table' then queue[queue.top], queue.top, depth[v] = v, queue.top + 1, math.min(depth[v] or math.huge, depth[node] + 1) end

View File

@ -237,7 +237,6 @@ return function (value, depth, l, format_value)
l[#l+1] = { 'align', 'func_def', width_of_strings_in_l(l, top_before) } l[#l+1] = { 'align', 'func_def', width_of_strings_in_l(l, top_before) }
-- Cleanup and finish -- Cleanup and finish
print(not l.options.more_function_info, depth ~= 0)
if not l.options.more_function_info or depth ~= 0 then if not l.options.more_function_info or depth ~= 0 then
l[#l+1] = (function_body:sub(1,1) == '\n') and '' or ' ' l[#l+1] = (function_body:sub(1,1) == '\n') and '' or ' '
l[#l+1] = function_body l[#l+1] = function_body

View File

@ -508,7 +508,7 @@ local function pretty_format (value, options)
local l = StringBuilder() local l = StringBuilder()
l.visited = { next_mark = 1 } l.visited = { next_mark = 1 }
l.options = options l.options = options
l.info = analyze_structure(value) l.info = analyze_structure(value, options.max_depth)
-- Format value. -- Format value.
format_value(value, 0, l) format_value(value, 0, l)

View File

@ -125,6 +125,29 @@ SUITE:addTest('Not Tabular, due to varying lengths', function ()
assert_equal(table_info.is_tabular, false) assert_equal(table_info.is_tabular, false)
end) end)
--------------------------------------------------------------------------------
-- Stops at max_depth
SUITE:addTest('Has info for root, and children', function ()
local input = { { 1 }, { 2, 3 }, { 4, 5, 6 } }
local table_info = analyze_structure(input, 1)
assert(table_info[input])
assert(table_info[input[1]])
assert(table_info[input[2]])
assert(table_info[input[3]])
end)
SUITE:addTest('Has info for root, but not children', function ()
local input = { { 1 }, { 2, 3 }, { 4, 5, 6 } }
local table_info = analyze_structure(input, 0)
assert(table_info[input])
assert(not table_info[input[1]])
assert(not table_info[input[2]])
assert(not table_info[input[3]])
end)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Corner cases. -- Corner cases.