2017-07-20 17:20:29 +00:00
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 ( )
2017-07-21 11:55:50 +00:00
assert_equal ( t.expect , format ( t.input , t.options ) )
2017-07-20 17:20:29 +00:00
end , { line = debug.getinfo ( 2 ) . currentline } )
2017-07-21 11:55:50 +00:00
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
2017-07-20 17:20:29 +00:00
end
--------------------------------------------------------------------------------
2017-07-21 11:15:04 +00:00
-- Shortform Strings
2017-07-20 17:20:29 +00:00
format_test {
input = ' Hello World ' ,
expect = ' \' Hello World \' ' ,
}
format_test {
input = ' Hello \' World \' ' ,
2017-07-21 11:15:04 +00:00
expect = ' \' Hello \\ \' World \\ \' \' ' ,
2017-07-20 17:20:29 +00:00
}
format_test {
input = ' Hello \" World \" ' ,
expect = ' \' Hello \" World \" \' ' ,
}
format_test {
input = ' Hello [[World]] ' ,
expect = ' \' Hello [[World]] \' ' ,
}
format_test {
input = ' \' Hello \' [[World]] ' ,
2017-07-21 11:15:04 +00:00
expect = ' \' \\ \' Hello \\ \' [[World]] \' ' ,
2017-07-20 17:20:29 +00:00
}
format_test {
input = ' \' Hello \' \" there \" [[World]] ' ,
2017-07-21 11:15:04 +00:00
expect = ' \' \\ \' Hello \\ \' \" there \" [[World]] \' ' ,
2017-07-20 17:20:29 +00:00
}
format_test {
input = ' \' Hello \' \" there \" [=[World]=] ' ,
2017-07-21 11:15:04 +00:00
expect = ' \' \\ \' Hello \\ \' \" there \" [=[World]=] \' ' ,
2017-07-20 17:20:29 +00:00
}
format_test {
input = ' \n Hello World ' ,
2017-07-21 11:15:04 +00:00
expect = [['\nHello World']] ,
2017-07-20 17:20:29 +00:00
}
format_test {
input = ' \' \" \n ' ,
2017-07-21 11:15:04 +00:00
expect = [['\'"\n']] ,
2017-07-20 17:20:29 +00:00
}
format_test {
input = ' \n ' ,
expect = ' \' \\ n \' ' ,
}
format_test {
input = ' \\ ' ,
2017-07-21 11:15:04 +00:00
expect = [['\\']] ,
2017-07-20 17:20:29 +00:00
}
format_test {
input = ' \000 ' ,
expect = ' \' \\ 000 \' ' ,
}
2017-07-21 11:15:04 +00:00
2017-07-20 17:20:29 +00:00
format_test {
input = ' \a \b \v \r \f ' ,
2017-07-21 11:15:04 +00:00
expect = [['\a\b\v\r\f']] ,
}
--------------------------------------------------------------------------------
-- Cut Strings
format_test {
name = ' Cut string basics ' ,
2017-07-21 11:55:50 +00:00
not_idempotent = true ,
2017-07-21 11:15:04 +00:00
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 ' ,
2017-07-21 11:55:50 +00:00
not_idempotent = true ,
2017-07-21 11:15:04 +00:00
input = { ' Lorem \t ipsum \t dolor \t sit \t amet, \t consectetur \t adipiscing \t elit. \t Nunc \t vestibulum \t tempus \t ligula. \t Sed \t ac \t lobortis \t mi. ' } ,
expect = ' { \n \' Lorem \\ tipsum \\ tdolor \\ tsit \\ tamet, \\ tcons \' ... \n } ' ,
}
format_test {
name = ' Cut strings are cut after escaping 2 ' ,
2017-07-21 11:55:50 +00:00
not_idempotent = true ,
2017-07-21 11:15:04 +00:00
input = { ' Lorem ipsum dolor sit amet, conse \t \t \t ctetur 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 ' ,
2017-07-21 11:55:50 +00:00
not_idempotent = true ,
2017-07-21 11:15:04 +00:00
input = { ' Lorem ipsum dolor sit amet, consec \t \t \t tetur 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 \\ ' ,
2017-07-21 11:55:50 +00:00
not_idempotent = true ,
2017-07-21 11:15:04 +00:00
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 ' ,
2017-07-21 11:55:50 +00:00
not_idempotent = true ,
2017-07-21 11:15:04 +00:00
input = { ' Lorem ipsum dolor sit amet, consect \014 etur 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 ' ,
2017-07-21 11:55:50 +00:00
not_idempotent = true ,
2017-07-21 11:15:04 +00:00
input = { ' Lorem ipsum dolor sit amet, consec \014 tetur adipiscing elit. Nunc vestibulum tempus ligula. Sed ac lobortis mi. ' } ,
expect = ' { \n \' Lorem ipsum dolor sit amet, consec \\ 14 \' ... \n } ' ,
}
--------------------------------------------------------------------------------
-- Concatted Strings
2017-07-21 11:46:46 +00:00
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 \000 Nunc 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.']] ,
}
2017-07-21 11:15:04 +00:00
--------------------------------------------------------------------------------
-- 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 .. ' ]] '
2017-07-20 17:20:29 +00:00
}
--------------------------------------------------------------------------------
-- Unicode
format_test {
input = ' ø ' ,
expect = ' \' ø \' ' ,
}
format_test {
2017-07-21 14:44:46 +00:00
name = ' Single utf8 continuation byte is escaped ' ,
input = ' abc \169 def ' ,
expect = ' \' abc \\ 169def \' ' ,
2017-07-20 17:20:29 +00:00
}
2017-07-21 14:44:46 +00:00
format_test {
name = ' Multiple utf8 continuation bytes are escaped ' ,
input = ' abc \169 \190 \169 \169 def ' ,
expect = ' \' abc \\ 169 \\ 190 \\ 169 \\ 169def \' ' ,
}
format_test {
name = ' Single start byte utf8 chars is escaped ' ,
input = ' abc \255 def ' ,
expect = ' \' abc \\ 255def \' ' ,
}
-- TODO: Add more malformed unicode tests: https://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
2017-07-20 17:20:29 +00:00
--------------------------------------------------------------------------------
return SUITE