msgpack 0.6.2-java → 0.7.0dev1-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3b998a195a0aaeb32b2c24895e9dd8749f5462c5
4
- data.tar.gz: 9e68941aa0f00733cd0369c8e3769b0002e62f3a
3
+ metadata.gz: 1074cd38a1217bb758be1ce482c293956f051c29
4
+ data.tar.gz: 53cac06789fb384cf3407e61e7cbb5d4ef2f263b
5
5
  SHA512:
6
- metadata.gz: b96a3334af1c4a758c5f57e4d0cc921825fac3bab974d4431669be75fd2056771b9ba724cd71d0f51ec29a8a0235fb959fadf15a88db62ccc913c2329d533d59
7
- data.tar.gz: ed88e2008f43ba77de7ad12ad2cd60b2784e2b26c0acce61d2e12aa710a0868b1e296077fc4899247ce9f8d499a378e18454181695c812703371cc4fb7b763a7
6
+ metadata.gz: 0d311e9a8d3729e29398660f0416d83a93e1d6c751851a0b240e11ec6ee18bd904d9b3ab2a249028a9591b8cb13281493eaf1fe7c97148ab56999be9bbea19f5
7
+ data.tar.gz: 274602def6f21adeb764c70ac6b506ef72f245983c9c63ab12d977bbd981da9e94976858d583750cd55c2d4c93c4a5333f2103ed04fa8feaa68079261ea8a8b4
@@ -11,3 +11,7 @@ else
11
11
  require "msgpack/msgpack"
12
12
  end
13
13
  end
14
+
15
+ require "msgpack/packer"
16
+ require "msgpack/unpacker"
17
+ require "msgpack/factory"
@@ -0,0 +1,60 @@
1
+ module MessagePack
2
+ class Factory
3
+ # see ext for other methods
4
+
5
+ # [ {type: id, class: Class(or nil), packer: arg, unpacker: arg}, ... ]
6
+ def registered_types(selector=:both)
7
+ packer, unpacker = registered_types_internal
8
+ # packer: Class -> [tid, proc, arg]
9
+ # unpacker: tid -> [klass, proc, arg]
10
+
11
+ list = []
12
+
13
+ case selector
14
+ when :both
15
+ packer.each_pair do |klass, ary|
16
+ type = ary[0]
17
+ packer_arg = ary[2]
18
+ unpacker_arg = nil
19
+ if unpacker.has_key?(type) && unpacker[type][0] == klass
20
+ unpacker_arg = unpacker.delete(type)[2]
21
+ end
22
+ list << {type: type, class: klass, packer: packer_arg, unpacker: unpacker_arg}
23
+ end
24
+
25
+ # unpacker definition only
26
+ unpacker.each_pair do |type, ary|
27
+ list << {type: type, class: ary[0], packer: nil, unpacker: ary[2]}
28
+ end
29
+
30
+ when :packer
31
+ packer.each_pair do |klass, ary|
32
+ list << {type: ary[0], class: klass, packer: ary[2]}
33
+ end
34
+
35
+ when :unpacker
36
+ unpacker.each_pair do |type, ary|
37
+ list << {type: type, class: ary[0], unpacker: ary[2]}
38
+ end
39
+
40
+ else
41
+ raise ArgumentError, "invalid selector #{selector}"
42
+ end
43
+
44
+ list.sort{|a, b| a[:type] <=> b[:type] }
45
+ end
46
+
47
+ def type_registered?(klass_or_type, selector=:both)
48
+ case klass_or_type
49
+ when Class
50
+ klass = klass_or_type
51
+ registered_types(selector).any?{|entry| klass <= entry[:class] }
52
+ when Integer
53
+ type = klass_or_type
54
+ registered_types(selector).any?{|entry| type == entry[:type] }
55
+ else
56
+ raise ArgumentError, "class or type id"
57
+ end
58
+ end
59
+ end
60
+ end
Binary file
@@ -0,0 +1,28 @@
1
+ module MessagePack
2
+ class Packer
3
+ # see ext for other methods
4
+
5
+ def registered_types
6
+ list = []
7
+
8
+ registered_types_internal.each_pair do |klass, ary|
9
+ list << {type: ary[0], class: klass, packer: ary[2]}
10
+ end
11
+
12
+ list.sort{|a, b| a[:type] <=> b[:type] }
13
+ end
14
+
15
+ def type_registered?(klass_or_type)
16
+ case klass_or_type
17
+ when Class
18
+ klass = klass_or_type
19
+ registered_types.any?{|entry| klass <= entry[:class] }
20
+ when Integer
21
+ type = klass_or_type
22
+ registered_types.any?{|entry| type == entry[:type] }
23
+ else
24
+ raise ArgumentError, "class or type id"
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ module MessagePack
2
+ class Unpacker
3
+ # see ext for other methods
4
+
5
+ def registered_types
6
+ list = []
7
+
8
+ registered_types_internal.each_pair do |type, ary|
9
+ list << {type: type, class: ary[0], unpacker: ary[2]}
10
+ end
11
+
12
+ list.sort{|a, b| a[:type] <=> b[:type] }
13
+ end
14
+
15
+ def type_registered?(klass_or_type)
16
+ case klass_or_type
17
+ when Class
18
+ klass = klass_or_type
19
+ registered_types.any?{|entry| klass == entry[:class] }
20
+ when Integer
21
+ type = klass_or_type
22
+ registered_types.any?{|entry| type == entry[:type] }
23
+ else
24
+ raise ArgumentError, "class or type id"
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,3 +1,3 @@
1
1
  module MessagePack
