2016-12-28 23:51:07 +00:00
|
|
|
|
|
|
|
local SUITE = require('TestSuite').new('pretty')
|
|
|
|
SUITE:setEnviroment{
|
|
|
|
format = require('pretty')
|
|
|
|
}
|
|
|
|
|
2016-12-29 14:33:43 +00:00
|
|
|
local ASSERT_ERROR_APPROX = [[
|
|
|
|
Approximate strings not similar enough:
|
|
|
|
Should match: %s
|
|
|
|
Gotten: %s
|
|
|
|
]]
|
|
|
|
|
2016-12-28 23:51:07 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2017-04-03 09:55:49 +00:00
|
|
|
local HAS_ADV_GETLOCAL = not not debug.getinfo(1, 'u').nparams -- Lua 5.1 compat
|
|
|
|
if not loadstring then loadstring = load end -- Lua 5.3 compat
|
2017-01-05 14:50:44 +00:00
|
|
|
|
2016-12-28 23:51:07 +00:00
|
|
|
local function format_test (t)
|
2017-01-05 14:50:44 +00:00
|
|
|
if t.longterm then return end
|
|
|
|
if t.adv_getlocal and not HAS_ADV_GETLOCAL then return end
|
2016-12-28 23:51:07 +00:00
|
|
|
SUITE:addTest(t.expect, function ()
|
2017-04-04 18:02:58 +00:00
|
|
|
local actual_result = format(t.input, t.options)
|
2016-12-29 14:33:43 +00:00
|
|
|
if not t.approx or type(actual_result) ~= 'string' then
|
2017-04-04 18:02:58 +00:00
|
|
|
assert_equal(t.expect, actual_result)
|
2016-12-29 14:33:43 +00:00
|
|
|
else
|
2017-04-04 18:02:58 +00:00
|
|
|
if not actual_result:match(t.expect) then
|
|
|
|
error(ASSERT_ERROR_APPROX:format(t.expect, actual_result))
|
2016-12-29 14:33:43 +00:00
|
|
|
end
|
|
|
|
end
|
2016-12-28 23:51:07 +00:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Strings
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = 'Hello World',
|
|
|
|
expect = '\'Hello World\'',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = 'Hello \'World\'',
|
|
|
|
expect = '\"Hello \'World\'\"',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = 'Hello \"World\"',
|
|
|
|
expect = '\'Hello \"World\"\'',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = 'Hello [[World]]',
|
|
|
|
expect = '\'Hello [[World]]\'',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = '\'Hello\' [[World]]',
|
|
|
|
expect = '\"\'Hello\' [[World]]\"',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = '\'Hello\' \"there\" [[World]]',
|
|
|
|
expect = '[=[\'Hello\' \"there\" [[World]]]=]',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = '\'Hello\' \"there\" [=[World]=]',
|
|
|
|
expect = '[[\'Hello\' \"there\" [=[World]=]]]',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = '\nHello World',
|
|
|
|
expect = '\'\\nHello World\'',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = '\'\"\n',
|
|
|
|
expect = '[[\n\'\"\n]]',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = '\n',
|
|
|
|
expect = '\'\\n\'',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = '\\',
|
|
|
|
expect = '\'\\\\\'',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = '\000',
|
|
|
|
expect = '\'\\000\'',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = '\a\b\v\r\f',
|
|
|
|
expect = '\'\\a\\b\\v\\r\\f\'',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = 'ø',
|
|
|
|
expect = '\'ø\'',
|
|
|
|
}
|
|
|
|
|
2016-12-29 14:33:43 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Primitive types
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = nil,
|
|
|
|
expect = 'nil',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = true,
|
|
|
|
expect = 'true',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = false,
|
|
|
|
expect = 'false',
|
|
|
|
}
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Userdata printing
|
|
|
|
|
2016-12-29 15:54:31 +00:00
|
|
|
-- TODO. First off figure out a way to test this stuff.
|
2016-12-29 17:40:30 +00:00
|
|
|
-- Maybe look into using the one available debug.getupvalue(pairs, 1)
|
2016-12-29 14:33:43 +00:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Thread printing
|
|
|
|
|
|
|
|
do
|
|
|
|
local suspended_coroutine = coroutine.create(function () end)
|
|
|
|
format_test {
|
|
|
|
input = suspended_coroutine,
|
|
|
|
approx = true,
|
|
|
|
expect = 'suspended coroutine: 0x%x+',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
do
|
|
|
|
local dead_coroutine = coroutine.create(function () end)
|
|
|
|
coroutine.resume(dead_coroutine)
|
|
|
|
format_test {
|
|
|
|
input = dead_coroutine,
|
|
|
|
approx = true,
|
|
|
|
expect = 'dead coroutine: 0x%x+',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-12-28 23:51:07 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Single-line tables
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = {},
|
|
|
|
expect = '{}',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = {1, 2, 3},
|
|
|
|
expect = '{ 1, 2, 3 }',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { 'Hello', 'World' },
|
|
|
|
expect = '{ \'Hello\', \'World\' }',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { a = 1, b = 2 },
|
|
|
|
expect = '{ a = 1, b = 2 }',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { __hello = true },
|
|
|
|
expect = '{ __hello = true }',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { [']]'] = true },
|
|
|
|
expect = '{ [\']]\'] = true }',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { ['and'] = true },
|
|
|
|
expect = '{ [\'and\'] = true }',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { [false] = false, [true] = true },
|
|
|
|
expect = '{ [false] = false, [true] = true }',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { [100] = 'Hi', [300] = 'Hello' },
|
|
|
|
expect = '{ [100] = \'Hi\', [300] = \'Hello\' }',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test { -- Order does not matter
|
|
|
|
input = { b = 1, a = 2 },
|
|
|
|
expect = '{ a = 2, b = 1 }',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test { -- Can include empty tables
|
|
|
|
input = { {}, {}, {} },
|
|
|
|
expect = '{ {}, {}, {} }',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test { -- Can include very small tables
|
|
|
|
input = { {1}, {2}, {3} },
|
|
|
|
expect = '{ { 1 }, { 2 }, { 3 } }',
|
|
|
|
}
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Multi-line tables
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { {1, 2, 3}, {4, 5, 6} },
|
|
|
|
expect = '{\n\t{ 1, 2, 3 },\n\t{ 4, 5, 6 }\n}',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { a = {1, 2, 3}, b = {4, 5, 6} },
|
|
|
|
expect = '{\n\ta = { 1, 2, 3 },\n\tb = { 4, 5, 6 }\n}',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { 'Hi', [300] = 'Hello' },
|
|
|
|
expect = '{\n\t[1] = \'Hi\',\n\t[300] = \'Hello\'\n}',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { { {} } },
|
|
|
|
expect = '{\n\t{ {} }\n}',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { [{ 1, 2 }] = { 2, 1 } },
|
|
|
|
expect = '{\n\t[{ 1, 2 }] = { 2, 1 }\n}',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { { {1, 2}, {3, 4} }, {5, 6} },
|
|
|
|
expect = '{\n\t{\n\t\t{ 1, 2 },\n\t\t{ 3, 4 }\n\t},\n\t{ 5, 6 }\n}',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { { {1, 2}, {3, 4} }, {5, 6} },
|
|
|
|
options = { max_depth = 0 },
|
|
|
|
expect = '{...}',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { { {1, 2}, {3, 4} }, {5, 6} },
|
|
|
|
options = { max_depth = 1 },
|
|
|
|
expect = '{\n\t{...},\n\t{...}\n}',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { { {1, 2}, {3, 4} }, {5, 6} },
|
|
|
|
options = { max_depth = 2 },
|
|
|
|
expect = '{\n\t{\n\t\t{...},\n\t\t{...}\n\t},\n\t{ 5, 6 }\n}',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { { {1, 2}, {3, 4} }, {5, 6} },
|
|
|
|
options = { max_depth = 3 },
|
|
|
|
expect = '{\n\t{\n\t\t{ 1, 2 },\n\t\t{ 3, 4 }\n\t},\n\t{ 5, 6 }\n}',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { [{ {1,2}, {3,4} }] = 'Hello World' },
|
|
|
|
expect = '{\n\t[{...}] = \'Hello World\'\n}',
|
|
|
|
}
|
|
|
|
|
2016-12-29 04:34:44 +00:00
|
|
|
format_test {
|
|
|
|
input = { a = {1,2}, bcdefg = {3,4} },
|
|
|
|
expect = '{\n\ta = { 1, 2 },\n\tbcdefg = { 3, 4 }\n}',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { [true] = 1, [1] = false },
|
|
|
|
expect = '{\n\t[true] = 1,\n\t[1] = false\n}',
|
|
|
|
}
|
|
|
|
|
2016-12-29 11:11:48 +00:00
|
|
|
format_test {
|
2017-01-05 13:28:31 +00:00
|
|
|
-- Proper indent
|
2016-12-29 11:11:48 +00:00
|
|
|
input = { [1] = 1, ['whatever'] = false },
|
|
|
|
expect = '{\n\t[1] = 1,\n\t[\'whatever\'] = false\n}',
|
|
|
|
}
|
|
|
|
|
2017-01-05 13:28:31 +00:00
|
|
|
format_test {
|
|
|
|
-- Table view, with indent.
|
|
|
|
input = { { a = 'hello', b = 'hi' }, { a = 'hi', b = 'hello' } },
|
|
|
|
expect = '{\n\t{ a = \'hello\', b = \'hi\' },\n\t{ a = \'hi\', b = \'hello\' }\n}',
|
|
|
|
}
|
|
|
|
|
2017-01-16 15:22:46 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Table recursion
|
|
|
|
|
|
|
|
do
|
|
|
|
local recursive = {}
|
|
|
|
recursive[1] = recursive
|
|
|
|
format_test {
|
|
|
|
input = recursive,
|
|
|
|
options = { max_depth = 5 },
|
|
|
|
expect = '{ {...} }',
|
|
|
|
}
|
|
|
|
format_test {
|
|
|
|
input = recursive,
|
|
|
|
options = { max_depth = 5, recursion = 'ignore' },
|
|
|
|
expect = '{ {...} }',
|
|
|
|
}
|
|
|
|
format_test {
|
|
|
|
input = recursive,
|
|
|
|
options = { max_depth = 5, recursion = 'marked' },
|
|
|
|
expect = '<1>{ <1>{...} }',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-01-16 15:10:10 +00:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Table Sorting
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { 'c', 'b', 'a' },
|
|
|
|
expect = '{ \'c\', \'b\', \'a\' }',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { a = 1, a1 = 0 },
|
|
|
|
expect = '{ a = 1, a1 = 0 }',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { a10 = 1, a1 = 0 },
|
|
|
|
expect = '{ a1 = 0, a10 = 1 }',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { a00 = 0, a1 = 1 },
|
|
|
|
expect = '{ a00 = 0, a1 = 1 }',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { a = {}, b = true },
|
|
|
|
expect = '{ b = true, a = {} }',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { a = {}, b = true, b1 = false },
|
|
|
|
expect = '{ b = true, b1 = false, a = {} }',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { {}, true, false, 5 },
|
|
|
|
expect = '{ {}, true, false, 5 }',
|
|
|
|
}
|
|
|
|
|
|
|
|
-- TODO: Add more tests for sorting.
|
|
|
|
|
2016-12-28 23:51:07 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
return SUITE
|