-
Notifications
You must be signed in to change notification settings - Fork 4
/
jsongen_test.go
287 lines (244 loc) · 7.66 KB
/
jsongen_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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Package jsongen forked from darjun/json-gen
package jsongen
import (
"encoding/json"
"fmt"
"testing"
)
var jsStr = `{"a":3,"b":[4,true]}`
func TestRawString(t *testing.T) {
testCases := []struct {
value RawString
expected string
}{
{"1234", "1234"},
{"12.34", "12.34"},
}
for _, c := range testCases {
if string(c.value.Serialize(nil)) != c.expected {
t.Errorf("actual(%s) != expected(%s)", string(c.value.Serialize(nil)), c.expected)
}
}
}
func TestRawBytes(t *testing.T) {
testCases := []struct {
value RawBytes
expected string
}{
{[]byte("1234"), "1234"},
{[]byte("12.34"), "12.34"},
}
for _, c := range testCases {
if string(c.value.Serialize(nil)) != c.expected {
t.Errorf("actual(%s) != expected(%s)", string(c.value.Serialize(nil)), c.expected)
}
}
}
func TestUnquotedValue(t *testing.T) {
testCases := []struct {
value UnquotedValue
expected string
}{
{"1234", "1234"},
{"12.34", "12.34"},
}
for _, c := range testCases {
if string(c.value.Serialize(nil)) != c.expected {
t.Errorf("actual(%s) != expected(%s)", string(c.value.Serialize(nil)), c.expected)
}
}
}
func TestQuotedValue(t *testing.T) {
testCases := []struct {
value QuotedValue
expected string
}{
{QuotedValue("string"), `"string"`},
{QuotedValue(`string with \`), `"string with \"`},
{QuotedValue(`string with "`), `"string with ""`},
}
for _, c := range testCases {
if string(c.value.Serialize(nil)) != c.expected {
t.Errorf("actual(%s) != expected(%s)", string(c.value.Serialize(nil)), c.expected)
}
}
}
func TestGenJSON(t *testing.T) {
js := NewMap()
js.PutRawString("raw", jsStr)
js.PutString("multiline", "x \n y \n \t\n ")
bs := js.Serialize(nil)
var v map[string]interface{}
if err := json.Unmarshal(bs, &v); err != nil {
t.Fatal("Invalid JSON")
}
}
func array1() (*Array, string) {
a1 := NewArray()
a1.AppendUint(123)
a1.AppendInt(-45)
a1.AppendFloat(12.34)
a1.AppendBool(true)
a1.AppendString("test string")
a1.AppendString(`string with \`)
a1.AppendString(`string with "`)
a1.AppendRawString(jsStr)
a1.AppendRawBytes([]byte(jsStr))
a1.AppendRawStringArray([]string{jsStr, jsStr})
a1.AppendRawBytesArray([][]byte{[]byte(jsStr), []byte(jsStr)})
expected1 := `[123,-45,12.34,true,"test string","string with \\","string with \"",` +
`{"a":3,"b":[4,true]},{"a":3,"b":[4,true]},` +
`[{"a":3,"b":[4,true]},{"a":3,"b":[4,true]}],[{"a":3,"b":[4,true]},{"a":3,"b":[4,true]}]]`
return a1, expected1
}
func array2() (*Array, string) {
a2 := NewArray()
a2.AppendUintArray([]uint64{123, 456, 789})
a2.AppendIntArray([]int64{-12, -45, -78})
a2.AppendFloatArray([]float64{12.34, -56.78, 9.0})
a2.AppendBoolArray([]bool{true, false, true})
a2.AppendStringArray([]string{"test string", `string with \`, `string with "`})
expected2 := `[[123,456,789],[-12,-45,-78],[12.34,-56.78,9],[true,false,true],["test string","string with \\","string with \""]]`
return a2, expected2
}
func array3() (*Array, string) {
a3 := NewArray()
m1 := NewMap()
m1.PutUint("uintkey", 123)
m1.PutInt("intkey", -456)
m1.PutFloat("floatkey", 12.34)
m1.PutBool("boolkey", true)
m1.PutString("stringkey", "test string")
a3.AppendMap(m1)
m2 := NewMap()
m2.PutUint("uintkey", 456)
m2.PutInt("intkey", -789)
m2.PutFloat("floatkey", 56.78)
m2.PutBool("boolkey", false)
m2.PutString("stringkey", `string with \`)
a3.AppendMap(m2)
expected3 := `[{"uintkey":123,"intkey":-456,"floatkey":12.34,"boolkey":true,"stringkey":"test string"},{"uintkey":456,"intkey":-789,"floatkey":56.78,"boolkey":false,"stringkey":"string with \\"}]`
return a3, expected3
}
func array4() (*Array, string) {
a4 := NewArray()
a1, expected1 := array1()
a2, expected2 := array2()
a3, expected3 := array3()
a4.AppendArray(*a1)
a4.AppendArray(*a2)
a4.AppendArray(*a3)
expected4 := fmt.Sprintf("[%s,%s,%s]", expected1, expected2, expected3)
return a4, expected4
}
func TestArrayValue(t *testing.T) {
a1, expected1 := array1()
a2, expected2 := array2()
a3, expected3 := array3()
a4, expected4 := array4()
testCases := []struct {
name string
value *Array
expected string
}{
{"basic", a1, expected1},
{"primitive array", a2, expected2},
{"map array", a3, expected3},
{"nested general array", a4, expected4},
}
for _, c := range testCases {
data := c.value.Serialize(nil)
if string(data) != c.expected {
t.Errorf("array name:%s actual:%s != expected:%s", c.name, string(data), c.expected)
}
if len(data) != c.value.Size() {
t.Errorf("array name:%s buf size error, actual:%d, expected:%d", c.name, len(data), c.value.Size())
}
var obj []interface{}
if err := json.Unmarshal(data, &obj); err != nil {
t.Errorf("array name:%s unmarshal error:%v", c.name, err)
}
}
}
func map1() (*Map, string) {
m1 := NewMap()
m1.PutUint("uintkey", 123)
m1.PutInt("intkey", -45)
m1.PutFloat("floatkey", 12.34)
m1.PutBool("boolkey", true)
m1.PutString("stringkey1", "teststring")
m1.PutString("stringkey2", `string with \`)
m1.PutString("stringkey3", `string with "`)
m1.PutRawString("raw_string", jsStr)
m1.PutRawBytes("raw_bytes", []byte(jsStr))
m1.PutRawStringArray("raw_sarr", []string{jsStr, jsStr})
m1.PutRawBytesArray("raw_barr", [][]byte{[]byte(jsStr), []byte(jsStr)})
expected1 := `{"uintkey":123,"intkey":-45,"floatkey":12.34,"boolkey":true,"stringkey1":"teststring",` +
`"stringkey2":"string with \\","stringkey3":"string with \"",` +
`"raw_string":{"a":3,"b":[4,true]},"raw_bytes":{"a":3,"b":[4,true]},` +
`"raw_sarr":[{"a":3,"b":[4,true]},{"a":3,"b":[4,true]}],"raw_barr":[{"a":3,"b":[4,true]},{"a":3,"b":[4,true]}]}`
return m1, expected1
}
func map2() (*Map, string) {
m2 := NewMap()
m2.PutUintArray("uintarray", []uint64{123, 456, 789})
m2.PutIntArray("intarray", []int64{-23, -45, -89})
m2.PutFloatArray("floatarray", []float64{12.34, -56.78, 90})
m2.PutBoolArray("boolarray", []bool{true, false, true})
m2.PutStringArray("stringarray", []string{"test string", `string with \`, `string with "`})
expected2 := `{"uintarray":[123,456,789],"intarray":[-23,-45,-89],"floatarray":[12.34,-56.78,90],"boolarray":[true,false,true],"stringarray":["test string","string with \\","string with \""]}`
return m2, expected2
}
func map3() (*Map, string) {
m3 := NewMap()
a1, expected1 := array1()
a2, expected2 := array2()
a3, expected3 := array3()
a4, expected4 := array4()
m3.PutArray("array1", a1)
m3.PutArray("array2", a2)
m3.PutArray("array3", a3)
m3.PutArray("array4", a4)
expected := fmt.Sprintf(`{"array1":%s,"array2":%s,"array3":%s,"array4":%s}`, expected1, expected2, expected3, expected4)
return m3, expected
}
func map4() (*Map, string) {
m4 := NewMap()
m1, expected1 := map1()
m2, expected2 := map2()
m3, expected3 := map3()
m4.PutMap("map1", m1)
m4.PutMap("map2", m2)
m4.PutMap("map3", m3)
expected := fmt.Sprintf(`{"map1":%s,"map2":%s,"map3":%s}`, expected1, expected2, expected3)
return m4, expected
}
func TestMapValue(t *testing.T) {
m1, expected1 := map1()
m2, expected2 := map2()
m3, expected3 := map3()
m4, expected4 := map4()
testCases := []struct {
name string
value *Map
expected string
}{
{"basic", m1, expected1},
{"primitive array", m2, expected2},
{"general array", m3, expected3},
{"nested map", m4, expected4},
}
for _, c := range testCases {
data := c.value.Serialize(nil)
if string(data) != c.expected {
t.Errorf("map name:%s actual:%s != expected:%s", c.name, string(data), c.expected)
}
if len(data) != c.value.Size() {
t.Errorf("map name:%s buf size error, actual:%d, expected:%d", c.name, len(data), c.value.Size())
}
var obj map[string]interface{}
if err := json.Unmarshal(data, &obj); err != nil {
t.Errorf("map name:%s unmarshal error:%v", c.name, err)
}
}
}