2
- VERSION = "0.6.2"
2
+ VERSION = "0.7.0dev1"
3
3
  end
@@ -32,7 +32,7 @@ describe Buffer do
32
32
  end
33
33
 
34
34
  it 'close closes internal io' do
35
- io.should_receive(:close)
35
+ expect(io).to receive(:close)
36
36
  buffer.close
37
37
  end
38
38
 
@@ -137,7 +137,7 @@ describe Buffer do
137
137
 
138
138
  50.times {
139
139
  fragments = []
140
- sx = r.bytes(0)
140
+ r.bytes(0)
141
141
 
142
142
  r.rand(4).times do
143
143
  n = r.rand(1024*1400)
@@ -253,4 +253,3 @@ describe Buffer do
253
253
  }
254
254
  end
255
255
  end
256
-
@@ -379,7 +379,6 @@ describe Buffer do
379
379
  end
380
380
 
381
381
  it 'big write -> short write' do
382
- biglen = 1024*1024 + 2
383
382
  big1 = "a" * (1024*1024 + 2)
384
383
 
385
384
  case_keys.each {|k|
@@ -557,7 +556,7 @@ describe Buffer do
557
556
  n = r.rand(1024*1400)
558
557
  begin
559
558
  b.skip_all(n)
560
- ex = s.slice!(0, n)
559
+ s.slice!(0, n)
561
560
  b.size.should == s.size
562
561
  ensure EOFError
563
562
  b.size.should == s.size
@@ -569,4 +568,3 @@ describe Buffer do
569
568
  }
570
569
  end
571
570
  end
572
-
@@ -21,7 +21,10 @@ describe Unpacker do
21
21
  unpacker.feed("\x81")
22
22
  lambda {
23
23
  unpacker.read_array_header
24
- }.should raise_error(MessagePack::TypeError)
24
+ }.should raise_error(MessagePack::TypeError) # TypeError is included in UnexpectedTypeError
25
+ lambda {
26
+ unpacker.read_array_header
27
+ }.should raise_error(MessagePack::UnexpectedTypeError)
25
28
  end
26
29
 
27
30
  it 'read_map_header converts an map to key-value sequence' do
@@ -55,7 +58,10 @@ describe Unpacker do
55
58
  unpacker.feed("\x91")
56
59
  lambda {
57
60
  unpacker.read_map_header
58
- }.should raise_error(MessagePack::TypeError)
61
+ }.should raise_error(MessagePack::TypeError) # TypeError is included in UnexpectedTypeError
62
+ lambda {
63
+ unpacker.read_map_header
64
+ }.should raise_error(MessagePack::UnexpectedTypeError)
59
65
  end
60
66
 
61
67
  it 'skip_nil succeeds' do
@@ -134,6 +140,12 @@ describe Unpacker do
134
140
  objects.should == [sample_object] * 4
135
141
  end
136
142
 
143
+ it 'feed_each enumerator' do
144
+ raw = sample_object.to_msgpack.to_s * 4
145
+
146
+ unpacker.feed_each(raw).to_a.should == [sample_object] * 4
147
+ end
148
+
137
149
  it 'reset clears internal buffer' do
138
150
  # 1-element array
139
151
  unpacker.feed("\x91")
