-
-
Notifications
You must be signed in to change notification settings - Fork 474
/
json-to-go.test.js
195 lines (187 loc) · 7.24 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
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
const jsonToGo = require("./json-to-go");
function quote(str) {
return "'" + str
.replace(/\t/g, "\\t")
.replace(/\n/g, "\\n")
.replace(/\r/g, "\\r")
.replace(/'/g, "\\'") + "'"
}
function test(includeExampleData) {
const testCases = [
{
input: '{"SourceCode": "exampleDataHere"}',
expected:
'type AutoGenerated struct {\n\tSourceCode string `json:"SourceCode"`\n}\n',
expectedWithExample:
'type AutoGenerated struct {\n\tSourceCode string `json:"SourceCode" example:"exampleDataHere"`\n}\n',
},
{
input: '{"source_code": "exampleDataHere"}',
expected:
'type AutoGenerated struct {\n\tSourceCode string `json:"source_code"`\n}\n',
expectedWithExample:
'type AutoGenerated struct {\n\tSourceCode string `json:"source_code" example:"exampleDataHere"`\n}\n',
},
{
input: '{"sourceCode": "exampleDataHere"}',
expected:
'type AutoGenerated struct {\n\tSourceCode string `json:"sourceCode"`\n}\n',
expectedWithExample:
'type AutoGenerated struct {\n\tSourceCode string `json:"sourceCode" example:"exampleDataHere"`\n}\n',
},
{
input: '{"SOURCE_CODE": ""}',
expected:
'type AutoGenerated struct {\n\tSourceCode string `json:"SOURCE_CODE"`\n}\n',
expectedWithExample:
'type AutoGenerated struct {\n\tSourceCode string `json:"SOURCE_CODE"`\n}\n',
},
{
input: '{"PublicIP": ""}',
expected:
'type AutoGenerated struct {\n\tPublicIP string `json:"PublicIP"`\n}\n',
expectedWithExample:
'type AutoGenerated struct {\n\tPublicIP string `json:"PublicIP"`\n}\n',
},
{
input: '{"public_ip": ""}',
expected:
'type AutoGenerated struct {\n\tPublicIP string `json:"public_ip"`\n}\n',
expectedWithExample:
'type AutoGenerated struct {\n\tPublicIP string `json:"public_ip"`\n}\n',
},
{
input: '{"publicIP": ""}',
expected:
'type AutoGenerated struct {\n\tPublicIP string `json:"publicIP"`\n}\n',
expectedWithExample:
'type AutoGenerated struct {\n\tPublicIP string `json:"publicIP"`\n}\n',
},
{
input: '{"PUBLIC_IP": ""}',
expected:
'type AutoGenerated struct {\n\tPublicIP string `json:"PUBLIC_IP"`\n}\n',
expectedWithExample:
'type AutoGenerated struct {\n\tPublicIP string `json:"PUBLIC_IP"`\n}\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}\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}\n',
},
{
input: '{"age": 46}',
expected:
'type AutoGenerated struct {\n\tAge int `json:"age"`\n}\n',
expectedWithExample:
'type AutoGenerated struct {\n\tAge int `json:"age" example:"46"`\n}\n',
},
{
input: '{"negativeFloat": -1.00}',
expected:
'type AutoGenerated struct {\n\tNegativeFloat float64 `json:"negativeFloat"`\n}\n',
expectedWithExample:
'type AutoGenerated struct {\n\tNegativeFloat float64 `json:"negativeFloat" example:"-1.1"`\n}\n',
},
{
input: '{"zeroFloat": 0.00}',
expected:
'type AutoGenerated struct {\n\tZeroFloat float64 `json:"zeroFloat"`\n}\n',
expectedWithExample:
'type AutoGenerated struct {\n\tZeroFloat float64 `json:"zeroFloat" example:"0.1"`\n}\n',
},
{
input: '{"positiveFloat": 1.00}',
expected:
'type AutoGenerated struct {\n\tPositiveFloat float64 `json:"positiveFloat"`\n}\n',
expectedWithExample:
'type AutoGenerated struct {\n\tPositiveFloat float64 `json:"positiveFloat" example:"1.1"`\n}\n',
},
{
input: '{"negativeFloats": [-1.00, -2.00, -3.00]}',
expected:
'type AutoGenerated struct {\n\tNegativeFloats []float64 `json:"negativeFloats"`\n}\n',
expectedWithExample:
'type AutoGenerated struct {\n\tNegativeFloats []float64 `json:"negativeFloats"`\n}\n',
},
{
input: '{"zeroFloats": [0.00, 0.00, 0.00]}',
expected:
'type AutoGenerated struct {\n\tZeroFloats []float64 `json:"zeroFloats"`\n}\n',
expectedWithExample:
'type AutoGenerated struct {\n\tZeroFloats []float64 `json:"zeroFloats"`\n}\n',
},
{
input: '{"positiveFloats": [1.00, 2.00, 3.00]}',
expected:
'type AutoGenerated struct {\n\tPositiveFloats []float64 `json:"positiveFloats"`\n}\n',
expectedWithExample:
'type AutoGenerated struct {\n\tPositiveFloats []float64 `json:"positiveFloats"`\n}\n',
},
{
input: '{"topLevel": { "secondLevel": "exampleDataHere"} }',
expected:
'type AutoGenerated struct {\n\tTopLevel struct {\n\t\tSecondLevel string `json:"secondLevel"`\n\t} `json:"topLevel"`\n}\n',
expectedWithExample:
'type AutoGenerated struct {\n\tTopLevel struct {\n\t\tSecondLevel string `json:"secondLevel" example:"exampleDataHere"`\n\t} `json:"topLevel"`\n}\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}\n',
expectedWithExample:
'type AutoGenerated struct {\n\tPeople []struct {\n\t\tName string `json:"name" example:"Frank"`\n\t} `json:"people"`\n}\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}`);
process.exitCode = 16
} else {
const exp = includeExampleData ? testCase.expectedWithExample : testCase.expected
const success = got.go === exp
console.assert(success,
`format('${testCase.input}'): \n\tgot: ${quote(got.go)}\n\twant: ${quote(exp)}`
);
if(!success) process.exitCode = 17
}
}
console.log(includeExampleData ? "done testing samples with data" : "done testing samples without data")
}
function testFiles() {
const fs = require('fs');
const path = require('path');
const testCases = [
"duplicate-top-level-structs",
"double-nested-objects",
"array-with-mixed-float-int",
"array-with-nonmatching-types",
];
for (const testCase of testCases) {
try {
const jsonData = fs.readFileSync(path.join('tests', testCase + '.json'), 'utf8');
const expectedGoData = fs.readFileSync(path.join('tests', testCase + '.go'), 'utf8');
const got = jsonToGo(jsonData);
if (got.error) {
console.assert(!got.error, `format('${jsonData}'): ${got.error}`);
process.exitCode = 18
} else {
const success = got.go === expectedGoData
console.assert(success,
`format('${jsonData}'): \n\tgot: ${quote(got.go)}\n\twant: ${quote(expectedGoData)}`
);
if(!success) process.exitCode = 19
}
} catch (err) {
console.error(err);
process.exitCode = 20
}
}
console.log("done testing files")
}
test(false);
test(true)
testFiles()