|
17 | 17 | package scale |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "bytes" |
20 | 21 | "math/big" |
21 | 22 | "reflect" |
22 | 23 | "testing" |
@@ -189,7 +190,6 @@ func Test_unmarshal_optionality(t *testing.T) { |
189 | 190 | if diff != "" { |
190 | 191 | t.Errorf("decodeState.unmarshal() = %s", diff) |
191 | 192 | } |
192 | | - |
193 | 193 | } |
194 | 194 | }) |
195 | 195 | } |
@@ -238,3 +238,66 @@ func Test_unmarshal_optionality_nil_case(t *testing.T) { |
238 | 238 | }) |
239 | 239 | } |
240 | 240 | } |
| 241 | + |
| 242 | +func Test_Decoder_Decode(t *testing.T) { |
| 243 | + for _, tt := range newTests(fixedWidthIntegerTests, variableWidthIntegerTests, stringTests, |
| 244 | + boolTests, sliceTests, arrayTests, |
| 245 | + ) { |
| 246 | + t.Run(tt.name, func(t *testing.T) { |
| 247 | + dst := reflect.New(reflect.TypeOf(tt.in)).Elem().Interface() |
| 248 | + wantBuf := bytes.NewBuffer(tt.want) |
| 249 | + d := NewDecoder(wantBuf) |
| 250 | + if err := d.Decode(&dst); (err != nil) != tt.wantErr { |
| 251 | + t.Errorf("Decoder.Decode() error = %v, wantErr %v", err, tt.wantErr) |
| 252 | + return |
| 253 | + } |
| 254 | + if !reflect.DeepEqual(dst, tt.in) { |
| 255 | + t.Errorf("Decoder.Decode() = %v, want %v", dst, tt.in) |
| 256 | + } |
| 257 | + }) |
| 258 | + } |
| 259 | +} |
| 260 | + |
| 261 | +func Test_Decoder_Decode_MultipleCalls(t *testing.T) { |
| 262 | + tests := []struct { |
| 263 | + name string |
| 264 | + ins []interface{} |
| 265 | + want []byte |
| 266 | + wantErr []bool |
| 267 | + }{ |
| 268 | + { |
| 269 | + name: "int64 and []byte", |
| 270 | + ins: []interface{}{int64(9223372036854775807), []byte{0x01}}, |
| 271 | + want: append([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, []byte{0x04, 0x01}...), |
| 272 | + }, |
| 273 | + { |
| 274 | + name: "eof error", |
| 275 | + ins: []interface{}{int64(9223372036854775807), []byte{0x01}}, |
| 276 | + want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, |
| 277 | + wantErr: []bool{false, true}, |
| 278 | + }, |
| 279 | + } |
| 280 | + for _, tt := range tests { |
| 281 | + t.Run(tt.name, func(t *testing.T) { |
| 282 | + buf := bytes.NewBuffer(tt.want) |
| 283 | + d := NewDecoder(buf) |
| 284 | + |
| 285 | + for i := range tt.ins { |
| 286 | + in := tt.ins[i] |
| 287 | + dst := reflect.New(reflect.TypeOf(in)).Elem().Interface() |
| 288 | + var wantErr bool |
| 289 | + if len(tt.wantErr) > i { |
| 290 | + wantErr = tt.wantErr[i] |
| 291 | + } |
| 292 | + if err := d.Decode(&dst); (err != nil) != wantErr { |
| 293 | + t.Errorf("Decoder.Decode() error = %v, wantErr %v", err, tt.wantErr[i]) |
| 294 | + return |
| 295 | + } |
| 296 | + if !wantErr && !reflect.DeepEqual(dst, in) { |
| 297 | + t.Errorf("Decoder.Decode() = %v, want %v", dst, in) |
| 298 | + return |
| 299 | + } |
| 300 | + } |
| 301 | + }) |
| 302 | + } |
| 303 | +} |
0 commit comments