|
1 | 1 | import { FULL_TEST } from "./constants.js"; |
| 2 | +import { replacePlaceholders } from "./replace-placeholders.js"; |
2 | 3 | import { format } from "./run-prettier.js"; |
3 | | -import consistentEndOfLine from "./utils/consistent-end-of-line.js"; |
4 | | -import createSnapshot from "./utils/create-snapshot.js"; |
5 | | -import visualizeEndOfLine from "./utils/visualize-end-of-line.js"; |
6 | 4 | import * as testAstCompare from "./test-ast-compare.js"; |
7 | 5 | import * as testBom from "./test-bom.js"; |
8 | 6 | import * as testEndOfLine from "./test-end-of-line.js"; |
| 7 | +import * as testFormat from "./test-format.js"; |
9 | 8 | import * as testSecondFormat from "./test-second-format.js"; |
10 | 9 | import * as testBytecodeCompare from "./test-bytecode-compare.js"; |
11 | 10 | import * as testAntlrFormat from "./test-antlr-format.js"; |
12 | 11 | import * as testVariantCoverage from "./test-variant-coverage.js"; |
13 | 12 | import { shouldThrowOnFormat } from "./utilities.js"; |
14 | 13 |
|
15 | | -async function runTest({ |
16 | | - parsers, |
17 | | - name, |
18 | | - filename, |
19 | | - code, |
20 | | - output, |
21 | | - parser, |
22 | | - mainParserFormatResult, |
23 | | - mainParserFormatOptions, |
24 | | -}) { |
25 | | - let formatOptions = mainParserFormatOptions; |
26 | | - let formatResult = mainParserFormatResult; |
| 14 | +async function testFixture(fixture) { |
| 15 | + const { name, context } = fixture; |
| 16 | + const { stringifiedOptions, parsers } = context; |
27 | 17 |
|
28 | | - // Verify parsers or error tests |
29 | | - if ( |
30 | | - mainParserFormatResult.error || |
31 | | - mainParserFormatOptions.parser !== parser |
32 | | - ) { |
33 | | - formatOptions = { ...mainParserFormatResult.options, parser }; |
34 | | - const runFormat = () => format(code, formatOptions); |
| 18 | + const title = `${name}${ |
| 19 | + stringifiedOptions ? ` - ${stringifiedOptions}` : "" |
| 20 | + }`; |
35 | 21 |
|
36 | | - if (shouldThrowOnFormat(name, formatOptions)) { |
37 | | - await expect(runFormat()).rejects.toThrowErrorMatchingSnapshot(); |
38 | | - return; |
| 22 | + describe(title, () => { |
| 23 | + const testCases = parsers.map((parser) => getTestCase(fixture, parser)); |
| 24 | + |
| 25 | + for (const testCase of testCases) { |
| 26 | + const testTitle = |
| 27 | + testCase.expectFail || testCase.formatOptions.parser !== testCase.parser |
| 28 | + ? `[${testCase.parser}] format` |
| 29 | + : "format"; |
| 30 | + |
| 31 | + test(testTitle, async () => { |
| 32 | + await testFormat.run(testCase); |
| 33 | + |
| 34 | + if (!FULL_TEST) { |
| 35 | + return; |
| 36 | + } |
| 37 | + await Promise.all( |
| 38 | + [ |
| 39 | + testAntlrFormat.run, |
| 40 | + testVariantCoverage.run, |
| 41 | + testSecondFormat.run, |
| 42 | + testAstCompare.run, |
| 43 | + testBom.run, |
| 44 | + testBytecodeCompare.run, |
| 45 | + ] |
| 46 | + .map((test) => test(testCase)) |
| 47 | + .join( |
| 48 | + ["\r\n", "\r"].map((eol) => testEndOfLine.run(testCase, eol)), |
| 49 | + ), |
| 50 | + ); |
| 51 | + }); |
39 | 52 | } |
| 53 | + }); |
| 54 | +} |
40 | 55 |
|
41 | | - // Verify parsers format result should be the same as main parser |
42 | | - output = mainParserFormatResult.outputWithCursor; |
43 | | - formatResult = await runFormat(); |
44 | | - } |
| 56 | +function getTestCase(fixture, parser) { |
| 57 | + const { code: originalText, context, filepath } = fixture; |
45 | 58 |
|
46 | | - // Make sure output has consistent EOL |
47 | | - expect(formatResult.eolVisualizedOutput).toEqual( |
48 | | - visualizeEndOfLine(consistentEndOfLine(formatResult.outputWithCursor)), |
| 59 | + const { text: code, options: formatOptions } = replacePlaceholders( |
| 60 | + originalText, |
| 61 | + { |
| 62 | + printWidth: 80, |
| 63 | + ...context.options, |
| 64 | + filepath, |
| 65 | + parser, |
| 66 | + }, |
49 | 67 | ); |
50 | 68 |
|
51 | | - // The result is assert to equals to `output` |
52 | | - if (typeof output === "string") { |
53 | | - expect(formatResult.eolVisualizedOutput).toEqual( |
54 | | - visualizeEndOfLine(output), |
55 | | - ); |
56 | | - return; |
57 | | - } |
| 69 | + const expectFail = shouldThrowOnFormat(fixture, formatOptions); |
58 | 70 |
|
59 | | - // All parsers have the same result, only snapshot the result from main parser |
60 | | - expect( |
61 | | - createSnapshot(formatResult, { parsers, formatOptions }), |
62 | | - ).toMatchSnapshot(); |
| 71 | + let promise; |
63 | 72 |
|
64 | | - if (!FULL_TEST) { |
65 | | - return; |
66 | | - } |
67 | | - await Promise.all( |
68 | | - [ |
69 | | - testAntlrFormat.run, |
70 | | - testVariantCoverage.run, |
71 | | - testSecondFormat.run, |
72 | | - testAstCompare.run, |
73 | | - testBom.run, |
74 | | - testBytecodeCompare.run, |
75 | | - ] |
76 | | - .map((run) => run(code, formatResult, filename, formatOptions)) |
77 | | - .join( |
78 | | - ["\r\n", "\r"].map((eol) => |
79 | | - testEndOfLine.run(code, formatResult, filename, formatOptions, eol), |
80 | | - ), |
81 | | - ), |
82 | | - ); |
| 73 | + return { |
| 74 | + context, |
| 75 | + parser, |
| 76 | + filepath, |
| 77 | + originalText, |
| 78 | + code, |
| 79 | + formatOptions, |
| 80 | + expectFail, |
| 81 | + expectedOutput: fixture.output, |
| 82 | + isEmpty: code.trim() === "", |
| 83 | + runFormat: () => |
| 84 | + promise === undefined ? (promise = format(code, formatOptions)) : promise, |
| 85 | + }; |
83 | 86 | } |
84 | 87 |
|
85 | | -export { runTest }; |
| 88 | +export { testFixture }; |
0 commit comments