2014-09-07 20:32:31 +00:00
|
|
|
local inspect = require 'inspect'
|
|
|
|
local unindent = require 'spec.unindent'
|
|
|
|
local is_luajit, ffi = pcall(require, 'ffi')
|
2015-11-21 16:04:28 +00:00
|
|
|
local has_rawlen = type(_G.rawlen) == 'function'
|
2011-04-20 23:11:42 +00:00
|
|
|
|
2013-01-20 16:07:41 +00:00
|
|
|
describe( 'inspect', function()
|
2011-04-20 23:11:42 +00:00
|
|
|
|
2013-01-20 16:07:41 +00:00
|
|
|
describe('numbers', function()
|
2012-10-29 20:11:20 +00:00
|
|
|
it('works', function()
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals("1", inspect(1))
|
|
|
|
assert.equals("1.5", inspect(1.5))
|
|
|
|
assert.equals("-3.14", inspect(-3.14))
|
2011-04-23 19:33:03 +00:00
|
|
|
end)
|
2011-04-23 18:17:43 +00:00
|
|
|
end)
|
|
|
|
|
2013-01-20 16:07:41 +00:00
|
|
|
describe('strings', function()
|
2012-10-29 20:11:20 +00:00
|
|
|
it('puts quotes around regular strings', function()
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals('"hello"', inspect("hello"))
|
2011-04-23 19:33:03 +00:00
|
|
|
end)
|
|
|
|
|
2012-10-29 20:11:20 +00:00
|
|
|
it('puts apostrophes around strings with quotes', function()
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals("'I have \"quotes\"'", inspect('I have "quotes"'))
|
2011-04-23 19:33:03 +00:00
|
|
|
end)
|
|
|
|
|
2012-10-29 20:11:20 +00:00
|
|
|
it('uses regular quotes if the string has both quotes and apostrophes', function()
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals('"I have \\"quotes\\" and \'apostrophes\'"', inspect("I have \"quotes\" and 'apostrophes'"))
|
2011-04-23 19:33:03 +00:00
|
|
|
end)
|
|
|
|
|
2014-01-07 13:56:10 +00:00
|
|
|
it('escapes newlines properly', function()
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals('"I have \\n new \\n lines"', inspect('I have \n new \n lines'))
|
2011-04-23 19:33:03 +00:00
|
|
|
end)
|
2014-01-07 13:56:10 +00:00
|
|
|
|
|
|
|
it('escapes tabs properly', function()
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals('"I have \\t a tab character"', inspect('I have \t a tab character'))
|
2014-01-07 13:56:10 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('escapes backspaces properly', function()
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals('"I have \\b a back space"', inspect('I have \b a back space'))
|
2014-01-07 13:56:10 +00:00
|
|
|
end)
|
|
|
|
|
2016-03-29 19:58:17 +00:00
|
|
|
it('escapes zeroes and other control characters properly', function()
|
2016-04-03 17:51:48 +00:00
|
|
|
assert.equals('"I have \\0 zeroes \\0000 and other \\1 control \\0010 characters \\6 \\17 \\0279 \\31"',
|
|
|
|
inspect('I have \0 zeroes \0000 and other \1 control \0010 characters \6 \17 \0279 \31'))
|
2016-03-29 19:58:17 +00:00
|
|
|
end)
|
|
|
|
|
2014-01-07 13:56:10 +00:00
|
|
|
it('backslashes its backslashes', function()
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals('"I have \\\\ a backslash"', inspect('I have \\ a backslash'))
|
|
|
|
assert.equals('"I have \\\\\\\\ two backslashes"', inspect('I have \\\\ two backslashes'))
|
|
|
|
assert.equals('"I have \\\\\\n a backslash followed by a newline"', inspect('I have \\\n a backslash followed by a newline'))
|
2014-01-07 13:56:10 +00:00
|
|
|
end)
|
|
|
|
|
2011-04-23 19:00:41 +00:00
|
|
|
end)
|
|
|
|
|
2012-10-29 20:11:20 +00:00
|
|
|
it('works with nil', function()
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals('nil', inspect(nil))
|
2011-05-09 22:31:56 +00:00
|
|
|
end)
|
|
|
|
|
2012-10-29 20:11:20 +00:00
|
|
|
it('works with functions', function()
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals('{ <function 1>, <function 2>, <function 1> }', inspect({ print, type, print }))
|
2011-04-23 17:48:43 +00:00
|
|
|
end)
|
2011-04-20 23:11:42 +00:00
|
|
|
|
2012-10-29 20:11:20 +00:00
|
|
|
it('works with booleans', function()
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals('true', inspect(true))
|
|
|
|
assert.equals('false', inspect(false))
|
2011-04-23 19:33:03 +00:00
|
|
|
end)
|
|
|
|
|
2013-06-27 10:05:19 +00:00
|
|
|
if is_luajit then
|
2014-09-19 20:56:57 +00:00
|
|
|
it('works with luajit cdata', function()
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals('{ <cdata 1>, <cdata 2>, <cdata 3> }', inspect({ ffi.new("int", 1), ffi.typeof("int"), ffi.typeof("int")(1) }))
|
2013-06-27 10:05:19 +00:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2013-01-20 16:07:41 +00:00
|
|
|
describe('tables', function()
|
2011-04-23 19:33:03 +00:00
|
|
|
|
2012-10-29 20:11:20 +00:00
|
|
|
it('works with simple array-like tables', function()
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals("{ 1, 2, 3 }", inspect({1,2,3}))
|
2011-04-23 19:33:03 +00:00
|
|
|
end)
|
|
|
|
|
2012-10-29 20:11:20 +00:00
|
|
|
it('works with nested arrays', function()
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals('{ "a", "b", "c", { "d", "e" }, "f" }', inspect({'a','b','c', {'d','e'}, 'f'}))
|
2011-04-23 21:29:52 +00:00
|
|
|
end)
|
|
|
|
|
2015-11-21 16:04:28 +00:00
|
|
|
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})
|
2015-11-21 16:13:48 +00:00
|
|
|
assert.equals("{ 1, 2, 3,\n <metatable> = {\n __len = <function 1>\n }\n}", inspect(arr))
|
2015-11-21 16:04:28 +00:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2012-10-29 20:11:20 +00:00
|
|
|
it('works with simple dictionary tables', function()
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals("{\n a = 1,\n b = 2\n}", inspect({a = 1, b = 2}))
|
2011-04-23 19:33:03 +00:00
|
|
|
end)
|
|
|
|
|
2016-03-06 16:06:14 +00:00
|
|
|
it('identifies tables with no number 1 as struct-like', function()
|
|
|
|
assert.equals(unindent([[{
|
|
|
|
[2] = 1,
|
|
|
|
[25] = 1,
|
|
|
|
id = 1
|
|
|
|
}
|
|
|
|
]]), inspect({[2]=1,[25]=1,id=1}))
|
|
|
|
end)
|
|
|
|
|
2014-09-19 20:50:48 +00:00
|
|
|
it('identifies numeric non-array keys as dictionary keys', function()
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals("{ 1, 2,\n [-1] = true\n}", inspect({1, 2, [-1] = true}))
|
|
|
|
assert.equals("{ 1, 2,\n [1.5] = true\n}", inspect({1, 2, [1.5] = true}))
|
2014-09-19 20:50:48 +00:00
|
|
|
end)
|
|
|
|
|
2012-10-29 20:11:20 +00:00
|
|
|
it('sorts keys in dictionary tables', function()
|
2011-04-23 23:11:51 +00:00
|
|
|
local t = { 1,2,3,
|
2012-10-29 20:11:20 +00:00
|
|
|
[print] = 1, ["buy more"] = 1, a = 1,
|
2014-09-07 20:32:31 +00:00
|
|
|
[coroutine.create(function() end)] = 1,
|
2011-04-23 23:11:51 +00:00
|
|
|
[14] = 1, [{c=2}] = 1, [true]= 1
|
|
|
|
}
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals(unindent([[
|
2014-09-07 20:32:31 +00:00
|
|
|
{ 1, 2, 3,
|
|
|
|
[14] = 1,
|
|
|
|
[true] = 1,
|
|
|
|
a = 1,
|
|
|
|
["buy more"] = 1,
|
|
|
|
[{
|
|
|
|
c = 2
|
|
|
|
}] = 1,
|
|
|
|
[<function 1>] = 1,
|
|
|
|
[<thread 1>] = 1
|
|
|
|
}
|
2014-11-18 17:02:55 +00:00
|
|
|
]]), inspect(t))
|
2011-04-23 23:11:51 +00:00
|
|
|
end)
|
|
|
|
|
2012-10-29 20:11:20 +00:00
|
|
|
it('works with nested dictionary tables', function()
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals(unindent([[{
|
2014-09-07 20:32:31 +00:00
|
|
|
a = 1,
|
|
|
|
b = {
|
|
|
|
c = 2
|
|
|
|
},
|
|
|
|
d = 3
|
2014-11-18 17:02:55 +00:00
|
|
|
}]]), inspect( {d=3, b={c=2}, a=1} ))
|
2011-04-23 22:16:36 +00:00
|
|
|
end)
|
|
|
|
|
2012-10-29 20:11:20 +00:00
|
|
|
it('works with hybrid tables', function()
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals(unindent([[
|
2014-09-07 20:32:31 +00:00
|
|
|
{ "a", {
|
|
|
|
b = 1
|
|
|
|
}, 2,
|
|
|
|
["ahoy you"] = 4,
|
|
|
|
c = 3
|
|
|
|
}
|
2014-11-18 17:02:55 +00:00
|
|
|
]]), inspect({ 'a', {b = 1}, 2, c = 3, ['ahoy you'] = 4 }))
|
2016-03-06 16:06:14 +00:00
|
|
|
|
|
|
|
|
2011-04-23 22:04:45 +00:00
|
|
|
end)
|
|
|
|
|
2013-09-15 09:23:01 +00:00
|
|
|
it('displays <table x> instead of repeating an already existing table', function()
|
|
|
|
local a = { 1, 2, 3 }
|
|
|
|
local b = { 'a', 'b', 'c', a }
|
|
|
|
a[4] = b
|
|
|
|
a[5] = a
|
|
|
|
a[6] = b
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals('<1>{ 1, 2, 3, <2>{ "a", "b", "c", <table 1> }, <table 1>, <table 2> }', inspect(a))
|
2013-09-15 09:23:01 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
describe('The depth parameter', function()
|
2011-04-23 23:02:05 +00:00
|
|
|
local level5 = { 1,2,3, a = { b = { c = { d = { e = 5 } } } } }
|
|
|
|
local keys = { [level5] = true }
|
|
|
|
|
2012-10-29 22:32:44 +00:00
|
|
|
it('has infinite depth by default', function()
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals(unindent([[
|
2014-09-07 20:32:31 +00:00
|
|
|
{ 1, 2, 3,
|
|
|
|
a = {
|
|
|
|
b = {
|
|
|
|
c = {
|
|
|
|
d = {
|
|
|
|
e = 5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-18 17:02:55 +00:00
|
|
|
]]), inspect(level5))
|
2011-04-23 23:02:05 +00:00
|
|
|
end)
|
2012-10-29 20:11:20 +00:00
|
|
|
it('is modifiable by the user', function()
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals(unindent([[
|
2014-09-07 20:32:31 +00:00
|
|
|
{ 1, 2, 3,
|
|
|
|
a = {
|
|
|
|
b = {...}
|
|
|
|
}
|
|
|
|
}
|
2014-11-18 17:02:55 +00:00
|
|
|
]]), inspect(level5, {depth = 2}))
|
2014-09-07 20:32:31 +00:00
|
|
|
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals(unindent([[
|
2014-09-07 20:32:31 +00:00
|
|
|
{ 1, 2, 3,
|
|
|
|
a = {...}
|
|
|
|
}
|
2014-11-18 17:02:55 +00:00
|
|
|
]]), inspect(level5, {depth = 1}))
|
2014-09-07 20:32:31 +00:00
|
|
|
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals(unindent([[
|
2014-09-07 20:32:31 +00:00
|
|
|
{ 1, 2, 3,
|
|
|
|
a = {
|
|
|
|
b = {
|
|
|
|
c = {
|
|
|
|
d = {...}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-18 17:02:55 +00:00
|
|
|
]]), inspect(level5, {depth = 4}))
|
2011-04-23 22:46:43 +00:00
|
|
|
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals("{...}", inspect(level5, {depth = 0}))
|
2011-04-23 23:02:05 +00:00
|
|
|
end)
|
2011-04-23 22:46:43 +00:00
|
|
|
|
2012-10-29 20:11:20 +00:00
|
|
|
it('respects depth on keys', function()
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals(unindent([[
|
2014-09-07 20:32:31 +00:00
|
|
|
{
|
|
|
|
[{ 1, 2, 3,
|
|
|
|
a = {
|
|
|
|
b = {
|
|
|
|
c = {...}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}] = true
|
|
|
|
}
|
2014-11-18 17:02:55 +00:00
|
|
|
]]), inspect(keys, {depth = 4}))
|
2011-04-23 23:02:05 +00:00
|
|
|
end)
|
2011-04-23 22:43:55 +00:00
|
|
|
end)
|
|
|
|
|
2014-09-13 10:33:20 +00:00
|
|
|
describe('the newline option', function()
|
|
|
|
it('changes the substring used for newlines', function()
|
|
|
|
local t = {a={b=1}}
|
|
|
|
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equal("{@ a = {@ b = 1@ }@}", inspect(t, {newline='@'}))
|
2014-09-13 10:33:20 +00:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('the indent option', function()
|
|
|
|
it('changes the substring used for indenting', function()
|
|
|
|
local t = {a={b=1}}
|
|
|
|
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equal("{\n>>>a = {\n>>>>>>b = 1\n>>>}\n}", inspect(t, {indent='>>>'}))
|
2014-09-13 10:33:20 +00:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2014-07-06 16:44:31 +00:00
|
|
|
describe('the process option', function()
|
|
|
|
|
2014-08-16 14:50:29 +00:00
|
|
|
it('removes one element', function()
|
2014-07-06 16:44:31 +00:00
|
|
|
local names = {'Andrew', 'Peter', 'Ann' }
|
|
|
|
local removeAnn = function(item) if item ~= 'Ann' then return item end end
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals('{ "Andrew", "Peter" }', inspect(names, {process = removeAnn}))
|
2014-07-06 16:44:31 +00:00
|
|
|
end)
|
|
|
|
|
2014-08-16 14:50:29 +00:00
|
|
|
it('uses the path', function()
|
2014-07-06 16:44:31 +00:00
|
|
|
local names = {'Andrew', 'Peter', 'Ann' }
|
|
|
|
local removeThird = function(item, path) if path[1] ~= 3 then return item end end
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals('{ "Andrew", "Peter" }', inspect(names, {process = removeThird}))
|
2014-07-06 16:44:31 +00:00
|
|
|
end)
|
|
|
|
|
2014-08-16 14:50:29 +00:00
|
|
|
it('replaces items', function()
|
2014-07-06 16:44:31 +00:00
|
|
|
local names = {'Andrew', 'Peter', 'Ann' }
|
|
|
|
local filterAnn = function(item) return item == 'Ann' and '<filtered>' or item end
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals('{ "Andrew", "Peter", "<filtered>" }', inspect(names, {process = filterAnn}))
|
2014-07-06 16:44:31 +00:00
|
|
|
end)
|
|
|
|
|
2014-08-16 14:50:29 +00:00
|
|
|
it('nullifies metatables', function()
|
2014-07-06 16:44:31 +00:00
|
|
|
local mt = {'world'}
|
|
|
|
local t = setmetatable({'hello'}, mt)
|
|
|
|
local removeMt = function(item) if item ~= mt then return item end end
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals('{ "hello" }', inspect(t, {process = removeMt}))
|
2014-07-06 16:44:31 +00:00
|
|
|
end)
|
|
|
|
|
2014-08-16 14:50:29 +00:00
|
|
|
it('nullifies metatables using their paths', function()
|
2014-07-06 16:44:31 +00:00
|
|
|
local mt = {'world'}
|
|
|
|
local t = setmetatable({'hello'}, mt)
|
2014-09-13 10:12:41 +00:00
|
|
|
local removeMt = function(item, path) if path[#path] ~= inspect.METATABLE then return item end end
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals('{ "hello" }', inspect(t, {process = removeMt}))
|
2014-07-06 16:44:31 +00:00
|
|
|
end)
|
|
|
|
|
2014-08-16 14:50:29 +00:00
|
|
|
it('nullifies the root object', function()
|
2014-07-06 16:44:31 +00:00
|
|
|
local names = {'Andrew', 'Peter', 'Ann' }
|
|
|
|
local removeNames = function(item) if item ~= names then return item end end
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals('nil', inspect(names, {process = removeNames}))
|
2014-07-06 16:44:31 +00:00
|
|
|
end)
|
|
|
|
|
2014-08-16 17:44:35 +00:00
|
|
|
it('changes keys', function()
|
|
|
|
local dict = {a = 1}
|
2014-09-19 20:54:35 +00:00
|
|
|
local changeKey = function(item) return item == 'a' and 'x' or item end
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals('{\n x = 1\n}', inspect(dict, {process = changeKey}))
|
2014-08-16 17:44:35 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('nullifies keys', function()
|
|
|
|
local dict = {a = 1, b = 2}
|
2014-09-19 20:54:35 +00:00
|
|
|
local removeA = function(item) return item ~= 'a' and item or nil end
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals('{\n b = 2\n}', inspect(dict, {process = removeA}))
|
2014-08-16 17:44:35 +00:00
|
|
|
end)
|
|
|
|
|
2014-09-13 10:12:41 +00:00
|
|
|
it('prints inspect.KEY & inspect.METATABLE', function()
|
|
|
|
local t = {inspect.KEY, inspect.METATABLE}
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals("{ inspect.KEY, inspect.METATABLE }", inspect(t))
|
2014-09-13 10:12:41 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('marks key paths with inspect.KEY and metatables with inspect.METATABLE', function()
|
2014-09-12 13:36:38 +00:00
|
|
|
local t = { [{a=1}] = setmetatable({b=2}, {c=3}) }
|
|
|
|
|
|
|
|
local items = {}
|
|
|
|
local addItem = function(item, path)
|
|
|
|
items[#items + 1] = {item = item, path = path}
|
|
|
|
return item
|
|
|
|
end
|
|
|
|
|
|
|
|
inspect(t, {process = addItem})
|
|
|
|
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.same({
|
2014-09-12 13:36:38 +00:00
|
|
|
{item = t, path = {}},
|
2014-09-13 10:12:41 +00:00
|
|
|
{item = {a=1}, path = {{a=1}, inspect.KEY}},
|
|
|
|
{item = 'a', path = {{a=1}, inspect.KEY, 'a', inspect.KEY}},
|
|
|
|
{item = 1, path = {{a=1}, inspect.KEY, 'a'}},
|
2014-09-12 13:36:38 +00:00
|
|
|
{item = setmetatable({b=2}, {c=3}), path = {{a=1}}},
|
2014-09-13 10:12:41 +00:00
|
|
|
{item = 'b', path = {{a=1}, 'b', inspect.KEY}},
|
2014-09-12 13:36:38 +00:00
|
|
|
{item = 2, path = {{a=1}, 'b'}},
|
2014-09-13 10:12:41 +00:00
|
|
|
{item = {c=3}, path = {{a=1}, inspect.METATABLE}},
|
|
|
|
{item = 'c', path = {{a=1}, inspect.METATABLE, 'c', inspect.KEY}},
|
|
|
|
{item = 3, path = {{a=1}, inspect.METATABLE, 'c'}}
|
2014-11-18 17:02:55 +00:00
|
|
|
}, items)
|
2014-08-16 17:44:35 +00:00
|
|
|
|
|
|
|
end)
|
2014-07-06 16:44:31 +00:00
|
|
|
end)
|
|
|
|
|
2013-01-20 16:07:41 +00:00
|
|
|
describe('metatables', function()
|
2011-04-24 00:09:24 +00:00
|
|
|
|
2012-10-29 20:11:20 +00:00
|
|
|
it('includes the metatable as an extra hash attribute', function()
|
2011-04-24 00:09:24 +00:00
|
|
|
local foo = { foo = 1, __mode = 'v' }
|
|
|
|
local bar = setmetatable({a = 1}, foo)
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals(unindent([[
|
2014-09-07 20:32:31 +00:00
|
|
|
{
|
|
|
|
a = 1,
|
|
|
|
<metatable> = {
|
|
|
|
__mode = "v",
|
|
|
|
foo = 1
|
|
|
|
}
|
|
|
|
}
|
2014-11-18 17:02:55 +00:00
|
|
|
]]), inspect(bar))
|
2011-04-24 00:09:24 +00:00
|
|
|
end)
|
2012-10-29 20:11:20 +00:00
|
|
|
|
|
|
|
it('includes the __tostring metamethod if it exists', function()
|
2011-04-24 00:09:24 +00:00
|
|
|
local foo = { foo = 1, __tostring = function() return 'hello\nworld' end }
|
|
|
|
local bar = setmetatable({a = 1}, foo)
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals(unindent([[
|
2014-09-07 20:32:31 +00:00
|
|
|
{ -- hello\nworld
|
|
|
|
a = 1,
|
|
|
|
<metatable> = {
|
|
|
|
__tostring = <function 1>,
|
|
|
|
foo = 1
|
|
|
|
}
|
|
|
|
}
|
2014-11-18 17:02:55 +00:00
|
|
|
]]), inspect(bar))
|
2011-04-24 00:09:24 +00:00
|
|
|
end)
|
|
|
|
|
2012-10-29 20:11:20 +00:00
|
|
|
it('includes an error string if __tostring metamethod throws an error', function()
|
2011-08-12 22:52:55 +00:00
|
|
|
local foo = { foo = 1, __tostring = function() error('hello', 0) end }
|
|
|
|
local bar = setmetatable({a = 1}, foo)
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals(unindent([[
|
2014-09-07 20:32:31 +00:00
|
|
|
{ -- error: hello
|
|
|
|
a = 1,
|
|
|
|
<metatable> = {
|
|
|
|
__tostring = <function 1>,
|
|
|
|
foo = 1
|
|
|
|
}
|
|
|
|
}
|
2014-11-18 17:02:55 +00:00
|
|
|
]]), inspect(bar))
|
2011-08-12 22:52:55 +00:00
|
|
|
end)
|
2013-01-20 16:23:36 +00:00
|
|
|
|
2015-11-27 11:04:02 +00:00
|
|
|
it('does not allow collecting weak tables while they are being inspected', function()
|
|
|
|
collectgarbage('stop')
|
|
|
|
finally(function() collectgarbage('restart') end)
|
|
|
|
local shimMetatable = {
|
|
|
|
__mode = 'v',
|
|
|
|
__index = function() return {} end,
|
|
|
|
__tostring = function() collectgarbage() return 'shim' end
|
|
|
|
}
|
|
|
|
local function shim() return setmetatable({}, shimMetatable) end
|
|
|
|
local t = shim()
|
|
|
|
t.key = shim()
|
|
|
|
assert.equals(unindent([[
|
|
|
|
{ -- shim
|
|
|
|
key = { -- shim
|
|
|
|
<metatable> = <1>{
|
|
|
|
__index = <function 1>,
|
|
|
|
__mode = "v",
|
|
|
|
__tostring = <function 2>
|
|
|
|
}
|
|
|
|
},
|
|
|
|
<metatable> = <table 1>
|
|
|
|
}
|
|
|
|
]]), inspect(t))
|
|
|
|
end)
|
|
|
|
|
2013-01-20 23:49:22 +00:00
|
|
|
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)
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals(unindent([[
|
2014-09-07 20:32:31 +00:00
|
|
|
<1>{
|
|
|
|
<metatable> = <table 1>
|
|
|
|
}
|
2014-11-18 17:02:55 +00:00
|
|
|
]]), inspect(x))
|
2013-01-20 23:49:22 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('can invoke the __tostring method without stack overflowing', function()
|
|
|
|
local t = {}
|
|
|
|
t.__index = t
|
|
|
|
setmetatable(t,t)
|
2014-11-18 17:02:55 +00:00
|
|
|
assert.equals(unindent([[
|
2014-09-07 20:32:31 +00:00
|
|
|
<1>{
|
|
|
|
__index = <table 1>,
|
|
|
|
<metatable> = <table 1>
|
|
|
|
}
|
2014-11-18 17:02:55 +00:00
|
|
|
]]), inspect(t))
|
2013-01-20 23:49:22 +00:00
|
|
|
end)
|
|
|
|
end)
|
2011-04-23 23:39:05 +00:00
|
|
|
end)
|
2011-04-23 18:17:43 +00:00
|
|
|
end)
|
2011-04-20 23:11:42 +00:00
|
|
|
end)
|