Skip to content

Commit 89d453e

Browse files
committed
add test for mergable objects within arrays
1 parent 4b17b38 commit 89d453e

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

��json-to-go.test.js

Lines changed: 31 additions & 2 deletions
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+
"array-with-mergable-objects",
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);
157-
test(true)
185+
test(true)
186+
testFiles()

‎tests/array-with-mergable-objects.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
type AutoGenerated struct {
2+
Booleanfield bool `json:"booleanfield"`
3+
Inconsistentarray []Inconsistentarray `json:"inconsistentarray,omitempty"`
4+
Date string `json:"date,omitempty"`
5+
}
6+
type Features struct {
7+
Age int `json:"age,omitempty"`
8+
Height int `json:"height,omitempty"`
9+
Gender string `json:"gender,omitempty"`
10+
}
11+
type Inconsistentarray struct {
12+
ID int `json:"id,omitempty"`
13+
Name string `json:"name,omitempty"`
14+
Features Features `json:"features,omitempty"`
15+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"booleanfield": true,
3+
"inconsistentarray": [
4+
{
5+
"id": 1,
6+
"name": "John Doe",
7+
"features": {
8+
"age": 49,
9+
"height": 175
10+
}
11+
},
12+
{
13+
"id": 2,
14+
"name": "Jane Doe",
15+
"features": {
16+
"height": 164
17+
}
18+
},
19+
{
20+
"id": 3,
21+
"name": "John Doe",
22+
"features": {
23+
"gender": "unknown"
24+
}
25+
}
26+
],
27+
"date": "2024-07-22"
28+
}

0 commit comments

Comments
 (0)