272 lines
5.3 KiB
Lua
272 lines
5.3 KiB
Lua
|
|
local SUITE = require('TestSuite').new('pretty')
|
|
SUITE:setEnviroment{
|
|
format = require('pretty')
|
|
}
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
local function format_test (t)
|
|
SUITE:addTest(t.expect, function ()
|
|
local input_value = t.input
|
|
local input_options = t.options
|
|
local expected_result = t.expect
|
|
local actual_result = format(input_value, input_options)
|
|
assert_equal(actual_result, expected_result)
|
|
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 = '\'ø\'',
|
|
}
|
|
|
|
--------------------------------------------------------------------------------
|
|
-- Numbers
|
|
|
|
format_test {
|
|
input = 0,
|
|
expect = '0',
|
|
}
|
|
|
|
format_test {
|
|
input = 2000,
|
|
expect = '2000',
|
|
}
|
|
|
|
format_test {
|
|
input = -2000,
|
|
expect = '-2000',
|
|
}
|
|
|
|
format_test {
|
|
input = 1/0, -- Same as math.huge
|
|
expect = '1/0',
|
|
}
|
|
|
|
format_test {
|
|
input = -1/0, -- Same as -math.huge
|
|
expect = '-1/0',
|
|
}
|
|
|
|
format_test {
|
|
input = 0/0, -- Same as nan
|
|
expect = '0/0',
|
|
}
|
|
|
|
format_test {
|
|
input = 1/0, -- Same as math.huge
|
|
options = { math_shorthand = true },
|
|
expect = 'inf',
|
|
}
|
|
|
|
format_test {
|
|
input = -1/0, -- Same as -math.huge
|
|
options = { math_shorthand = true },
|
|
expect = '-inf',
|
|
}
|
|
|
|
format_test {
|
|
input = 0/0, -- Same as nan
|
|
options = { math_shorthand = true },
|
|
expect = 'nan',
|
|
}
|
|
|
|
--------------------------------------------------------------------------------
|
|
-- 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}',
|
|
}
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
return SUITE
|