-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathvalue_test.go
95 lines (85 loc) · 2.41 KB
/
value_test.go
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
package test
import (
"encoding/json"
"fmt"
"testing"
jsoniter "github.com/json-iterator/go"
"github.com/modern-go/reflect2"
"github.com/stretchr/testify/require"
)
type unmarshalCase struct {
obj func() interface{}
ptr interface{}
input string
selected bool
}
var unmarshalCases []unmarshalCase
var marshalCases = []interface{}{
nil,
}
var marshalSelfRecursiveCases = []interface{}{}
type selectedMarshalCase struct {
marshalCase interface{}
}
func Test_unmarshal(t *testing.T) {
for _, testCase := range unmarshalCases {
if testCase.selected {
unmarshalCases = []unmarshalCase{testCase}
break
}
}
for i, testCase := range unmarshalCases {
t.Run(fmt.Sprintf("[%v]%s", i, testCase.input), func(t *testing.T) {
should := require.New(t)
var obj1 interface{}
var obj2 interface{}
if testCase.obj != nil {
obj1 = testCase.obj()
obj2 = testCase.obj()
} else {
valType := reflect2.TypeOfPtr(testCase.ptr).Elem()
obj1 = valType.New()
obj2 = valType.New()
}
err1 := json.Unmarshal([]byte(testCase.input), obj1)
should.NoError(err1, "json")
err2 := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(testCase.input), obj2)
should.NoError(err2, "jsoniter")
should.Equal(obj1, obj2)
})
}
}
func Test_marshal(t *testing.T) {
for _, testCase := range marshalCases {
selectedMarshalCase, found := testCase.(selectedMarshalCase)
if found {
marshalCases = []interface{}{selectedMarshalCase.marshalCase}
break
}
}
for i, testCase := range marshalCases {
var name string
if testCase != nil {
name = fmt.Sprintf("[%v]%v/%s", i, testCase, reflect2.TypeOf(testCase).String())
}
t.Run(name, func(t *testing.T) {
should := require.New(t)
output1, err1 := json.Marshal(testCase)
should.NoError(err1, "json")
output2, err2 := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(testCase)
should.NoError(err2, "jsoniter")
should.Equal(string(output1), string(output2))
})
}
}
func Test_marshal_self_recursive(t *testing.T) {
for i, testCase := range marshalSelfRecursiveCases {
t.Run(fmt.Sprintf("[%v]%s", i, reflect2.TypeOf(testCase).String()), func(t *testing.T) {
should := require.New(t)
_, err1 := json.Marshal(testCase)
should.ErrorContains(err1, "encountered a cycle")
_, err2 := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(testCase)
should.ErrorContains(err2, "encountered a cycle")
})
}
}