Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,14 +359,39 @@ class JSONFixture extends LanguageFixture {
return 0;
}

compareJsonFileToJson(
comparisonArgs(
// The analog of the JSON Schema fixture's `.out.<feature>.json`
// convention: a JSON input `foo.json` can come with an expected-output
// file `foo.out.<key>.json`, which applies when `<key>` is one of the
// language's `features`, or the name of a renderer option this
// particular run sets (via `quickTestRendererOptions`). When it
// applies, the output must match it exactly, without the usual
// round-trip leniency for null properties. This is how output that
// legitimately differs from the input — e.g. Go's `omitempty`
// dropping null fields — gets asserted.
let expectedFilename = filename;
let strict = false;
const expectedOutputKeys = [
...this.language.features,
...Object.keys(additionalRendererOptions),
];
for (const key of expectedOutputKeys) {
const outFilename = filename.replace(/\.json$/, `.out.${key}.json`);
if (fs.existsSync(outFilename)) {
expectedFilename = outFilename;
strict = true;
break;
}
}

compareJsonFileToJson({
...comparisonArgs(
this.language,
filename,
filename,
expectedFilename,
additionalRendererOptions,
),
);
strict,
});

if (
this.language.diffViaSchema &&
Expand Down
11 changes: 11 additions & 0 deletions test/inputs/json/priority/omit-empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"results": [
{
"age": 52,
"name": null
},
{
"age": 27
}
]
}
10 changes: 10 additions & 0 deletions test/inputs/json/priority/omit-empty.out.omit-empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"results": [
{
"age": 52
},
{
"age": 27
}
]
}
7 changes: 6 additions & 1 deletion test/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,12 @@ export const GoLanguage: Language = {
"postman-collection.schema",
],
rendererOptions: {},
quickTestRendererOptions: [],
quickTestRendererOptions: [
// Runs against the expected-output file
// `omit-empty.out.omit-empty.json`, which asserts that `omitempty`
// actually drops the null field.
["omit-empty.json", { "omit-empty": "true" }],
],
sourceFiles: ["src/language/Golang/index.ts"],
};

Expand Down
6 changes: 5 additions & 1 deletion test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,11 @@ export async function inDir(dir: string, work: () => Promise<void>) {
}

export function testsInDir(dir: string, extension: string): string[] {
return shell.ls(`${dir}/*.${extension}`);
// Expected-output files (`foo.out.<key>.json`) accompany test inputs;
// they are never test inputs themselves.
return shell
.ls(`${dir}/*.${extension}`)
.filter((fn) => !/\.out\.[^./]+\.[^./]+$/.test(fn));
}

export interface Sample {
Expand Down
Loading