2013-11-03 21:50:05 +00:00
local serialize = require ' ser '
local succeeded = 0
local failed = 0
function case ( input , expected , message )
local output = serialize ( input )
if output == expected then
succeeded = succeeded + 1
else
print ( ' test failed: ' .. message )
print ( ' expected: ' )
print ( expected )
print ( ' got: ' )
print ( output )
failed = failed + 1
end
end
function case_error ( input , expected , message )
local success , err = pcall ( serialize , input )
if not success and err == expected then
succeeded = succeeded + 1
else
print ( ' test failed: ' .. message )
print ( ' expected error: ' )
print ( expected )
print ( ' got: ' )
print ( success , err )
failed = failed + 1
end
end
case ( { } , ' return {} ' , ' empty table ' )
case ( { true } , ' return {true} ' , ' simple table ' )
case ( { { } } , [ [
2015-05-20 00:13:34 +00:00
local _ = { }
_ [ 1 ] = { }
2014-12-23 22:56:46 +00:00
return { _ [ 1 ] } ] ] , ' empty table within a table ' )
2013-11-03 21:50:05 +00:00
local _t = { }
_t.self = _t
2015-05-20 00:13:34 +00:00
case ( _t , [ = [ local _ = { }
_ [ 0 ] = { }
_ [ 0 ] . self = _ [ 0 ]
2014-12-23 22:56:46 +00:00
return _ [ 0 ] ] = ] , ' simple cycle ' )
2013-11-03 21:50:05 +00:00
case_error ( { coroutine.create ( function ( ) end ) } , ' ./ser.lua:27: Trying to serialize unsupported type thread ' , ' unsupported type ' )
2015-05-20 00:13:34 +00:00
case ( { " a " , foo = " bar " , [ " 3f " ] = true , _1 = false , [ " 00 " ] = 9 } , ' return {"a",["3f"]=true,_1=false,["00"]=9,foo="bar"} ' , ' various ' )
2013-11-03 21:50:05 +00:00
case ( { ' \127 \230 \255 \254 \128 \12 \012 8 \n \31 ' } , ' return {" \\ 127 \\ 230 \\ 255 \\ 254 \\ 128 \\ 12 \\ 0128 \\ n \\ 31"} ' , ' non-ASCII or control characters in string value ' )
2015-05-20 00:13:34 +00:00
case ( { [ ' \127 \230 \255 \254 \128 \12 \012 8 \n \31 ' ] = ' \0 ' } , ' return {[" \\ 127 \\ 230 \\ 255 \\ 254 \\ 128 \\ 12 \\ 0128 \\ n \\ 31"]=" \\ 0"} ' , ' non-ASCII or control characters in string key ' )
2013-11-03 21:50:05 +00:00
local x = { }
2014-12-23 22:56:46 +00:00
case ( { x , { x } , x } , [ = [
2015-05-20 00:13:34 +00:00
local _ = { }
_ [ 2 ] = { }
_ [ 1 ] = { }
_ [ 0 ] = { _ [ 1 ] , _ [ 2 ] , _ [ 1 ] }
_ [ 2 ] [ 1 ] = _ [ 1 ]
2014-12-23 22:56:46 +00:00
return _ [ 0 ] ] = ] , ' repeated table ' )
2013-11-03 21:50:05 +00:00
2015-05-20 00:13:34 +00:00
case ( { [ ' end ' ] = true , [ ' false ' ] = false } , ' return {["false"]=false,["end"]=true} ' , ' keywords as table keys ' )
2013-11-03 21:50:05 +00:00
2015-05-20 00:13:34 +00:00
case ( { 1 / 0 , - 1 / 0 , 0 / 0 } , ' return {1/0,-1/0,0/0} ' , ' representation of infinity and NaN ' )
2013-11-03 21:50:05 +00:00
print ( failed .. ' tests failed ' )
print ( succeeded .. ' tests succeeded ' )