2016-12-28 23:51:07 +00:00
|
|
|
|
2017-07-17 11:36:56 +00:00
|
|
|
local SUITE = require 'TestSuite' 'pretty'
|
2016-12-28 23:51:07 +00:00
|
|
|
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-06-25 10:18:24 +00:00
|
|
|
-- Compat
|
|
|
|
if not loadstring then loadstring = load end -- Lua 5.3 compat
|
|
|
|
local HAS_ADV_GETLOCAL = not not debug.getinfo(1, 'u').nparams -- Lua 5.1 compat
|
|
|
|
local HAS_UNICODE_IDEN = not not loadstring 'local ϕ = 1; return ϕ' -- Lua 5.1 compat
|
|
|
|
local HAS_JIT_LIBRARY = type(rawget(_G, 'jit')) == 'table' -- Non-LuaJIT 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
|
2017-06-15 17:03:42 +00:00
|
|
|
SUITE:addTest(t.name or t.expect, function ()
|
2017-04-12 13:15:03 +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-12 13:15:03 +00:00
|
|
|
assert_equal(t.expect, actual_result)
|
2016-12-29 14:33:43 +00:00
|
|
|
else
|
2017-04-12 13:15:03 +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
|
2017-04-12 13:15:03 +00:00
|
|
|
end, { line = debug.getinfo(2).currentline })
|
2016-12-28 23:51:07 +00:00
|
|
|
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
|
|
|
|
|
2017-04-14 11:24:29 +00:00
|
|
|
-- TODO: Figure out a way to print userdata.
|
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 { -- 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} },
|
2017-04-14 10:19:23 +00:00
|
|
|
expect = '{ { 1, 2, 3 }, { 4, 5, 6 } }',
|
2016-12-28 23:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { a = {1, 2, 3}, b = {4, 5, 6} },
|
2017-04-14 12:01:01 +00:00
|
|
|
expect = '{ a = { 1, 2, 3 }, b = { 4, 5, 6 } }',
|
2016-12-28 23:51:07 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 11:16:34 +00:00
|
|
|
format_test {
|
|
|
|
name = 'Unicode characters can be used as string keys in tables',
|
|
|
|
input = { ['a'] = 1, ['ψ'] = 2 },
|
|
|
|
expect = '{ a = 1, ψ = 2 }',
|
|
|
|
}
|
2017-06-15 17:03:42 +00:00
|
|
|
|
2017-04-14 11:06:43 +00:00
|
|
|
format_test {
|
|
|
|
input = { [100] = 'Hi', [300] = 'Hello' },
|
|
|
|
expect = '{ [100] = \'Hi\', [300] = \'Hello\' }',
|
|
|
|
}
|
|
|
|
|
2016-12-28 23:51:07 +00:00
|
|
|
format_test {
|
|
|
|
input = { 'Hi', [300] = 'Hello' },
|
2017-04-14 11:06:43 +00:00
|
|
|
expect = '{ [1] = \'Hi\', [300] = \'Hello\' }',
|
2016-12-28 23:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { { {} } },
|
2017-04-14 10:19:23 +00:00
|
|
|
expect = '{ { {} } }',
|
2016-12-28 23:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { [{ 1, 2 }] = { 2, 1 } },
|
2017-04-14 10:19:23 +00:00
|
|
|
expect = '{ [{ 1, 2 }] = { 2, 1 } }',
|
2016-12-28 23:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { { {1, 2}, {3, 4} }, {5, 6} },
|
2017-04-14 10:19:23 +00:00
|
|
|
expect = '{ { { 1, 2 }, { 3, 4 } }, { 5, 6 } }',
|
2016-12-28 23:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 },
|
2017-04-14 10:19:23 +00:00
|
|
|
expect = '{ {...}, {...} }',
|
2016-12-28 23:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { { {1, 2}, {3, 4} }, {5, 6} },
|
|
|
|
options = { max_depth = 2 },
|
2017-04-14 10:19:23 +00:00
|
|
|
expect = '{ { {...}, {...} }, { 5, 6 } }',
|
2016-12-28 23:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { { {1, 2}, {3, 4} }, {5, 6} },
|
|
|
|
options = { max_depth = 3 },
|
2017-04-14 10:19:23 +00:00
|
|
|
expect = '{ { { 1, 2 }, { 3, 4 } }, { 5, 6 } }',
|
2016-12-28 23:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { [{ {1,2}, {3,4} }] = 'Hello World' },
|
2017-04-14 10:19:23 +00:00
|
|
|
expect = '{ [{...}] = \'Hello World\' }',
|
2016-12-28 23:51:07 +00:00
|
|
|
}
|
|
|
|
|
2016-12-29 04:34:44 +00:00
|
|
|
format_test {
|
|
|
|
input = { a = {1,2}, bcdefg = {3,4} },
|
2017-04-14 12:01:01 +00:00
|
|
|
expect = '{ a = { 1, 2 }, bcdefg = { 3, 4 } }',
|
2016-12-29 04:34:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { [true] = 1, [1] = false },
|
2017-04-14 10:19:23 +00:00
|
|
|
expect = '{ [1] = false, [true] = 1 }',
|
2016-12-29 04:34:44 +00:00
|
|
|
}
|
|
|
|
|
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 },
|
2017-04-14 10:19:23 +00:00
|
|
|
expect = '{ [1] = 1, [\'whatever\'] = false }',
|
2016-12-29 11:11:48 +00:00
|
|
|
}
|
|
|
|
|
2017-01-05 13:28:31 +00:00
|
|
|
format_test {
|
|
|
|
-- Table view, with indent.
|
|
|
|
input = { { a = 'hello', b = 'hi' }, { a = 'hi', b = 'hello' } },
|
2017-06-09 15:24:05 +00:00
|
|
|
expect = '{\n { a = \'hello\', b = \'hi\' },\n { a = \'hi\', b = \'hello\' }\n}',
|
2017-01-05 13:28:31 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 11:16:34 +00:00
|
|
|
format_test {
|
|
|
|
name = 'Proper alignment when using unicode characters as keys',
|
|
|
|
input = {
|
|
|
|
['djævle'] = 'dyr?',
|
|
|
|
['europa'] = 'måne',
|
|
|
|
['øå'] = 'en å på en ø?',
|
|
|
|
},
|
|
|
|
expect = '{\n djævle = \'dyr?\',\n europa = \'måne\',\n øå = \'en å på en ø?\'\n}',
|
|
|
|
}
|
2017-06-15 17:03:42 +00:00
|
|
|
|
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-04-12 13:15:03 +00:00
|
|
|
do
|
|
|
|
local a = {}
|
|
|
|
local b = { a }
|
|
|
|
a[1] = b
|
|
|
|
local rec = { a = a, b = b }
|
|
|
|
format_test {
|
|
|
|
name = 'Top layers should be expanded, rather than lower layers.',
|
|
|
|
input = rec,
|
|
|
|
options = { max_depth = 5 },
|
2017-06-09 15:24:05 +00:00
|
|
|
expect = '{\n a = { {...} },\n b = { {...} }\n}',
|
2017-04-12 13:15:03 +00:00
|
|
|
}
|
|
|
|
end
|
2017-01-16 15:10:10 +00:00
|
|
|
|
2016-12-29 14:33:43 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- CDATA
|
|
|
|
|
|
|
|
-- TODO: Add more advanced understanding of cdata.
|
|
|
|
|
2017-06-25 10:18:24 +00:00
|
|
|
if HAS_JIT_LIBRARY then
|
2016-12-29 14:33:43 +00:00
|
|
|
|
|
|
|
local ffi = require('ffi')
|
|
|
|
ffi.cdef[[
|
|
|
|
int poll(struct pollfd *fds, unsigned long nfds, int timeout);
|
|
|
|
]]
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = ffi.C.poll,
|
|
|
|
approx = true,
|
|
|
|
expect = 'cdata<.+>: 0x%x+',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = ffi.new('int[10]'),
|
|
|
|
approx = true,
|
|
|
|
expect = 'cdata<.+>: 0x%x+',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-07-15 21:29:51 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- General
|
|
|
|
|
|
|
|
SUITE:addTest('UseCase: Can print _G with max_depth = 1', function ()
|
|
|
|
format(_G, {max_depth = 1})
|
|
|
|
assert(true)
|
|
|
|
end)
|
|
|
|
|
|
|
|
SUITE:addTest('UseCase: Can load function from file that is shortly deleted', function ()
|
|
|
|
local module_name = 'tmp_'..os.time()
|
|
|
|
-- Create module
|
|
|
|
local file = io.open('./'..module_name..'.lua', 'w')
|
|
|
|
file:write '\nlocal function yo ()\n -- Hello World\n return math.random()\nend\n\nreturn yo\n'
|
|
|
|
file:close()
|
|
|
|
-- Load module
|
|
|
|
local yo = require(module_name)
|
|
|
|
-- Remove module
|
|
|
|
os.remove('./'..module_name..'.lua')
|
2017-07-17 11:36:56 +00:00
|
|
|
package.loaded[module_name] = nil
|
|
|
|
|
2017-07-15 21:29:51 +00:00
|
|
|
-- Format the function, even though the module it came from is gone.
|
|
|
|
format(yo)
|
|
|
|
assert(true)
|
|
|
|
end)
|
|
|
|
|
2016-12-28 23:51:07 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
return SUITE
|