diff --git a/inspect.lua b/inspect.lua index 4fee019..9bdaf99 100644 --- a/inspect.lua +++ b/inspect.lua @@ -138,13 +138,16 @@ end local copySequence = function(s) local copy, len = {}, #s - for i=1, len do copy[i] = copy[i] end + for i=1, len do copy[i] = s[i] end return copy, len end -local function makePath(path, key) +local function makePath(path, ...) + local keys = {...} local newPath, len = copySequence(path) - newPath[len + 1] = key + for i=1, #keys do + newPath[len + i] = keys[i] + end return newPath end @@ -157,7 +160,7 @@ local function processRecursive(process, item, path) local processedKey for k,v in pairs(processed) do - processedKey = processRecursive(process, k, makePath(path, '')) + processedKey = processRecursive(process, k, makePath(path, k, '')) if processedKey ~= nil then processedCopy[processedKey] = processRecursive(process, v, makePath(path, processedKey)) end diff --git a/spec/inspect_spec.lua b/spec/inspect_spec.lua index dc57cc3..2e3511b 100644 --- a/spec/inspect_spec.lua +++ b/spec/inspect_spec.lua @@ -249,14 +249,30 @@ describe( 'inspect', function() assert.equals(inspect(dict, {process = removeA}), '{\n b = 2\n}') end) - it('marks key paths with ', function() - local names = {a = 1} - local paths = {} - local addPath = function(item, path) paths[#paths + 1] = path; return item end + it('marks key paths with and metatables with ', function() + local t = { [{a=1}] = setmetatable({b=2}, {c=3}) } + + local items = {} + local addItem = function(item, path) + items[#items + 1] = {item = item, path = path} + return item + end + + inspect(t, {process = addItem}) + + assert.same(items, { + {item = t, path = {}}, + {item = {a=1}, path = {{a=1}, ''}}, + {item = 'a', path = {{a=1}, '', 'a', ''}}, + {item = 1, path = {{a=1}, '', 'a'}}, + {item = setmetatable({b=2}, {c=3}), path = {{a=1}}}, + {item = 'b', path = {{a=1}, 'b', ''}}, + {item = 2, path = {{a=1}, 'b'}}, + {item = {c=3}, path = {{a=1}, ''}}, + {item = 'c', path = {{a=1}, '', 'c', ''}}, + {item = 3, path = {{a=1}, '', 'c'}} + }) - inspect(names, {process = addPath}) - - assert.same(paths, { {}, {''}, {'a'} }) end) end)