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 () local actual_result = format(t.input, t.options) assert_equal(t.expect, actual_result) end, { line = debug.getinfo(2).currentline }) 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', 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', 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', 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', 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 \\', 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', 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', 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 -- TODO -------------------------------------------------------------------------------- -- 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 = 'Malformed Unicode is escaped', input = '\000\001\003\012\169\003\000\030', expect = '\'\\000\\000\\001\\003\\012\\169\\003\\000\\030\'', } -------------------------------------------------------------------------------- return SUITE