-
Notifications
You must be signed in to change notification settings - Fork 21
/
json2go_test.go
453 lines (421 loc) · 13.6 KB
/
json2go_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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package json2go
import (
"bytes"
"fmt"
"testing"
)
var basic = []byte(`{
"foo": "fooer",
"bar": "bars",
"biz": 1,
"baz": 42.1,
"foo_bar": "frood"
}`)
var expectedBasic = "type Basic struct {\n\tBar string `json:\"bar\"`\n\tBaz float64 `json:\"baz\"`\n\tBiz int `json:\"biz\"`\n\tFoo string `json:\"foo\"`\n\tFooBar string `json:\"foo_bar\"`\n}\n"
var expectedBasicPkg = fmt.Sprintf("package main\n\n%s", expectedBasic)
func TestBasicStruct(t *testing.T) {
// create reader
r := bytes.NewReader(basic)
var buff bytes.Buffer
calvin := NewTransmogrifier("basic", r, &buff)
err := calvin.Gen()
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if buff.String() != expectedBasicPkg {
t.Errorf("expected %q got %q", expectedBasicPkg, buff.String())
}
}
var intermediate = []byte(`{
"name": "Marvin",
"id": 42,
"bot": true,
"quotes": [
"I think you ought to know I'm feeling very depressed",
"Life? Don't talk to me about life!"
],
"ints": [
1,
2,
3,
4
],
"floats": [
1.01,
2.01,
3.01
],
"bools": [
true,
false
],
"date": "Fri Jan 23 13:02:46 +0000 2015"
}`)
var expectedIntermediate = "type Intermediate struct {\n\tBools []bool `json:\"bools\"`\n\tBot bool `json:\"bot\"`\n\tDate string `json:\"date\"`\n\tFloats []float64 `json:\"floats\"`\n\tID int `json:\"id\"`\n\tInts []int `json:\"ints\"`\n\tName string `json:\"name\"`\n\tQuotes []string `json:\"quotes\"`\n}\n"
var expectedIntermediatePkg = fmt.Sprintf("package main\n\n%s", expectedIntermediate)
func TestIntermediateStruct(t *testing.T) {
// create reader
r := bytes.NewReader(intermediate)
var buff bytes.Buffer
calvin := NewTransmogrifier("intermediate", r, &buff)
err := calvin.Gen()
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if buff.String() != expectedIntermediatePkg {
t.Errorf("expected %q got %q", expectedIntermediatePkg, buff.String())
}
}
// widget example from json.org/example.html
var widget = []byte(`{
"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}
}`)
var expectedWidget = "type TestW struct {\n\tWidget `json:\"widget\"`\n}\n\ntype Widget struct {\n\tDebug string `json:\"debug\"`\n\tImage `json:\"image\"`\n\tText `json:\"text\"`\n\tWindow `json:\"window\"`\n}\n\ntype Image struct {\n\tAlignment string `json:\"alignment\"`\n\tHOffset int `json:\"hOffset\"`\n\tName string `json:\"name\"`\n\tSrc string `json:\"src\"`\n\tVOffset int `json:\"vOffset\"`\n}\n\ntype Text struct {\n\tAlignment string `json:\"alignment\"`\n\tData string `json:\"data\"`\n\tHOffset int `json:\"hOffset\"`\n\tName string `json:\"name\"`\n\tOnMouseUp string `json:\"onMouseUp\"`\n\tSize int `json:\"size\"`\n\tStyle string `json:\"style\"`\n\tVOffset int `json:\"vOffset\"`\n}\n\ntype Window struct {\n\tHeight int `json:\"height\"`\n\tName string `json:\"name\"`\n\tTitle string `json:\"title\"`\n\tWidth int `json:\"width\"`\n}\n"
var expectedWidgetPkg = fmt.Sprintf("package main\n\n%s", expectedWidget)
func TestWidget(t *testing.T) {
// create reader
r := bytes.NewReader(widget)
var buff bytes.Buffer
calvin := NewTransmogrifier("TestW", r, &buff)
err := calvin.Gen()
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if buff.String() != expectedWidgetPkg {
t.Errorf("expected %q got %q", expectedWidgetPkg, buff.String())
}
}
var wnull = []byte(`{
"foo": "fooer",
"bar": null,
"biz": 1,
"baz": 42.1,
"foo_bar": "frood"
}`)
var expectedWNull = "type WNull struct {\n\tBar interface{} `json:\"bar\"`\n\tBaz float64 `json:\"baz\"`\n\tBiz int `json:\"biz\"`\n\tFoo string `json:\"foo\"`\n\tFooBar string `json:\"foo_bar\"`\n}\n"
var expectedWNullPkg = fmt.Sprintf("package main\n\n%s", expectedWNull)
func TestWNull(t *testing.T) {
// create reader
r := bytes.NewReader(wnull)
var buff bytes.Buffer
calvin := NewTransmogrifier("WNull", r, &buff)
err := calvin.Gen()
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if buff.String() != expectedWNullPkg {
t.Errorf("expected %q got %q", expectedWNullPkg, buff.String())
}
}
var basicArr = []byte(`[{
"foo": "fooer",
"bar": "bars",
"biz_id": 1,
"baz": 42.1,
"foo_bar": "frood"
}]`)
var expectedBasicArr = "type BasicArr struct {\n\tBar string `json:\"bar\"`\n\tBaz float64 `json:\"baz\"`\n\tBizID int `json:\"biz_id\"`\n\tFoo string `json:\"foo\"`\n\tFooBar string `json:\"foo_bar\"`\n}\n"
var expectedBasicArrPkg = fmt.Sprintf("package main\n\n%s", expectedBasicArr)
func TestBasicArr(t *testing.T) {
// create reader
r := bytes.NewReader(basicArr)
var buff bytes.Buffer
calvin := NewTransmogrifier("BasicArr", r, &buff)
err := calvin.Gen()
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if buff.String() != expectedBasicArrPkg {
t.Errorf("expected %q got %q", expectedBasicArrPkg, buff.String())
}
}
var sliceMap = []byte(`{
"foo": [
{
"bar": "biz",
"foo_bar": "frood"
},
{
"bar": "baz",
"foo_bar": "hoopy"
}
]
}`)
var expectedSliceMap = "type SliceMap struct {\n\tFoos []Foo `json:\"foo\"`\n}\n\ntype Foo struct {\n\tBar string `json:\"bar\"`\n\tFooBar string `json:\"foo_bar\"`\n}\n"
var expectedSliceMapPkg = fmt.Sprintf("package main\n\n%s", expectedSliceMap)
func TestArrays(t *testing.T) {
// create reader
r := bytes.NewReader(sliceMap)
var buff bytes.Buffer
calvin := NewTransmogrifier("SliceMap", r, &buff)
err := calvin.Gen()
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if buff.String() != expectedSliceMapPkg {
t.Errorf("expected %q got %q", expectedSliceMapPkg, buff.String())
}
}
var mapType = []byte(`{
"example.com": {
"name": "example.com",
"type": "SOA",
"ttl": 300,
"content": "ns1.example.com. hostmaster.example.com. 1299682996 300 1800 604800 300"
}
}`)
var expectedMapTypeStruct = "type Zone map[string]Struct\n\ntype Struct struct {\n\tContent string `json:\"content\"`\n\tName string `json:\"name\"`\n\tTTL int `json:\"ttl\"`\n\tType string `json:\"type\"`\n}\n"
var expectedMapTypeStructPkg = fmt.Sprintf("package main\n\n%s", expectedMapTypeStruct)
var expectedMapTypeDomain = "type Zone map[string]Domain\n\ntype Domain struct {\n\tContent string `json:\"content\"`\n\tName string `json:\"name\"`\n\tTTL int `json:\"ttl\"`\n\tType string `json:\"type\"`\n}\n"
var expectedMapTypeDomainPkg = fmt.Sprintf("package main\n\n%s", expectedMapTypeDomain)
func TestMapType(t *testing.T) {
// create reader
tests := []struct {
structName string
expected string
}{
{"", expectedMapTypeStructPkg},
{"domain", expectedMapTypeDomainPkg},
}
for _, test := range tests {
r := bytes.NewReader(mapType)
var buff bytes.Buffer
calvin := NewTransmogrifier("Zone", r, &buff)
calvin.MapType = true
if test.structName != "" {
calvin.SetStructName(test.structName)
}
err := calvin.Gen()
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if buff.String() != test.expected {
t.Errorf("expected %q got %q", test.expected, buff.String())
}
}
}
var mapSliceType = []byte(`{
"example.com": [
{
"name": "example.com",
"type": "SOA",
"ttl": 300,
"content": "ns1.example.com. hostmaster.example.com. 1299682996 300 1800 604800 300"
}
]
}`)
var expectedMapSliceTypeStruct = "type Zone map[string][]Struct\n\ntype Struct struct {\n\tContent string `json:\"content\"`\n\tName string `json:\"name\"`\n\tTTL int `json:\"ttl\"`\n\tType string `json:\"type\"`\n}\n"
var expectedMapSliceTypeStructkg = fmt.Sprintf("package main\n\n%s", expectedMapSliceTypeStruct)
var expectedMapSliceTypeDomain = "type Zone map[string][]Domain\n\ntype Domain struct {\n\tContent string `json:\"content\"`\n\tName string `json:\"name\"`\n\tTTL int `json:\"ttl\"`\n\tType string `json:\"type\"`\n}\n"
var expectedMapSliceTypeDomainPkg = fmt.Sprintf("package main\n\n%s", expectedMapSliceTypeDomain)
func TestMapSliceType(t *testing.T) {
// create reader
tests := []struct {
structName string
expected string
}{
{"", expectedMapSliceTypeStructkg},
{"domain", expectedMapSliceTypeDomainPkg},
}
for _, test := range tests {
r := bytes.NewReader(mapSliceType)
var buff bytes.Buffer
calvin := NewTransmogrifier("Zone", r, &buff)
calvin.MapType = true
if test.structName != "" {
calvin.SetStructName(test.structName)
}
err := calvin.Gen()
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if buff.String() != test.expected {
t.Errorf("expected %q got %q", test.expected, buff.String())
}
}
}
func TestDefineFieldTags(t *testing.T) {
tests := []struct {
keys []string
value string
expected string
}{
{nil, "field", "`json:\"field\"`"},
{[]string{}, "field", "`json:\"field\"`"},
{[]string{"xml"}, "field", "`json:\"field\" xml:\"field\"`"},
{[]string{"xml", "yaml", "db"}, "field", "`json:\"field\" xml:\"field\" yaml:\"field\" db:\"field\"`"},
}
for i, test := range tests {
tag := defineFieldTags(test.value, test.keys)
if tag != test.expected {
t.Errorf("%d: got %q, want %q", i, tag, test.expected)
}
}
}
func TestTransmogrify(t *testing.T) {
tests := []struct {
pkg string
name string
structName string
MapType bool
importJSON bool
json []byte
expected string
}{
{"", "Basic", "", false, false, basic, fmt.Sprintf("package main\n\n%s", expectedBasic)},
{"test", "Basic", "", false, false, basic, fmt.Sprintf("package test\n\n%s", expectedBasic)},
{"", "Basic", "", false, true, basic, fmt.Sprintf("package main\n\nimport (\n\t\"encoding/json\"\n)\n\n%s", expectedBasic)},
{"test", "Basic", "", false, true, basic, fmt.Sprintf("package test\n\nimport (\n\t\"encoding/json\"\n)\n\n%s", expectedBasic)},
{"", "zone", "", true, false, mapType, fmt.Sprintf("package main\n\n%s", expectedMapTypeStruct)},
{"test", "zone", "", true, false, mapType, fmt.Sprintf("package test\n\n%s", expectedMapTypeStruct)},
{"", "zone", "", true, true, mapType, fmt.Sprintf("package main\n\nimport (\n\t\"encoding/json\"\n)\n\n%s", expectedMapTypeStruct)},
{"test", "zone", "", true, true, mapType, fmt.Sprintf("package test\n\nimport (\n\t\"encoding/json\"\n)\n\n%s", expectedMapTypeStruct)},
{"", "zone", "domain", true, false, mapType, fmt.Sprintf("package main\n\n%s", expectedMapTypeDomain)},
{"test", "zone", "domain", true, false, mapType, fmt.Sprintf("package test\n\n%s", expectedMapTypeDomain)},
{"", "zone", "domain", true, true, mapType, fmt.Sprintf("package main\n\nimport (\n\t\"encoding/json\"\n)\n\n%s", expectedMapTypeDomain)},
{"test", "zone", "domain", true, true, mapType, fmt.Sprintf("package test\n\nimport (\n\t\"encoding/json\"\n)\n\n%s", expectedMapTypeDomain)},
}
for i, test := range tests {
var b bytes.Buffer
// create reader
r := bytes.NewReader(test.json)
calvin := NewTransmogrifier(test.name, r, &b)
if test.pkg != "" {
calvin.SetPkg(test.pkg)
}
calvin.ImportJSON = test.importJSON
if test.MapType {
calvin.MapType = test.MapType
if test.structName != "" {
calvin.SetStructName(test.structName)
}
}
err := calvin.Gen()
if err != nil {
t.Errorf("%d: unexpected error %q", i, err)
continue
}
if b.String() != test.expected {
t.Errorf("%d: expected %q, got %q", i, test.expected, b.String())
}
}
}
func TestNumToAlpha(t *testing.T) {
tests := []struct {
char rune
expected string
}{
{'0', "Zero"},
{'1', "One"},
{'2', "Two"},
{'3', "Three"},
{'4', "Four"},
{'5', "Five"},
{'6', "Six"},
{'7', "Seven"},
{'8', "Eight"},
{'9', "Nine"},
}
for i, test := range tests {
s := numToAlpha(test.char)
if s != test.expected {
t.Errorf("%d: expected %s got %s", i, test.expected, s)
}
}
}
func TestShouldDiscard(t *testing.T) {
tests := []struct {
char rune
expected bool
}{
{'~', true},
{'!', true},
{'@', true},
{'#', true},
{'$', true},
{'%', true},
{'^', true},
{'&', true},
{'-', true},
{'_', true},
{'*', true},
{'=', true},
{'+', true},
{':', true},
{'.', true},
{'<', true},
{'>', true},
{'a', false},
{'z', false},
{'你', false},
}
for i, test := range tests {
b := shouldDiscard(test.char)
if b != test.expected {
t.Errorf("%d: expected %t, got %t", i, test.expected, b)
}
}
}
func TestCleanFieldName(t *testing.T) {
tests := []struct {
name string
expected string
}{
{"abdcn", "Abdcn"},
{">asb", "Asb"},
{"_asdf", "Asdf"},
{"<>field", "Field"},
{"日本語a", "日本語a"},
{"бsdf", "Бsdf"},
{"6dsaf", "Sixdsaf"},
}
for i, test := range tests {
s := cleanFieldName(test.name)
if s != test.expected {
t.Errorf("%d: expected %q, got %q", i, test.expected, s)
}
}
}
// this is to validate https://github.com/mohae/json2go/pull/3
func TestEmptySlice(t *testing.T) {
test := `{
"vals": [
]
}`
expected := "package main\n\ntype Test struct {\n\tVals []interface{} `json:\"vals\"`\n}\n"
r := bytes.NewReader([]byte(test))
var buff bytes.Buffer
calvin := NewTransmogrifier("test", r, &buff)
err := calvin.Gen()
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if buff.String () != expected {
t.Errorf("got %q want %q", buff.String(), expected)
}
}