Skip to content

Commit a2ed0f1

Browse files
committed
passing the testCase to each test, standardising the functions signature
1 parent 7d17417 commit a2ed0f1

9 files changed

Lines changed: 56 additions & 71 deletions

tests/config/run-test.js

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import * as testVariantCoverage from "./test-variant-coverage.js";
1212
import { shouldThrowOnFormat } from "./utilities.js";
1313

1414
async function testFixture(fixture) {
15-
const { name, context, filepath } = fixture;
15+
const { name, context } = fixture;
1616
const { stringifiedOptions, parsers } = context;
1717

1818
const title = `${name}${
@@ -29,17 +29,7 @@ async function testFixture(fixture) {
2929
: "format";
3030

3131
test(testTitle, async () => {
32-
let { code, expectedOutput, parser, formatOptions } = testCase;
33-
let formatResult = await testCase.runFormat();
34-
35-
await testFormat.run(
36-
code,
37-
formatResult,
38-
parser,
39-
parsers,
40-
expectedOutput,
41-
formatOptions,
42-
);
32+
await testFormat.run(testCase);
4333

4434
if (!FULL_TEST) {
4535
return;
@@ -53,17 +43,9 @@ async function testFixture(fixture) {
5343
testBom.run,
5444
testBytecodeCompare.run,
5545
]
56-
.map((test) => test(code, formatResult, filepath, formatOptions))
46+
.map((test) => test(testCase))
5747
.join(
58-
["\r\n", "\r"].map((eol) =>
59-
testEndOfLine.run(
60-
code,
61-
formatResult,
62-
filepath,
63-
formatOptions,
64-
eol,
65-
),
66-
),
48+
["\r\n", "\r"].map((eol) => testEndOfLine.run(testCase, eol)),
6749
),
6850
);
6951
});

tests/config/test-antlr-format.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@ import getPrettier from "./get-prettier.js";
33
import getCreateParser from "./get-create-parser.js";
44
import getPlugins from "./get-plugins.js";
55

6-
async function testAntlrFormat(source, formatResult, filename, formatOptions) {
6+
async function testAntlrFormat(testCase) {
7+
const { code, filepath, formatOptions } = testCase;
8+
const formatResult = await testCase.runFormat();
9+
710
if (
811
formatOptions.parser === "slang" &&
9-
!failedTests.isAntlrMismatch(filename, formatOptions)
12+
!failedTests.isAntlrMismatch(filepath, formatOptions)
1013
) {
1114
// Compare with ANTLR's format
1215
const createParser = await getCreateParser();
13-
const { parser } = createParser(source, formatOptions);
16+
const { parser } = createParser(code, formatOptions);
1417
const prettier = await getPrettier();
15-
const { formatted: antlrOutput } = await prettier.formatWithCursor(source, {
18+
const { formatted: antlrOutput } = await prettier.formatWithCursor(code, {
1619
...formatOptions,
1720
// Since Slang forces us to decide on a compiler version, we need to do the
1821
// same for ANTLR unless it was already given as an option.

tests/config/test-ast-compare.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import * as failedTests from "./failed-format-tests.js";
22
import { parse } from "./run-prettier.js";
33

4-
async function testAstCompare(source, formatResult, filename, formatOptions) {
5-
const isAstUnstableTest = failedTests.isAstUnstable(filename, formatOptions);
4+
async function testAstCompare(testCase) {
5+
const { code, filepath, formatOptions } = testCase;
6+
const formatResult = await testCase.runFormat();
7+
8+
const isAstUnstableTest = failedTests.isAstUnstable(filepath, formatOptions);
69
// Some parsers skip parsing empty files
7-
if (formatResult.changed && source.trim()) {
10+
if (formatResult.changed && code.trim()) {
811
const [originalAst, formattedAst] = await Promise.all(
912
[formatResult.input, formatResult.output].map((code) =>
1013
parse(code, formatResult.options),

tests/config/test-bom.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { BOM } from "./constants.js";
22
import { format } from "./run-prettier.js";
33

4-
async function testBom(source, formatResult, _filename, formatOptions) {
5-
if (source.charAt(0) !== BOM) {
4+
async function testBom(testCase) {
5+
const { code, formatOptions } = testCase;
6+
const formatResult = await testCase.runFormat();
7+
8+
if (code.charAt(0) !== BOM) {
69
const { eolVisualizedOutput: output } = await format(
7-
BOM + source,
10+
BOM + code,
811
formatOptions,
912
);
1013
const expected = BOM + formatResult.eolVisualizedOutput;

tests/config/test-bytecode-compare.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ import path from "node:path";
22
import createEsmUtils from "esm-utils";
33
import compileContract from "./utils/compile-contract.js";
44

5-
async function testBytecodeCompare(
6-
_source,
7-
formatResult,
8-
filename,
9-
formatOptions,
10-
) {
11-
if (shouldCompareBytecode(filename, formatOptions)) {
12-
const output = compileContract(filename, formatResult.output);
13-
const expected = compileContract(filename, formatResult.input);
5+
async function testBytecodeCompare(testCase) {
6+
const { filepath, formatOptions } = testCase;
7+
const formatResult = await testCase.runFormat();
8+
9+
if (shouldCompareBytecode(filepath, formatOptions)) {
10+
const output = compileContract(filepath, formatResult.output);
11+
const expected = compileContract(filepath, formatResult.input);
1412
expect(output).toEqual(expected);
1513
}
1614
}
@@ -41,8 +39,8 @@ const testsWithAstChanges = new Map(
4139
}),
4240
);
4341

44-
const shouldCompareBytecode = (filename, options) => {
45-
const testFunction = testsWithAstChanges.get(filename);
42+
const shouldCompareBytecode = (filepath, options) => {
43+
const testFunction = testsWithAstChanges.get(filepath);
4644

4745
if (!testFunction) {
4846
return false;

tests/config/test-end-of-line.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import { format } from "./run-prettier.js";
22
import visualizeEndOfLine from "./utils/visualize-end-of-line.js";
33

4-
async function testEndOfLine(
5-
source,
6-
formatResult,
7-
_filename,
8-
formatOptions,
9-
eol,
10-
) {
11-
if (!shouldSkipEolTest(source, formatResult.options)) {
4+
async function testEndOfLine(testCase, eol) {
5+
const { code, formatOptions } = testCase;
6+
const formatResult = await testCase.runFormat();
7+
8+
if (!shouldSkipEolTest(code, formatResult.options)) {
129
const { eolVisualizedOutput: output } = await format(
13-
source.replace(/\n/gu, eol),
10+
code.replace(/\n/gu, eol),
1411
formatOptions,
1512
);
1613
// Only if `endOfLine: "auto"` the result will be different

tests/config/test-format.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ import consistentEndOfLine from "./utils/consistent-end-of-line.js";
22
import createSnapshot from "./utils/create-snapshot.js";
33
import visualizeEndOfLine from "./utils/visualize-end-of-line.js";
44

5-
async function testFormat(
6-
code,
7-
formatResult,
8-
parser,
9-
parsers,
10-
expectedOutput,
11-
formatOptions,
12-
) {
5+
async function testFormat(testCase) {
6+
const { code, parser, expectedOutput, formatOptions } = testCase;
7+
const formatResult = await testCase.runFormat();
8+
139
// Verify parsers or error tests
1410
if (formatOptions.parser !== parser) {
1511
const runFormat = () => format(code, formatOptions);
@@ -39,7 +35,10 @@ async function testFormat(
3935

4036
// All parsers have the same result, only snapshot the result from main parser
4137
expect(
42-
createSnapshot(formatResult, { parsers, formatOptions }),
38+
createSnapshot(formatResult, {
39+
parsers: testCase.context.parsers,
40+
formatOptions,
41+
}),
4342
).toMatchSnapshot();
4443
}
4544

tests/config/test-second-format.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import * as failedTests from "./failed-format-tests.js";
22
import { format } from "./run-prettier.js";
33

4-
async function testSecondFormat(source, formatResult, filename, formatOptions) {
5-
const isUnstableTest = failedTests.isUnstable(filename, formatOptions);
4+
async function testSecondFormat(testCase) {
5+
const { code, filepath, formatOptions } = testCase;
6+
const formatResult = await testCase.runFormat();
7+
8+
const isUnstableTest = failedTests.isUnstable(filepath, formatOptions);
69
if (
710
(formatResult.changed || isUnstableTest) &&
811
// No range and cursor
9-
formatResult.input === source
12+
formatResult.input === code
1013
) {
1114
const { eolVisualizedOutput: firstOutput, output } = formatResult;
1215
const { eolVisualizedOutput: secondOutput } = await format(

tests/config/test-variant-coverage.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import getCreateParser from "./get-create-parser.js";
22
import getVariantCoverage from "./get-variant-coverage.js";
33

4-
async function testVariantCoverage(
5-
source,
6-
_formatResult,
7-
_filename,
8-
formatOptions,
9-
) {
4+
async function testVariantCoverage(testCase) {
5+
const { code, formatOptions } = testCase;
6+
107
if (formatOptions.parser === "slang") {
118
const createParser = await getCreateParser();
129
const variantCoverage = await getVariantCoverage();
13-
const { parseOutput } = createParser(source, formatOptions);
10+
const { parseOutput } = createParser(code, formatOptions);
1411

1512
// Check coverage
1613
variantCoverage(parseOutput.tree.asNonterminalNode());

0 commit comments

Comments
 (0)