gojay

high performance JSON encoder/decoder with stream API for Golang
git clone git://git.lair.cx/gojay
Log | Files | Refs | README | LICENSE

encode_interface_test.go (5246B)


      1 package gojay
      2 
      3 import (
      4 	"fmt"
      5 	"strings"
      6 	"testing"
      7 
      8 	"github.com/stretchr/testify/assert"
      9 )
     10 
     11 var encoderTestCases = []struct {
     12 	v            interface{}
     13 	expectations func(t *testing.T, b string, err error)
     14 }{
     15 	{
     16 		v: 100,
     17 		expectations: func(t *testing.T, b string, err error) {
     18 			assert.Nil(t, err, "err should be nil")
     19 			assert.Equal(t, "100", b, "b should equal 100")
     20 		},
     21 	},
     22 	{
     23 		v: int64(100),
     24 		expectations: func(t *testing.T, b string, err error) {
     25 			assert.Nil(t, err, "err should be nil")
     26 			assert.Equal(t, "100", b, "b should equal 100")
     27 		},
     28 	},
     29 	{
     30 		v: int32(100),
     31 		expectations: func(t *testing.T, b string, err error) {
     32 			assert.Nil(t, err, "err should be nil")
     33 			assert.Equal(t, "100", b, "b should equal 100")
     34 		},
     35 	},
     36 	{
     37 		v: int8(100),
     38 		expectations: func(t *testing.T, b string, err error) {
     39 			assert.Nil(t, err, "err should be nil")
     40 			assert.Equal(t, "100", b, "b should equal 100")
     41 		},
     42 	},
     43 	{
     44 		v: uint64(100),
     45 		expectations: func(t *testing.T, b string, err error) {
     46 			assert.Nil(t, err, "err should be nil")
     47 			assert.Equal(t, "100", b, "b should equal 100")
     48 		},
     49 	},
     50 	{
     51 		v: uint32(100),
     52 		expectations: func(t *testing.T, b string, err error) {
     53 			assert.Nil(t, err, "err should be nil")
     54 			assert.Equal(t, "100", b, "b should equal 100")
     55 		},
     56 	},
     57 	{
     58 		v: uint16(100),
     59 		expectations: func(t *testing.T, b string, err error) {
     60 			assert.Nil(t, err, "err should be nil")
     61 			assert.Equal(t, "100", b, "b should equal 100")
     62 		},
     63 	},
     64 	{
     65 		v: uint8(100),
     66 		expectations: func(t *testing.T, b string, err error) {
     67 			assert.Nil(t, err, "err should be nil")
     68 			assert.Equal(t, "100", b, "b should equal 100")
     69 		},
     70 	},
     71 	{
     72 		v: float64(100.12),
     73 		expectations: func(t *testing.T, b string, err error) {
     74 			assert.Nil(t, err, "err should be nil")
     75 			assert.Equal(t, "100.12", b, "b should equal 100.12")
     76 		},
     77 	},
     78 	{
     79 		v: float32(100.12),
     80 		expectations: func(t *testing.T, b string, err error) {
     81 			assert.Nil(t, err, "err should be nil")
     82 			assert.Equal(t, "100.12", b, "b should equal 100.12")
     83 		},
     84 	},
     85 	{
     86 		v: true,
     87 		expectations: func(t *testing.T, b string, err error) {
     88 			assert.Nil(t, err, "err should be nil")
     89 			assert.Equal(t, "true", b, "b should equal true")
     90 		},
     91 	},
     92 	{
     93 		v: "hello world",
     94 		expectations: func(t *testing.T, b string, err error) {
     95 			assert.Nil(t, err, "err should be nil")
     96 			assert.Equal(t, `"hello world"`, b, `b should equal "hello world"`)
     97 		},
     98 	},
     99 	{
    100 		v: "hello world",
    101 		expectations: func(t *testing.T, b string, err error) {
    102 			assert.Nil(t, err, "err should be nil")
    103 			assert.Equal(t, `"hello world"`, b, `b should equal "hello world"`)
    104 		},
    105 	},
    106 	{
    107 		v: &TestEncodingArrStrings{"hello world", "foo bar"},
    108 		expectations: func(t *testing.T, b string, err error) {
    109 			assert.Nil(t, err, "err should be nil")
    110 			assert.Equal(t, `["hello world","foo bar"]`, b, `b should equal ["hello world","foo bar"]`)
    111 		},
    112 	},
    113 	{
    114 		v: &testObject{
    115 			"漢字", nil, 1, nil, 1, nil, 1, nil, 1, nil, 1, nil,
    116 			1, nil, 1, nil, 1, nil, 1, nil, 1.1, nil, 1.1, nil, true, nil,
    117 			&testObject{}, testSliceInts{}, []interface{}{"h", "o", "l", "a"},
    118 		},
    119 		expectations: func(t *testing.T, b string, err error) {
    120 			assert.Nil(t, err, "err should be nil")
    121 			assert.Equal(t, `{"testStr":"漢字","testInt":1,"testInt64":1,"testInt32":1,"testInt16":1,"testInt8":1,"testUint64":1,"testUint32":1,"testUint16":1,"testUint8":1,"testFloat64":1.1,"testFloat32":1.1,"testBool":true}`, string(b), `string(b) should equal {"testStr":"漢字","testInt":1,"testInt64":1,"testInt32":1,"testInt16":1,"testInt8":1,"testUint64":1,"testUint32":1,"testUint16":1,"testUint8":1,"testFloat64":1.1,"testFloat32":1.1,"testBool":true}`)
    122 		},
    123 	},
    124 	{
    125 		v: &struct{}{},
    126 		expectations: func(t *testing.T, b string, err error) {
    127 			assert.NotNil(t, err, "err should be nil")
    128 			assert.IsType(t, InvalidMarshalError(""), err, "err should be of type InvalidMarshalError")
    129 			var s = struct{}{}
    130 			assert.Equal(t, fmt.Sprintf(invalidMarshalErrorMsg, &s), err.Error(), "err message should be equal to invalidMarshalErrorMsg")
    131 		},
    132 	},
    133 }
    134 
    135 func TestEncoderInterfaceEncodeAPI(t *testing.T) {
    136 	t.Run("encode-all-types", func(t *testing.T) {
    137 		for _, test := range encoderTestCases {
    138 			builder := &strings.Builder{}
    139 			enc := BorrowEncoder(builder)
    140 			err := enc.Encode(test.v)
    141 			enc.Release()
    142 			test.expectations(t, builder.String(), err)
    143 		}
    144 	})
    145 	t.Run("encode-all-types-write-error", func(t *testing.T) {
    146 		v := ""
    147 		w := TestWriterError("")
    148 		enc := BorrowEncoder(w)
    149 		err := enc.Encode(v)
    150 		assert.NotNil(t, err, "err should not be nil")
    151 	})
    152 	t.Run("encode-all-types-pool-error", func(t *testing.T) {
    153 		v := ""
    154 		w := TestWriterError("")
    155 		enc := BorrowEncoder(w)
    156 		enc.isPooled = 1
    157 		defer func() {
    158 			err := recover()
    159 			assert.NotNil(t, err, "err should not be nil")
    160 			assert.IsType(t, InvalidUsagePooledEncoderError(""), err, "err should be of type InvalidUsagePooledEncoderError")
    161 		}()
    162 		_ = enc.Encode(v)
    163 		assert.True(t, false, "should not be called as decoder should have panicked")
    164 	})
    165 }
    166 
    167 func TestEncoderInterfaceMarshalAPI(t *testing.T) {
    168 	t.Run("marshal-all-types", func(t *testing.T) {
    169 		for _, test := range encoderTestCases {
    170 			b, err := Marshal(test.v)
    171 			test.expectations(t, string(b), err)
    172 		}
    173 	})
    174 }