From bfbfe4de56da2187be7ec87051b530c3ccbbb6c3 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Fri, 21 Jul 2017 13:55:50 +0200 Subject: [PATCH] Added additional tests for parseability and idempotency. --- test/test_pstring.lua | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/test/test_pstring.lua b/test/test_pstring.lua index 8272b66..3c2b8d8 100644 --- a/test/test_pstring.lua +++ b/test/test_pstring.lua @@ -12,9 +12,21 @@ if not loadstring then loadstring = load end -- Lua 5.3 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) + assert_equal(t.expect, format(t.input, t.options)) end, { line = debug.getinfo(2).currentline }) + + if not t.not_idempotent then + -- Additional parsable test + SUITE:addTest((t.name or t.expect) .. ' (parsable?)', function () + local chunk, error_msg = loadstring('return '..format(t.input, t.options)) + if not chunk then error(error_msg) end + end, { line = debug.getinfo(2).currentline }) + -- Additional idempotent test + SUITE:addTest((t.name or t.expect) .. ' (idempotent?)', function () + local output = loadstring('return '..format(t.input, t.options))() + assert_equal(t.input, output) + end, { line = debug.getinfo(2).currentline }) + end end -------------------------------------------------------------------------------- @@ -90,36 +102,42 @@ format_test { 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}', } @@ -127,6 +145,7 @@ format_test { 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}', }