-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathrun-test.js
More file actions
85 lines (76 loc) · 2.51 KB
/
run-test.js
File metadata and controls
85 lines (76 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { FULL_TEST } from "./constants.js";
import { format } from "./run-prettier.js";
import consistentEndOfLine from "./utils/consistent-end-of-line.js";
import createSnapshot from "./utils/create-snapshot.js";
import visualizeEndOfLine from "./utils/visualize-end-of-line.js";
import * as testAstCompare from "./test-ast-compare.js";
import * as testBom from "./test-bom.js";
import * as testEndOfLine from "./test-end-of-line.js";
import * as testSecondFormat from "./test-second-format.js";
import * as testBytecodeCompare from "./test-bytecode-compare.js";
import * as testAntlrFormat from "./test-antlr-format.js";
import * as testVariantCoverage from "./test-variant-coverage.js";
import { shouldThrowOnFormat } from "./utilities.js";
async function runTest({
parsers,
name,
filename,
code,
output,
parser,
mainParserFormatResult,
mainParserFormatOptions,
}) {
let formatOptions = mainParserFormatOptions;
let formatResult = mainParserFormatResult;
// Verify parsers or error tests
if (
mainParserFormatResult.error ||
mainParserFormatOptions.parser !== parser
) {
formatOptions = { ...mainParserFormatResult.options, parser };
const runFormat = () => format(code, formatOptions);
if (shouldThrowOnFormat(name, formatOptions)) {
await expect(runFormat()).rejects.toThrowErrorMatchingSnapshot();
return;
}
// Verify parsers format result should be the same as main parser
output = mainParserFormatResult.outputWithCursor;
formatResult = await runFormat();
}
// Make sure output has consistent EOL
expect(formatResult.eolVisualizedOutput).toEqual(
visualizeEndOfLine(consistentEndOfLine(formatResult.outputWithCursor)),
);
// The result is assert to equals to `output`
if (typeof output === "string") {
expect(formatResult.eolVisualizedOutput).toEqual(
visualizeEndOfLine(output),
);
return;
}
// All parsers have the same result, only snapshot the result from main parser
expect(
createSnapshot(formatResult, { parsers, formatOptions }),
).toMatchSnapshot();
if (!FULL_TEST) {
return;
}
await Promise.all(
[
testAntlrFormat.run,
testVariantCoverage.run,
testSecondFormat.run,
testAstCompare.run,
testBom.run,
testBytecodeCompare.run,
]
.map((run) => run(code, formatResult, filename, formatOptions))
.join(
["\r\n", "\r"].map((eol) =>
testEndOfLine.run(code, formatResult, filename, formatOptions, eol),
),
),
);
}
export { runTest };