diff --git a/inspect.lua b/inspect.lua index f01a6b2..b507663 100644 --- a/inspect.lua +++ b/inspect.lua @@ -64,7 +64,7 @@ local function getDictionaryKeys(t) end local function getToStringResultSafely(t, mt) - local __tostring = type(mt) == 'table' and mt.__tostring + local __tostring = type(mt) == 'table' and rawget(mt, '__tostring') local string, status if type(__tostring) == 'function' then status, string = pcall(__tostring, t) diff --git a/spec/inspect_spec.lua b/spec/inspect_spec.lua index 174a735..71a15f3 100644 --- a/spec/inspect_spec.lua +++ b/spec/inspect_spec.lua @@ -192,14 +192,26 @@ describe( 'inspect', function() }]]) end) - it('accepts a table that is its own metatable without stack overflowing', function() - local x = {} - setmetatable(x,x) - assert.equals(inspect(x), [[<1>{ + describe('When a table is its own metatable', function() + it('accepts a table that is its own metatable without stack overflowing', function() + local x = {} + setmetatable(x,x) + assert.equals(inspect(x), [[<1>{ = }]]) - end) + end) + it('can invoke the __tostring method without stack overflowing', function() + local t = {} + t.__index = t + setmetatable(t,t) + assert.equals(inspect(t), [[<1>{ + __index =
, + =
+}]]) + end) + + end) end) end) end)