(t *testing.T)
| 365 | } |
| 366 | |
| 367 | func TestCodec(t *testing.T) { |
| 368 | for _, v1 := range testValues { |
| 369 | t.Run(testName(v1), func(t *testing.T) { |
| 370 | v2 := newValue(v1) |
| 371 | |
| 372 | a, err := json.MarshalIndent(v1, "", "\t") |
| 373 | if err != nil { |
| 374 | t.Error(err) |
| 375 | return |
| 376 | } |
| 377 | a = append(a, '\n') |
| 378 | |
| 379 | buf := &bytes.Buffer{} |
| 380 | enc := NewEncoder(buf) |
| 381 | enc.SetIndent("", "\t") |
| 382 | |
| 383 | if err := enc.Encode(v1); err != nil { |
| 384 | t.Error(err) |
| 385 | return |
| 386 | } |
| 387 | b := buf.Bytes() |
| 388 | |
| 389 | if !Valid(b) { |
| 390 | t.Error("invalid JSON representation") |
| 391 | } |
| 392 | |
| 393 | if !bytes.Equal(a, b) { |
| 394 | t.Error("JSON representations mismatch") |
| 395 | t.Log("expected:", string(a)) |
| 396 | t.Log("found: ", string(b)) |
| 397 | } |
| 398 | |
| 399 | dec := NewDecoder(bytes.NewBuffer(b)) |
| 400 | |
| 401 | if err := dec.Decode(v2.Interface()); err != nil { |
| 402 | t.Errorf("%T: %v", err, err) |
| 403 | return |
| 404 | } |
| 405 | |
| 406 | x1 := v1 |
| 407 | x2 := v2.Elem().Interface() |
| 408 | |
| 409 | if !reflect.DeepEqual(x1, x2) { |
| 410 | t.Error("values mismatch") |
| 411 | t.Logf("expected: %#v", x1) |
| 412 | t.Logf("found: %#v", x2) |
| 413 | } |
| 414 | |
| 415 | if b, err := io.ReadAll(dec.Buffered()); err != nil { |
| 416 | t.Error(err) |
| 417 | } else if len(b) != 0 { |
| 418 | t.Errorf("leftover trailing bytes in the decoder: %q", b) |
| 419 | } |
| 420 | }) |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | // TestCodecDuration isolates testing of time.Duration. The stdlib un/marshals |
nothing calls this directly
no test coverage detected
searching dependent graphs…