@@ -69,97 +69,125 @@ func TestMarshal(t *testing.T) {
69
69
70
70
testCases := map [string ]struct {
71
71
input interface {}
72
- expected []byte
72
+ expected [][] byte // one of
73
73
}{
74
74
"Omitempty" : {
75
75
& struct { EBML TestOmitempty }{},
76
- []byte {
77
- 0x1a , 0x45 , 0xDF , 0xA3 , 0x80 ,
78
- },
76
+ [][]byte {{0x1a , 0x45 , 0xDF , 0xA3 , 0x80 }},
79
77
},
80
78
"NoOmitempty" : {
81
79
& 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
+ },
87
87
},
88
88
},
89
89
"SliceOmitempty" : {
90
90
& struct {
91
91
EBML TestSliceOmitempty
92
92
}{TestSliceOmitempty {make ([]uint64 , 0 )}},
93
- []byte {
94
- 0x1a , 0x45 , 0xDF , 0xA3 , 0x80 ,
95
- },
93
+ [][]byte {{0x1a , 0x45 , 0xDF , 0xA3 , 0x80 }},
96
94
},
97
95
"SliceOmitemptyNested" : {
98
96
& struct {
99
97
EBML []TestSliceOmitempty `ebml:"EBML,omitempty"`
100
98
}{make ([]TestSliceOmitempty , 3 )},
101
- []byte {},
99
+ [][] byte {{} },
102
100
},
103
101
"SliceNoOmitempty" : {
104
102
& struct {
105
103
EBML TestSliceNoOmitempty
106
104
}{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
+ },
111
111
},
112
112
},
113
113
"Sized" : {
114
114
& 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
+ },
122
124
},
123
125
},
124
126
"SizedAndOverflow" : {
125
127
& 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
+ },
133
137
},
134
138
},
135
139
"Ptr" : {
136
140
& 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
+ },
141
147
},
142
148
},
143
149
"PtrOmitempty" : {
144
150
& struct { EBML TestPtrOmitempty }{TestPtrOmitempty {& str , & uinteger }},
145
- []byte {
146
- 0x1A , 0x45 , 0xDF , 0xA3 , 0x80 ,
147
- },
151
+ [][]byte {{0x1A , 0x45 , 0xDF , 0xA3 , 0x80 }},
148
152
},
149
153
"Interface" : {
150
154
& 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
+ },
155
161
},
156
162
},
157
163
"InterfacePtr" : {
158
164
& 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
+ },
163
191
},
164
192
},
165
193
"Block" : {
@@ -168,31 +196,31 @@ func TestMarshal(t *testing.T) {
168
196
TrackNumber : 0x01 , Timecode : 0x0123 , Lacing : LacingNo , Data : [][]byte {{0x01 }},
169
197
},
170
198
},
171
- []byte {0xA3 , 0x85 , 0x81 , 0x01 , 0x23 , 0x00 , 0x01 },
199
+ [][] byte {{ 0xA3 , 0x85 , 0x81 , 0x01 , 0x23 , 0x00 , 0x01 } },
172
200
},
173
201
"BlockXiph" : {
174
202
& TestBlocks {
175
203
Block : Block {
176
204
TrackNumber : 0x01 , Timecode : 0x0123 , Lacing : LacingXiph , Data : [][]byte {{0x01 }, {0x02 }},
177
205
},
178
206
},
179
- []byte {0xA3 , 0x88 , 0x81 , 0x01 , 0x23 , 0x02 , 0x01 , 0x01 , 0x01 , 0x02 },
207
+ [][] byte {{ 0xA3 , 0x88 , 0x81 , 0x01 , 0x23 , 0x02 , 0x01 , 0x01 , 0x01 , 0x02 } },
180
208
},
181
209
"BlockFixed" : {
182
210
& TestBlocks {
183
211
Block : Block {
184
212
TrackNumber : 0x01 , Timecode : 0x0123 , Lacing : LacingFixed , Data : [][]byte {{0x01 }, {0x02 }},
185
213
},
186
214
},
187
- []byte {0xA3 , 0x87 , 0x81 , 0x01 , 0x23 , 0x04 , 0x01 , 0x01 , 0x02 },
215
+ [][] byte {{ 0xA3 , 0x87 , 0x81 , 0x01 , 0x23 , 0x04 , 0x01 , 0x01 , 0x02 } },
188
216
},
189
217
"BlockEBML" : {
190
218
& TestBlocks {
191
219
Block : Block {
192
220
TrackNumber : 0x01 , Timecode : 0x0123 , Lacing : LacingEBML , Data : [][]byte {{0x01 }, {0x02 }},
193
221
},
194
222
},
195
- []byte {0xA3 , 0x88 , 0x81 , 0x01 , 0x23 , 0x06 , 0x01 , 0x81 , 0x01 , 0x02 },
223
+ [][] byte {{ 0xA3 , 0x88 , 0x81 , 0x01 , 0x23 , 0x06 , 0x01 , 0x81 , 0x01 , 0x02 } },
196
224
},
197
225
}
198
226
@@ -202,9 +230,12 @@ func TestMarshal(t *testing.T) {
202
230
if err := Marshal (c .input , & b ); err != nil {
203
231
t .Fatalf ("Unexpected error: '%v'" , err )
204
232
}
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
+ }
207
237
}
238
+ t .Errorf ("Marshaled binary doesn't match:\n expected one of:\n %v,\n got:\n %v" , c .expected , b .Bytes ())
208
239
})
209
240
}
210
241
}
@@ -224,6 +255,14 @@ func TestMarshal_Error(t *testing.T) {
224
255
}{},
225
256
ErrUnknownElementName ,
226
257
},
258
+ "InvalidMapKey" : {
259
+ & map [int ]interface {}{1 : "test" },
260
+ ErrNonStringMapKey ,
261
+ },
262
+ "InvalidType" : {
263
+ & []int {},
264
+ ErrIncompatibleType ,
265
+ },
227
266
}
228
267
for n , c := range testCases {
229
268
t .Run (n , func (t * testing.T ) {
0 commit comments