commit e3df0eb2b312b848e3f0d9eaacbf800e6dafad3a
parent 8e6acaa77036a0122630fd41e99ce12c044b8114
Author: francoispqt <francois@parquet.ninja>
Date: Tue, 1 May 2018 20:02:00 +0800
clean encode tests
Diffstat:
M | encode_array_test.go | | | 374 | +++++++++++++++++++++++++++++++++++++++++-------------------------------------- |
M | encode_bool_test.go | | | 84 | +++++++++++++++++++++++++++++++++++++++++++++++-------------------------------- |
M | encode_interface_test.go | | | 70 | ++++++++++++++++++++++++++++++++++++---------------------------------- |
M | encode_number_test.go | | | 446 | +++++++++++++++++++++++++++++++++++++------------------------------------------ |
M | encode_object_test.go | | | 462 | ++++++++++++++++++++++++++++++++++++++++++------------------------------------- |
M | encode_string_test.go | | | 110 | +++++++++++++++++++++++++++++++++++++++++++++---------------------------------- |
6 files changed, 798 insertions(+), 748 deletions(-)
diff --git a/encode_array_test.go b/encode_array_test.go
@@ -29,59 +29,6 @@ func (t TestEncodingArr) IsNil() bool {
return t == nil
}
-func TestEncoderArrayObjects(t *testing.T) {
- v := &TestEncodingArr{
- &TestEncoding{
- test: "hello world",
- test2: "漢字",
- testInt: 1,
- testBool: true,
- testInterface: 1,
- sub: &SubObject{
- test1: 10,
- test2: "hello world",
- test3: 1.23543,
- testBool: true,
- sub: &SubObject{
- test1: 10,
- testBool: false,
- test2: "hello world",
- },
- },
- },
- &TestEncoding{
- test: "hello world",
- test2: "漢字",
- testInt: 1,
- testBool: true,
- sub: &SubObject{
- test1: 10,
- test2: "hello world",
- test3: 1.23543,
- testBool: true,
- sub: &SubObject{
- test1: 10,
- testBool: false,
- test2: "hello world",
- },
- },
- },
- nil,
- }
- r, err := Marshal(v)
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `[{"test":"hello world","test2":"漢字","testInt":1,"testBool":true,`+
- `"testArr":[],"testF64":0,"testF32":0,"testInterface":1,"sub":{"test1":10,"test2":"hello world",`+
- `"test3":1.23543,"testBool":true,"sub":{"test1":10,"test2":"hello world",`+
- `"test3":0,"testBool":false,"sub":{}}}},{"test":"hello world","test2":"漢字","testInt":1,`+
- `"testBool":true,"testArr":[],"testF64":0,"testF32":0,"sub":{"test1":10,"test2":"hello world","test3":1.23543,`+
- `"testBool":true,"sub":{"test1":10,"test2":"hello world","test3":0,"testBool":false,"sub":{}}}},{}]`,
- string(r),
- "Result of marshalling is different as the one expected")
-}
-
type testEncodingArrInterfaces []interface{}
func (t testEncodingArrInterfaces) MarshalArray(enc *Encoder) {
@@ -93,82 +40,138 @@ func (t testEncodingArrInterfaces) IsNil() bool {
return t == nil
}
-func TestEncoderArrayInterfaces(t *testing.T) {
- v := &testEncodingArrInterfaces{
- 1,
- int64(1),
- int32(1),
- int16(1),
- int8(1),
- uint64(1),
- uint32(1),
- uint16(1),
- uint8(1),
- float64(1.31),
- float32(1.31),
- &TestEncodingArr{},
- &TestEncodingArrStrings{},
- true,
- false,
- "test",
- &TestEncoding{
- test: "hello world",
- test2: "foobar",
- testInt: 1,
- testBool: true,
- },
- }
- r, err := MarshalArray(v)
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `[1,1,1,1,1,1,1,1,1.31,1.31,[],[],true,false,"test",{"test":"hello world","test2":"foobar","testInt":1,"testBool":true,"testArr":[],"testF64":0,"testF32":0,"sub":{}}]`,
- string(r),
- "Result of marshalling is different as the one expected")
-}
-
-func TestEncoderArrayInterfacesEncoderAPI(t *testing.T) {
- v := &testEncodingArrInterfaces{
- 1,
- int64(1),
- int32(1),
- int16(1),
- int8(1),
- uint64(1),
- uint32(1),
- uint16(1),
- uint8(1),
- float64(1.31),
- // float32(1.31),
- &TestEncodingArr{},
- true,
- "test",
- &TestEncoding{
- test: "hello world",
- test2: "foobar",
- testInt: 1,
- testBool: true,
- },
- }
- builder := &strings.Builder{}
- enc := BorrowEncoder(builder)
- defer enc.Release()
- err := enc.EncodeArray(v)
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `[1,1,1,1,1,1,1,1,1.31,[],true,"test",{"test":"hello world","test2":"foobar","testInt":1,"testBool":true,"testArr":[],"testF64":0,"testF32":0,"sub":{}}]`,
- builder.String(),
- "Result of marshalling is different as the one expected")
+func TestEncoderArrayMarshalAPI(t *testing.T) {
+ t.Run("array-objects", func(t *testing.T) {
+ v := &TestEncodingArr{
+ &TestEncoding{
+ test: "hello world",
+ test2: "漢字",
+ testInt: 1,
+ testBool: true,
+ testInterface: 1,
+ sub: &SubObject{
+ test1: 10,
+ test2: "hello world",
+ test3: 1.23543,
+ testBool: true,
+ sub: &SubObject{
+ test1: 10,
+ testBool: false,
+ test2: "hello world",
+ },
+ },
+ },
+ &TestEncoding{
+ test: "hello world",
+ test2: "漢字",
+ testInt: 1,
+ testBool: true,
+ sub: &SubObject{
+ test1: 10,
+ test2: "hello world",
+ test3: 1.23543,
+ testBool: true,
+ sub: &SubObject{
+ test1: 10,
+ testBool: false,
+ test2: "hello world",
+ },
+ },
+ },
+ nil,
+ }
+ r, err := Marshal(v)
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `[{"test":"hello world","test2":"漢字","testInt":1,"testBool":true,`+
+ `"testArr":[],"testF64":0,"testF32":0,"testInterface":1,"sub":{"test1":10,"test2":"hello world",`+
+ `"test3":1.23543,"testBool":true,"sub":{"test1":10,"test2":"hello world",`+
+ `"test3":0,"testBool":false,"sub":{}}}},{"test":"hello world","test2":"漢字","testInt":1,`+
+ `"testBool":true,"testArr":[],"testF64":0,"testF32":0,"sub":{"test1":10,"test2":"hello world","test3":1.23543,`+
+ `"testBool":true,"sub":{"test1":10,"test2":"hello world","test3":0,"testBool":false,"sub":{}}}},{}]`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("array-interfaces", func(t *testing.T) {
+ v := &testEncodingArrInterfaces{
+ 1,
+ int64(1),
+ int32(1),
+ int16(1),
+ int8(1),
+ uint64(1),
+ uint32(1),
+ uint16(1),
+ uint8(1),
+ float64(1.31),
+ float32(1.31),
+ &TestEncodingArr{},
+ &TestEncodingArrStrings{},
+ true,
+ false,
+ "test",
+ &TestEncoding{
+ test: "hello world",
+ test2: "foobar",
+ testInt: 1,
+ testBool: true,
+ },
+ }
+ r, err := MarshalArray(v)
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `[1,1,1,1,1,1,1,1,1.31,1.31,[],[],true,false,"test",{"test":"hello world","test2":"foobar","testInt":1,"testBool":true,"testArr":[],"testF64":0,"testF32":0,"sub":{}}]`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
}
-func TestEncoderArrayInterfacesEncoderAPIWriteError(t *testing.T) {
- v := &testEncodingArrInterfaces{}
- w := TestWriterError("")
- enc := BorrowEncoder(w)
- defer enc.Release()
- err := enc.EncodeArray(v)
- assert.NotNil(t, err, "err should not be nil")
+func TestEncoderArrayEncodeAPI(t *testing.T) {
+ t.Run("array-interfaces", func(t *testing.T) {
+ v := &testEncodingArrInterfaces{
+ 1,
+ int64(1),
+ int32(1),
+ int16(1),
+ int8(1),
+ uint64(1),
+ uint32(1),
+ uint16(1),
+ uint8(1),
+ float64(1.31),
+ // float32(1.31),
+ &TestEncodingArr{},
+ true,
+ "test",
+ &TestEncoding{
+ test: "hello world",
+ test2: "foobar",
+ testInt: 1,
+ testBool: true,
+ },
+ }
+ builder := &strings.Builder{}
+ enc := BorrowEncoder(builder)
+ defer enc.Release()
+ err := enc.EncodeArray(v)
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `[1,1,1,1,1,1,1,1,1.31,[],true,"test",{"test":"hello world","test2":"foobar","testInt":1,"testBool":true,"testArr":[],"testF64":0,"testF32":0,"sub":{}}]`,
+ builder.String(),
+ "Result of marshalling is different as the one expected")
+ })
+
+ t.Run("array-interfaces-write-error", func(t *testing.T) {
+ v := &testEncodingArrInterfaces{}
+ w := TestWriterError("")
+ enc := BorrowEncoder(w)
+ defer enc.Release()
+ err := enc.EncodeArray(v)
+ assert.NotNil(t, err, "err should not be nil")
+ })
}
// Array add with omit key tests
@@ -262,60 +265,69 @@ func (t TestEncodingObjOmitEmpty) IsNil() bool {
}
func TestEncoderArrayOmitEmpty(t *testing.T) {
- intArr := TestEncodingIntOmitEmpty{0, 1, 0, 1}
- b, err := Marshal(intArr)
- assert.Nil(t, err, "err must be nil")
- assert.Equal(t, `[1,1]`, string(b), "string(b) must be equal to `[1,1]`")
-
- floatArr := TestEncodingFloatOmitEmpty{0, 1, 0, 1}
- b, err = Marshal(floatArr)
- assert.Nil(t, err, "err must be nil")
- assert.Equal(t, `[1,1]`, string(b), "string(b) must be equal to `[1,1]`")
-
- float32Arr := TestEncodingFloat32OmitEmpty{0, 1, 0, 1}
- b, err = Marshal(float32Arr)
- assert.Nil(t, err, "err must be nil")
- assert.Equal(t, `[1,1]`, string(b), "string(b) must be equal to `[1,1]`")
-
- stringArr := TestEncodingStringOmitEmpty{"", "hello", "", "world"}
- b, err = Marshal(stringArr)
- assert.Nil(t, err, "err must be nil")
- assert.Equal(t, `["hello","world"]`, string(b), "string(b) must be equal to `[\"hello\",\"world\"]`")
-
- boolArr := TestEncodingBoolOmitEmpty{false, true, false, true}
- b, err = Marshal(boolArr)
- assert.Nil(t, err, "err must be nil")
- assert.Equal(t, `[true,true]`, string(b), "string(b) must be equal to `[true,true]`")
-
- arrArr := TestEncodingArrOmitEmpty{TestEncodingBoolOmitEmpty{true}, nil, TestEncodingBoolOmitEmpty{true}, nil}
- b, err = Marshal(arrArr)
- assert.Nil(t, err, "err must be nil")
- assert.Equal(t, `[[true],[true]]`, string(b), "string(b) must be equal to `[[true],[true]]`")
-
- objArr := TestEncodingObjOmitEmpty{&TestObjEmpty{true}, &TestObjEmpty{false}, &TestObjEmpty{true}, &TestObjEmpty{false}}
- b, err = Marshal(objArr)
- assert.Nil(t, err, "err must be nil")
- assert.Equal(t, `[{},{}]`, string(b), "string(b) must be equal to `[{},{}]`")
-}
-
-func TestEncoderAddInterfaceError(t *testing.T) {
- builder := &strings.Builder{}
- enc := NewEncoder(builder)
- enc.AddInterface(nil)
- assert.Nil(t, enc.err, "enc.Err() should not be nil")
- assert.Equal(t, "", builder.String(), "builder.String() should not be ''")
+ t.Run("omit-int", func(t *testing.T) {
+ intArr := TestEncodingIntOmitEmpty{0, 1, 0, 1}
+ b, err := Marshal(intArr)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, `[1,1]`, string(b), "string(b) must be equal to `[1,1]`")
+ })
+ t.Run("omit-float", func(t *testing.T) {
+ floatArr := TestEncodingFloatOmitEmpty{0, 1, 0, 1}
+ b, err := Marshal(floatArr)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, `[1,1]`, string(b), "string(b) must be equal to `[1,1]`")
+ })
+ t.Run("omit-float32", func(t *testing.T) {
+ float32Arr := TestEncodingFloat32OmitEmpty{0, 1, 0, 1}
+ b, err := Marshal(float32Arr)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, `[1,1]`, string(b), "string(b) must be equal to `[1,1]`")
+ })
+ t.Run("omit-string", func(t *testing.T) {
+ stringArr := TestEncodingStringOmitEmpty{"", "hello", "", "world"}
+ b, err := Marshal(stringArr)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, `["hello","world"]`, string(b), "string(b) must be equal to `[\"hello\",\"world\"]`")
+ })
+ t.Run("omit-bool", func(t *testing.T) {
+ boolArr := TestEncodingBoolOmitEmpty{false, true, false, true}
+ b, err := Marshal(boolArr)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, `[true,true]`, string(b), "string(b) must be equal to `[true,true]`")
+ })
+ t.Run("omit-arr", func(t *testing.T) {
+ arrArr := TestEncodingArrOmitEmpty{TestEncodingBoolOmitEmpty{true}, nil, TestEncodingBoolOmitEmpty{true}, nil}
+ b, err := Marshal(arrArr)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, `[[true],[true]]`, string(b), "string(b) must be equal to `[[true],[true]]`")
+ })
+ t.Run("omit-obj", func(t *testing.T) {
+ objArr := TestEncodingObjOmitEmpty{&TestObjEmpty{true}, &TestObjEmpty{false}, &TestObjEmpty{true}, &TestObjEmpty{false}}
+ b, err := Marshal(objArr)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, `[{},{}]`, string(b), "string(b) must be equal to `[{},{}]`")
+ })
}
-func TestEncoderArrayPooledError(t *testing.T) {
- v := &testEncodingArrInterfaces{}
- enc := BorrowEncoder(nil)
- enc.Release()
- defer func() {
- err := recover()
- assert.NotNil(t, err, "err shouldnot be nil")
- assert.IsType(t, InvalidUsagePooledEncoderError(""), err, "err should be of type InvalidUsagePooledEncoderError")
- assert.Equal(t, "Invalid usage of pooled encoder", err.(InvalidUsagePooledEncoderError).Error(), "err should be of type InvalidUsagePooledDecoderError")
- }()
- _ = enc.EncodeArray(v)
- assert.True(t, false, "should not be called as it should have panicked")
+func TestEncoderArrErrors(t *testing.T) {
+ t.Run("add-interface-error", func(t *testing.T) {
+ builder := &strings.Builder{}
+ enc := NewEncoder(builder)
+ enc.AddInterface(nil)
+ assert.Nil(t, enc.err, "enc.Err() should not be nil")
+ assert.Equal(t, "", builder.String(), "builder.String() should not be ''")
+ })
+ t.Run("array-pooled-error", func(t *testing.T) {
+ v := &testEncodingArrInterfaces{}
+ enc := BorrowEncoder(nil)
+ enc.Release()
+ defer func() {
+ err := recover()
+ assert.NotNil(t, err, "err shouldnot be nil")
+ assert.IsType(t, InvalidUsagePooledEncoderError(""), err, "err should be of type InvalidUsagePooledEncoderError")
+ assert.Equal(t, "Invalid usage of pooled encoder", err.(InvalidUsagePooledEncoderError).Error(), "err should be of type InvalidUsagePooledDecoderError")
+ }()
+ _ = enc.EncodeArray(v)
+ assert.True(t, false, "should not be called as it should have panicked")
+ })
}
diff --git a/encode_bool_test.go b/encode_bool_test.go
@@ -7,42 +7,58 @@ import (
"github.com/stretchr/testify/assert"
)
-func TestEncoderBoolTrue(t *testing.T) {
- builder := &strings.Builder{}
- enc := BorrowEncoder(builder)
- defer enc.Release()
- err := enc.EncodeBool(true)
- assert.Nil(t, err, "err must be nil")
- assert.Equal(t, "true", builder.String(), "string(b) must be equal to 'true'")
+func TestEncoderBoolMarshalAPI(t *testing.T) {
+ t.Run("true", func(t *testing.T) {
+ b, err := Marshal(true)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, "true", string(b), "string(b) must be equal to 'true'")
+ })
+ t.Run("false", func(t *testing.T) {
+ b, err := Marshal(false)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, "false", string(b), "string(b) must be equal to 'false'")
+ })
}
-func TestEncoderBoolFalse(t *testing.T) {
- builder := &strings.Builder{}
- enc := BorrowEncoder(builder)
- defer enc.Release()
- err := enc.EncodeBool(false)
- assert.Nil(t, err, "err must be nil")
- assert.Equal(t, "false", builder.String(), "string(b) must be equal to 'false'")
+func TestEncoderBoolEncodeAPI(t *testing.T) {
+ t.Run("true", func(t *testing.T) {
+ builder := &strings.Builder{}
+ enc := BorrowEncoder(builder)
+ defer enc.Release()
+ err := enc.EncodeBool(true)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, "true", builder.String(), "string(b) must be equal to 'true'")
+ })
+ t.Run("false", func(t *testing.T) {
+ builder := &strings.Builder{}
+ enc := BorrowEncoder(builder)
+ defer enc.Release()
+ err := enc.EncodeBool(false)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, "false", builder.String(), "string(b) must be equal to 'false'")
+ })
}
-func TestEncoderBoolPoolError(t *testing.T) {
- builder := &strings.Builder{}
- enc := BorrowEncoder(builder)
- enc.Release()
- defer func() {
- err := recover()
- assert.NotNil(t, err, "err shouldnot be nil")
- assert.IsType(t, InvalidUsagePooledEncoderError(""), err, "err should be of type InvalidUsagePooledEncoderError")
- assert.Equal(t, "Invalid usage of pooled encoder", err.(InvalidUsagePooledEncoderError).Error(), "err should be of type InvalidUsagePooledEncoderError")
- }()
- _ = enc.EncodeBool(false)
- assert.True(t, false, "should not be called as it should have panicked")
-}
-func TestEncoderBoolPoolEncoderAPIWriteError(t *testing.T) {
- v := true
- w := TestWriterError("")
- enc := BorrowEncoder(w)
- defer enc.Release()
- err := enc.EncodeBool(v)
- assert.NotNil(t, err, "err should not be nil")
+func TestEncoderBoolErrors(t *testing.T) {
+ t.Run("pool-error", func(t *testing.T) {
+ builder := &strings.Builder{}
+ enc := BorrowEncoder(builder)
+ enc.Release()
+ defer func() {
+ err := recover()
+ assert.NotNil(t, err, "err shouldnot be nil")
+ assert.IsType(t, InvalidUsagePooledEncoderError(""), err, "err should be of type InvalidUsagePooledEncoderError")
+ assert.Equal(t, "Invalid usage of pooled encoder", err.(InvalidUsagePooledEncoderError).Error(), "err should be of type InvalidUsagePooledEncoderError")
+ }()
+ _ = enc.EncodeBool(false)
+ assert.True(t, false, "should not be called as it should have panicked")
+ })
+ t.Run("encode-api-write-error", func(t *testing.T) {
+ v := true
+ w := TestWriterError("")
+ enc := BorrowEncoder(w)
+ defer enc.Release()
+ err := enc.EncodeBool(v)
+ assert.NotNil(t, err, "err should not be nil")
+ })
}
diff --git a/encode_interface_test.go b/encode_interface_test.go
@@ -128,41 +128,43 @@ var encoderTestCases = []struct {
},
}
-func TestEncoderInterfaceAllTypesEncoderAPI(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)
- }
-}
-
-func TestEncoderInterfaceAllTypesEncoderAPIWriteError(t *testing.T) {
- v := ""
- w := TestWriterError("")
- enc := BorrowEncoder(w)
- err := enc.Encode(v)
- assert.NotNil(t, err, "err should not be nil")
-}
-
-func TestEncoderInterfaceAllTypesEncoderAPIPoolError(t *testing.T) {
- v := ""
- w := TestWriterError("")
- enc := BorrowEncoder(w)
- enc.Release()
- defer func() {
- err := recover()
+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")
- 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")
+ })
+ t.Run("encode-all-types-pool-error", func(t *testing.T) {
+ v := ""
+ w := TestWriterError("")
+ enc := BorrowEncoder(w)
+ enc.Release()
+ 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 TestEncoderInterfaceAllTypesMarshalAPI(t *testing.T) {
- for _, test := range encoderTestCases {
- b, err := Marshal(test.v)
- test.expectations(t, string(b), err)
- }
+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)
+ }
+ })
}
diff --git a/encode_number_test.go b/encode_number_test.go
@@ -7,247 +7,223 @@ import (
"github.com/stretchr/testify/assert"
)
-func TestEncoderInt(t *testing.T) {
- r, err := Marshal(1)
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `1`,
- string(r),
- "Result of marshalling is different as the one expected")
-}
-func TestEncoderIntEncodeAPI(t *testing.T) {
- builder := &strings.Builder{}
- enc := NewEncoder(builder)
- err := enc.EncodeInt(1)
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `1`,
- builder.String(),
- "Result of marshalling is different as the one expected")
-}
-func TestEncoderIntEncodeAPIPoolError(t *testing.T) {
- builder := &strings.Builder{}
- enc := NewEncoder(builder)
- enc.Release()
- 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.EncodeInt(1)
- assert.True(t, false, "should not be called as decoder should have panicked")
-}
-func TestEncoderIntEncodeAPIWriteError(t *testing.T) {
- w := TestWriterError("")
- enc := NewEncoder(w)
- err := enc.EncodeInt(1)
- assert.NotNil(t, err, "err should not be nil")
- assert.Equal(t, "Test Error", err.Error(), "err should be of type InvalidUsagePooledEncoderError")
-}
-
-func TestEncoderInt64(t *testing.T) {
- r, err := Marshal(int64(1))
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `1`,
- string(r),
- "Result of marshalling is different as the one expected")
-}
-func TestEncoderInt64EncodeAPI(t *testing.T) {
- builder := &strings.Builder{}
- enc := NewEncoder(builder)
- err := enc.EncodeInt64(int64(1))
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `1`,
- builder.String(),
- "Result of marshalling is different as the one expected")
-}
-func TestEncoderInt64EncodeAPIPoolError(t *testing.T) {
- builder := &strings.Builder{}
- enc := NewEncoder(builder)
- enc.Release()
- 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.EncodeInt64(1)
- assert.True(t, false, "should not be called as decoder should have panicked")
-}
-func TestEncoderInt64EncodeAPIWriteError(t *testing.T) {
- w := TestWriterError("")
- enc := NewEncoder(w)
- err := enc.EncodeInt64(1)
- assert.NotNil(t, err, "err should not be nil")
- assert.Equal(t, "Test Error", err.Error(), "err should be of type InvalidUsagePooledEncoderError")
-}
-
-func TestEncoderInt32(t *testing.T) {
- r, err := Marshal(int32(1))
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `1`,
- string(r),
- "Result of marshalling is different as the one expected")
-}
-
-func TestEncoderInt16(t *testing.T) {
- r, err := Marshal(int16(1))
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `1`,
- string(r),
- "Result of marshalling is different as the one expected")
-}
+func TestEncoderNumberEncodeAPI(t *testing.T) {
+ t.Run("encoder-int", func(t *testing.T) {
+ builder := &strings.Builder{}
+ enc := NewEncoder(builder)
+ err := enc.EncodeInt(1)
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `1`,
+ builder.String(),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("encode-int64", func(t *testing.T) {
+ builder := &strings.Builder{}
+ enc := NewEncoder(builder)
+ err := enc.EncodeInt64(int64(1))
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `1`,
+ builder.String(),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("encode-float64", func(t *testing.T) {
+ builder := &strings.Builder{}
+ enc := NewEncoder(builder)
+ err := enc.EncodeFloat(float64(1.1))
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `1.1`,
+ builder.String(),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("encode-float32", func(t *testing.T) {
+ builder := &strings.Builder{}
+ enc := NewEncoder(builder)
+ err := enc.EncodeFloat32(float32(1.12))
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `1.12`,
+ builder.String(),
+ "Result of marshalling is different as the one expected")
-func TestEncoderInt8(t *testing.T) {
- r, err := Marshal(int8(1))
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `1`,
- string(r),
- "Result of marshalling is different as the one expected")
+ })
}
-func TestEncoderUint64(t *testing.T) {
- r, err := Marshal(uint64(1))
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `1`,
- string(r),
- "Result of marshalling is different as the one expected")
-}
-func TestEncoderUint32(t *testing.T) {
- r, err := Marshal(uint32(1))
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `1`,
- string(r),
- "Result of marshalling is different as the one expected")
-}
-func TestEncoderUint16(t *testing.T) {
- r, err := Marshal(uint16(1))
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `1`,
- string(r),
- "Result of marshalling is different as the one expected")
-}
-func TestEncoderUint8(t *testing.T) {
- r, err := Marshal(uint8(1))
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `1`,
- string(r),
- "Result of marshalling is different as the one expected")
-}
-func TestEncoderFloat(t *testing.T) {
- r, err := Marshal(1.1)
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `1.1`,
- string(r),
- "Result of marshalling is different as the one expected")
-}
-func TestEncoderFloatEncodeAPI(t *testing.T) {
- builder := &strings.Builder{}
- enc := NewEncoder(builder)
- err := enc.EncodeFloat(float64(1.1))
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `1.1`,
- builder.String(),
- "Result of marshalling is different as the one expected")
-}
-func TestEncoderFloatEncodeAPIPoolError(t *testing.T) {
- builder := &strings.Builder{}
- enc := NewEncoder(builder)
- enc.Release()
- defer func() {
- err := recover()
+func TestEncoderNumberEncodeAPIErrors(t *testing.T) {
+ t.Run("encode-int-pool-error", func(t *testing.T) {
+ builder := &strings.Builder{}
+ enc := NewEncoder(builder)
+ enc.Release()
+ 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.EncodeInt(1)
+ assert.True(t, false, "should not be called as encoder should have panicked")
+ })
+ t.Run("encode-int-write-error", func(t *testing.T) {
+ w := TestWriterError("")
+ enc := NewEncoder(w)
+ err := enc.EncodeInt(1)
assert.NotNil(t, err, "err should not be nil")
- assert.IsType(t, InvalidUsagePooledEncoderError(""), err, "err should be of type InvalidUsagePooledEncoderError")
- }()
- _ = enc.EncodeFloat(1.1)
- assert.True(t, false, "should not be called as decoder should have panicked")
-}
-func TestEncoderFloatEncodeAPIWriteError(t *testing.T) {
- w := TestWriterError("")
- enc := NewEncoder(w)
- err := enc.EncodeFloat(1.1)
- assert.NotNil(t, err, "err should not be nil")
- assert.Equal(t, "Test Error", err.Error(), "err should be of type InvalidUsagePooledEncoderError")
-}
-
-func TestEncoderFloat32EncodeAPI(t *testing.T) {
- builder := &strings.Builder{}
- enc := NewEncoder(builder)
- err := enc.EncodeFloat32(float32(1.12))
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `1.12`,
- builder.String(),
- "Result of marshalling is different as the one expected")
-}
-func TestEncoderFloat32EncodeAPIPoolError(t *testing.T) {
- builder := &strings.Builder{}
- enc := NewEncoder(builder)
- enc.Release()
- defer func() {
- err := recover()
+ assert.Equal(t, "Test Error", err.Error(), "err should be of type InvalidUsagePooledEncoderError")
+ })
+ t.Run("encode-int64-pool-error", func(t *testing.T) {
+ builder := &strings.Builder{}
+ enc := NewEncoder(builder)
+ enc.Release()
+ 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.EncodeInt64(1)
+ assert.True(t, false, "should not be called as encoder should have panicked")
+ })
+ t.Run("encode-int64-write-error", func(t *testing.T) {
+ w := TestWriterError("")
+ enc := NewEncoder(w)
+ err := enc.EncodeInt64(1)
assert.NotNil(t, err, "err should not be nil")
- assert.IsType(t, InvalidUsagePooledEncoderError(""), err, "err should be of type InvalidUsagePooledEncoderError")
- }()
- _ = enc.EncodeFloat32(float32(1.1))
- assert.True(t, false, "should not be called as decoder should have panicked")
-}
-func TestEncoderFloat32EncodeAPIWriteError(t *testing.T) {
- w := TestWriterError("")
- enc := NewEncoder(w)
- err := enc.EncodeFloat32(float32(1.1))
- assert.NotNil(t, err, "err should not be nil")
- assert.Equal(t, "Test Error", err.Error(), "err should be of type InvalidUsagePooledEncoderError")
-}
+ assert.Equal(t, "Test Error", err.Error(), "err should be of type InvalidUsagePooledEncoderError")
-func TestEncoderIntPooledError(t *testing.T) {
- v := 1
- enc := BorrowEncoder(nil)
- enc.Release()
- defer func() {
- err := recover()
- assert.NotNil(t, err, "err shouldnot be nil")
- assert.IsType(t, InvalidUsagePooledEncoderError(""), err, "err should be of type InvalidUsagePooledEncoderError")
- assert.Equal(t, "Invalid usage of pooled encoder", err.(InvalidUsagePooledEncoderError).Error(), "err should be of type InvalidUsagePooledDecoderError")
- }()
- _ = enc.EncodeInt(v)
- assert.True(t, false, "should not be called as it should have panicked")
+ })
+ t.Run("encode-float64-pool-error", func(t *testing.T) {
+ builder := &strings.Builder{}
+ enc := NewEncoder(builder)
+ enc.Release()
+ 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.EncodeFloat(1.1)
+ assert.True(t, false, "should not be called as encoder should have panicked")
+ })
+ t.Run("encode-float64-write-error", func(t *testing.T) {
+ w := TestWriterError("")
+ enc := NewEncoder(w)
+ err := enc.EncodeFloat(1.1)
+ assert.NotNil(t, err, "err should not be nil")
+ assert.Equal(t, "Test Error", err.Error(), "err should be of type InvalidUsagePooledEncoderError")
+ })
+ t.Run("encode-float32-pool-error", func(t *testing.T) {
+ builder := &strings.Builder{}
+ enc := NewEncoder(builder)
+ enc.Release()
+ 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.EncodeFloat32(float32(1.1))
+ assert.True(t, false, "should not be called as encoder should have panicked")
+ })
+ t.Run("encode-float32-write-error", func(t *testing.T) {
+ w := TestWriterError("")
+ enc := NewEncoder(w)
+ err := enc.EncodeFloat32(float32(1.1))
+ assert.NotNil(t, err, "err should not be nil")
+ assert.Equal(t, "Test Error", err.Error(), "err should be of type InvalidUsagePooledEncoderError")
+ })
}
-func TestEncoderFloatPooledError(t *testing.T) {
- v := 1.1
- enc := BorrowEncoder(nil)
- enc.Release()
- defer func() {
- err := recover()
- assert.NotNil(t, err, "err shouldnot be nil")
- assert.IsType(t, InvalidUsagePooledEncoderError(""), err, "err should be of type InvalidUsagePooledEncoderError")
- assert.Equal(t, "Invalid usage of pooled encoder", err.(InvalidUsagePooledEncoderError).Error(), "err should be of type InvalidUsagePooledDecoderError")
- }()
- _ = enc.EncodeFloat(v)
- assert.True(t, false, "should not be called as it should have panicked")
+func TestEncoderNumberMarshalAPI(t *testing.T) {
+ t.Run("int", func(t *testing.T) {
+ r, err := Marshal(1)
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `1`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("int64", func(t *testing.T) {
+ r, err := Marshal(int64(1))
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `1`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("int32", func(t *testing.T) {
+ r, err := Marshal(int32(1))
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `1`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("int16", func(t *testing.T) {
+ r, err := Marshal(int16(1))
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `1`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("int8", func(t *testing.T) {
+ r, err := Marshal(int8(1))
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `1`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("uint64", func(t *testing.T) {
+ r, err := Marshal(uint64(1))
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `1`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("uint32", func(t *testing.T) {
+ r, err := Marshal(uint32(1))
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `1`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("uint16", func(t *testing.T) {
+ r, err := Marshal(uint16(1))
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `1`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("uint8", func(t *testing.T) {
+ r, err := Marshal(uint8(1))
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `1`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("float64", func(t *testing.T) {
+ r, err := Marshal(1.1)
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `1.1`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
}
diff --git a/encode_object_test.go b/encode_object_test.go
@@ -55,45 +55,6 @@ func (t *testObjectWithUnknownType) MarshalObject(enc *Encoder) {
enc.AddInterfaceKey("unknownType", t.unknownType)
}
-func TestEncoderObjectBasic(t *testing.T) {
- r, err := Marshal(&testObject{"漢字", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.1, 1.1, true})
- assert.Nil(t, err, "Error 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(r),
- "Result of marshalling is different as the one expected",
- )
-}
-func TestEncoderObjectBasicEncoderApi(t *testing.T) {
- builder := &strings.Builder{}
- enc := NewEncoder(builder)
- err := enc.EncodeObject(&testObject{"漢字", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.1, 1.1, true})
- assert.Nil(t, err, "Error 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}`,
- builder.String(),
- "Result of marshalling is different as the one expected",
- )
-}
-
-func TestEncoderObjectInterfaceEncoderApiError(t *testing.T) {
- builder := &strings.Builder{}
- enc := NewEncoder(builder)
- err := enc.EncodeObject(&testObjectWithUnknownType{struct{}{}})
- assert.NotNil(t, err, "Error should not be nil")
- assert.Equal(t, "Invalid type struct {} provided to Marshal", err.Error(), "err.Error() should be 'Invalid type struct {} provided to Marshal'")
-}
-
-func TestEncoderObjectBasicEncoderApiError(t *testing.T) {
- w := TestWriterError("")
- enc := NewEncoder(w)
- err := enc.EncodeObject(&testObject{"漢字", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.1, 1.1, true})
- assert.NotNil(t, err, "Error should not be nil")
- assert.Equal(t, "Test Error", err.Error(), "err.Error() should be 'Test Error'")
-}
-
type TestEncoding struct {
test string
test2 string
@@ -142,42 +103,6 @@ func (t *SubObject) MarshalObject(enc *Encoder) {
enc.AddObjectKey("sub", t.sub)
}
-func TestEncoderObjectComplex(t *testing.T) {
- v := &TestEncoding{
- test: "hello world",
- test2: "foobar",
- testInt: 1,
- testBool: true,
- testF32: 120.53,
- testF64: 120.15,
- testInterface: true,
- testArr: TestEncodingArr{
- &TestEncoding{
- test: "1",
- },
- },
- sub: &SubObject{
- test1: 10,
- test2: "hello world",
- test3: 1.23543,
- testBool: true,
- sub: &SubObject{
- test1: 10,
- testBool: false,
- test2: "hello world",
- },
- },
- }
- r, err := MarshalObject(v)
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `{"test":"hello world","test2":"foobar","testInt":1,"testBool":true,"testArr":[{"test":"1","test2":"","testInt":0,"testBool":false,"testArr":[],"testF64":0,"testF32":0,"sub":{}}],"testF64":120.15,"testF32":120.53,"testInterface":true,"sub":{"test1":10,"test2":"hello world","test3":1.23543,"testBool":true,"sub":{"test1":10,"test2":"hello world","test3":0,"testBool":false,"sub":{}}}}`,
- string(r),
- "Result of marshalling is different as the one expected",
- )
-}
-
type testEncodingObjInterfaces struct {
interfaceVal interface{}
}
@@ -190,103 +115,189 @@ func (t *testEncodingObjInterfaces) MarshalObject(enc *Encoder) {
enc.AddInterfaceKey("interfaceVal", t.interfaceVal)
}
-func TestEncoderObjectInterfaces(t *testing.T) {
- v := testEncodingObjInterfaces{"string"}
- r, err := Marshal(&v)
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `{"interfaceVal":"string"}`,
- string(r),
- "Result of marshalling is different as the one expected")
- v = testEncodingObjInterfaces{1}
- r, err = Marshal(&v)
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `{"interfaceVal":1}`,
- string(r),
- "Result of marshalling is different as the one expected")
- v = testEncodingObjInterfaces{int64(1)}
- r, err = Marshal(&v)
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `{"interfaceVal":1}`,
- string(r),
- "Result of marshalling is different as the one expected")
- v = testEncodingObjInterfaces{int32(1)}
- r, err = Marshal(&v)
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `{"interfaceVal":1}`,
- string(r),
- "Result of marshalling is different as the one expected")
- v = testEncodingObjInterfaces{int16(1)}
- r, err = Marshal(&v)
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `{"interfaceVal":1}`,
- string(r),
- "Result of marshalling is different as the one expected")
- v = testEncodingObjInterfaces{int8(1)}
- r, err = Marshal(&v)
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `{"interfaceVal":1}`,
- string(r),
- "Result of marshalling is different as the one expected")
- v = testEncodingObjInterfaces{uint64(1)}
- r, err = Marshal(&v)
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `{"interfaceVal":1}`,
- string(r),
- "Result of marshalling is different as the one expected")
- v = testEncodingObjInterfaces{uint32(1)}
- r, err = Marshal(&v)
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `{"interfaceVal":1}`,
- string(r),
- "Result of marshalling is different as the one expected")
- v = testEncodingObjInterfaces{uint16(1)}
- r, err = Marshal(&v)
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `{"interfaceVal":1}`,
- string(r),
- "Result of marshalling is different as the one expected")
- v = testEncodingObjInterfaces{uint8(1)}
- r, err = Marshal(&v)
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `{"interfaceVal":1}`,
- string(r),
- "Result of marshalling is different as the one expected")
- v = testEncodingObjInterfaces{float64(1.1)}
- r, err = Marshal(&v)
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `{"interfaceVal":1.1}`,
- string(r),
- "Result of marshalling is different as the one expected")
- v = testEncodingObjInterfaces{float32(1.1)}
- r, err = Marshal(&v)
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `{"interfaceVal":1.1}`,
- string(r),
- "Result of marshalling is different as the one expected")
+func TestEncoderObjectEncodeAPI(t *testing.T) {
+ t.Run("encode-basic", func(t *testing.T) {
+ builder := &strings.Builder{}
+ enc := NewEncoder(builder)
+ err := enc.EncodeObject(&testObject{"漢字", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.1, 1.1, true})
+ assert.Nil(t, err, "Error 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}`,
+ builder.String(),
+ "Result of marshalling is different as the one expected",
+ )
+ })
+}
+
+func TestEncoderObjectMarshalAPI(t *testing.T) {
+ t.Run("marshal-basic", func(t *testing.T) {
+ r, err := Marshal(&testObject{"漢字", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.1, 1.1, true})
+ assert.Nil(t, err, "Error 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(r),
+ "Result of marshalling is different as the one expected",
+ )
+ })
+
+ t.Run("marshal-complex", func(t *testing.T) {
+ v := &TestEncoding{
+ test: "hello world",
+ test2: "foobar",
+ testInt: 1,
+ testBool: true,
+ testF32: 120.53,
+ testF64: 120.15,
+ testInterface: true,
+ testArr: TestEncodingArr{
+ &TestEncoding{
+ test: "1",
+ },
+ },
+ sub: &SubObject{
+ test1: 10,
+ test2: "hello world",
+ test3: 1.23543,
+ testBool: true,
+ sub: &SubObject{
+ test1: 10,
+ testBool: false,
+ test2: "hello world",
+ },
+ },
+ }
+ r, err := MarshalObject(v)
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `{"test":"hello world","test2":"foobar","testInt":1,"testBool":true,"testArr":[{"test":"1","test2":"","testInt":0,"testBool":false,"testArr":[],"testF64":0,"testF32":0,"sub":{}}],"testF64":120.15,"testF32":120.53,"testInterface":true,"sub":{"test1":10,"test2":"hello world","test3":1.23543,"testBool":true,"sub":{"test1":10,"test2":"hello world","test3":0,"testBool":false,"sub":{}}}}`,
+ string(r),
+ "Result of marshalling is different as the one expected",
+ )
+ })
+
+ t.Run("marshal-interface-string", func(t *testing.T) {
+ v := testEncodingObjInterfaces{"string"}
+ r, err := Marshal(&v)
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `{"interfaceVal":"string"}`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("marshal-interface-int", func(t *testing.T) {
+ v := testEncodingObjInterfaces{1}
+ r, err := Marshal(&v)
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `{"interfaceVal":1}`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("marshal-interface-int64", func(t *testing.T) {
+ v := testEncodingObjInterfaces{int64(1)}
+ r, err := Marshal(&v)
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `{"interfaceVal":1}`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("marshal-interface-int32", func(t *testing.T) {
+ v := testEncodingObjInterfaces{int32(1)}
+ r, err := Marshal(&v)
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `{"interfaceVal":1}`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("marshal-interface-int16", func(t *testing.T) {
+ v := testEncodingObjInterfaces{int16(1)}
+ r, err := Marshal(&v)
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `{"interfaceVal":1}`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("marshal-interface-int8", func(t *testing.T) {
+ v := testEncodingObjInterfaces{int8(1)}
+ r, err := Marshal(&v)
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `{"interfaceVal":1}`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("marshal-interface-uint64", func(t *testing.T) {
+ v := testEncodingObjInterfaces{uint64(1)}
+ r, err := Marshal(&v)
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `{"interfaceVal":1}`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("marshal-interface-uint32", func(t *testing.T) {
+ v := testEncodingObjInterfaces{uint32(1)}
+ r, err := Marshal(&v)
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `{"interfaceVal":1}`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("marshal-interface-uint16", func(t *testing.T) {
+ v := testEncodingObjInterfaces{uint16(1)}
+ r, err := Marshal(&v)
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `{"interfaceVal":1}`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("marshal-interface-uint8", func(t *testing.T) {
+ v := testEncodingObjInterfaces{uint8(1)}
+ r, err := Marshal(&v)
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `{"interfaceVal":1}`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("marshal-interface-float64", func(t *testing.T) {
+ v := testEncodingObjInterfaces{float64(1.1)}
+ r, err := Marshal(&v)
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `{"interfaceVal":1.1}`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("marshal-interface-float32", func(t *testing.T) {
+ v := testEncodingObjInterfaces{float32(1.1)}
+ r, err := Marshal(&v)
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `{"interfaceVal":1.1}`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
}
type TestObectOmitEmpty struct {
@@ -321,22 +332,6 @@ func (t *TestObectOmitEmpty) MarshalObject(enc *Encoder) {
enc.AddArrayKeyOmitEmpty("testArray", TestEncodingArrStrings{"foo"})
}
-func TestEncoderObjectOmitEmpty(t *testing.T) {
- v := &TestObectOmitEmpty{
- nonNiler: 1,
- testInt: 0,
- testObect: &TestObectOmitEmpty{testInt: 1},
- }
- r, err := MarshalObject(v)
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `{"testIntNotEmpty":1,"testFloatNotEmpty":1.1,"testFloat32NotEmpty":1.1,"testStringNotEmpty":"foo","testBoolNotEmpty":true,"testObect":{"testInt":1,"testIntNotEmpty":1,"testFloatNotEmpty":1.1,"testFloat32NotEmpty":1.1,"testStringNotEmpty":"foo","testBoolNotEmpty":true,"testArray":["foo"]},"testArray":["foo"]}`,
- string(r),
- "Result of marshalling is different as the one expected",
- )
-}
-
type TestObectOmitEmptyInterface struct{}
func (t *TestObectOmitEmptyInterface) IsNil() bool {
@@ -367,35 +362,68 @@ func (t *TestObectOmitEmptyInterface) MarshalObject(enc *Encoder) {
enc.AddInterfaceKeyOmitEmpty("testArr", &TestEncodingArrStrings{})
}
-func TestEncoderObjectInterfaceOmitEmpty(t *testing.T) {
- v := &TestObectOmitEmptyInterface{}
- r, err := MarshalObject(v)
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `{"testIntNotEmpty":1,"testFloatNotEmpty":1.1,"testFloat32NotEmpty":1.1,"testStringNotEmpty":"foo","testBoolNotEmpty":true,"testObect":{"test":"","test2":"","testInt":0,"testBool":false,"testArr":[],"testF64":0,"testF32":0,"sub":{}}}`,
- string(r),
- "Result of marshalling is different as the one expected",
- )
-}
-
-func TestEncoderAddInterfaceKeyError(t *testing.T) {
- builder := &strings.Builder{}
- enc := NewEncoder(builder)
- enc.AddInterfaceKeyOmitEmpty("test", struct{}{})
- assert.NotNil(t, enc.err, "enc.Err() should not be nil")
+func TestEncoderObjectOmitEmpty(t *testing.T) {
+ t.Run("encoder-omit-empty-all-types", func(t *testing.T) {
+ v := &TestObectOmitEmpty{
+ nonNiler: 1,
+ testInt: 0,
+ testObect: &TestObectOmitEmpty{testInt: 1},
+ }
+ r, err := MarshalObject(v)
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `{"testIntNotEmpty":1,"testFloatNotEmpty":1.1,"testFloat32NotEmpty":1.1,"testStringNotEmpty":"foo","testBoolNotEmpty":true,"testObect":{"testInt":1,"testIntNotEmpty":1,"testFloatNotEmpty":1.1,"testFloat32NotEmpty":1.1,"testStringNotEmpty":"foo","testBoolNotEmpty":true,"testArray":["foo"]},"testArray":["foo"]}`,
+ string(r),
+ "Result of marshalling is different as the one expected",
+ )
+ })
+
+ t.Run("encoder-omit-empty-interface", func(t *testing.T) {
+ v := &TestObectOmitEmptyInterface{}
+ r, err := MarshalObject(v)
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `{"testIntNotEmpty":1,"testFloatNotEmpty":1.1,"testFloat32NotEmpty":1.1,"testStringNotEmpty":"foo","testBoolNotEmpty":true,"testObect":{"test":"","test2":"","testInt":0,"testBool":false,"testArr":[],"testF64":0,"testF32":0,"sub":{}}}`,
+ string(r),
+ "Result of marshalling is different as the one expected",
+ )
+ })
}
-func TestEncoderObjectPooledError(t *testing.T) {
- v := &TestEncoding{}
- enc := BorrowEncoder(nil)
- enc.Release()
- defer func() {
- err := recover()
- assert.NotNil(t, err, "err shouldnot be nil")
- assert.IsType(t, InvalidUsagePooledEncoderError(""), err, "err should be of type InvalidUsagePooledEncoderError")
- assert.Equal(t, "Invalid usage of pooled encoder", err.(InvalidUsagePooledEncoderError).Error(), "err should be of type InvalidUsagePooledDecoderError")
- }()
- _ = enc.EncodeObject(v)
- assert.True(t, false, "should not be called as it should have panicked")
+func TestEncoderObjectEncodeAPIError(t *testing.T) {
+ t.Run("interface-key-error", func(t *testing.T) {
+ builder := &strings.Builder{}
+ enc := NewEncoder(builder)
+ err := enc.EncodeObject(&testObjectWithUnknownType{struct{}{}})
+ assert.NotNil(t, err, "Error should not be nil")
+ assert.Equal(t, "Invalid type struct {} provided to Marshal", err.Error(), "err.Error() should be 'Invalid type struct {} provided to Marshal'")
+ })
+ t.Run("write-error", func(t *testing.T) {
+ w := TestWriterError("")
+ enc := NewEncoder(w)
+ err := enc.EncodeObject(&testObject{"漢字", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.1, 1.1, true})
+ assert.NotNil(t, err, "Error should not be nil")
+ assert.Equal(t, "Test Error", err.Error(), "err.Error() should be 'Test Error'")
+ })
+ t.Run("interface-error", func(t *testing.T) {
+ builder := &strings.Builder{}
+ enc := NewEncoder(builder)
+ enc.AddInterfaceKeyOmitEmpty("test", struct{}{})
+ assert.NotNil(t, enc.err, "enc.Err() should not be nil")
+ })
+ t.Run("pool-error", func(t *testing.T) {
+ v := &TestEncoding{}
+ enc := BorrowEncoder(nil)
+ enc.Release()
+ defer func() {
+ err := recover()
+ assert.NotNil(t, err, "err shouldnot be nil")
+ assert.IsType(t, InvalidUsagePooledEncoderError(""), err, "err should be of type InvalidUsagePooledEncoderError")
+ assert.Equal(t, "Invalid usage of pooled encoder", err.(InvalidUsagePooledEncoderError).Error(), "err should be of type InvalidUsagePooledDecoderError")
+ }()
+ _ = enc.EncodeObject(v)
+ assert.True(t, false, "should not be called as it should have panicked")
+ })
}
diff --git a/encode_string_test.go b/encode_string_test.go
@@ -7,56 +7,72 @@ import (
"github.com/stretchr/testify/assert"
)
-func TestEncoderString(t *testing.T) {
- r, err := Marshal("string")
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `"string"`,
- string(r),
- "Result of marshalling is different as the one expected")
-}
func TestEncoderStringEncodeAPI(t *testing.T) {
- builder := &strings.Builder{}
- enc := NewEncoder(builder)
- err := enc.EncodeString("漢字")
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `"漢字"`,
- builder.String(),
- "Result of marshalling is different as the one expected")
-}
-
-func TestEncoderStringUTF8(t *testing.T) {
- r, err := Marshal("漢字")
- assert.Nil(t, err, "Error should be nil")
- assert.Equal(
- t,
- `"漢字"`,
- string(r),
- "Result of marshalling is different as the one expected")
+ t.Run("basic", func(t *testing.T) {
+ builder := &strings.Builder{}
+ enc := NewEncoder(builder)
+ err := enc.EncodeString("hello world")
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `"hello world"`,
+ builder.String(),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("utf8", func(t *testing.T) {
+ builder := &strings.Builder{}
+ enc := NewEncoder(builder)
+ err := enc.EncodeString("漢字")
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `"漢字"`,
+ builder.String(),
+ "Result of marshalling is different as the one expected")
+ })
}
-func TestEncoderStringPooledError(t *testing.T) {
- v := ""
- enc := BorrowEncoder(nil)
- enc.Release()
- defer func() {
- err := recover()
- assert.NotNil(t, err, "err shouldnot be nil")
- assert.IsType(t, InvalidUsagePooledEncoderError(""), err, "err should be of type InvalidUsagePooledEncoderError")
- assert.Equal(t, "Invalid usage of pooled encoder", err.(InvalidUsagePooledEncoderError).Error(), "err should be of type InvalidUsagePooledDecoderError")
- }()
- _ = enc.EncodeString(v)
- assert.True(t, false, "should not be called as it should have panicked")
+func TestEncoderStringEncodeAPIErrors(t *testing.T) {
+ t.Run("pool-error", func(t *testing.T) {
+ v := ""
+ enc := BorrowEncoder(nil)
+ enc.Release()
+ defer func() {
+ err := recover()
+ assert.NotNil(t, err, "err shouldnot be nil")
+ assert.IsType(t, InvalidUsagePooledEncoderError(""), err, "err should be of type InvalidUsagePooledEncoderError")
+ assert.Equal(t, "Invalid usage of pooled encoder", err.(InvalidUsagePooledEncoderError).Error(), "err should be of type InvalidUsagePooledDecoderError")
+ }()
+ _ = enc.EncodeString(v)
+ assert.True(t, false, "should not be called as it should have panicked")
+ })
+ t.Run("write-error", func(t *testing.T) {
+ v := "test"
+ w := TestWriterError("")
+ enc := BorrowEncoder(w)
+ defer enc.Release()
+ err := enc.EncodeString(v)
+ assert.NotNil(t, err, "err should not be nil")
+ })
}
-func TestEncoderStringPoolEncoderAPIWriteError(t *testing.T) {
- v := "test"
- w := TestWriterError("")
- enc := BorrowEncoder(w)
- defer enc.Release()
- err := enc.EncodeString(v)
- assert.NotNil(t, err, "err should not be nil")
+func TestEncoderStringMarshalAPI(t *testing.T) {
+ t.Run("basic", func(t *testing.T) {
+ r, err := Marshal("string")
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `"string"`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
+ t.Run("utf8", func(t *testing.T) {
+ r, err := Marshal("漢字")
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `"漢字"`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
}