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 -------------------------------------------------------------------------------- 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\'', } -------------------------------------------------------------------------------- -- 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