From acbd68acaae647c9bb5ef8d933f06c4ab14549b6 Mon Sep 17 00:00:00 2001 From: Adam724 Date: Fri, 16 Feb 2024 15:52:10 -0500 Subject: [PATCH] Add comparisonArgs and filter by language --- test/fixtures.ts | 35 +++++++++++++++++++++++++---------- test/lib/optionMap.ts | 18 ++++++++++++++++-- test/utils.ts | 21 +++++++++++++++++---- 3 files changed, 58 insertions(+), 16 deletions(-) diff --git a/test/fixtures.ts b/test/fixtures.ts index a6b17a091..b8d6172dc 100644 --- a/test/fixtures.ts +++ b/test/fixtures.ts @@ -189,6 +189,7 @@ abstract class LanguageFixture extends Fixture { filename: string, additionalRendererOptions: RendererOptions, additionalFiles: string[], + testComparisonArgs?: Partial, expectedFilename?: string ): Promise; @@ -225,7 +226,13 @@ abstract class LanguageFixture extends Fixture { try { numFiles = await timeout( - this.test(sampleFile, sample.additionalRendererOptions, additionalFiles, sampleOutFile), + this.test( + sampleFile, + sample.additionalRendererOptions, + additionalFiles, + sample.comparisonArgs, + sampleOutFile + ), MAX_TEST_RUNTIME_MS ); } catch (e) { @@ -276,6 +283,7 @@ class JSONFixture extends LanguageFixture { filename: string, additionalRendererOptions: RendererOptions, _additionalFiles: string[], + testComparisonArgs?: Partial, expectedFilename?: string ): Promise { if (this.language.compileCommand) { @@ -283,15 +291,19 @@ class JSONFixture extends LanguageFixture { } if (this.language.runCommand === undefined) return 0; - compareJsonFileToJson({ - ...comparisonArgs( - this.language, - filename, - expectedFilename ? expectedFilename : filename, - additionalRendererOptions - ), - strict: expectedFilename ? true : false - }); + let sampleComparisonArgs = comparisonArgs( + this.language, + filename, + expectedFilename ? expectedFilename : filename, + additionalRendererOptions + ); + + // Add any additional comparison args specified in optionMap for this test + if (testComparisonArgs) { + sampleComparisonArgs = { ...sampleComparisonArgs, ...testComparisonArgs }; + } + + compareJsonFileToJson(sampleComparisonArgs); if (this.language.diffViaSchema && !_.includes(this.language.skipDiffViaSchema, path.basename(filename))) { debug("* Diffing with code generated via JSON Schema"); @@ -318,6 +330,9 @@ class JSONFixture extends LanguageFixture { if (fs.statSync(sample.path).size > 32 * 1024 * 1024) { return true; } + if (sample.language && this.language.name !== sample.language.name) { + return true; + } if (this.language.includeJSON !== undefined) { return !_.includes(this.language.includeJSON, path.basename(sample.path)); } diff --git a/test/lib/optionMap.ts b/test/lib/optionMap.ts index 0f4427f92..5891c35ca 100644 --- a/test/lib/optionMap.ts +++ b/test/lib/optionMap.ts @@ -1,8 +1,22 @@ import { RendererOptions } from "quicktype-core"; +import { ComparisonArgs } from "../utils"; +import { GoLanguage, Language } from "../languages"; -const optionMap: Record = { +type TestOptions = { + cliOptions: RendererOptions; + language: Language; + comparisonArgs?: Partial; +}; + +const optionMap: Record = { "test/inputs/json/misc/227e2.in.json": { - "omit-empty": true + cliOptions: { + "omit-empty": true + }, + language: GoLanguage, + comparisonArgs: { + strict: true + } } }; diff --git a/test/utils.ts b/test/utils.ts index 6a4531afd..59c2e617a 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -1,7 +1,7 @@ import * as fs from "fs"; import * as path from "path"; -import * as _ from "lodash"; +import _ from "lodash"; import * as shell from "shelljs"; import { main as quicktype_, CLIOptions } from "../src"; @@ -12,6 +12,7 @@ import deepEquals from "./lib/deepEquals"; import optionMap from "./lib/optionMap"; import chalk from "chalk"; +import { Language } from "./languages"; const strictDeepEquals: (x: any, y: any) => boolean = require("deep-equal"); const DEBUG = process.env.DEBUG !== undefined; @@ -186,12 +187,24 @@ export interface Sample { additionalRendererOptions: RendererOptions; saveOutput: boolean; outPath?: string; + comparisonArgs?: Partial; + language?: Language; } function sampleFromPath(path: string): Sample { - // Check optionMap for any CLI options the test in this path should run with - const options: RendererOptions = optionMap[path] ? optionMap[path] : {}; - const currentSample: Sample = { path: path, additionalRendererOptions: options, saveOutput: true }; + const currentSample: Sample = { + path: path, + additionalRendererOptions: {}, + saveOutput: true + }; + + // Check optionMap for any CLI options and comparison options the test in this path should run with + if (optionMap[path]) { + const { cliOptions, language, comparisonArgs } = optionMap[path]; + currentSample.additionalRendererOptions = cliOptions; + currentSample.language = language; + currentSample.comparisonArgs = comparisonArgs; + } // If this is an input file, we should expect a corresponding output file to compare against const inputFileMatch = path.match(inputFilePattern);