(t *testing.T)
| 571 | func (n *NilObject) IsNil() bool { return true } |
| 572 | |
| 573 | func TestEncodeObjectWithKeys(t *testing.T) { |
| 574 | t.Run( |
| 575 | "should not encode any key", |
| 576 | func(t *testing.T) { |
| 577 | var b strings.Builder |
| 578 | var enc = NewEncoder(&b) |
| 579 | var o = &ObjectWithKeys{} |
| 580 | var err = enc.EncodeObjectKeys(o, []string{}) |
| 581 | assert.Nil(t, err) |
| 582 | assert.Equal(t, `{}`, b.String()) |
| 583 | }, |
| 584 | ) |
| 585 | t.Run( |
| 586 | "should encode some keys", |
| 587 | func(t *testing.T) { |
| 588 | var b strings.Builder |
| 589 | var enc = NewEncoder(&b) |
| 590 | var o = &ObjectWithKeys{Str: "hello", Int: 420} |
| 591 | var err = enc.EncodeObjectKeys(o, []string{"string", "int"}) |
| 592 | assert.Nil(t, err) |
| 593 | assert.Equal( |
| 594 | t, |
| 595 | `{"string":"hello","string":"hello","string":"hello","int":420,"int":420,"int":420}`, |
| 596 | b.String(), |
| 597 | ) |
| 598 | }, |
| 599 | ) |
| 600 | t.Run("write-error", func(t *testing.T) { |
| 601 | w := TestWriterError("") |
| 602 | enc := NewEncoder(w) |
| 603 | o := &ObjectWithKeys{Str: "hello", Int: 420} |
| 604 | err := enc.EncodeObjectKeys(o, []string{"string", "int"}) |
| 605 | assert.NotNil(t, err, "Error should not be nil") |
| 606 | assert.Equal(t, "Test Error", err.Error(), "err.Error() should be 'Test Error'") |
| 607 | }) |
| 608 | t.Run("pool-error", func(t *testing.T) { |
| 609 | v := &TestEncoding{} |
| 610 | enc := BorrowEncoder(nil) |
| 611 | enc.isPooled = 1 |
| 612 | defer func() { |
| 613 | err := recover() |
| 614 | assert.NotNil(t, err, "err shouldnt be nil") |
| 615 | assert.IsType(t, InvalidUsagePooledEncoderError(""), err, "err should be of type InvalidUsagePooledEncoderError") |
| 616 | assert.Equal(t, "Invalid usage of pooled encoder", err.(InvalidUsagePooledEncoderError).Error(), "err should be of type InvalidUsagePooledDecoderError") |
| 617 | }() |
| 618 | _ = enc.EncodeObjectKeys(v, []string{}) |
| 619 | assert.True(t, false, "should not be called as it should have panicked") |
| 620 | }) |
| 621 | t.Run("interface-key-error", func(t *testing.T) { |
| 622 | builder := &strings.Builder{} |
| 623 | enc := NewEncoder(builder) |
| 624 | err := enc.EncodeObjectKeys(&testObjectWithUnknownType{struct{}{}}, []string{}) |
| 625 | assert.NotNil(t, err, "Error should not be nil") |
| 626 | assert.Equal(t, "Invalid type struct {} provided to Marshal", err.Error(), "err.Error() should be 'Invalid type struct {} provided to Marshal'") |
| 627 | }) |
| 628 | t.Run("encode-object-with-keys", func(t *testing.T) { |
| 629 | b := &strings.Builder{} |
| 630 | enc := NewEncoder(b) |
nothing calls this directly
no test coverage detected
searching dependent graphs…