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