263 lines
8.1 KiB
Lua
263 lines
8.1 KiB
Lua
|
|
local SUITE = require 'TestSuite' 'string'
|
|
SUITE:setEnviroment{
|
|
format = require('pretty')
|
|
}
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Compat
|
|
if not loadstring then loadstring = load end -- Lua 5.3 compat
|
|
--
|
|
|
|
local function format_test (t)
|
|
SUITE:addTest(t.name or t.expect, function (mode)
|
|
local fmt = format(t.input, t.options)
|
|
--
|
|
assert_equal(t.expect, fmt)
|
|
if mode == 'format' then return end
|
|
--
|
|
local chunk, load_error_msg = loadstring('return '..fmt)
|
|
if not chunk then error(load_error_msg) end
|
|
if mode == 'parse' then return end
|
|
--
|
|
local output = chunk()
|
|
assert_equal(t.input, output)
|
|
|
|
end, { line = debug.getinfo(2).currentline, data = {
|
|
'format',
|
|
not t.not_idempotent and 'parse' or nil,
|
|
not t.not_idempotent and 'idempotent' or nil,
|
|
}})
|
|
end
|
|
|
|
--------------------------------------------------------------------------------
|
|
-- Shortform 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']],
|
|
}
|
|
|
|
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']],
|
|
}
|
|
|
|
--------------------------------------------------------------------------------
|
|
-- Cut Strings
|
|
|
|
format_test {
|
|
name = 'Cut string basics',
|
|
not_idempotent = true,
|
|
input = {'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc vestibulum tempus ligula. Sed ac lobortis mi.'},
|
|
expect = '{\n \'Lorem ipsum dolor sit amet, consectet\'...\n}',
|
|
}
|
|
|
|
format_test {
|
|
name = 'Cut strings are cut after escaping',
|
|
not_idempotent = true,
|
|
input = {'Lorem\tipsum\tdolor\tsit\tamet,\tconsectetur\tadipiscing\telit.\tNunc\tvestibulum\ttempus\tligula.\tSed\tac\tlobortis\tmi.'},
|
|
expect = '{\n \'Lorem\\tipsum\\tdolor\\tsit\\tamet,\\tcons\'...\n}',
|
|
}
|
|
|
|
format_test {
|
|
name = 'Cut strings are cut after escaping 2',
|
|
not_idempotent = true,
|
|
input = {'Lorem ipsum dolor sit amet, conse\t\t\tctetur adipiscing elit. Nunc vestibulum tempus ligula. Sed ac lobortis mi.'},
|
|
expect = '{\n \'Lorem ipsum dolor sit amet, conse\\t\\t\'...\n}',
|
|
}
|
|
|
|
format_test {
|
|
name = 'Cut strings are not cut in the middle of an escape code',
|
|
not_idempotent = true,
|
|
input = {'Lorem ipsum dolor sit amet, consec\t\t\ttetur adipiscing elit. Nunc vestibulum tempus ligula. Sed ac lobortis mi.'},
|
|
expect = '{\n \'Lorem ipsum dolor sit amet, consec\\t\'...\n}',
|
|
}
|
|
|
|
format_test {
|
|
name = 'Cut strings are not cut in the middle of escaping \\',
|
|
not_idempotent = true,
|
|
input = {'Lorem ipsum dolor sit amet, conse\\\\\\\\ctetur adipiscing elit. Nunc vestibulum tempus ligula. Sed ac lobortis mi.'},
|
|
expect = '{\n \'Lorem ipsum dolor sit amet, conse\\\\\\\\\'...\n}',
|
|
}
|
|
|
|
format_test {
|
|
name = 'Cut strings are not cut in the middle of decimal escape codes',
|
|
not_idempotent = true,
|
|
input = {'Lorem ipsum dolor sit amet, consect\014etur adipiscing elit. Nunc vestibulum tempus ligula. Sed ac lobortis mi.'},
|
|
expect = '{\n \'Lorem ipsum dolor sit amet, consect\'...\n}',
|
|
}
|
|
|
|
format_test {
|
|
-- NOTE: Not priority functionallity.
|
|
name = 'Cut strings can shorten decimal escape codes, if nessesary and possible',
|
|
not_idempotent = true,
|
|
input = {'Lorem ipsum dolor sit amet, consec\014tetur adipiscing elit. Nunc vestibulum tempus ligula. Sed ac lobortis mi.'},
|
|
expect = '{\n \'Lorem ipsum dolor sit amet, consec\\14\'...\n}',
|
|
}
|
|
|
|
--------------------------------------------------------------------------------
|
|
-- Concatted Strings
|
|
|
|
format_test {
|
|
name = 'Concatted string basics',
|
|
input = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc vestibulum tempus ligula. Sed \004\002\000 ac lobortis mi.',
|
|
expect = [['Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc vestibulum te' ..]]..'\n'..[['mpus ligula. Sed \004\002\000 ac lobortis mi.']],
|
|
}
|
|
|
|
format_test {
|
|
name = 'Concatted string basics 2',
|
|
input = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.\004\002\000Nunc vestibulum tempus ligula. Sed ac lobortis mi.',
|
|
expect = [['Lorem ipsum dolor sit amet, consectetur adipiscing elit.\004\002\000Nunc ve' ..]]..'\n'..[['stibulum tempus ligula. Sed ac lobortis mi.']],
|
|
}
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
-- Longform Strings
|
|
|
|
local LONG_STRING = [[
|
|
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
|
|
Nunc vestibulum tempus ligula. Sed ac lobortis mi.
|
|
Morbi eu arcu id nunc cursus auctor. Nulla enim tortor, sodales ut nunc non,
|
|
euismod aliquam libero. Aliquam neque est, iaculis in nibh vel, mollis ultricies
|
|
ante. Sed egestas et massa sit amet posuere. Integer at suscipit lorem, non
|
|
consectetur lacus. Vivamus ac facilisis sem. Proin lacinia ex eu volutpat
|
|
interdum.
|
|
]]
|
|
|
|
format_test {
|
|
name = 'Longform string basics',
|
|
input = LONG_STRING,
|
|
expect = '[[\n'..LONG_STRING..']]'
|
|
}
|
|
|
|
local LONG_STRING_WITH_LEVELS = [=[
|
|
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc vestibulum tempus
|
|
ligula. Sed ac lobortis mi. [Morbi eu arcu id nunc cursus auctor. [Nulla enim
|
|
tortor, sodales ut nunc non, euismod aliquam libero.]] Aliquam neque est,
|
|
iaculis in nibh vel, mollis ultricies ante. Sed egestas et massa sit amet
|
|
posuere. Integer at suscipit lorem, non consectetur lacus. Vivamus ac facilisis
|
|
sem. Proin lacinia ex eu volutpat interdum.
|
|
]=]
|
|
|
|
format_test {
|
|
name = 'Longform string that requires level 1',
|
|
input = LONG_STRING_WITH_LEVELS,
|
|
expect = '[=[\n'..LONG_STRING_WITH_LEVELS..']=]'
|
|
}
|
|
|
|
local LONG_STRING_WITH_HIGH_LEVELS = [[
|
|
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc vestibulum tempus
|
|
ligula. Sed ac lobortis mi. [=[Morbi eu arcu id nunc cursus auctor. [Nulla enim
|
|
tortor, sodales ut nunc non, euismod aliquam libero.]=] Aliquam neque est,
|
|
iaculis in nibh vel, mollis ultricies ante. Sed egestas et massa sit amet
|
|
posuere. Integer at suscipit lorem, non consectetur lacus. Vivamus ac facilisis
|
|
sem. Proin lacinia ex eu volutpat interdum.
|
|
]]
|
|
|
|
format_test {
|
|
name = 'Longform string that requires level 0, but not 1',
|
|
input = LONG_STRING_WITH_HIGH_LEVELS,
|
|
expect = '[[\n'..LONG_STRING_WITH_HIGH_LEVELS..']]'
|
|
}
|
|
|
|
--------------------------------------------------------------------------------
|
|
-- Unicode
|
|
|
|
format_test {
|
|
input = 'ø',
|
|
expect = '\'ø\'',
|
|
}
|
|
|
|
format_test {
|
|
name = 'Single utf8 continuation byte is escaped',
|
|
input = 'abc\169def',
|
|
expect = '\'abc\\169def\'',
|
|
}
|
|
|
|
format_test {
|
|
name = 'Multiple utf8 continuation bytes are escaped',
|
|
input = 'abc\169\190\169\169def',
|
|
expect = '\'abc\\169\\190\\169\\169def\'',
|
|
}
|
|
|
|
format_test {
|
|
name = 'Single start byte utf8 chars is escaped',
|
|
input = 'abc\255def',
|
|
expect = '\'abc\\255def\'',
|
|
}
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
do
|
|
local data = {}
|
|
for str, name in SUITE:getExamples 'STRING_UTF8' do
|
|
data[#data+1] = { name, str }
|
|
end
|
|
|
|
SUITE:addTest('UTF8 Idempotency', function (_, str)
|
|
assert_equal(str, loadstring('return '..format(str))() )
|
|
end, { data = data })
|
|
end
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
return SUITE
|