2011-04-20 23:11:42 +00:00
|
|
|
local inspect = require 'inspect'
|
|
|
|
|
|
|
|
context( 'inspect', function()
|
|
|
|
|
2011-04-23 19:33:03 +00:00
|
|
|
context('numbers', function()
|
|
|
|
test('Should work with integers', function()
|
|
|
|
assert_equal(inspect(1), "1")
|
|
|
|
end)
|
|
|
|
|
|
|
|
test('Should work with decimals', function()
|
|
|
|
assert_equal(inspect(1.5), "1.5")
|
|
|
|
end)
|
|
|
|
|
|
|
|
test('Should work with negative numbers', function()
|
|
|
|
assert_equal(inspect(-3.14), "-3.14")
|
|
|
|
end)
|
2011-04-23 18:17:43 +00:00
|
|
|
end)
|
|
|
|
|
2011-04-23 19:33:03 +00:00
|
|
|
context('strings', function()
|
|
|
|
test('Should put quotes around regular strings', function()
|
|
|
|
assert_equal(inspect("hello"), '"hello"')
|
|
|
|
end)
|
|
|
|
|
|
|
|
test('Should put apostrophes around strings with quotes', function()
|
|
|
|
assert_equal(inspect('I have "quotes"'), "'I have \"quotes\"'")
|
|
|
|
end)
|
|
|
|
|
|
|
|
test('Should use regular quotes if the string has both quotes and apostrophes', function()
|
2011-04-23 19:00:41 +00:00
|
|
|
assert_equal(inspect("I have \"quotes\" and 'apostrophes'"), '"I have \\"quotes\\" and \'apostrophes\'"')
|
2011-04-23 19:33:03 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
test('Should escape escape control characters', function()
|
|
|
|
assert_equal(inspect('I have \n new \n lines'), '"I have \\\\n new \\\\n lines"')
|
|
|
|
assert_equal(inspect('I have \b a back space'), '"I have \\\\b a back space"')
|
|
|
|
end)
|
2011-04-23 19:00:41 +00:00
|
|
|
end)
|
|
|
|
|
2011-04-23 19:33:03 +00:00
|
|
|
test('Should work with functions', function()
|
|
|
|
assert_equal(inspect(print), '<function>')
|
2011-04-23 17:48:43 +00:00
|
|
|
end)
|
2011-04-20 23:11:42 +00:00
|
|
|
|
2011-04-23 19:33:03 +00:00
|
|
|
test('Should work with booleans', function()
|
|
|
|
assert_equal(inspect(true), 'true')
|
|
|
|
assert_equal(inspect(false), 'false')
|
|
|
|
end)
|
|
|
|
|
|
|
|
context('tables', function()
|
|
|
|
|
|
|
|
test('Should work with simple array-like tables', function()
|
2011-04-23 22:43:55 +00:00
|
|
|
assert_equal(inspect({1,2,3}), "{ 1, 2, 3 }" )
|
2011-04-23 19:33:03 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
test('Should work with nested arrays', function()
|
2011-04-23 22:43:55 +00:00
|
|
|
assert_equal(inspect({'a','b','c', {'d','e'}, 'f'}), '{ "a", "b", "c", { "d", "e" }, "f" }' )
|
2011-04-23 21:29:52 +00:00
|
|
|
end)
|
|
|
|
|
2011-04-23 22:04:45 +00:00
|
|
|
test('Should work with simple dictionary tables', function()
|
2011-04-23 21:29:52 +00:00
|
|
|
assert_equal(inspect({a = 1, b = 2}), "{\n a = 1,\n b = 2\n}")
|
2011-04-23 19:33:03 +00:00
|
|
|
end)
|
|
|
|
|
2011-04-23 23:11:51 +00:00
|
|
|
test('Should sort keys in dictionary tables', function()
|
|
|
|
local t = { 1,2,3,
|
|
|
|
[print] = 1, ["buy more"] = 1, a = 1,
|
|
|
|
[14] = 1, [{c=2}] = 1, [true]= 1
|
|
|
|
}
|
|
|
|
assert_equal(inspect(t), [[{ 1, 2, 3,
|
|
|
|
[14] = 1,
|
|
|
|
[true] = 1,
|
|
|
|
a = 1,
|
|
|
|
["buy more"] = 1,
|
|
|
|
[{
|
|
|
|
c = 2
|
|
|
|
}] = 1,
|
|
|
|
[<function>] = 1
|
|
|
|
}]])
|
|
|
|
end)
|
|
|
|
|
2011-04-23 22:04:45 +00:00
|
|
|
test('Should work with nested dictionary tables', function()
|
2011-04-23 22:16:36 +00:00
|
|
|
assert_equal(inspect( {d=3, b={c=2}, a=1} ), [[{
|
2011-04-23 22:04:45 +00:00
|
|
|
a = 1,
|
|
|
|
b = {
|
|
|
|
c = 2
|
|
|
|
},
|
|
|
|
d = 3
|
2011-04-23 22:16:36 +00:00
|
|
|
}]])
|
|
|
|
end)
|
|
|
|
|
|
|
|
test('Should work with hybrid tables', function()
|
2011-04-23 22:43:55 +00:00
|
|
|
assert_equal(inspect({ 'a', {b = 1}, 2, c = 3, ['ahoy you'] = 4 }), [[{ "a", {
|
2011-04-23 22:16:36 +00:00
|
|
|
b = 1
|
|
|
|
}, 2,
|
|
|
|
["ahoy you"] = 4,
|
|
|
|
c = 3
|
|
|
|
}]])
|
2011-04-23 22:04:45 +00:00
|
|
|
end)
|
|
|
|
|
2011-04-23 23:02:05 +00:00
|
|
|
context('depth', function()
|
|
|
|
local level5 = { 1,2,3, a = { b = { c = { d = { e = 5 } } } } }
|
|
|
|
local keys = { [level5] = true }
|
|
|
|
|
|
|
|
test('Should have a default depth of 4', function()
|
|
|
|
assert_equal(inspect(level5), [[{ 1, 2, 3,
|
2011-04-23 22:43:55 +00:00
|
|
|
a = {
|
|
|
|
b = {
|
|
|
|
c = {
|
2011-04-23 23:02:05 +00:00
|
|
|
d = {...}
|
2011-04-23 22:43:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}]])
|
2011-04-23 23:02:05 +00:00
|
|
|
end)
|
|
|
|
test('Should be modifiable by the user', function()
|
|
|
|
assert_equal(inspect(level5, 2), [[{ 1, 2, 3,
|
2011-04-23 22:43:55 +00:00
|
|
|
a = {
|
2011-04-23 23:02:05 +00:00
|
|
|
b = {...}
|
2011-04-23 22:43:55 +00:00
|
|
|
}
|
|
|
|
}]])
|
2011-04-23 23:02:05 +00:00
|
|
|
assert_equal(inspect(level5, 1), [[{ 1, 2, 3,
|
|
|
|
a = {...}
|
|
|
|
}]])
|
|
|
|
assert_equal(inspect(level5, 0), "{...}")
|
|
|
|
assert_equal(inspect(level5, 6), [[{ 1, 2, 3,
|
2011-04-23 22:43:55 +00:00
|
|
|
a = {
|
2011-04-23 23:02:05 +00:00
|
|
|
b = {
|
|
|
|
c = {
|
|
|
|
d = {
|
|
|
|
e = 5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-23 22:43:55 +00:00
|
|
|
}
|
|
|
|
}]])
|
2011-04-23 22:46:43 +00:00
|
|
|
|
2011-04-23 23:02:05 +00:00
|
|
|
end)
|
2011-04-23 22:46:43 +00:00
|
|
|
|
2011-04-23 23:02:05 +00:00
|
|
|
test('Should respect depth on keys', function()
|
|
|
|
assert_equal(inspect(keys), [[{
|
|
|
|
[{ 1, 2, 3,
|
|
|
|
a = {
|
|
|
|
b = {
|
|
|
|
c = {...}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}] = true
|
|
|
|
}]])
|
|
|
|
end)
|
2011-04-23 22:46:43 +00:00
|
|
|
|
2011-04-23 22:43:55 +00:00
|
|
|
end)
|
|
|
|
|
2011-04-24 00:09:24 +00:00
|
|
|
context('metatables', function()
|
|
|
|
|
|
|
|
test('Should include the metatable as an extra hash attribute', function()
|
|
|
|
local foo = { foo = 1, __mode = 'v' }
|
|
|
|
local bar = setmetatable({a = 1}, foo)
|
|
|
|
assert_equal(inspect(bar), [[{
|
2011-04-23 23:39:05 +00:00
|
|
|
a = 1,
|
|
|
|
<metatable> = {
|
2011-04-24 00:09:24 +00:00
|
|
|
__mode = "v",
|
|
|
|
foo = 1
|
2011-04-23 23:39:05 +00:00
|
|
|
}
|
|
|
|
}]])
|
2011-04-24 00:09:24 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
test('Should include the __tostring metamethod if it exists', function()
|
|
|
|
local foo = { foo = 1, __tostring = function() return 'hello\nworld' end }
|
|
|
|
local bar = setmetatable({a = 1}, foo)
|
|
|
|
assert_equal(inspect(bar), [[{ -- hello\nworld
|
|
|
|
a = 1,
|
|
|
|
<metatable> = {
|
|
|
|
__tostring = <function>,
|
|
|
|
foo = 1
|
|
|
|
}
|
|
|
|
}]])
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
2011-04-23 23:39:05 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
|
2011-04-23 18:17:43 +00:00
|
|
|
end)
|
|
|
|
|
2011-04-23 22:43:55 +00:00
|
|
|
|
2011-04-20 23:11:42 +00:00
|
|
|
end)
|