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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
# encoding: ascii-8bit
require 'spec/spec_helper'
describe MessagePack::Factory do
subject do
described_class.new
end
describe '#packer' do
it 'creates a Packer instance' do
subject.packer.should be_kind_of(MessagePack::Packer)
end
it 'creates new instance' do
subject.packer.object_id.should_not == subject.packer.object_id
end
end
describe '#unpacker' do
it 'creates a Unpacker instance' do
subject.unpacker.should be_kind_of(MessagePack::Unpacker)
end
it 'creates new instance' do
subject.unpacker.object_id.should_not == subject.unpacker.object_id
end
it 'creates unpacker with symbolize_keys option' do
unpacker = subject.unpacker(symbolize_keys: true)
unpacker.feed(MessagePack.pack({'k'=>'v'}))
unpacker.read.should == {:k => 'v'}
end
it 'creates unpacker with allow_unknown_ext option' do
unpacker = subject.unpacker(allow_unknown_ext: true)
unpacker.feed(MessagePack::ExtensionValue.new(1, 'a').to_msgpack)
unpacker.read.should == MessagePack::ExtensionValue.new(1, 'a')
end
it 'creates unpacker without allow_unknown_ext option' do
unpacker = subject.unpacker
unpacker.feed(MessagePack::ExtensionValue.new(1, 'a').to_msgpack)
expect{ unpacker.read }.to raise_error(MessagePack::UnknownExtTypeError)
end
end
class MyType
def initialize(a, b)
@a = a
@b = b
end
attr_reader :a, :b
def to_msgpack_ext
[a, b].pack('CC')
end
def self.from_msgpack_ext(data)
new(*data.unpack('CC'))
end
def to_msgpack_ext_only_a
[a, 0].pack('CC')
end
def self.from_msgpack_ext_only_b(data)
a, b = *data.unpack('CC')
new(0, b)
end
end
class MyType2 < MyType
end
describe '#registered_types' do
it 'returns Array' do
expect(subject.registered_types).to be_instance_of(Array)
end
it 'returns Array of Hash contains :type, :class, :packer, :unpacker' do
subject.register_type(0x20, ::MyType)
subject.register_type(0x21, ::MyType2)
list = subject.registered_types
expect(list.size).to eq(2)
expect(list[0]).to be_instance_of(Hash)
expect(list[1]).to be_instance_of(Hash)
expect(list[0].keys.sort).to eq([:type, :class, :packer, :unpacker].sort)
expect(list[1].keys.sort).to eq([:type, :class, :packer, :unpacker].sort)
expect(list[0][:type]).to eq(0x20)
expect(list[0][:class]).to eq(::MyType)
expect(list[0][:packer]).to eq(:to_msgpack_ext)
expect(list[0][:unpacker]).to eq(:from_msgpack_ext)
expect(list[1][:type]).to eq(0x21)
expect(list[1][:class]).to eq(::MyType2)
expect(list[1][:packer]).to eq(:to_msgpack_ext)
expect(list[1][:unpacker]).to eq(:from_msgpack_ext)
end
it 'returns Array of Hash which has nil for unregistered feature' do
subject.register_type(0x21, ::MyType2, unpacker: :from_msgpack_ext)
subject.register_type(0x20, ::MyType, packer: :to_msgpack_ext)
list = subject.registered_types
expect(list.size).to eq(2)
expect(list[0]).to be_instance_of(Hash)
expect(list[1]).to be_instance_of(Hash)
expect(list[0].keys.sort).to eq([:type, :class, :packer, :unpacker].sort)
expect(list[1].keys.sort).to eq([:type, :class, :packer, :unpacker].sort)
expect(list[0][:type]).to eq(0x20)
expect(list[0][:class]).to eq(::MyType)
expect(list[0][:packer]).to eq(:to_msgpack_ext)
expect(list[0][:unpacker]).to be_nil
expect(list[1][:type]).to eq(0x21)
expect(list[1][:class]).to eq(::MyType2)
expect(list[1][:packer]).to be_nil
expect(list[1][:unpacker]).to eq(:from_msgpack_ext)
end
end
describe '#type_registered?' do
it 'receive Class or Integer, and return bool' do
expect(subject.type_registered?(0x00)).to be_falsy
expect(subject.type_registered?(0x01)).to be_falsy
expect(subject.type_registered?(::MyType)).to be_falsy
end
it 'has option to specify what types are registered for' do
expect(subject.type_registered?(0x00, :both)).to be_falsy
expect(subject.type_registered?(0x00, :packer)).to be_falsy
expect(subject.type_registered?(0x00, :unpacker)).to be_falsy
expect{ subject.type_registered?(0x00, :something) }.to raise_error(ArgumentError)
end
it 'returns true if specified type or class is already registered' do
subject.register_type(0x20, ::MyType)
subject.register_type(0x21, ::MyType2)
expect(subject.type_registered?(0x00)).to be_falsy
expect(subject.type_registered?(0x01)).to be_falsy
expect(subject.type_registered?(0x20)).to be_truthy
expect(subject.type_registered?(0x21)).to be_truthy
expect(subject.type_registered?(::MyType)).to be_truthy
expect(subject.type_registered?(::MyType2)).to be_truthy
end
end
describe '#register_type' do
let :src do
::MyType.new(1, 2)
end
it 'registers #to_msgpack_ext and .from_msgpack_ext by default' do
subject.register_type(0x7f, ::MyType)
data = subject.packer.write(src).to_s
my = subject.unpacker.feed(data).read
my.a.should == 1
my.b.should == 2
end
it 'registers custom packer method name' do
subject.register_type(0x7f, ::MyType, packer: :to_msgpack_ext_only_a, unpacker: :from_msgpack_ext)
data = subject.packer.write(src).to_s
my = subject.unpacker.feed(data).read
my.a.should == 1
my.b.should == 0
end
it 'registers custom unpacker method name' do
subject.register_type(0x7f, ::MyType, packer: :to_msgpack_ext, unpacker: 'from_msgpack_ext_only_b')
data = subject.packer.write(src).to_s
my = subject.unpacker.feed(data).read
my.a.should == 0
my.b.should == 2
end
it 'registers custom proc objects' do
pk = lambda {|obj| [obj.a + obj.b].pack('C') }
uk = lambda {|data| ::MyType.new(data.unpack('C').first, -1) }
subject.register_type(0x7f, ::MyType, packer: pk, unpacker: uk)
data = subject.packer.write(src).to_s
my = subject.unpacker.feed(data).read
my.a.should == 3
my.b.should == -1
end
it 'does not affect existent packer and unpackers' do
subject.register_type(0x7f, ::MyType)
packer = subject.packer
unpacker = subject.unpacker
subject.register_type(0x7f, ::MyType, packer: :to_msgpack_ext_only_a, unpacker: :from_msgpack_ext_only_b)
data = packer.write(src).to_s
my = unpacker.feed(data).read
my.a.should == 1
my.b.should == 2
end
end
describe 'the special treatment of symbols with ext type' do
let(:packer) { subject.packer }
let(:unpacker) { subject.unpacker }
def symbol_after_roundtrip
packed_symbol = packer.pack(:symbol).to_s
unpacker.feed(packed_symbol).unpack
end
context 'if no ext type is registered for symbols' do
it 'converts symbols to string' do
expect(symbol_after_roundtrip).to eq 'symbol'
end
end
context 'if an ext type is registered for symbols' do
context 'if using the default serializer' do
before { subject.register_type(0x00, ::Symbol) }
it 'lets symbols survive a roundtrip' do
expect(symbol_after_roundtrip).to be :symbol
end
end
context 'if using a custom serializer' do
before do
class Symbol
alias_method :to_msgpack_ext_orig, :to_msgpack_ext
def to_msgpack_ext
self.to_s.codepoints.to_a.pack('n*')
end
end
class << Symbol
alias_method :from_msgpack_ext_orig, :from_msgpack_ext
def from_msgpack_ext(data)
data.unpack('n*').map(&:chr).join.to_sym
end
end
end
before { subject.register_type(0x00, ::Symbol) }
it 'lets symbols survive a roundtrip' do
expect(symbol_after_roundtrip).to be :symbol
end
after do
class Symbol
alias_method :to_msgpack_ext, :to_msgpack_ext_orig
end
class << Symbol
alias_method :from_msgpack_ext, :from_msgpack_ext_orig
end
end
end
end
end
describe 'DefaultFactory' do
it 'is a factory' do
MessagePack::DefaultFactory.should be_kind_of(MessagePack::Factory)
end
require_relative 'exttypes'
it 'should be referred by MessagePack.pack and MessagePack.unpack' do
MessagePack::DefaultFactory.register_type(DummyTimeStamp1::TYPE, DummyTimeStamp1)
MessagePack::DefaultFactory.register_type(DummyTimeStamp2::TYPE, DummyTimeStamp2, packer: :serialize, unpacker: :deserialize)
t = Time.now
dm1 = DummyTimeStamp1.new(t.to_i, t.usec)
expect(MessagePack.unpack(MessagePack.pack(dm1))).to eq(dm1)
dm2 = DummyTimeStamp1.new(t.to_i, t.usec)
expect(MessagePack.unpack(MessagePack.pack(dm2))).to eq(dm2)
end
end
end
|