1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
#! /usr/bin/lua
require 'Test.More'
plan(24)
local mp = require 'MessagePack'
error_like( function ()
mp.pack( print )
end,
"pack 'function' is unimplemented" )
error_like( function ()
mp.pack( coroutine.create(plan) )
end,
"pack 'thread' is unimplemented" )
error_like( function ()
mp.pack( io.stdin )
end,
"pack 'userdata' is unimplemented" )
error_like( function ()
local a = {}
a.foo = a
mp.pack( a )
end,
"stack overflow", -- from Lua interpreter
"direct cycle" )
error_like( function ()
local a = {}
local b = {}
a.foo = b
b.foo = a
mp.pack( a )
end,
"stack overflow", -- from Lua interpreter
"indirect cycle" )
error_like( function ()
mp.unpack(string.char(0xC1))
end,
"unpack '0xc1' is unimplemented" )
is( mp.unpack(mp.pack("text")), "text" )
error_like( function ()
mp.unpack(mp.pack("text"):sub(1, -2))
end,
"missing bytes" )
error_like( function ()
mp.unpack(mp.pack("text") .. "more")
end,
"extra bytes" )
error_like( function ()
mp.unpack(mp.pack("text") .. "1")
end,
"extra bytes" )
error_like( function ()
mp.unpack( {} )
end,
"bad argument #1 to unpack %(string expected, got table%)" )
error_like( function ()
mp.unpacker( false )
end,
"bad argument #1 to unpacker %(string or function expected, got boolean%)" )
error_like( function ()
mp.unpacker( {} )
end,
"bad argument #1 to unpacker %(string or function expected, got table%)" )
for _, val in mp.unpacker(string.rep(mp.pack("text"), 2)) do
is( val, "text" )
end
error_like( function ()
for _, val in mp.unpacker(string.rep(mp.pack("text"), 2):sub(1, -2)) do
is( val, "text" )
end
end,
"missing bytes" )
error_like( function ()
mp.set_string'bad'
end,
"bad argument #1 to set_string %(invalid option 'bad'%)" )
error_like( function ()
mp.set_number'bad'
end,
"bad argument #1 to set_number %(invalid option 'bad'%)" )
error_like( function ()
mp.set_integer'bad'
end,
"bad argument #1 to set_integer %(invalid option 'bad'%)" )
error_like( function ()
mp.set_array'bad'
end,
"bad argument #1 to set_array %(invalid option 'bad'%)" )
error_like( function ()
mp.packers['fixext4'](nil, 1, '123')
end,
"bad length for fixext4" )
lives_ok( function ()
mp.packers['fixext4']({}, 1, '1234')
end,
"fixext4" )
lives_ok( function ()
for _ in ipairs(mp.packers) do end
end,
"cannot iterate packers" )
|