msgpack 0.6.2-x86-mingw32 → 0.7.0dev1-x86-mingw32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +33 -0
  3. data/README.rdoc +2 -6
  4. data/Rakefile +9 -0
  5. data/appveyor.yml +18 -0
  6. data/doclib/msgpack/error.rb +6 -1
  7. data/doclib/msgpack/extension_value.rb +9 -0
  8. data/doclib/msgpack/factory.rb +68 -0
  9. data/doclib/msgpack/packer.rb +38 -0
  10. data/doclib/msgpack/unpacker.rb +39 -2
  11. data/ext/java/org/msgpack/jruby/Buffer.java +4 -0
  12. data/ext/java/org/msgpack/jruby/Decoder.java +44 -0
  13. data/ext/java/org/msgpack/jruby/Encoder.java +46 -4
  14. data/ext/java/org/msgpack/jruby/ExtensionValue.java +55 -60
  15. data/ext/java/org/msgpack/jruby/MessagePackLibrary.java +18 -5
  16. data/ext/java/org/msgpack/jruby/Packer.java +17 -4
  17. data/ext/java/org/msgpack/jruby/Unpacker.java +59 -2
  18. data/ext/msgpack/buffer_class.c +2 -2
  19. data/ext/msgpack/buffer_class.h +1 -1
  20. data/ext/msgpack/compat.h +12 -0
  21. data/ext/msgpack/core_ext.c +15 -0
  22. data/ext/msgpack/extconf.rb +4 -1
  23. data/ext/msgpack/extension_value_class.c +34 -0
  24. data/ext/msgpack/extension_value_class.h +31 -0
  25. data/ext/msgpack/factory_class.c +213 -0
  26. data/ext/msgpack/factory_class.h +35 -0
  27. data/ext/msgpack/packer.c +14 -7
  28. data/ext/msgpack/packer.h +46 -8
  29. data/ext/msgpack/packer_class.c +92 -45
  30. data/ext/msgpack/packer_class.h +4 -2
  31. data/ext/msgpack/packer_ext_registry.c +87 -0
  32. data/ext/msgpack/packer_ext_registry.h +101 -0
  33. data/ext/msgpack/rbinit.c +4 -0
  34. data/ext/msgpack/unpacker.c +128 -23
  35. data/ext/msgpack/unpacker.h +11 -0
  36. data/ext/msgpack/unpacker_class.c +117 -38
  37. data/ext/msgpack/unpacker_class.h +4 -2
  38. data/ext/msgpack/unpacker_ext_registry.c +68 -0
  39. data/ext/msgpack/unpacker_ext_registry.h +62 -0
  40. data/lib/msgpack.rb +4 -0
  41. data/lib/msgpack/factory.rb +60 -0
  42. data/lib/msgpack/packer.rb +28 -0
  43. data/lib/msgpack/unpacker.rb +28 -0
  44. data/lib/msgpack/version.rb +1 -1
  45. data/msgpack.gemspec +4 -3
  46. data/spec/cruby/buffer_io_spec.rb +2 -3
  47. data/spec/cruby/buffer_spec.rb +1 -3
  48. data/spec/cruby/unpacker_spec.rb +14 -2
  49. data/spec/ext_value_spec.rb +99 -0
  50. data/spec/exttypes.rb +51 -0
  51. data/spec/factory_spec.rb +236 -0
  52. data/spec/jruby/msgpack/unpacker_spec.rb +25 -0
  53. data/spec/{jruby/msgpack_spec.rb → msgpack_spec.rb} +1 -1
  54. data/spec/pack_spec.rb +0 -6
  55. data/spec/packer_spec.rb +95 -0
  56. data/spec/spec_helper.rb +12 -1
  57. data/spec/unpack_spec.rb +1 -4
  58. data/spec/unpacker_spec.rb +133 -0
  59. metadata +50 -15
  60. data/Dockerfile +0 -55
data/lib/msgpack.rb CHANGED
@@ -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
@@ -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
data/msgpack.gemspec CHANGED
@@ -24,8 +24,9 @@ Gem::Specification.new do |s|
24
24
 
25
25
  s.add_development_dependency 'bundler', ['~> 1.0']
26
26
  s.add_development_dependency 'rake', ['~> 0.9.2']
27
- s.add_development_dependency 'rake-compiler', ['~> 0.8.3']
28
- s.add_development_dependency 'rspec', ['~> 2.11']
29
- s.add_development_dependency 'json', ['~> 1.7']
27
+ s.add_development_dependency 'rake-compiler', ['~> 0.9.4']
28
+ s.add_development_dependency 'rake-compiler-dock', ['~> 0.4.3']
29
+ s.add_development_dependency 'rspec', ['~> 3.3']
30
30
  s.add_development_dependency 'yard', ['~> 0.8.2']
31
+ s.add_development_dependency 'json'
31
32
  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
data/spec/exttypes.rb ADDED
@@ -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