forked from mholt/json-to-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json-to-go.test.js
157 lines (152 loc) · 5.96 KB
/
json-to-go.test.js
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
const jsonToGo = require("./json-to-go");
function quote(str) {
return "'" + str
.replace(/\t/g, "\\t")
.replace(/\n/g, "\\n")
.replace(/'/g, "\\'") + "'"
}
function test(includeExampleData) {
const testCases = [
{
input: '{"SourceCode": "exampleDataHere"}',
expected:
'type AutoGenerated struct {\n\tSourceCode string `json:"SourceCode"`\n}',
expectedWithExample:
'type AutoGenerated struct {\n\tSourceCode string `json:"SourceCode" example:"exampleDataHere"`\n}',
},
{
input: '{"source_code": "exampleDataHere"}',
expected:
'type AutoGenerated struct {\n\tSourceCode string `json:"source_code"`\n}',
expectedWithExample:
'type AutoGenerated struct {\n\tSourceCode string `json:"source_code" example:"exampleDataHere"`\n}'
},
{
input: '{"sourceCode": "exampleDataHere"}',
expected:
'type AutoGenerated struct {\n\tSourceCode string `json:"sourceCode"`\n}',
expectedWithExample:
'type AutoGenerated struct {\n\tSourceCode string `json:"sourceCode" example:"exampleDataHere"`\n}' },
{
input: '{"SOURCE_CODE": ""}',
expected:
'type AutoGenerated struct {\n\tSourceCode string `json:"SOURCE_CODE"`\n}',
expectedWithExample:
'type AutoGenerated struct {\n\tSourceCode string `json:"SOURCE_CODE"`\n}'
},
{
input: '{"PublicIP": ""}',
expected:
'type AutoGenerated struct {\n\tPublicIP string `json:"PublicIP"`\n}',
expectedWithExample:
'type AutoGenerated struct {\n\tPublicIP string `json:"PublicIP"`\n}'
},
{
input: '{"public_ip": ""}',
expected:
'type AutoGenerated struct {\n\tPublicIP string `json:"public_ip"`\n}',
expectedWithExample:
'type AutoGenerated struct {\n\tPublicIP string `json:"public_ip"`\n}'
},
{
input: '{"publicIP": ""}',
expected:
'type AutoGenerated struct {\n\tPublicIP string `json:"publicIP"`\n}',
expectedWithExample:
'type AutoGenerated struct {\n\tPublicIP string `json:"publicIP"`\n}'
},
{
input: '{"PUBLIC_IP": ""}',
expected:
'type AutoGenerated struct {\n\tPublicIP string `json:"PUBLIC_IP"`\n}',
expectedWithExample:
'type AutoGenerated struct {\n\tPublicIP string `json:"PUBLIC_IP"`\n}'
},
{
input: '{"+1": "Fails", "-1": "This should not cause duplicate field name"}',
expected:
'type AutoGenerated struct {\n\tNum1 string `json:"+1"`\n\tNum10 string `json:"-1"`\n}',
expectedWithExample:
'type AutoGenerated struct {\n\tNum1 string `json:"+1" example:"Fails"`\n\tNum10 string `json:"-1" example:"This should not cause duplicate field name"`\n}'
},
{
input: '{"age": 46}',
expected:
'type AutoGenerated struct {\n\tAge int `json:"age"`\n}',
expectedWithExample:
'type AutoGenerated struct {\n\tAge int `json:"age" example:"46"`\n}'
},
{
input: '{"negativeFloat": -1.00}',
expected:
'type AutoGenerated struct {\n\tNegativeFloat float64 `json:"negativeFloat"`\n}',
expectedWithExample:
'type AutoGenerated struct {\n\tNegativeFloat float64 `json:"negativeFloat" example:"-1.1"`\n}'
},
{
input: '{"zeroFloat": 0.00}',
expected:
'type AutoGenerated struct {\n\tZeroFloat float64 `json:"zeroFloat"`\n}',
expectedWithExample:
'type AutoGenerated struct {\n\tZeroFloat float64 `json:"zeroFloat" example:"0.1"`\n}'
},
{
input: '{"positiveFloat": 1.00}',
expected:
'type AutoGenerated struct {\n\tPositiveFloat float64 `json:"positiveFloat"`\n}',
expectedWithExample:
'type AutoGenerated struct {\n\tPositiveFloat float64 `json:"positiveFloat" example:"1.1"`\n}'
},
{
input: '{"negativeFloats": [-1.00, -2.00, -3.00]}',
expected:
'type AutoGenerated struct {\n\tNegativeFloats []float64 `json:"negativeFloats"`\n}',
expectedWithExample:
'type AutoGenerated struct {\n\tNegativeFloats []float64 `json:"negativeFloats"`\n}'
},
{
input: '{"zeroFloats": [0.00, 0.00, 0.00]}',
expected:
'type AutoGenerated struct {\n\tZeroFloats []float64 `json:"zeroFloats"`\n}',
expectedWithExample:
'type AutoGenerated struct {\n\tZeroFloats []float64 `json:"zeroFloats"`\n}'
},
{
input: '{"positiveFloats": [1.00, 2.00, 3.00]}',
expected:
'type AutoGenerated struct {\n\tPositiveFloats []float64 `json:"positiveFloats"`\n}',
expectedWithExample:
'type AutoGenerated struct {\n\tPositiveFloats []float64 `json:"positiveFloats"`\n}'
},
{
input: '{"topLevel": { "secondLevel": "exampleDataHere"} }',
expected:
'type AutoGenerated struct {\n\tTopLevel struct {\n\t\tSecondLevel string `json:"secondLevel"`\n\t} `json:"topLevel"`\n}',
expectedWithExample:
'type AutoGenerated struct {\n\tTopLevel struct {\n\t\tSecondLevel string `json:"secondLevel" example:"exampleDataHere"`\n\t} `json:"topLevel"`\n}'
},
{
input: '{"people": [{ "name": "Frank"}, {"name": "Dennis"}, {"name": "Dee"}, {"name": "Charley"}, {"name":"Mac"}] }',
expected:
'type AutoGenerated struct {\n\tPeople []struct {\n\t\tName string `json:"name"`\n\t} `json:"people"`\n}',
expectedWithExample:
'type AutoGenerated struct {\n\tPeople []struct {\n\t\tName string `json:"name" example:"Frank"`\n\t} `json:"people"`\n}'
} ];
for (const testCase of testCases) {
const got = jsonToGo(testCase.input, null, null, includeExampleData);
if (got.error) {
console.assert(!got.error, `format('${testCase.input}'): ${got.error}`);
} else {
exp = includeExampleData ? testCase.expectedWithExample : testCase.expected
console.assert(
got.go === exp,
`format('${testCase.input}'): \n\tgot: ${quote(got.go)}\n\twant: ${
quote(exp)
}`
);
}
}
console.log("done")
}
test(false);
test(true)