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 124 125 126 127
|
# encoding: ascii-8bit
require 'spec/spec_helper'
require 'stringio'
if defined?(Encoding)
Encoding.default_external = 'ASCII-8BIT'
end
describe Packer do
let :packer do
Packer.new
end
it 'initialize' do
Packer.new
Packer.new(nil)
Packer.new(StringIO.new)
Packer.new({})
Packer.new(StringIO.new, {})
end
#it 'Packer' do
# Packer(packer).object_id.should == packer.object_id
# Packer(nil).class.should == Packer
# Packer('').class.should == Packer
# Packer('initbuf').to_s.should == 'initbuf'
#end
it 'write' do
packer.write([])
packer.to_s.should == "\x90"
end
it 'write_nil' do
packer.write_nil
packer.to_s.should == "\xc0"
end
it 'write_array_header 0' do
packer.write_array_header(0)
packer.to_s.should == "\x90"
end
it 'write_array_header 1' do
packer.write_array_header(1)
packer.to_s.should == "\x91"
end
it 'write_map_header 0' do
packer.write_map_header(0)
packer.to_s.should == "\x80"
end
it 'write_map_header 1' do
packer.write_map_header(1)
packer.to_s.should == "\x81"
end
it 'flush' do
io = StringIO.new
pk = Packer.new(io)
pk.write_nil
pk.flush
pk.to_s.should == ''
io.string.should == "\xc0"
end
it 'buffer' do
o1 = packer.buffer.object_id
packer.buffer << 'frsyuki'
packer.buffer.to_s.should == 'frsyuki'
packer.buffer.object_id.should == o1
end
it 'to_msgpack returns String' do
nil.to_msgpack.class.should == String
true.to_msgpack.class.should == String
false.to_msgpack.class.should == String
1.to_msgpack.class.should == String
1.0.to_msgpack.class.should == String
"".to_msgpack.class.should == String
Hash.new.to_msgpack.class.should == String
Array.new.to_msgpack.class.should == String
end
class CustomPack01
def to_msgpack(pk=nil)
return MessagePack.pack(self, pk) unless pk.class == MessagePack::Packer
pk.write_array_header(2)
pk.write(1)
pk.write(2)
return pk
end
end
class CustomPack02
def to_msgpack(pk=nil)
[1,2].to_msgpack(pk)
end
end
it 'calls custom to_msgpack method' do
MessagePack.pack(CustomPack01.new).should == [1,2].to_msgpack
MessagePack.pack(CustomPack02.new).should == [1,2].to_msgpack
CustomPack01.new.to_msgpack.should == [1,2].to_msgpack
CustomPack02.new.to_msgpack.should == [1,2].to_msgpack
end
it 'calls custom to_msgpack method with io' do
s01 = StringIO.new
MessagePack.pack(CustomPack01.new, s01)
s01.string.should == [1,2].to_msgpack
s02 = StringIO.new
MessagePack.pack(CustomPack02.new, s02)
s02.string.should == [1,2].to_msgpack
s03 = StringIO.new
CustomPack01.new.to_msgpack(s03)
s03.string.should == [1,2].to_msgpack
s04 = StringIO.new
CustomPack02.new.to_msgpack(s04)
s04.string.should == [1,2].to_msgpack
end
end
|