2017-07-20 17:20:29 +00:00
local SUITE = require ' TestSuite ' ' string '
2017-10-22 09:59:55 +00:00
SUITE : setEnvironment {
2017-07-20 17:20:29 +00:00
format = require ( ' pretty ' )
}
--------------------------------------------------------------------------------
-- Compat
2017-10-21 10:53:32 +00:00
if not loadstring then loadstring = load end -- Lua 5.3 compat
2017-07-20 17:20:29 +00:00
--
local function format_test ( t )
2017-10-21 10:53:32 +00:00
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 ,
} } )
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 \' ' ,
}
2017-10-21 10:53:32 +00:00
--------------------------------------------------------------------------------
do
local data = { }
for str , name in SUITE : getExamples ' STRING_UTF8 ' do
data [ # data + 1 ] = { name , str }
2017-07-31 14:29:56 +00:00
end
2017-10-21 10:53:32 +00:00
SUITE : addTest ( ' UTF8 Idempotency ' , function ( _ , str )
assert_equal ( str , loadstring ( ' return ' .. format ( str ) ) ( ) )
end , { data = data } )
end
2017-07-21 14:44:46 +00:00
2017-07-20 17:20:29 +00:00
--------------------------------------------------------------------------------
return SUITE