diff --git a/test/fixtures.ts b/test/fixtures.ts index dece181ef6..45f51eb937 100644 --- a/test/fixtures.ts +++ b/test/fixtures.ts @@ -359,14 +359,39 @@ class JSONFixture extends LanguageFixture { return 0; } - compareJsonFileToJson( - comparisonArgs( + // The analog of the JSON Schema fixture's `.out..json` + // convention: a JSON input `foo.json` can come with an expected-output + // file `foo.out..json`, which applies when `` 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 && diff --git a/test/inputs/json/priority/omit-empty.json b/test/inputs/json/priority/omit-empty.json new file mode 100644 index 0000000000..56425f9b78 --- /dev/null +++ b/test/inputs/json/priority/omit-empty.json @@ -0,0 +1,11 @@ +{ + "results": [ + { + "age": 52, + "name": null + }, + { + "age": 27 + } + ] +} diff --git a/test/inputs/json/priority/omit-empty.out.omit-empty.json b/test/inputs/json/priority/omit-empty.out.omit-empty.json new file mode 100644 index 0000000000..809e3cfa02 --- /dev/null +++ b/test/inputs/json/priority/omit-empty.out.omit-empty.json @@ -0,0 +1,10 @@ +{ + "results": [ + { + "age": 52 + }, + { + "age": 27 + } + ] +} diff --git a/test/languages.ts b/test/languages.ts index 6bcfd43611..1aedcb4024 100644 --- a/test/languages.ts +++ b/test/languages.ts @@ -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"], }; diff --git a/test/utils.ts b/test/utils.ts index db98e3101b..3a6d1f4bc2 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -186,7 +186,11 @@ export async function inDir(dir: string, work: () => Promise) { } export function testsInDir(dir: string, extension: string): string[] { - return shell.ls(`${dir}/*.${extension}`); + // Expected-output files (`foo.out..json`) accompany test inputs; + // they are never test inputs themselves. + return shell + .ls(`${dir}/*.${extension}`) + .filter((fn) => !/\.out\.[^./]+\.[^./]+$/.test(fn)); } export interface Sample {