More robust super_tostring
.
This commit is contained in:
parent
79b036e51e
commit
5e657c6504
|
@ -42,6 +42,8 @@ local LEAF_VALUE_TYPES = {
|
||||||
local SHORT_STRING_MAX_LEN = 7
|
local SHORT_STRING_MAX_LEN = 7
|
||||||
local MINIMUM_NUMBER_OF_SET_ELEMENTS = 2
|
local MINIMUM_NUMBER_OF_SET_ELEMENTS = 2
|
||||||
|
|
||||||
|
local RECURSIVE_TOSTRING_TIMEOUT = 10
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
local function is_identifier(s)
|
local function is_identifier(s)
|
||||||
|
@ -226,12 +228,14 @@ local function super_tostring (t)
|
||||||
-- function cannot find a string.
|
-- function cannot find a string.
|
||||||
|
|
||||||
local seen = { [t] = true }
|
local seen = { [t] = true }
|
||||||
while true do
|
for i = 1, RECURSIVE_TOSTRING_TIMEOUT do
|
||||||
local nt = tostring(t)
|
local success, nt = pcall(tostring, t)
|
||||||
|
if not success then return 'error on tostring: '..tostring(nt) end
|
||||||
if type(nt) == 'string' or nt == nil then return tostring(nt) end
|
if type(nt) == 'string' or nt == nil then return tostring(nt) end
|
||||||
if seen[nt] then return nil end
|
if seen[nt] then return nil end
|
||||||
seen[nt], t = true, nt
|
seen[nt], t = true, nt
|
||||||
end
|
end
|
||||||
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
|
@ -161,13 +161,10 @@ end
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- Strange metatable
|
-- Strange metatable
|
||||||
|
|
||||||
SUITE:addTest('Metatable: Error on %s operator', function (op)
|
SUITE:addTest('Metatable: Error on operator', function (op)
|
||||||
pretty( setmetatable( { a = 2, b = 4, 1, 2, 3 } , {['__'..op] = function() error 'This should be unreachable!' end}) )
|
pretty( setmetatable( { a = 2, b = 4, 1, 2, 3 } , {[op] = function() error 'This should be unreachable!' end}) )
|
||||||
assert(true)
|
assert(true)
|
||||||
end, { data = {
|
end, { data = { '__add', '__sub', '__mul', '__div', '__mod', '__pow', '__unm', '__concat','__len', '__eq', '__lt', '__le', '__index', '__newindex', '__call', '__tostring' } })
|
||||||
{'add'}, {'sub'}, {'mul'}, {'div'}, {'mod'},{'pow'}, {'unm'}, {'concat'},
|
|
||||||
{'len'}, {'eq'}, {'lt'}, {'le'}, {'index'}, {'newindex'}, {'call'} }
|
|
||||||
})
|
|
||||||
|
|
||||||
SUITE:addTest('Metatable: # returns bogus length', function ()
|
SUITE:addTest('Metatable: # returns bogus length', function ()
|
||||||
assert_equal('{ 1, 2, 3, 4 }', pretty( setmetatable({ 1, 2, 3, 4 }, {__len = function() return 2 end})))
|
assert_equal('{ 1, 2, 3, 4 }', pretty( setmetatable({ 1, 2, 3, 4 }, {__len = function() return 2 end})))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user