File: encoder_test.go

package info (click to toggle)
golang-github-viant-toolbox 0.33.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 1,280 kB
  • sloc: makefile: 16
file content (37 lines) | stat: -rw-r--r-- 726 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package toolbox_test

import (
	"bytes"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/viant/toolbox"
)

func TestEncoderFactory(t *testing.T) {
	buffer := new(bytes.Buffer)
	assert.NotNil(t, toolbox.NewJSONEncoderFactory().Create(buffer))
}

func TestMarshalEncoderFactory(t *testing.T) {
	buffer := new(bytes.Buffer)
	encoder := toolbox.NewMarshalerEncoderFactory().Create(buffer)
	foo := &Foo200{"abc"}
	err := encoder.Encode(foo)
	assert.Nil(t, err)
	assert.Equal(t, "abc", string(buffer.Bytes()))
	err = encoder.Encode(&Foo201{})
	assert.NotNil(t, err)
}

type Foo200 struct {
	Attr string
}

func (m *Foo200) Marshal() ([]byte, error) {
	return []byte(m.Attr), nil
}

type Foo201 struct {
	Attr string
}