@@ -0,0 +1,99 @@
1
+ # encoding: ascii-8bit
2
+ require 'spec_helper'
3
+
4
+ describe MessagePack::ExtensionValue do
5
+ subject do
6
+ described_class.new(1, "data")
7
+ end
8
+
9
+ describe '#type/#type=' do
10
+ it 'returns value set by #initialize' do
11
+ subject.type.should == 1
12
+ end
13
+
14
+ it 'assigns a value' do
15
+ subject.type = 2
16
+ subject.type.should == 2
17
+ end
18
+ end
19
+
20
+ describe '#payload/#payload=' do
21
+ it 'returns value set by #initialize' do
22
+ subject.payload.should == "data"
23
+ end
24
+
25
+ it 'assigns a value' do
26
+ subject.payload = "a"
27
+ subject.payload.should == "a"
28
+ end
29
+ end
30
+
31
+ describe '#==/#eql?/#hash' do
32
+ it 'returns equivalent if the content is same' do
33
+ ext1 = MessagePack::ExtensionValue.new(1, "data")
34
+ ext2 = MessagePack::ExtensionValue.new(1, "data")
35
+ (ext1 == ext2).should be true
36
+ ext1.eql?(ext2).should be true
37
+ (ext1.hash == ext2.hash).should be true
38
+ end
39
+
40
+ it 'returns not equivalent if type is not same' do
41
+ ext1 = MessagePack::ExtensionValue.new(1, "data")
42
+ ext2 = MessagePack::ExtensionValue.new(2, "data")
43
+ (ext1 == ext2).should be false
44
+ ext1.eql?(ext2).should be false
45
+ (ext1.hash == ext2.hash).should be false
46
+ end
47
+
48
+ it 'returns not equivalent if payload is not same' do
49
+ ext1 = MessagePack::ExtensionValue.new(1, "data")
50
+ ext2 = MessagePack::ExtensionValue.new(1, "value")
51
+ (ext1 == ext2).should be false
52
+ ext1.eql?(ext2).should be false
53
+ (ext1.hash == ext2.hash).should be false
54
+ end
55
+ end
56
+
57
+ describe '#to_msgpack' do
58
+ it 'serializes very short payload' do
59
+ ext = MessagePack::ExtensionValue.new(1, "a"*2).to_msgpack
60
+ ext.should == "\xd5\x01" + "a"*2
61
+ end
62
+
63
+ it 'serializes short payload' do
64
+ ext = MessagePack::ExtensionValue.new(1, "a"*18).to_msgpack
65
+ ext.should == "\xc7\x12\x01" + "a"*18
66
+ end
67
+
68
+ it 'serializes long payload' do
69
+ ext = MessagePack::ExtensionValue.new(1, "a"*65540).to_msgpack
70
+ ext.should == "\xc9\x00\x01\x00\x04\x01" + "a"*65540
71
+ end
72
+
73
+ it 'with a packer serializes to a packer' do
74
+ ext = MessagePack::ExtensionValue.new(1, "aa")
75
+ packer = MessagePack::Packer.new
76
+ ext.to_msgpack(packer)
77
+ packer.buffer.to_s.should == "\xd5\x01aa"
78
+ end
79
+
80
+ [-129, -65540, -(2**40), 128, 65540, 2**40].each do |type|
81
+ context "with invalid type (#{type})" do
82
+ it 'raises RangeError' do
83
+ lambda { MessagePack::ExtensionValue.new(type, "a").to_msgpack }.should raise_error(RangeError)
84
+ end
85
+ end
86
+ end
87
+ end
88
+
89
+ describe '#dup' do
90
+ it 'duplicates' do
91
+ ext1 = MessagePack::ExtensionValue.new(1, "data")
92
+ ext2 = ext1.dup
93
+ ext2.type = 2
94
+ ext2.payload = "data2"
95
+ ext1.type.should == 1
96
+ ext1.payload.should == "data"
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,51 @@
1
+ class DummyTimeStamp1
2
+ TYPE = 15
3
+
4
+ attr_reader :utime, :usec, :time
5
+
6
+ def initialize(utime, usec)
7
+ @utime = utime
8
+ @usec = usec
9
+ @time = Time.at(utime, usec)
10
+ end
11
+
12
+ def ==(other)
13
+ self.utime == other.utime && self.usec == other.usec
14
+ end
15
+
16
+ def self.type_id
17
+ 15
18
+ end
19
+
20
+ def self.from_msgpack_ext(data)
21
+ new(*data.unpack('I*'))
22
+ end
23
+
24
+ def to_msgpack_ext
25
+ [@utime,@usec].pack('I*')
26
+ end
27
+ end
28
+
29
+ class DummyTimeStamp2
30
+ TYPE = 16
31
+
32
+ attr_reader :utime, :usec, :time
33
+
34
+ def initialize(utime, usec)
35
+ @utime = utime
36
+ @usec = usec
37
+ @time = Time.at(utime, usec)
38
+ end
39
+
40
+ def ==(other)
41
+ self.utime == other.utime && self.usec == other.usec
42
+ end
43
+
44
+ def self.deserialize(data)
45
+ new(* data.split(',', 2).map(&:to_i))
46
+ end
47
+
48
+ def serialize
49
+ [@utime,@usec].map(&:to_s).join(',')
50
+ end
51
+ end
@@ -0,0 +1,236 @@
1
+ # encoding: ascii-8bit
2
+ require 'spec_helper'
3
+
4
+ eval("return") if java?
5
+
6
+ describe MessagePack::Factory do
7
+ subject do
8
+ described_class.new
9
+ end
10
+
11
+ describe '#packer' do
12
+ it 'creates a Packer instance' do
13
+ subject.packer.should be_kind_of(MessagePack::Packer)
14
+ end
15
+
16
+ it 'creates new instance' do
17
+ subject.packer.object_id.should_not == subject.packer.object_id
18
+ end
19
+ end
20
+
21
+ describe '#unpacker' do
22
+ it 'creates a Unpacker instance' do
23
+ subject.unpacker.should be_kind_of(MessagePack::Unpacker)
24
+ end
25
+
26
+ it 'creates new instance' do
27
+ subject.unpacker.object_id.should_not == subject.unpacker.object_id
28
+ end
29
+
30
+ it 'creates unpacker with symbolize_keys option' do
31
+ unpacker = subject.unpacker(symbolize_keys: true)
32
+ unpacker.feed(MessagePack.pack({'k'=>'v'}))
33
+ unpacker.read.should == {:k => 'v'}
34
+ end
35
+
36
+ it 'creates unpacker with allow_unknown_ext option' do
37
+ unpacker = subject.unpacker(allow_unknown_ext: true)
38
+ unpacker.feed(MessagePack::ExtensionValue.new(1, 'a').to_msgpack)
39
+ unpacker.read.should == MessagePack::ExtensionValue.new(1, 'a')
40
+ end
41
+
42
+ it 'creates unpacker without allow_unknown_ext option' do
43
+ unpacker = subject.unpacker
44
+ unpacker.feed(MessagePack::ExtensionValue.new(1, 'a').to_msgpack)
45
+ expect{ unpacker.read }.to raise_error(MessagePack::UnknownExtTypeError)
46
+ end
47
+ end
48
+
49
+ class MyType
50
+ def initialize(a, b)
51
+ @a = a
52
+ @b = b
53
+ end
54
+
55
+ attr_reader :a, :b
56
+
57
+ def to_msgpack_ext
58
+ [a, b].pack('CC')
59
+ end
60
+
61
+ def self.from_msgpack_ext(data)
62
+ new(*data.unpack('CC'))
63
+ end
64
+
65
+ def to_msgpack_ext_only_a
66
+ [a, 0].pack('CC')
67
+ end
68
+
69
+ def self.from_msgpack_ext_only_b(data)
70
+ a, b = *data.unpack('CC')
71
+ new(0, b)
72
+ end
73
+ end
74
+
75
+ class MyType2 < MyType
76
+ end
77
+
78
+ describe '#registered_types' do
79
+ it 'returns Array' do
80
+ expect(subject.registered_types).to be_instance_of(Array)
81
+ end
82
+
83
+ it 'returns Array of Hash contains :type, :class, :packer, :unpacker' do
84
+ subject.register_type(0x20, ::MyType)
85
+ subject.register_type(0x21, ::MyType2)
86
+
87
+ list = subject.registered_types
88
+
89
+ expect(list.size).to eq(2)
90
+ expect(list[0]).to be_instance_of(Hash)
91
+ expect(list[1]).to be_instance_of(Hash)
92
+ expect(list[0].keys.sort).to eq([:type, :class, :packer, :unpacker].sort)
93
+ expect(list[1].keys.sort).to eq([:type, :class, :packer, :unpacker].sort)
94
+
95
+ expect(list[0][:type]).to eq(0x20)
96
+ expect(list[0][:class]).to eq(::MyType)
97
+ expect(list[0][:packer]).to eq(:to_msgpack_ext)
98
+ expect(list[0][:unpacker]).to eq(:from_msgpack_ext)
99
+
100
+ expect(list[1][:type]).to eq(0x21)
101
+ expect(list[1][:class]).to eq(::MyType2)
102
+ expect(list[1][:packer]).to eq(:to_msgpack_ext)
103
+ expect(list[1][:unpacker]).to eq(:from_msgpack_ext)
104
+ end
105
+
106
+ it 'returns Array of Hash which has nil for unregistered feature' do
107
+ subject.register_type(0x21, ::MyType2, unpacker: :from_msgpack_ext)
108
+ subject.register_type(0x20, ::MyType, packer: :to_msgpack_ext)
109
+
110
+ list = subject.registered_types
111
+
112
+ expect(list.size).to eq(2)
113
+ expect(list[0]).to be_instance_of(Hash)
114
+ expect(list[1]).to be_instance_of(Hash)
115
+ expect(list[0].keys.sort).to eq([:type, :class, :packer, :unpacker].sort)
116
+ expect(list[1].keys.sort).to eq([:type, :class, :packer, :unpacker].sort)
117
+
118
+ expect(list[0][:type]).to eq(0x20)
119
+ expect(list[0][:class]).to eq(::MyType)
120
+ expect(list[0][:packer]).to eq(:to_msgpack_ext)
121
+ expect(list[0][:unpacker]).to be_nil
122
+
123
+ expect(list[1][:type]).to eq(0x21)
124
+ expect(list[1][:class]).to eq(::MyType2)
125
+ expect(list[1][:packer]).to be_nil
126
+ expect(list[1][:unpacker]).to eq(:from_msgpack_ext)
127
+ end
128
+ end
129
+
130
+ describe '#type_registered?' do
131
+ it 'receive Class or Integer, and return bool' do
132
+ expect(subject.type_registered?(0x00)).to be_falsy
133
+ expect(subject.type_registered?(0x01)).to be_falsy
134
+ expect(subject.type_registered?(::MyType)).to be_falsy
135
+ end
136
+
137
+ it 'has option to specify what types are registered for' do
138
+ expect(subject.type_registered?(0x00, :both)).to be_falsy
139
+ expect(subject.type_registered?(0x00, :packer)).to be_falsy
140
+ expect(subject.type_registered?(0x00, :unpacker)).to be_falsy
141
+ expect{ subject.type_registered?(0x00, :something) }.to raise_error(ArgumentError)
142
+ end
143
+
144
+ it 'returns true if specified type or class is already registered' do
145
+ subject.register_type(0x20, ::MyType)
146
+ subject.register_type(0x21, ::MyType2)
147
+
148
+ expect(subject.type_registered?(0x00)).to be_falsy
149
+ expect(subject.type_registered?(0x01)).to be_falsy
150
+
151
+ expect(subject.type_registered?(0x20)).to be_truthy
152
+ expect(subject.type_registered?(0x21)).to be_truthy
153
+ expect(subject.type_registered?(::MyType)).to be_truthy
154
+ expect(subject.type_registered?(::MyType2)).to be_truthy
155
+ end
156
+ end
157
+
158
+ describe '#register_type' do
159
+ let :src do
160
+ ::MyType.new(1, 2)
161
+ end
162
+
163
+ it 'registers #to_msgpack_ext and .from_msgpack_ext by default' do
164
+ subject.register_type(0x7f, ::MyType)
165
+
166
+ data = subject.packer.write(src).to_s
167
+ my = subject.unpacker.feed(data).read
168
+ my.a.should == 1
169
+ my.b.should == 2
170
+ end
171
+
172
+ it 'registers custom packer method name' do
173
+ subject.register_type(0x7f, ::MyType, packer: :to_msgpack_ext_only_a, unpacker: :from_msgpack_ext)
174
+
175
+ data = subject.packer.write(src).to_s
176
+ my = subject.unpacker.feed(data).read
177
+ my.a.should == 1
178
+ my.b.should == 0
179
+ end
180
+
181
+ it 'registers custom unpacker method name' do
182
+ subject.register_type(0x7f, ::MyType, packer: :to_msgpack_ext, unpacker: 'from_msgpack_ext_only_b')
183
+
184
+ data = subject.packer.write(src).to_s
185
+ my = subject.unpacker.feed(data).read
186
+ my.a.should == 0
187
+ my.b.should == 2
188
+ end
189
+
190
+ it 'registers custom proc objects' do
191
+ pk = lambda {|obj| [obj.a + obj.b].pack('C') }
192
+ uk = lambda {|data| ::MyType.new(data.unpack('C').first, -1) }
193
+ subject.register_type(0x7f, ::MyType, packer: pk, unpacker: uk)
194
+
195
+ data = subject.packer.write(src).to_s
196
+ my = subject.unpacker.feed(data).read
197
+ my.a.should == 3
198
+ my.b.should == -1
199
+ end
200
+
201
+ it 'does not affect existent packer and unpackers' do
202
+ subject.register_type(0x7f, ::MyType)
203
+ packer = subject.packer
204
+ unpacker = subject.unpacker
205
+
206
+ subject.register_type(0x7f, ::MyType, packer: :to_msgpack_ext_only_a, unpacker: :from_msgpack_ext_only_b)
207
+
208
+ data = packer.write(src).to_s
209
+ my = unpacker.feed(data).read
210
+ my.a.should == 1
211
+ my.b.should == 2
212
+ end
213
+ end
214
+
215
+ describe 'DefaultFactory' do
216
+ it 'is a factory' do
217
+ MessagePack::DefaultFactory.should be_kind_of(MessagePack::Factory)
218
+ end
219
+
220
+ require_relative 'exttypes'
221
+
222
+ it 'should be referred by MessagePack.pack and MessagePack.unpack' do
223
+ skip("not supported yet in JRuby implementation") if java?
224
+ MessagePack::DefaultFactory.register_type(DummyTimeStamp1::TYPE, DummyTimeStamp1)
225
+ MessagePack::DefaultFactory.register_type(DummyTimeStamp2::TYPE, DummyTimeStamp2, packer: :serialize, unpacker: :deserialize)
226
+
227
+ t = Time.now
228
+
229
+ dm1 = DummyTimeStamp1.new(t.to_i, t.usec)
230
+ expect(MessagePack.unpack(MessagePack.pack(dm1))).to eq(dm1)
231
+
232
+ dm2 = DummyTimeStamp1.new(t.to_i, t.usec)
233
+ expect(MessagePack.unpack(MessagePack.pack(dm2))).to eq(dm2)
234
+ end
235
+ end
236
+ end
@@ -72,6 +72,31 @@ describe ::MessagePack::Unpacker do
72
72
  subject.should_not be_finished
