From 387a2f683a9c71e1969f738188ac3d9f09b260ad Mon Sep 17 00:00:00 2001 From: kikito Date: Sat, 21 Nov 2015 17:04:28 +0100 Subject: [PATCH 1/4] adds test for rawlen --- spec/inspect_spec.lua | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/inspect_spec.lua b/spec/inspect_spec.lua index 4d7ef2a..91e46ea 100644 --- a/spec/inspect_spec.lua +++ b/spec/inspect_spec.lua @@ -1,6 +1,7 @@ local inspect = require 'inspect' local unindent = require 'spec.unindent' local is_luajit, ffi = pcall(require, 'ffi') +local has_rawlen = type(_G.rawlen) == 'function' describe( 'inspect', function() @@ -74,6 +75,13 @@ describe( 'inspect', function() assert.equals('{ "a", "b", "c", { "d", "e" }, "f" }', inspect({'a','b','c', {'d','e'}, 'f'})) end) + if has_rawlen then + it('handles arrays with a __len metatable correctly (ignoring the __len metatable and using rawlen)', function() + local arr = setmetatable({1,2,3}, {__len = function() return nil end}) + assert.equals("{ 1, 2, 3 }", inspect(arr)) + end) + end + it('works with simple dictionary tables', function() assert.equals("{\n a = 1,\n b = 2\n}", inspect({a = 1, b = 2})) end) From f01a007c913862fdd4ed9c396eef35085f7b048d Mon Sep 17 00:00:00 2001 From: kikito Date: Sat, 21 Nov 2015 17:08:09 +0100 Subject: [PATCH 2/4] attempts to fix failing travis --- inspect.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/inspect.lua b/inspect.lua index b7107d4..61a9ec2 100644 --- a/inspect.lua +++ b/inspect.lua @@ -31,6 +31,9 @@ local inspect ={ inspect.KEY = setmetatable({}, {__tostring = function() return 'inspect.KEY' end}) inspect.METATABLE = setmetatable({}, {__tostring = function() return 'inspect.METATABLE' end}) +-- returns the length of a table, ignoring __len (if it exists) +local rawlen = _G.rawlen or function(t) return #t end + -- Apostrophizes the string if it has quotes, but not aphostrophes -- Otherwise, it returns a regular quoted string local function smartQuote(str) @@ -84,7 +87,7 @@ local function sortKeys(a, b) end local function getNonSequentialKeys(t) - local keys, length = {}, #t + local keys, length = {}, rawlen(t) for k,_ in pairs(t) do if not isSequenceKey(k, length) then table.insert(keys, k) end end From 162d497b0de763a73e04d4ef600f5fbd669a2eeb Mon Sep 17 00:00:00 2001 From: kikito Date: Sat, 21 Nov 2015 17:10:23 +0100 Subject: [PATCH 3/4] second attempt at fixing travis --- inspect.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inspect.lua b/inspect.lua index 61a9ec2..71f3b75 100644 --- a/inspect.lua +++ b/inspect.lua @@ -235,7 +235,7 @@ function Inspector:putTable(t) if self.tableAppearances[t] > 1 then self:puts('<', self:getId(t), '>') end local nonSequentialKeys = getNonSequentialKeys(t) - local length = #t + local length = rawlen(t) local mt = getmetatable(t) local toStringResult = getToStringResultSafely(t, mt) From 4f9761b63110d9cb78fa0d04f26e10505ce85d9b Mon Sep 17 00:00:00 2001 From: kikito Date: Sat, 21 Nov 2015 17:13:48 +0100 Subject: [PATCH 4/4] fixes __len issue using rawlen instead of #t in a couple places --- spec/inspect_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/inspect_spec.lua b/spec/inspect_spec.lua index 91e46ea..5f9d46e 100644 --- a/spec/inspect_spec.lua +++ b/spec/inspect_spec.lua @@ -78,7 +78,7 @@ describe( 'inspect', function() if has_rawlen then it('handles arrays with a __len metatable correctly (ignoring the __len metatable and using rawlen)', function() local arr = setmetatable({1,2,3}, {__len = function() return nil end}) - assert.equals("{ 1, 2, 3 }", inspect(arr)) + assert.equals("{ 1, 2, 3,\n = {\n __len = \n }\n}", inspect(arr)) end) end