MCPcopy Create free account
hub / github.com/francoispqt/gojay / TestEncodeObjectWithKeys

Function TestEncodeObjectWithKeys

encode_object_test.go:573–732  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

571func (n *NilObject) IsNil() bool { return true }
572
573func 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)

Callers

nothing calls this directly

Calls 15

EncodeObjectKeysMethod · 0.95
ObjectKeyWithKeysMethod · 0.95
StringKeyMethod · 0.95
EncodeObjectMethod · 0.95
writeByteMethod · 0.95
EncodeArrayMethod · 0.95
ObjectWithKeysMethod · 0.95
NewEncoderFunction · 0.85
TestWriterErrorTypeAlias · 0.85
BorrowEncoderFunction · 0.85
EncodeObjectFuncFuncType · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…