73
73
  end
74
74
  end
75
+
76
+ describe '#read' do
77
+ context 'with a buffer' do
78
+ it 'reads objects' do
79
+ objects = []
80
+ subject.feed(buffer1)
81
+ subject.feed(buffer2)
82
+ subject.feed(buffer3)
83
+ objects << subject.read
84
+ objects << subject.read
85
+ objects << subject.read
86
+ objects.should == [{'foo' => 'bar'}, {'hello' => {'world' => [1, 2, 3]}}, {'x' => 'y'}]
87
+ end
88
+
89
+ it 'reads map header' do
90
+ subject.feed({}.to_msgpack)
91
+ subject.read_map_header.should == 0
92
+ end
93
+
94
+ it 'reads array header' do
95
+ subject.feed([].to_msgpack)
96
+ subject.read_array_header.should == 0
97
+ end
98
+ end
99
+ end
75
100
 
76
101
  describe '#each' do
77
102
  context 'with a buffer' do
@@ -112,7 +112,7 @@ describe MessagePack do
112
112
 
113
113
  context 'with other things' do
114
114
  it 'raises an error on #pack with an unsupported type' do
115
- expect { MessagePack.pack(self) }.to raise_error(ArgumentError, /^Cannot pack type:/)
115
+ expect { MessagePack.pack(self) }.to raise_error(NoMethodError, /^undefined method `to_msgpack'/)
116
116
  end
117
117
 
118
118
  it 'rasies an error on #unpack with garbage' do
@@ -35,9 +35,6 @@ describe MessagePack do
35
35
  end
36
36
 
37
37
  it 'calls custom to_msgpack method' do
38
- if /java/ =~ RUBY_PLATFORM
39
- skip "custom to_msgpack fallback is not supported yet in JRuby implementation"
40
- end
41
38
  MessagePack.pack(CustomPack01.new).should == [1,2].to_msgpack
42
39
  MessagePack.pack(CustomPack02.new).should == [1,2].to_msgpack
43
40
  CustomPack01.new.to_msgpack.should == [1,2].to_msgpack
@@ -45,9 +42,6 @@ describe MessagePack do
45
42
  end
46
43
 
47
44
  it 'calls custom to_msgpack method with io' do
48
- if /java/ =~ RUBY_PLATFORM
49
- skip "custom to_msgpack fallback with io is not supported yet in JRuby implementation"
50
- end
51
45
  s01 = StringIO.new
52
46
  MessagePack.pack(CustomPack01.new, s01)
53
47
  s01.string.should == [1,2].to_msgpack
@@ -0,0 +1,95 @@
1
+ require 'spec_helper'
2
+
3
+ describe MessagePack::Packer do
4
+ class ValueOne
5
+ def initialize(num)
6
+ @num = num
7
+ end
8
+ def num
9
+ @num
10
+ end
11
+ def to_msgpack_ext
12
+ @num.to_msgpack
13
+ end
14
+ def self.from_msgpack_ext(data)
15
+ self.new(MessagePack.unpack(data))
16
+ end
17
+ end
18
+
19
+ class ValueTwo
20
+ def initialize(num)
21
+ @num_s = num.to_s
22
+ end
23
+ def num
24
+ @num_s.to_i
25
+ end
26
+ def to_msgpack_ext
27
+ @num_s.to_msgpack
28
+ end
29
+ def self.from_msgpack_ext(data)
30
+ self.new(MessagePack.unpack(data))
31
+ end
32
+ end
33
+
34
+ describe '#type_registered?' do
35
+ it 'receive Class or Integer, and return bool' do
36
+ skip("not supported yet in JRuby implementation") if java?
37
+ expect(subject.type_registered?(0x00)).to be_falsy
38
+ expect(subject.type_registered?(0x01)).to be_falsy
39
+ expect(subject.type_registered?(::ValueOne)).to be_falsy
40
+ end
41
+
42
+ it 'returns true if specified type or class is already registered' do
43
+ skip("not supported yet in JRuby implementation") if java?
44
+ subject.register_type(0x30, ::ValueOne, :to_msgpack_ext)
45
+ subject.register_type(0x31, ::ValueTwo, :to_msgpack_ext)
46
+
47
+ expect(subject.type_registered?(0x00)).to be_falsy
48
+ expect(subject.type_registered?(0x01)).to be_falsy
49
+
50
+ expect(subject.type_registered?(0x30)).to be_truthy
51
+ expect(subject.type_registered?(0x31)).to be_truthy
52
+ expect(subject.type_registered?(::ValueOne)).to be_truthy
53
+ expect(subject.type_registered?(::ValueTwo)).to be_truthy
54
+ end
55
+ end
56
+
57
+ describe '#register_type' do
58
+ it 'get type and class mapping for packing' do
59
+ skip("not supported yet in JRuby implementation") if java?
60
+ packer = MessagePack::Packer.new
61
+ packer.register_type(0x01, ValueOne){|obj| obj.to_msgpack_ext }
62
+ packer.register_type(0x02, ValueTwo){|obj| obj.to_msgpack_ext }
63
+
64
+ packer = MessagePack::Packer.new
65
+ packer.register_type(0x01, ValueOne, :to_msgpack_ext)
66
+ packer.register_type(0x02, ValueTwo, :to_msgpack_ext)
67
+
68
+ packer = MessagePack::Packer.new
69
+ packer.register_type(0x01, ValueOne, &:to_msgpack_ext)
70
+ packer.register_type(0x02, ValueTwo, &:to_msgpack_ext)
71
+ end
72
+
73
+ it 'returns a Hash which contains map of Class and type' do
74
+ skip("not supported yet in JRuby implementation") if java?
75
+ packer = MessagePack::Packer.new
76
+ packer.register_type(0x01, ValueOne, :to_msgpack_ext)
77
+ packer.register_type(0x02, ValueTwo, :to_msgpack_ext)
78
+
79
+ expect(packer.registered_types).to be_a(Array)
80
+ expect(packer.registered_types.size).to eq(2)
81
+
82
+ one = packer.registered_types[0]
83
+ expect(one.keys.sort).to eq([:type, :class, :packer].sort)
84
+ expect(one[:type]).to eq(0x01)
85
+ expect(one[:class]).to eq(ValueOne)
86
+ expect(one[:packer]).to eq(:to_msgpack_ext)
87
+
88
+ two = packer.registered_types[1]
89
+ expect(two.keys.sort).to eq([:type, :class, :packer].sort)
90
+ expect(two[:type]).to eq(0x02)
91
+ expect(two[:class]).to eq(ValueTwo)
92
+ expect(two[:packer]).to eq(:to_msgpack_ext)
93
+ end
94
+ end
95
+ end
@@ -15,13 +15,24 @@ end
15
15
 
16
16
  require 'msgpack'
17
17
 
18
- if /java/ =~ RUBY_PLATFORM
18
+ def java?
19
+ /java/ =~ RUBY_PLATFORM
20
+ end
21
+
22
+ if java?
19
23
  RSpec.configure do |c|
20
24
  c.treat_symbols_as_metadata_keys_with_true_values = true
21
25
  c.filter_run_excluding :encodings => !(defined? Encoding)
22
26
  end
23
27
  else
28
+ RSpec.configure do |config|
29
+ config.expect_with :rspec do |c|
30
+ c.syntax = [:should, :expect]
31
+ end
32
+ end
24
33
  Packer = MessagePack::Packer
25
34
  Unpacker = MessagePack::Unpacker
26
35
  Buffer = MessagePack::Buffer
36
+ Factory = MessagePack::Factory
37
+ ExtensionValue = MessagePack::ExtensionValue
27
38
  end
@@ -13,10 +13,7 @@ describe MessagePack do
13
13
  MessagePack.unpack(MessagePack.pack(symbolized_hash), :symbolize_keys => true).should == symbolized_hash
14
14
  end
15
15
 
16
- it 'Unpacker#unpack symbolize_keys' do
17
- if /java/ =~ RUBY_PLATFORM
18
- skip "Unpacker#unpack returns nil in JRuby (incompatible)"
19
- end
16
+ it 'Unpacker#read symbolize_keys' do
20
17
  unpacker = MessagePack::Unpacker.new(:symbolize_keys => true)
21
18
  symbolized_hash = {:a => 'b', :c => 'd'}
22
19
  unpacker.feed(MessagePack.pack(symbolized_hash)).read.should == symbolized_hash
@@ -0,0 +1,133 @@
1
+ require 'spec_helper'
2
+
3
+ describe MessagePack::Unpacker do
4
+ class ValueOne
5
+ attr_reader :num
6
+ def initialize(num)
7
+ @num = num
8
+ end
9
+ def ==(obj)
10
+ self.num == obj.num
11
+ end
12
+ def num
13
+ @num
14
+ end
15
+ def to_msgpack_ext
16
+ @num.to_msgpack
17
+ end
18
+ def self.from_msgpack_ext(data)
19
+ self.new(MessagePack.unpack(data))
20
+ end
21
+ end
22
+
23
+ class ValueTwo
24
+ attr_reader :num_s
25
+ def initialize(num)
26
+ @num_s = num.to_s
27
+ end
28
+ def ==(obj)
29
+ self.num_s == obj.num_s
30
+ end
31
+ def num
32
+ @num_s.to_i
33
+ end
34
+ def to_msgpack_ext
35
+ @num_s.to_msgpack
36
+ end
37
+ def self.from_msgpack_ext(data)
38
+ self.new(MessagePack.unpack(data))
39
+ end
40
+ end
41
+
42
+ describe '#type_registered?' do
43
+ it 'receive Class or Integer, and return bool' do
44
+ skip("not supported yet in JRuby implementation") if java?
45
+ expect(subject.type_registered?(0x00)).to be_falsy
46
+ expect(subject.type_registered?(0x01)).to be_falsy
47
+ expect(subject.type_registered?(::ValueOne)).to be_falsy
48
+ end
49
+
50
+ it 'returns true if specified type or class is already registered' do
51
+ skip("not supported yet in JRuby implementation") if java?
52
+ subject.register_type(0x30, ::ValueOne, :from_msgpack_ext)
53
+ subject.register_type(0x31, ::ValueTwo, :from_msgpack_ext)
54
+
55
+ expect(subject.type_registered?(0x00)).to be_falsy
56
+ expect(subject.type_registered?(0x01)).to be_falsy
57
+
58
+ expect(subject.type_registered?(0x30)).to be_truthy
59
+ expect(subject.type_registered?(0x31)).to be_truthy
60
+ expect(subject.type_registered?(::ValueOne)).to be_truthy
61
+ expect(subject.type_registered?(::ValueTwo)).to be_truthy
62
+ end
63
+
64
+ it 'cannot detect unpack rule with block, not method' do
65
+ skip("not supported yet in JRuby implementation") if java?
66
+ subject.register_type(0x40){|data| ValueOne.from_msgpack_ext(data) }
67
+
68
+ expect(subject.type_registered?(0x40)).to be_truthy
69
+ expect(subject.type_registered?(ValueOne)).to be_falsy
70
+ end
71
+ end
72
+
73
+ context 'with ext definitions' do
74
+ it 'get type and class mapping for packing' do
75
+ skip("not supported yet in JRuby implementation") if java?
76
+ unpacker = MessagePack::Unpacker.new
77
+ unpacker.register_type(0x01){|data| ValueOne.from_msgpack_ext }
78
+ unpacker.register_type(0x02){|data| ValueTwo.from_msgpack_ext(data) }
79
+
80
+ unpacker = MessagePack::Unpacker.new
81
+ unpacker.register_type(0x01, ValueOne, :from_msgpack_ext)
82
+ unpacker.register_type(0x02, ValueTwo, :from_msgpack_ext)
83
+ end
84
+
85
+ it 'returns a Array of Hash which contains :type, :class and :unpacker' do
86
+ skip("not supported yet in JRuby implementation") if java?
87
+ unpacker = MessagePack::Unpacker.new
88
+ unpacker.register_type(0x02, ValueTwo, :from_msgpack_ext)
89
+ unpacker.register_type(0x01, ValueOne, :from_msgpack_ext)
90
+
91
+ list = unpacker.registered_types
92
+
93
+ expect(list).to be_a(Array)
94
+ expect(list.size).to eq(2)
95
+
96
+ one = list[0]
97
+ expect(one.keys.sort).to eq([:type, :class, :unpacker].sort)
98
+ expect(one[:type]).to eq(0x01)
99
+ expect(one[:class]).to eq(ValueOne)
100
+ expect(one[:unpacker]).to eq(:from_msgpack_ext)
101
+
102
+ two = list[1]
103
+ expect(two.keys.sort).to eq([:type, :class, :unpacker].sort)
104
+ expect(two[:type]).to eq(0x02)
105
+ expect(two[:class]).to eq(ValueTwo)
106
+ expect(two[:unpacker]).to eq(:from_msgpack_ext)
107
+ end
108
+
109
+ it 'returns a Array of Hash, which contains nil for class if block unpacker specified' do
110
+ skip("not supported yet in JRuby implementation") if java?
111
+ unpacker = MessagePack::Unpacker.new
112
+ unpacker.register_type(0x01){|data| ValueOne.from_msgpack_ext }
113
+ unpacker.register_type(0x02, &ValueTwo.method(:from_msgpack_ext))
114
+
115
+ list = unpacker.registered_types
116
+
117
+ expect(list).to be_a(Array)
118
+ expect(list.size).to eq(2)
119
+
120
+ one = list[0]
121
+ expect(one.keys.sort).to eq([:type, :class, :unpacker].sort)
122
+ expect(one[:type]).to eq(0x01)
123
+ expect(one[:class]).to be_nil
124
+ expect(one[:unpacker]).to be_instance_of(Proc)
125
+
126
+ two = list[1]
127
+ expect(two.keys.sort).to eq([:type, :class, :unpacker].sort)
128
+ expect(two[:type]).to eq(0x02)
129
+ expect(two[:class]).to be_nil
130
+ expect(two[:unpacker]).to be_instance_of(Proc)
131
+ end
132
+ end
133
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: msgpack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.7.0dev1
5
5
  platform: java
6
6
  authors:
7
7
  - Sadayuki Furuhashi
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-07-22 00:00:00.000000000 Z
12
+ date: 2015-09-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  requirement: !ruby/object:Gem::Requirement
@@ -44,7 +44,7 @@ dependencies:
44
44
  requirements:
45
45
  - - ~>
46
46
  - !ruby/object:Gem::Version
47
- version: 0.8.3
47
+ version: 0.9.4
48
48
  name: rake-compiler
49
49
  prerelease: false
50
50
  type: :development
@@ -52,35 +52,35 @@ dependencies:
52
52
  requirements:
53
53
  - - ~>
54
54
  - !ruby/object:Gem::Version
55
- version: 0.8.3
55
+ version: 0.9.4
56
56
  - !ruby/object:Gem::Dependency
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: '2.11'
62
- name: rspec
61
+ version: 0.4.3
62
+ name: rake-compiler-dock
63
63
  prerelease: false
64
64
  type: :development
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
67
  - - ~>
68
68
  - !ruby/object:Gem::Version
69
- version: '2.11'
69
+ version: 0.4.3
70
70
  - !ruby/object:Gem::Dependency
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ~>
74
74
  - !ruby/object:Gem::Version
75
- version: '1.7'
76
- name: json
75
+ version: '3.3'
76
+ name: rspec
77
77
  prerelease: false
78
78
  type: :development
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
81
  - - ~>
82
82
  - !ruby/object:Gem::Version
83
- version: '1.7'
83
+ version: '3.3'
84
84
  - !ruby/object:Gem::Dependency
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
@@ -95,6 +95,20 @@ dependencies:
95
95
  - - ~>
96
96
  - !ruby/object:Gem::Version
97
97
  version: 0.8.2
98
+ - !ruby/object:Gem::Dependency
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ name: json
105
+ prerelease: false
106
+ type: :development
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
98
112
  description: MessagePack is a binary-based efficient object serialization library. It enables to exchange structured objects between many languages like JSON. But unlike JSON, it is very fast and small.
99
113
  email:
100
114
  - frsyuki@gmail.com
@@ -104,7 +118,10 @@ extensions: []
104
118
  extra_rdoc_files: []
105
119
  files:
106
120
  - lib/msgpack.rb
121
+ - lib/msgpack/factory.rb
107
122
  - lib/msgpack/msgpack.jar
123
+ - lib/msgpack/packer.rb
124
+ - lib/msgpack/unpacker.rb
108
125
  - lib/msgpack/version.rb
109
126
  - spec/cases.json
110
127
  - spec/cases.msg
@@ -116,15 +133,20 @@ files:
116
133
  - spec/cruby/buffer_unpacker.rb
117
134
  - spec/cruby/packer_spec.rb
118
135
  - spec/cruby/unpacker_spec.rb
136
+ - spec/ext_value_spec.rb
137
+ - spec/exttypes.rb
138
+ - spec/factory_spec.rb
119
139
  - spec/format_spec.rb
120
140
  - spec/jruby/benchmarks/shootout_bm.rb
121
141
  - spec/jruby/benchmarks/symbolize_keys_bm.rb
122
142
  - spec/jruby/msgpack/unpacker_spec.rb
123
- - spec/jruby/msgpack_spec.rb
143
+ - spec/msgpack_spec.rb
124
144
  - spec/pack_spec.rb
145
+ - spec/packer_spec.rb
125
146
  - spec/random_compat.rb
126
147
  - spec/spec_helper.rb
127
148
  - spec/unpack_spec.rb
149
+ - spec/unpacker_spec.rb
128
150
  homepage: http://msgpack.org/
129
151
  licenses:
130
152
  - Apache 2.0
@@ -140,9 +162,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
140
162
  version: '0'
141
163
  required_rubygems_version: !ruby/object:Gem::Requirement
142
164
  requirements:
143
- - - '>='
165
+ - - '>'
144
166
  - !ruby/object:Gem::Version
145
- version: '0'
167
+ version: 1.3.1
146
168
  requirements: []
147
169
  rubyforge_project: msgpack
148
170
  rubygems_version: 2.4.6
@@ -160,13 +182,18 @@ test_files:
160
182
  - spec/cruby/buffer_unpacker.rb
161
183
  - spec/cruby/packer_spec.rb
162
184
  - spec/cruby/unpacker_spec.rb
185
+ - spec/ext_value_spec.rb
186
+ - spec/exttypes.rb
187
+ - spec/factory_spec.rb
163
188
  - spec/format_spec.rb
164
189
  - spec/jruby/benchmarks/shootout_bm.rb
165
190
  - spec/jruby/benchmarks/symbolize_keys_bm.rb
166
191
  - spec/jruby/msgpack/unpacker_spec.rb
167
- - spec/jruby/msgpack_spec.rb
192
+ - spec/msgpack_spec.rb
168
193
  - spec/pack_spec.rb
194
+ - spec/packer_spec.rb
169
195
  - spec/random_compat.rb
170
196
  - spec/spec_helper.rb
171
197
  - spec/unpack_spec.rb
198
+ - spec/unpacker_spec.rb
172
199
  has_rdoc: false