mirror of
https://github.com/kikito/inspect.lua.git
synced 2024-12-15 14:34:21 +00:00
Use '<key>' and '<metatable> on keys correctly
This commit is contained in:
parent
b9b780caad
commit
1fb5373a45
11
inspect.lua
11
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, '<key>'))
|
||||
processedKey = processRecursive(process, k, makePath(path, k, '<key>'))
|
||||
if processedKey ~= nil then
|
||||
processedCopy[processedKey] = processRecursive(process, v, makePath(path, processedKey))
|
||||
end
|
||||
|
@ -249,14 +249,30 @@ describe( 'inspect', function()
|
||||
assert.equals(inspect(dict, {process = removeA}), '{\n b = 2\n}')
|
||||
end)
|
||||
|
||||
it('marks key paths with <key>', function()
|
||||
local names = {a = 1}
|
||||
local paths = {}
|
||||
local addPath = function(item, path) paths[#paths + 1] = path; return item end
|
||||
it('marks key paths with <key> and metatables with <metatable>', function()
|
||||
local t = { [{a=1}] = setmetatable({b=2}, {c=3}) }
|
||||
|
||||
inspect(names, {process = addPath})
|
||||
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}, '<key>'}},
|
||||
{item = 'a', path = {{a=1}, '<key>', 'a', '<key>'}},
|
||||
{item = 1, path = {{a=1}, '<key>', 'a'}},
|
||||
{item = setmetatable({b=2}, {c=3}), path = {{a=1}}},
|
||||
{item = 'b', path = {{a=1}, 'b', '<key>'}},
|
||||
{item = 2, path = {{a=1}, 'b'}},
|
||||
{item = {c=3}, path = {{a=1}, '<metatable>'}},
|
||||
{item = 'c', path = {{a=1}, '<metatable>', 'c', '<key>'}},
|
||||
{item = 3, path = {{a=1}, '<metatable>', 'c'}}
|
||||
})
|
||||
|
||||
assert.same(paths, { {}, {'<key>'}, {'a'} })
|
||||
end)
|
||||
end)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user