3535 assert_equal ( "😎" , MessagePack . unpack ( "😎" . to_msgpack ) )
3636end
3737
38- assert ( "Symbol#to_msgpack" ) do
39- assert_equal ( 'symbol' , MessagePack . unpack ( :symbol . to_msgpack ) )
40- assert_equal ( 'symbol' , MessagePack . unpack ( MessagePack . pack ( :symbol ) ) )
38+ if MessagePackTest ::SYMBOLS_ENABLED
39+ assert ( "Symbol#to_msgpack MRB_MSGPACK_SYMBOLS" ) do
40+ assert_equal ( :symbol , MessagePack . unpack ( :symbol . to_msgpack ) )
41+ assert_equal ( :symbol , MessagePack . unpack ( MessagePack . pack ( :symbol ) ) )
42+ end
43+ else
44+ assert ( "Symbol#to_msgpack" ) do
45+ assert_equal ( 'symbol' , MessagePack . unpack ( :symbol . to_msgpack ) )
46+ assert_equal ( 'symbol' , MessagePack . unpack ( MessagePack . pack ( :symbol ) ) )
47+ end
48+
49+ assert ( "Symbol#to_msgpack with registered ext type" ) do
50+ MessagePack . register_pack_type ( 0 , Symbol ) { |symbol | symbol . to_s }
51+ MessagePack . register_unpack_type ( 0 ) { |data | data . to_sym }
52+ assert_equal ( :symbol , MessagePack . unpack ( :symbol . to_msgpack ) )
53+
54+ hash = { key : 123 , nested : [ :array ] }
55+ assert_equal ( hash , MessagePack . unpack ( hash . to_msgpack ) )
56+ assert_equal ( hash , MessagePack . unpack ( MessagePack . pack ( hash ) ) )
57+ end
4158end
4259
4360assert ( "Array#to_msgpack" ) do
98115 assert_equal ( unpacked , [ value1 , value2 , value3 ] )
99116end
100117
118+ class TestClassFoo ; end
119+
101120assert ( "MessagePack.register_pack_type" ) do
102121 assert_raise ( RangeError , "ext type out of range" ) do
103- MessagePack . register_pack_type ( -1 , Symbol )
122+ MessagePack . register_pack_type ( -1 , TestClassFoo )
104123 end
105124
106125 assert_raise ( RangeError , "ext type out of range" ) do
107- MessagePack . register_pack_type ( 128 , Symbol )
126+ MessagePack . register_pack_type ( 128 , TestClassFoo )
108127 end
109128
110129 assert_raise ( ArgumentError , "no block given" ) do
111- MessagePack . register_pack_type ( 1 , Symbol )
130+ MessagePack . register_pack_type ( 127 , TestClassFoo )
112131 end
113132
114- assert_nil ( MessagePack . register_pack_type ( 1 , Symbol ) { } )
133+ assert_nil ( MessagePack . register_pack_type ( 127 , TestClassFoo ) { } )
115134end
116135
117136assert ( "MessagePack.register_unpack_type" ) do
124143 end
125144
126145 assert_raise ( ArgumentError , "no block given" ) do
127- MessagePack . register_unpack_type ( 1 )
146+ MessagePack . register_unpack_type ( 127 )
128147 end
129148
130- assert_nil ( MessagePack . register_unpack_type ( 1 ) { } )
131- end
132-
133- assert ( "Symbol#to_msgpack with registered ext type" ) do
134- MessagePack . register_pack_type ( 0 , Symbol ) { |symbol | symbol . to_s }
135- MessagePack . register_unpack_type ( 0 ) { |data | data . to_sym }
136- assert_equal ( :symbol , MessagePack . unpack ( :symbol . to_msgpack ) )
137-
138- hash = { key : 123 , nested : [ :array ] }
139- assert_equal ( hash , MessagePack . unpack ( hash . to_msgpack ) )
140- assert_equal ( hash , MessagePack . unpack ( MessagePack . pack ( hash ) ) )
149+ assert_nil ( MessagePack . register_unpack_type ( 127 ) { } )
141150end
142151
143152assert ( "Class#to_msgpack with registered ext type" ) do
144- MessagePack . register_pack_type ( 0 , Class ) { |mod | mod . to_s }
145- MessagePack . register_unpack_type ( 0 ) { |data | data . constantize }
153+ MessagePack . register_pack_type ( 1 , Class ) { |mod | mod . to_s }
154+ MessagePack . register_unpack_type ( 1 ) { |data | data . constantize }
146155 assert_equal ( MessagePack ::Error , MessagePack . unpack ( MessagePack ::Error . to_msgpack ) )
147156end
148157
149158assert ( "Registered ext type for one of the core types is ignored" ) do
150- MessagePack . register_pack_type ( 0 , Array ) { |array | nil }
151- MessagePack . register_unpack_type ( 0 ) { |data | nil }
159+ MessagePack . register_pack_type ( 1 , Array ) { |array | nil }
160+ MessagePack . register_unpack_type ( 1 ) { |data | nil }
152161 assert_equal ( [ 'item' ] , MessagePack . unpack ( [ 'item' ] . to_msgpack ) )
153162end
154163
155- assert ( "Unknown Ext Type raises a Exception when tried to unpack" ) do
156- assert_raise ( MessagePack ::Error ) do
157- MessagePack . register_pack_type ( 50 , Symbol ) { |symbol | symbol . to_s }
158- MessagePack . unpack ( :hallo . to_msgpack )
159- end
160- end
161164
162165assert ( "Extension types are inherited" ) do
163166 class Test
@@ -174,8 +177,8 @@ def ==(other)
174177
175178 class InheritsTest < Test ; end
176179
177- MessagePack . register_pack_type ( 0 , Test ) { |test | test . class . to_s + '#' + test . id }
178- MessagePack . register_unpack_type ( 0 ) do |data |
180+ MessagePack . register_pack_type ( 1 , Test ) { |test | test . class . to_s + '#' + test . id }
181+ MessagePack . register_unpack_type ( 1 ) do |data |
179182 class_name , id = data . split ( '#' )
180183 class_name . constantize . new ( id )
181184 end
@@ -186,12 +189,12 @@ class InheritsTest < Test; end
186189
187190assert ( "Extension types for modules" ) do
188191 module Mod ; end
189- MessagePack . register_pack_type ( 0 , Mod ) { |obj | 'packed' }
192+ MessagePack . register_pack_type ( 1 , Mod ) { |obj | 'packed' }
190193
191194 class Cls ; include Mod end
192- assert_equal ( Cls . new . to_msgpack , "\xc7 \x06 \x00 packed " )
195+ assert_equal ( Cls . new . to_msgpack , "\xc7 \x06 \x01 packed " )
193196
194- assert_equal ( Object . new . extend ( Mod ) . to_msgpack , "\xc7 \x06 \x00 packed " )
197+ assert_equal ( Object . new . extend ( Mod ) . to_msgpack , "\xc7 \x06 \x01 packed " )
195198end
196199
197200assert ( "C Packing and unpacking" ) do
0 commit comments