@@ -69,97 +69,125 @@ func TestMarshal(t *testing.T) {
6969
7070 testCases := map [string ]struct {
7171 input interface {}
72- expected []byte
72+ expected [][] byte // one of
7373 }{
7474 "Omitempty" : {
7575 & struct { EBML TestOmitempty }{},
76- []byte {
77- 0x1a , 0x45 , 0xDF , 0xA3 , 0x80 ,
78- },
76+ [][]byte {{0x1a , 0x45 , 0xDF , 0xA3 , 0x80 }},
7977 },
8078 "NoOmitempty" : {
8179 & struct { EBML TestNoOmitempty }{},
82- []byte {
83- 0x1A , 0x45 , 0xDF , 0xA3 , 0x8B ,
84- 0x42 , 0x82 , 0x81 , 0x00 ,
85- 0x42 , 0x87 , 0x81 , 0x00 ,
86- 0x53 , 0xAB , 0x80 ,
80+ [][]byte {
81+ {
82+ 0x1A , 0x45 , 0xDF , 0xA3 , 0x8B ,
83+ 0x42 , 0x82 , 0x81 , 0x00 ,
84+ 0x42 , 0x87 , 0x81 , 0x00 ,
85+ 0x53 , 0xAB , 0x80 ,
86+ },
8787 },
8888 },
8989 "SliceOmitempty" : {
9090 & struct {
9191 EBML TestSliceOmitempty
9292 }{TestSliceOmitempty {make ([]uint64 , 0 )}},
93- []byte {
94- 0x1a , 0x45 , 0xDF , 0xA3 , 0x80 ,
95- },
93+ [][]byte {{0x1a , 0x45 , 0xDF , 0xA3 , 0x80 }},
9694 },
9795 "SliceOmitemptyNested" : {
9896 & struct {
9997 EBML []TestSliceOmitempty `ebml:"EBML,omitempty"`
10098 }{make ([]TestSliceOmitempty , 3 )},
101- []byte {},
99+ [][] byte {{} },
102100 },
103101 "SliceNoOmitempty" : {
104102 & struct {
105103 EBML TestSliceNoOmitempty
106104 }{TestSliceNoOmitempty {make ([]uint64 , 2 )}},
107- []byte {
108- 0x1a , 0x45 , 0xDF , 0xA3 , 0x88 ,
109- 0x42 , 0x87 , 0x81 , 0x00 ,
110- 0x42 , 0x87 , 0x81 , 0x00 ,
105+ [][]byte {
106+ {
107+ 0x1a , 0x45 , 0xDF , 0xA3 , 0x88 ,
108+ 0x42 , 0x87 , 0x81 , 0x00 ,
109+ 0x42 , 0x87 , 0x81 , 0x00 ,
110+ },
111111 },
112112 },
113113 "Sized" : {
114114 & struct { EBML TestSized }{TestSized {"a" , 1 , 0.0 , 0.0 , []byte {0x01 }}},
115- []byte {
116- 0x1A , 0x45 , 0xDF , 0xA3 , 0xA2 ,
117- 0x42 , 0x82 , 0x83 , 0x61 , 0x00 , 0x00 ,
118- 0x42 , 0x87 , 0x82 , 0x00 , 0x01 ,
119- 0x44 , 0x89 , 0x88 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
120- 0x44 , 0x89 , 0x84 , 0x00 , 0x00 , 0x00 , 0x00 ,
121- 0x53 , 0xAB , 0x82 , 0x01 , 0x00 ,
115+ [][]byte {
116+ {
117+ 0x1A , 0x45 , 0xDF , 0xA3 , 0xA2 ,
118+ 0x42 , 0x82 , 0x83 , 0x61 , 0x00 , 0x00 ,
119+ 0x42 , 0x87 , 0x82 , 0x00 , 0x01 ,
120+ 0x44 , 0x89 , 0x88 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
121+ 0x44 , 0x89 , 0x84 , 0x00 , 0x00 , 0x00 , 0x00 ,
122+ 0x53 , 0xAB , 0x82 , 0x01 , 0x00 ,
123+ },
122124 },
123125 },
124126 "SizedAndOverflow" : {
125127 & struct { EBML TestSized }{TestSized {"abc" , 0x012345 , 0.0 , 0.0 , []byte {0x01 , 0x02 , 0x03 }}},
126- []byte {
127- 0x1A , 0x45 , 0xDF , 0xA3 , 0xA5 ,
128- 0x42 , 0x82 , 0x84 , 0x61 , 0x62 , 0x63 , 0x00 ,
129- 0x42 , 0x87 , 0x83 , 0x01 , 0x23 , 0x45 ,
130- 0x44 , 0x89 , 0x88 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
131- 0x44 , 0x89 , 0x84 , 0x00 , 0x00 , 0x00 , 0x00 ,
132- 0x53 , 0xAB , 0x83 , 0x01 , 0x02 , 0x03 ,
128+ [][]byte {
129+ {
130+ 0x1A , 0x45 , 0xDF , 0xA3 , 0xA5 ,
131+ 0x42 , 0x82 , 0x84 , 0x61 , 0x62 , 0x63 , 0x00 ,
132+ 0x42 , 0x87 , 0x83 , 0x01 , 0x23 , 0x45 ,
133+ 0x44 , 0x89 , 0x88 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
134+ 0x44 , 0x89 , 0x84 , 0x00 , 0x00 , 0x00 , 0x00 ,
135+ 0x53 , 0xAB , 0x83 , 0x01 , 0x02 , 0x03 ,
136+ },
133137 },
134138 },
135139 "Ptr" : {
136140 & struct { EBML TestPtr }{TestPtr {& str , & uinteger }},
137- []byte {
138- 0x1A , 0x45 , 0xDF , 0xA3 , 0x88 ,
139- 0x42 , 0x82 , 0x81 , 0x00 ,
140- 0x42 , 0x87 , 0x81 , 0x00 ,
141+ [][]byte {
142+ {
143+ 0x1A , 0x45 , 0xDF , 0xA3 , 0x88 ,
144+ 0x42 , 0x82 , 0x81 , 0x00 ,
145+ 0x42 , 0x87 , 0x81 , 0x00 ,
146+ },
141147 },
142148 },
143149 "PtrOmitempty" : {
144150 & struct { EBML TestPtrOmitempty }{TestPtrOmitempty {& str , & uinteger }},
145- []byte {
146- 0x1A , 0x45 , 0xDF , 0xA3 , 0x80 ,
147- },
151+ [][]byte {{0x1A , 0x45 , 0xDF , 0xA3 , 0x80 }},
148152 },
149153 "Interface" : {
150154 & struct { EBML TestInterface }{TestInterface {str , uinteger }},
151- []byte {
152- 0x1A , 0x45 , 0xDF , 0xA3 , 0x88 ,
153- 0x42 , 0x82 , 0x81 , 0x00 ,
154- 0x42 , 0x87 , 0x81 , 0x00 ,
155+ [][]byte {
156+ {
157+ 0x1A , 0x45 , 0xDF , 0xA3 , 0x88 ,
158+ 0x42 , 0x82 , 0x81 , 0x00 ,
159+ 0x42 , 0x87 , 0x81 , 0x00 ,
160+ },
155161 },
156162 },
157163 "InterfacePtr" : {
158164 & struct { EBML TestInterface }{TestInterface {& str , & uinteger }},
159- []byte {
160- 0x1A , 0x45 , 0xDF , 0xA3 , 0x88 ,
161- 0x42 , 0x82 , 0x81 , 0x00 ,
162- 0x42 , 0x87 , 0x81 , 0x00 ,
165+ [][]byte {
166+ {
167+ 0x1A , 0x45 , 0xDF , 0xA3 , 0x88 ,
168+ 0x42 , 0x82 , 0x81 , 0x00 ,
169+ 0x42 , 0x87 , 0x81 , 0x00 ,
170+ },
171+ },
172+ },
173+ "Map" : {
174+ & map [string ]interface {}{
175+ "Info" : map [string ]interface {}{
176+ "MuxingApp" : "test" ,
177+ "WritingApp" : "abcd" ,
178+ },
179+ },
180+ [][]byte {
181+ {
182+ 0x15 , 0x49 , 0xA9 , 0x66 , 0x90 ,
183+ 0x4D , 0x80 , 0x85 , 0x74 , 0x65 , 0x73 , 0x74 , 0x00 ,
184+ 0x57 , 0x41 , 0x85 , 0x61 , 0x62 , 0x63 , 0x64 , 0x00 ,
185+ },
186+ { // Go map element order is unstable
187+ 0x15 , 0x49 , 0xA9 , 0x66 , 0x90 ,
188+ 0x57 , 0x41 , 0x85 , 0x61 , 0x62 , 0x63 , 0x64 , 0x00 ,
189+ 0x4D , 0x80 , 0x85 , 0x74 , 0x65 , 0x73 , 0x74 , 0x00 ,
190+ },
163191 },
164192 },
165193 "Block" : {
@@ -168,31 +196,31 @@ func TestMarshal(t *testing.T) {
168196 TrackNumber : 0x01 , Timecode : 0x0123 , Lacing : LacingNo , Data : [][]byte {{0x01 }},
169197 },
170198 },
171- []byte {0xA3 , 0x85 , 0x81 , 0x01 , 0x23 , 0x00 , 0x01 },
199+ [][] byte {{ 0xA3 , 0x85 , 0x81 , 0x01 , 0x23 , 0x00 , 0x01 } },
172200 },
173201 "BlockXiph" : {
174202 & TestBlocks {
175203 Block : Block {
176204 TrackNumber : 0x01 , Timecode : 0x0123 , Lacing : LacingXiph , Data : [][]byte {{0x01 }, {0x02 }},
177205 },
178206 },
179- []byte {0xA3 , 0x88 , 0x81 , 0x01 , 0x23 , 0x02 , 0x01 , 0x01 , 0x01 , 0x02 },
207+ [][] byte {{ 0xA3 , 0x88 , 0x81 , 0x01 , 0x23 , 0x02 , 0x01 , 0x01 , 0x01 , 0x02 } },
180208 },
181209 "BlockFixed" : {
182210 & TestBlocks {
183211 Block : Block {
184212 TrackNumber : 0x01 , Timecode : 0x0123 , Lacing : LacingFixed , Data : [][]byte {{0x01 }, {0x02 }},
185213 },
186214 },
187- []byte {0xA3 , 0x87 , 0x81 , 0x01 , 0x23 , 0x04 , 0x01 , 0x01 , 0x02 },
215+ [][] byte {{ 0xA3 , 0x87 , 0x81 , 0x01 , 0x23 , 0x04 , 0x01 , 0x01 , 0x02 } },
188216 },
189217 "BlockEBML" : {
190218 & TestBlocks {
191219 Block : Block {
192220 TrackNumber : 0x01 , Timecode : 0x0123 , Lacing : LacingEBML , Data : [][]byte {{0x01 }, {0x02 }},
193221 },
194222 },
195- []byte {0xA3 , 0x88 , 0x81 , 0x01 , 0x23 , 0x06 , 0x01 , 0x81 , 0x01 , 0x02 },
223+ [][] byte {{ 0xA3 , 0x88 , 0x81 , 0x01 , 0x23 , 0x06 , 0x01 , 0x81 , 0x01 , 0x02 } },
196224 },
197225 }
198226
@@ -202,9 +230,12 @@ func TestMarshal(t *testing.T) {
202230 if err := Marshal (c .input , & b ); err != nil {
203231 t .Fatalf ("Unexpected error: '%v'" , err )
204232 }
205- if ! bytes .Equal (c .expected , b .Bytes ()) {
206- t .Errorf ("Marshaled binary doesn't match:\n expected: %v,\n got: %v" , c .expected , b .Bytes ())
233+ for _ , expected := range c .expected {
234+ if bytes .Equal (expected , b .Bytes ()) {
235+ return
236+ }
207237 }
238+ t .Errorf ("Marshaled binary doesn't match:\n expected one of:\n %v,\n got:\n %v" , c .expected , b .Bytes ())
208239 })
209240 }
210241}
@@ -224,6 +255,14 @@ func TestMarshal_Error(t *testing.T) {
224255 }{},
225256 ErrUnknownElementName ,
226257 },
258+ "InvalidMapKey" : {
259+ & map [int ]interface {}{1 : "test" },
260+ ErrNonStringMapKey ,
261+ },
262+ "InvalidType" : {
263+ & []int {},
264+ ErrIncompatibleType ,
265+ },
227266 }
228267 for n , c := range testCases {
229268 t .Run (n , func (t * testing.T ) {
0 commit comments