-
Notifications
You must be signed in to change notification settings - Fork 0
/
value_test.go
262 lines (224 loc) · 6.75 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package jsonutil_test
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/yusufsyaifudin/jsonutil"
)
const sampleComplexJsonData = `{"real_string":"123","real_int":123,"real_float":12.3,"real_object":{"foo":{"any":{"float":1.1,"int":1},"float":{"float":1.1},"int":{"int":1}}},"real_array":["abc"],"numeric":{"float32":3.2,"float64":6.4,"int":1,"int8":8,"int16":16,"int32":32,"int64":64,"uint":10,"uint8":80,"uint16":160,"uint32":320,"uint64":640,"uintptr":9999},"null":null}`
type Numeric struct {
Float32 float32 `json:"float32"`
Float64 float64 `json:"float64"`
Int int `json:"int"`
Int8 int8 `json:"int8"`
Int16 int16 `json:"int16"`
Int32 int32 `json:"int32"`
Int64 int64 `json:"int64"`
UInt uint `json:"uint"`
UInt8 uint8 `json:"uint8"`
UInt16 uint16 `json:"uint16"`
UInt32 uint32 `json:"uint32"`
UInt64 uint64 `json:"uint64"`
Uintptr uintptr `json:"uintptr"`
}
type Complex struct {
RealString jsonutil.Value `json:"real_string"`
RealInt jsonutil.Value `json:"real_int"`
RealFloat jsonutil.Value `json:"real_float"`
RealObject jsonutil.Value `json:"real_object"`
RealArray jsonutil.Value `json:"real_array"`
Numeric Numeric `json:"numeric"`
Null jsonutil.Value `json:"null"`
}
var obj = map[string]interface{}{
"foo": map[string]interface{}{
"int": map[string]int{
"int": 1,
},
"float": map[string]float64{
"float": 1.1,
},
"any": map[string]interface{}{
"int": 1,
"float": 1.1,
},
},
}
var arrData = []string{"abc"}
var numericData = Numeric{
Float32: 3.2,
Float64: 6.4,
Int: 1,
Int8: 8,
Int16: 16,
Int32: 32,
Int64: 64,
UInt: 10,
UInt8: 80,
UInt16: 160,
UInt32: 320,
UInt64: 640,
Uintptr: 9999,
}
func TestValue(t *testing.T) {
t.Run("marshal_unmarshal", func(t *testing.T) {
expected := Complex{
RealString: jsonutil.NewValue("123"),
RealInt: jsonutil.NewValue(123),
RealFloat: jsonutil.NewValue(12.3),
RealObject: jsonutil.NewValue(obj),
RealArray: jsonutil.NewValue(arrData),
Numeric: numericData,
Null: jsonutil.NewValue(nil),
}
bytes, err := json.Marshal(expected)
assert.NotNil(t, bytes)
assert.NoError(t, err)
var actual Complex
err = json.Unmarshal(bytes, &actual)
assert.NoError(t, err)
// assert each field
assert.EqualValues(t, expected.RealString, actual.RealString)
// int will save as raw float64 after unmarshal, so we get the actual value instead of comparing struct Value
assert.EqualValues(t, expected.RealInt.Interface(), actual.RealInt.Interface())
assert.EqualValues(t, expected.RealFloat, actual.RealFloat)
// For type interface such as map, slice or struct,
// when created using NewValue it uses real type such as map[string]string or []string{}
// but after unmarshal, it becomes map[string]interface{} or []interface{}
// This is expected and handling this is negligible since the important thing is the value, not the type.
assert.ObjectsAreEqual(expected.RealObject, actual.RealObject)
assert.ObjectsAreEqual(expected.RealArray, actual.RealArray)
assert.EqualValues(t, expected.Numeric, actual.Numeric)
})
t.Run("unmarshal_marshal", func(t *testing.T) {
var data Complex
err := json.Unmarshal([]byte(sampleComplexJsonData), &data)
assert.NoError(t, err)
bytes, err := json.Marshal(data)
assert.NotNil(t, bytes)
assert.NoError(t, err)
assert.EqualValues(t, sampleComplexJsonData, string(bytes))
})
t.Run("unmarshal", func(t *testing.T) {
var data Complex
err := json.Unmarshal([]byte(sampleComplexJsonData), &data)
assert.NoError(t, err)
// assert data is string but can convert into int64 or float64
assert.EqualValues(t, "123", data.RealString.String())
i, err := data.RealString.Int64()
assert.EqualValues(t, int64(123), i)
assert.NoError(t, err)
f, err := data.RealString.Float64()
assert.EqualValues(t, 123, f)
assert.NoError(t, err)
// assert data actual is int64, but can be converted as string or float64
assert.EqualValues(t, "123", data.RealInt.String())
i, err = data.RealInt.Int64()
assert.EqualValues(t, int64(123), i)
assert.NoError(t, err)
f, err = data.RealInt.Float64()
assert.EqualValues(t, 123, f)
assert.NoError(t, err)
// assert data actual is float64, but can be converted as string
assert.EqualValues(t, "12.3", data.RealFloat.String())
// float64 cannot be converted into int64, hence return 0 and error
i, err = data.RealFloat.Int64()
assert.EqualValues(t, int64(0), i)
assert.Error(t, err)
f, err = data.RealFloat.Float64()
assert.EqualValues(t, 12.3, f)
assert.NoError(t, err)
// object,
// actual is converted to map[]interface while the expected as map[]float64 or any concrete type
// so, use ObjectsAreEqualValues to compare
assert.ObjectsAreEqualValues(obj, data.RealObject.Interface())
assert.ObjectsAreEqualValues(arrData, data.RealArray.Interface())
assert.EqualValues(t, numericData, data.Numeric)
})
}
func TestValue_MarshalJSON(t *testing.T) {
t.Run("nil value", func(t *testing.T) {
data := jsonutil.NewValue(nil)
b, err := json.Marshal(data)
assert.EqualValues(t, []byte("null"), b)
assert.NoError(t, err)
})
}
func BenchmarkValue_MarshalJSON(b *testing.B) {
complexData := Complex{
RealString: jsonutil.NewValue("123"),
RealInt: jsonutil.NewValue(123),
RealFloat: jsonutil.NewValue(12.3),
RealObject: jsonutil.NewValue(obj),
RealArray: jsonutil.NewValue(arrData),
Numeric: numericData,
Null: jsonutil.NewValue(nil),
}
for i := 0; i < b.N; i++ {
_, err := json.Marshal(complexData)
if err != nil {
b.Fatal(err)
return
}
}
}
func BenchmarkValue_UnmarshalJSON(b *testing.B) {
complexData := Complex{
RealString: jsonutil.NewValue("123"),
RealInt: jsonutil.NewValue(123),
RealFloat: jsonutil.NewValue(12.3),
RealObject: jsonutil.NewValue(obj),
RealArray: jsonutil.NewValue(arrData),
Numeric: numericData,
Null: jsonutil.NewValue(nil),
}
data, err := json.Marshal(complexData)
if err != nil {
b.Fatal(err)
return
}
for i := 0; i < b.N; i++ {
var c Complex
err = json.Unmarshal(data, &c)
if err != nil {
b.Fatal(err)
return
}
}
}
func TestSample(t *testing.T) {
type TestCase struct {
Name string
Value string
}
testCases := []TestCase{
{
Name: "string",
Value: `"string"`,
},
{
Name: "numeric",
Value: `1`,
},
{
Name: "float",
Value: `1.1`,
},
{
Name: "object",
Value: `{"foo": {"foo": "bar"}}`,
},
{
Name: "multi type array",
Value: `["array", 1, {"foo": "bar"}]`,
},
}
for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
var value jsonutil.Value
err := json.Unmarshal([]byte(testCase.Value), &value)
assert.NoError(t, err)
t.Log(value)
})
}
}