2018-02-13 23:49:40 +08:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"reflect"
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/json-iterator/go"
|
2018-02-16 15:42:37 +08:00
|
|
|
"fmt"
|
2018-02-13 23:49:40 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type unmarshalCase struct {
|
|
|
|
ptr interface{}
|
|
|
|
input string
|
|
|
|
}
|
|
|
|
|
|
|
|
var unmarshalCases []unmarshalCase
|
|
|
|
|
2018-02-14 08:39:18 +08:00
|
|
|
var marshalCases = []interface{}{
|
|
|
|
nil,
|
|
|
|
}
|
2018-02-13 23:49:40 +08:00
|
|
|
|
2018-02-16 15:42:37 +08:00
|
|
|
type selectedMarshalCase struct {
|
|
|
|
marshalCase interface{}
|
|
|
|
}
|
|
|
|
|
2018-02-13 23:49:40 +08:00
|
|
|
func Test_unmarshal(t *testing.T) {
|
|
|
|
should := require.New(t)
|
|
|
|
for _, testCase := range unmarshalCases {
|
|
|
|
valType := reflect.TypeOf(testCase.ptr).Elem()
|
|
|
|
ptr1Val := reflect.New(valType)
|
|
|
|
err1 := json.Unmarshal([]byte(testCase.input), ptr1Val.Interface())
|
|
|
|
should.NoError(err1)
|
|
|
|
ptr2Val := reflect.New(valType)
|
2018-02-14 08:28:17 +08:00
|
|
|
err2 := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(testCase.input), ptr2Val.Interface())
|
2018-02-13 23:49:40 +08:00
|
|
|
should.NoError(err2)
|
|
|
|
should.Equal(ptr1Val.Interface(), ptr2Val.Interface())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_marshal(t *testing.T) {
|
|
|
|
for _, testCase := range marshalCases {
|
2018-02-16 15:42:37 +08:00
|
|
|
selectedMarshalCase, found := testCase.(selectedMarshalCase)
|
|
|
|
if found {
|
|
|
|
marshalCases = []interface{}{selectedMarshalCase.marshalCase}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i, testCase := range marshalCases {
|
2018-02-14 15:04:23 +08:00
|
|
|
var name string
|
|
|
|
if testCase != nil {
|
2018-02-16 15:42:37 +08:00
|
|
|
name = fmt.Sprintf("[%v]%v/%s", i, testCase, reflect.TypeOf(testCase).String())
|
2018-02-14 15:04:23 +08:00
|
|
|
}
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
should := require.New(t)
|
|
|
|
output1, err1 := json.Marshal(testCase)
|
2018-02-18 22:49:06 +08:00
|
|
|
should.NoError(err1, "json")
|
2018-02-14 15:04:23 +08:00
|
|
|
output2, err2 := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(testCase)
|
2018-02-18 22:49:06 +08:00
|
|
|
should.NoError(err2, "jsoniter")
|
2018-02-14 15:04:23 +08:00
|
|
|
should.Equal(string(output1), string(output2))
|
|
|
|
})
|
2018-02-13 23:49:40 +08:00
|
|
|
}
|
|
|
|
}
|