Fix float keys being masked in tables with array elements

This commit is contained in:
kikito 2014-09-19 22:50:48 +02:00
parent 99deb522f8
commit 182e6f28c1
2 changed files with 15 additions and 11 deletions

View File

@ -56,12 +56,11 @@ local function isIdentifier(str)
return type(str) == 'string' and str:match( "^[_%a][_%a%d]*$" ) return type(str) == 'string' and str:match( "^[_%a][_%a%d]*$" )
end end
local function isArrayKey(k, length) local function isSequenceKey(k, length)
return type(k) == 'number' and 1 <= k and k <= length return type(k) == 'number'
end and 1 <= k
and k <= length
local function isDictionaryKey(k, length) and math.floor(k) == k
return not isArrayKey(k, length)
end end
local defaultTypeOrders = { local defaultTypeOrders = {
@ -86,10 +85,10 @@ local function sortKeys(a, b)
return ta < tb return ta < tb
end end
local function getDictionaryKeys(t) local function getNonSequentialKeys(t)
local keys, length = {}, #t local keys, length = {}, #t
for k,_ in pairs(t) do for k,_ in pairs(t) do
if isDictionaryKey(k, length) then table.insert(keys, k) end if not isSequenceKey(k, length) then table.insert(keys, k) end
end end
table.sort(keys, sortKeys) table.sort(keys, sortKeys)
return keys return keys
@ -234,7 +233,7 @@ function Inspector:putTable(t)
else else
if self.tableAppearances[t] > 1 then self:puts('<', self:getId(t), '>') end if self.tableAppearances[t] > 1 then self:puts('<', self:getId(t), '>') end
local dictKeys = getDictionaryKeys(t) local nonSequentialKeys = getNonSequentialKeys(t)
local length = #t local length = #t
local mt = getmetatable(t) local mt = getmetatable(t)
local to_string_result = getToStringResultSafely(t, mt) local to_string_result = getToStringResultSafely(t, mt)
@ -254,7 +253,7 @@ function Inspector:putTable(t)
count = count + 1 count = count + 1
end end
for _,k in ipairs(dictKeys) do for _,k in ipairs(nonSequentialKeys) do
if count > 0 then self:puts(',') end if count > 0 then self:puts(',') end
self:tabify() self:tabify()
self:putKey(k) self:putKey(k)
@ -272,7 +271,7 @@ function Inspector:putTable(t)
end end
end) end)
if #dictKeys > 0 or mt then -- dictionary table. Justify closing } if #nonSequentialKeys > 0 or mt then -- result is multi-lined. Justify closing }
self:tabify() self:tabify()
elseif length > 0 then -- array tables have one extra space before closing } elseif length > 0 then -- array tables have one extra space before closing }
self:puts(' ') self:puts(' ')

View File

@ -80,6 +80,11 @@ describe( 'inspect', function()
assert.equals(inspect({a = 1, b = 2}), "{\n a = 1,\n b = 2\n}") assert.equals(inspect({a = 1, b = 2}), "{\n a = 1,\n b = 2\n}")
end) end)
it('identifies numeric non-array keys as dictionary keys', function()
assert.equals(inspect({1, 2, [-1] = true}), "{ 1, 2,\n [-1] = true\n}")
assert.equals(inspect({1, 2, [1.5] = true}), "{ 1, 2,\n [1.5] = true\n}")
end)
it('sorts keys in dictionary tables', function() it('sorts keys in dictionary tables', function()
local t = { 1,2,3, local t = { 1,2,3,
[print] = 1, ["buy more"] = 1, a = 1, [print] = 1, ["buy more"] = 1, a = 1,