-
Notifications
You must be signed in to change notification settings - Fork 2
/
standard_compatible_test.go
152 lines (133 loc) · 3.4 KB
/
standard_compatible_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
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
package jzon
import (
"encoding/json"
"fmt"
"os"
"reflect"
"strings"
"testing"
"unsafe"
"github.com/stretchr/testify/require"
)
var (
compatibleOnError = os.Getenv("COMPATIBLE_ON_ERROR") == "1"
)
func nestedArray1(count int) string {
return strings.Repeat(" [", count) + " [ ] " +
strings.Repeat("] ", count)
}
func nestedArray2(count int) string {
return strings.Repeat(" [ [ ], ", count) + " [ ] " +
strings.Repeat("] ", count)
}
func nestedArrayWithObject(count int) string {
return strings.Repeat(" [ { }, ", count) + " [ ] " +
strings.Repeat("] ", count)
}
func nestedObject(count int) string {
return strings.Repeat(` { "a" : { }, "b": `, count) + " { } " +
strings.Repeat("} ", count)
}
func nestedObjectWithArray(count int) string {
return strings.Repeat(` { "a" : [ ], "b": `, count) + " [ ] " +
strings.Repeat("} ", count)
}
type printValueKey struct {
rtype rtype
ptr uintptr
}
func (pk printValueKey) String() string {
return fmt.Sprintf("<%x %x>", pk.rtype, pk.ptr)
}
func printValue(t *testing.T, prefix string, o interface{}) {
prefix += " "
if o == nil {
t.Logf(prefix + "nil")
return
}
visited := map[printValueKey]bool{}
oV := reflect.ValueOf(o)
for indent := prefix; ; indent += " " {
i := oV.Interface()
ef := (*eface)(unsafe.Pointer(&i))
vk := printValueKey{ef.rtype, uintptr(ef.data)}
k := oV.Kind()
t.Logf(indent+"%+v %+v %v", oV.Type(), oV, vk)
if k != reflect.Interface && k != reflect.Ptr {
break
}
if oV.IsNil() {
break
}
if visited[vk] {
t.Logf(indent + " visited...")
break
}
visited[vk] = true
oV = oV.Elem()
}
}
func checkDecodeWithStandard(t *testing.T, decCfg *DecoderConfig, data string, ex error, exp, got interface{}) {
b := []byte(data)
expErr := json.Unmarshal(b, exp)
gotErr := decCfg.Unmarshal(b, got)
t.Logf("\nexpErr: %+v\ngotErr: %+v", expErr, gotErr)
noError := expErr == nil
if noError {
printValue(t, "exp", reflect.ValueOf(exp).Elem().Interface())
}
require.Equal(t, noError, gotErr == nil,
"exp %+v\ngot %+v", expErr, gotErr)
require.Equalf(t, noError, ex == nil, "exp err: %v\ngot err: %v", ex, gotErr)
if ex != nil {
checkError(t, ex, gotErr)
// if reflect.TypeOf(errors.New("")) == reflect.TypeOf(ex) {
// require.Equalf(t, ex, gotErr, "exp err:%v\ngot err:%v", ex, gotErr)
// } else {
// require.IsTypef(t, ex, gotErr, "exp err:%v\ngot err:%v", ex, gotErr)
// }
}
if !noError && !compatibleOnError {
return
}
if exp == nil {
require.Equal(t, nil, got)
return
}
expV := reflect.ValueOf(exp)
gotV := reflect.ValueOf(got)
if expV.IsNil() {
require.True(t, gotV.IsNil())
return
}
expI := expV.Elem().Interface()
gotI := gotV.Elem().Interface()
printValue(t, "got", gotI)
require.Equalf(t, expI, gotI, "exp %+v\ngot %+v", expI, gotI)
}
func TestValid(t *testing.T) {
f := func(t *testing.T, s string) {
data := localStringToBytes(s)
require.Equal(t, json.Valid(data), Valid(data))
}
t.Run("empty", func(t *testing.T) {
f(t, "")
})
t.Run("empty object", func(t *testing.T) {
f(t, "{}")
})
t.Run("data remained", func(t *testing.T) {
f(t, "{}1")
})
}
func TestUnmarshal(t *testing.T) {
t.Run("data remained", func(t *testing.T) {
var i, i2 interface{}
checkDecodeWithStandard(t, DefaultDecoderConfig, "{}{", ErrDataRemained, &i, &i2)
})
}
func TestMarshal(t *testing.T) {
b, err := Marshal("123")
require.NoError(t, err)
require.Equal(t, []byte(`"123"`), b)
}