github.com/viant/toolbox@v0.34.5/encoder_test.go (about)

     1  package toolbox_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/viant/toolbox"
     9  )
    10  
    11  func TestEncoderFactory(t *testing.T) {
    12  	buffer := new(bytes.Buffer)
    13  	assert.NotNil(t, toolbox.NewJSONEncoderFactory().Create(buffer))
    14  }
    15  
    16  func TestMarshalEncoderFactory(t *testing.T) {
    17  	buffer := new(bytes.Buffer)
    18  	encoder := toolbox.NewMarshalerEncoderFactory().Create(buffer)
    19  	foo := &Foo200{"abc"}
    20  	err := encoder.Encode(foo)
    21  	assert.Nil(t, err)
    22  	assert.Equal(t, "abc", string(buffer.Bytes()))
    23  	err = encoder.Encode(&Foo201{})
    24  	assert.NotNil(t, err)
    25  }
    26  
    27  type Foo200 struct {
    28  	Attr string
    29  }
    30  
    31  func (m *Foo200) Marshal() ([]byte, error) {
    32  	return []byte(m.Attr), nil
    33  }
    34  
    35  type Foo201 struct {
    36  	Attr string
    37  }