Skip to content

Commit 4a88b58

Browse files
committed
add tests for duplicate top-level structs
1 parent 08f2078 commit 4a88b58

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

json-to-go.test.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,38 @@ function test(includeExampleData) {
149149
);
150150
}
151151
}
152+
console.log(includeExampleData ? "done testing samples with data" : "done testing samples without data")
153+
}
154+
155+
function testFiles() {
156+
const fs = require('fs');
157+
const path = require('path');
158+
159+
const testCases = [
160+
"duplicate-top-level-structs",
161+
];
152162

153-
console.log("done")
163+
for (const testCase of testCases) {
164+
165+
try {
166+
const jsonData = fs.readFileSync(path.join('tests', testCase + '.json'), 'utf8');
167+
const expectedGoData = fs.readFileSync(path.join('tests', testCase + '.go'), 'utf8');
168+
const got = jsonToGo(jsonData);
169+
if (got.error) {
170+
console.assert(!got.error, `format('${jsonData}'): ${got.error}`);
171+
} else {
172+
console.assert(
173+
got.go === expectedGoData,
174+
`format('${jsonData}'): \n\tgot: ${quote(got.go)}\n\twant: ${quote(expectedGoData)}`
175+
);
176+
}
177+
} catch (err) {
178+
console.error(err);
179+
}
180+
}
181+
console.log("done testing files")
154182
}
155183

156184
test(false);
157185
test(true)
186+
testFiles()

tests/duplicate-top-level-structs.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
type AutoGenerated struct {
2+
Region Region `json:"region"`
3+
Municipality Municipality `json:"municipality"`
4+
Building Building `json:"building"`
5+
}
6+
type Identifier struct {
7+
Type string `json:"type"`
8+
ID int `json:"id"`
9+
}
10+
type Region struct {
11+
Identifier Identifier `json:"identifier"`
12+
Autonomous bool `json:"autonomous"`
13+
}
14+
type MunicipalityIdentifier struct {
15+
Type string `json:"type"`
16+
Name string `json:"name"`
17+
}
18+
type Municipality struct {
19+
MunicipalityIdentifier MunicipalityIdentifier `json:"identifier"`
20+
}
21+
type Postal struct {
22+
Type string `json:"type"`
23+
ID int `json:"id"`
24+
}
25+
type Road struct {
26+
Name string `json:"name"`
27+
ID int `json:"id"`
28+
}
29+
type BuildingIdentifier struct {
30+
Postal Postal `json:"postal"`
31+
Road Road `json:"road"`
32+
}
33+
type Building struct {
34+
BuildingIdentifier BuildingIdentifier `json:"identifier"`
35+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"region": {
3+
"identifier": {
4+
"type": "ISO 3166-1",
5+
"id": 234
6+
},
7+
"autonomous": true
8+
},
9+
"municipality": {
10+
"identifier": {
11+
"type": "local",
12+
"name": "Tórshavn"
13+
}
14+
},
15+
"building": {
16+
"identifier": {
17+
"postal": {
18+
"type": "local",
19+
"id": 100
20+
},
21+
"road": {
22+
"name": "Gríms Kambansgøta",
23+
"id": 1
24+
}
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)