Skip to content

Commit

Permalink
Add comparisonArgs and filter by language
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam724 committed Feb 16, 2024
1 parent 14818ab commit acbd68a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
35 changes: 25 additions & 10 deletions test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ abstract class LanguageFixture extends Fixture {
filename: string,
additionalRendererOptions: RendererOptions,
additionalFiles: string[],
testComparisonArgs?: Partial<ComparisonArgs>,
expectedFilename?: string
): Promise<number>;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -276,22 +283,27 @@ class JSONFixture extends LanguageFixture {
filename: string,
additionalRendererOptions: RendererOptions,
_additionalFiles: string[],
testComparisonArgs?: Partial<ComparisonArgs>,
expectedFilename?: string
): Promise<number> {
if (this.language.compileCommand) {
await execAsync(this.language.compileCommand);
}
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");
Expand All @@ -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));
}
Expand Down
18 changes: 16 additions & 2 deletions test/lib/optionMap.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import { RendererOptions } from "quicktype-core";
import { ComparisonArgs } from "../utils";
import { GoLanguage, Language } from "../languages";

const optionMap: Record<string, RendererOptions> = {
type TestOptions = {
cliOptions: RendererOptions;
language: Language;
comparisonArgs?: Partial<ComparisonArgs>;
};

const optionMap: Record<string, TestOptions> = {
"test/inputs/json/misc/227e2.in.json": {
"omit-empty": true
cliOptions: {
"omit-empty": true
},
language: GoLanguage,
comparisonArgs: {
strict: true
}
}
};

Expand Down
21 changes: 17 additions & 4 deletions test/utils.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;
Expand Down Expand Up @@ -186,12 +187,24 @@ export interface Sample {
additionalRendererOptions: RendererOptions;
saveOutput: boolean;
outPath?: string;
comparisonArgs?: Partial<ComparisonArgs>;
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);
Expand Down

0 comments on commit acbd68a

Please sign in to comment.