Ignore __pairs when iterating over tables

Related with #33
master
Enrique García Cota 7 years ago
parent b6bb292f68
commit 9dc10c968f
  1. 27
      inspect.lua
  2. 12
      spec/inspect_spec.lua

@ -33,6 +33,10 @@ local tostring = tostring
inspect.KEY = setmetatable({}, {__tostring = function() return 'inspect.KEY' end})
inspect.METATABLE = setmetatable({}, {__tostring = function() return 'inspect.METATABLE' end})
local function rawpairs(t)
return next, t, nil
end
-- Apostrophizes the string if it has quotes, but not aphostrophes
-- Otherwise, it returns a regular quoted string
local function smartQuote(str)
@ -108,13 +112,16 @@ local function getSequenceLength(t)
end
local function getNonSequentialKeys(t)
local keys = {}
local keys, keysLength = {}, 0
local sequenceLength = getSequenceLength(t)
for k,_ in pairs(t) do
if not isSequenceKey(k, sequenceLength) then table.insert(keys, k) end
for k,_ in rawpairs(t) do
if not isSequenceKey(k, sequenceLength) then
keysLength = keysLength + 1
keys[keysLength] = k
end
end
table.sort(keys, sortKeys)
return keys, sequenceLength
return keys, keysLength, sequenceLength
end
local function getToStringResultSafely(t, mt)
@ -133,7 +140,7 @@ local function countTableAppearances(t, tableAppearances)
if type(t) == 'table' then
if not tableAppearances[t] then
tableAppearances[t] = 1
for k,v in pairs(t) do
for k,v in rawpairs(t) do
countTableAppearances(k, tableAppearances)
countTableAppearances(v, tableAppearances)
end
@ -162,7 +169,6 @@ local function makePath(path, ...)
end
local function processRecursive(process, item, path, visited)
if item == nil then return nil end
if visited[item] then return visited[item] end
@ -172,7 +178,7 @@ local function processRecursive(process, item, path, visited)
visited[item] = processedCopy
local processedKey
for k,v in pairs(processed) do
for k,v in rawpairs(processed) do
processedKey = processRecursive(process, k, makePath(path, k, inspect.KEY), visited)
if processedKey ~= nil then
processedCopy[processedKey] = processRecursive(process, v, makePath(path, processedKey), visited)
@ -246,7 +252,7 @@ function Inspector:putTable(t)
else
if self.tableAppearances[t] > 1 then self:puts('<', self:getId(t), '>') end
local nonSequentialKeys, sequenceLength = getNonSequentialKeys(t)
local nonSequentialKeys, nonSequentialKeysLength, sequenceLength = getNonSequentialKeys(t)
local mt = getmetatable(t)
local toStringResult = getToStringResultSafely(t, mt)
@ -265,7 +271,8 @@ function Inspector:putTable(t)
count = count + 1
end
for _,k in ipairs(nonSequentialKeys) do
for i=1, nonSequentialKeysLength do
local k = nonSequentialKeys[i]
if count > 0 then self:puts(',') end
self:tabify()
self:putKey(k)
@ -282,7 +289,7 @@ function Inspector:putTable(t)
end
end)
if #nonSequentialKeys > 0 or type(mt) == 'table' then -- result is multi-lined. Justify closing }
if nonSequentialKeysLength > 0 or type(mt) == 'table' then -- result is multi-lined. Justify closing }
self:tabify()
elseif sequenceLength > 0 then -- array tables have one extra space before closing }
self:puts(' ')

@ -92,6 +92,18 @@ describe( 'inspect', function()
local arr = setmetatable({1,2,3}, {__len = function() return nil end})
assert.equals("{ 1, 2, 3,\n <metatable> = {\n __len = <function 1>\n }\n}", inspect(arr))
end)
it('handles tables with a __pairs metamethod (ignoring the __pairs metamethod and using next)', function()
local t = setmetatable({ {}, name = "yeah" }, { __pairs = function() end })
assert.equals(
unindent([[{ {},
name = "yeah",
<metatable> = {
__pairs = <function 1>
}
}]]),
inspect(t))
end)
end
it('works with simple dictionary tables', function()

Loading…
Cancel
Save