<key>, <metatable> -> inspect.KEY, inspect.METATABLE

dev
kikito 10 years ago
parent 1fb5373a45
commit 9054ee1051
  1. 8
      README.md
  2. 11
      inspect.lua
  3. 23
      spec/inspect_spec.lua

@ -139,10 +139,10 @@ local processed_item = function(item, path)
* `path` is an array-like table built with all the keys that have been used to reach `item`, from the root. * `path` is an array-like table built with all the keys that have been used to reach `item`, from the root.
* For values, it is just a regular list of keys. For example, to reach the 1 in `{a = {b = 1}}`, the `path` * For values, it is just a regular list of keys. For example, to reach the 1 in `{a = {b = 1}}`, the `path`
will be `{'a', 'b'}` will be `{'a', 'b'}`
* For keys, a special value called `<key>` is inserted. For example, to reach the `c` in `{a = {b = {c = 1}}}`, * For keys, the special value `inspect.KEY` is inserted. For example, to reach the `c` in `{a = {b = {c = 1}}}`,
the path will be `{'a', 'b', 'c', '<key>' }` the path will be `{'a', 'b', 'c', inspect.KEY }`
* For metatables, a special value called `<metatable>` is inserted. For `{a = {b = 1}}}`, the path * For metatables, the special value `inspect.METATABLE` is inserted. For `{a = {b = 1}}}`, the path
`{'a', 'b', '<metatable>'}` means "the metatable of the table `{b = 1}`". `{'a', {b = 1}, inspect.METATABLE}` means "the metatable of the table `{b = 1}`".
* `processed_item` is the value returned by `options.process`. If it is equal to `item`, then the inspected * `processed_item` is the value returned by `options.process`. If it is equal to `item`, then the inspected
table will look unchanged. If it is different, then the table will look different; most notably, if it's `nil`, table will look unchanged. If it is different, then the table will look different; most notably, if it's `nil`,
the item will dissapear on the inspected table. the item will dissapear on the inspected table.

@ -28,6 +28,9 @@ local inspect ={
]] ]]
} }
inspect.KEY = setmetatable({}, {__tostring = function() return 'inspect.KEY' end})
inspect.METATABLE = setmetatable({}, {__tostring = function() return 'inspect.METATABLE' end})
-- Apostrophizes the string if it has quotes, but not aphostrophes -- Apostrophizes the string if it has quotes, but not aphostrophes
-- Otherwise, it returns a regular quoted string -- Otherwise, it returns a regular quoted string
local function smartQuote(str) local function smartQuote(str)
@ -160,13 +163,13 @@ local function processRecursive(process, item, path)
local processedKey local processedKey
for k,v in pairs(processed) do for k,v in pairs(processed) do
processedKey = processRecursive(process, k, makePath(path, k, '<key>')) processedKey = processRecursive(process, k, makePath(path, k, inspect.KEY))
if processedKey ~= nil then if processedKey ~= nil then
processedCopy[processedKey] = processRecursive(process, v, makePath(path, processedKey)) processedCopy[processedKey] = processRecursive(process, v, makePath(path, processedKey))
end end
end end
local mt = processRecursive(process, getmetatable(processed), makePath(path, '<metatable>')) local mt = processRecursive(process, getmetatable(processed), makePath(path, inspect.METATABLE))
setmetatable(processedCopy, mt) setmetatable(processedCopy, mt)
processed = processedCopy processed = processedCopy
end end
@ -227,7 +230,9 @@ function Inspector:putKey(k)
end end
function Inspector:putTable(t) function Inspector:putTable(t)
if self:alreadyVisited(t) then if t == inspect.KEY or t == inspect.METATABLE then
self:puts(tostring(t))
elseif self:alreadyVisited(t) then
self:puts('<table ', self:getId(t), '>') self:puts('<table ', self:getId(t), '>')
elseif self.level >= self.depth then elseif self.level >= self.depth then
self:puts('{...}') self:puts('{...}')

@ -227,7 +227,7 @@ describe( 'inspect', function()
it('nullifies metatables using their paths', function() it('nullifies metatables using their paths', function()
local mt = {'world'} local mt = {'world'}
local t = setmetatable({'hello'}, mt) local t = setmetatable({'hello'}, mt)
local removeMt = function(item, path) if path[#path] ~= '<metatable>' then return item end end local removeMt = function(item, path) if path[#path] ~= inspect.METATABLE then return item end end
assert.equals(inspect(t, {process = removeMt}), '{ "hello" }') assert.equals(inspect(t, {process = removeMt}), '{ "hello" }')
end) end)
@ -249,7 +249,12 @@ describe( 'inspect', function()
assert.equals(inspect(dict, {process = removeA}), '{\n b = 2\n}') assert.equals(inspect(dict, {process = removeA}), '{\n b = 2\n}')
end) end)
it('marks key paths with <key> and metatables with <metatable>', function() it('prints inspect.KEY & inspect.METATABLE', function()
local t = {inspect.KEY, inspect.METATABLE}
assert.equals(inspect(t), "{ inspect.KEY, inspect.METATABLE }")
end)
it('marks key paths with inspect.KEY and metatables with inspect.METATABLE', function()
local t = { [{a=1}] = setmetatable({b=2}, {c=3}) } local t = { [{a=1}] = setmetatable({b=2}, {c=3}) }
local items = {} local items = {}
@ -262,15 +267,15 @@ describe( 'inspect', function()
assert.same(items, { assert.same(items, {
{item = t, path = {}}, {item = t, path = {}},
{item = {a=1}, path = {{a=1}, '<key>'}}, {item = {a=1}, path = {{a=1}, inspect.KEY}},
{item = 'a', path = {{a=1}, '<key>', 'a', '<key>'}}, {item = 'a', path = {{a=1}, inspect.KEY, 'a', inspect.KEY}},
{item = 1, path = {{a=1}, '<key>', 'a'}}, {item = 1, path = {{a=1}, inspect.KEY, 'a'}},
{item = setmetatable({b=2}, {c=3}), path = {{a=1}}}, {item = setmetatable({b=2}, {c=3}), path = {{a=1}}},
{item = 'b', path = {{a=1}, 'b', '<key>'}}, {item = 'b', path = {{a=1}, 'b', inspect.KEY}},
{item = 2, path = {{a=1}, 'b'}}, {item = 2, path = {{a=1}, 'b'}},
{item = {c=3}, path = {{a=1}, '<metatable>'}}, {item = {c=3}, path = {{a=1}, inspect.METATABLE}},
{item = 'c', path = {{a=1}, '<metatable>', 'c', '<key>'}}, {item = 'c', path = {{a=1}, inspect.METATABLE, 'c', inspect.KEY}},
{item = 3, path = {{a=1}, '<metatable>', 'c'}} {item = 3, path = {{a=1}, inspect.METATABLE, 'c'}}
}) })
end) end)

Loading…
Cancel
Save