11import { FULL_TEST } from "./constants.js" ;
2+ import { replacePlaceholders } from "./replace-placeholders.js" ;
23import { format } from "./run-prettier.js" ;
34import consistentEndOfLine from "./utils/consistent-end-of-line.js" ;
45import createSnapshot from "./utils/create-snapshot.js" ;
@@ -12,74 +13,121 @@ import * as testAntlrFormat from "./test-antlr-format.js";
1213import * as testVariantCoverage from "./test-variant-coverage.js" ;
1314import { shouldThrowOnFormat } from "./utilities.js" ;
1415
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 ;
27-
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 ) ;
35-
36- if ( shouldThrowOnFormat ( name , formatOptions ) ) {
37- await expect ( runFormat ( ) ) . rejects . toThrowErrorMatchingSnapshot ( ) ;
38- return ;
16+ async function testFixture ( fixture ) {
17+ const { name, context, filepath } = fixture ;
18+ const { stringifiedOptions, parsers } = context ;
19+
20+ const title = `${ name } ${
21+ stringifiedOptions ? ` - ${ stringifiedOptions } ` : ""
22+ } `;
23+
24+ describe ( title , ( ) => {
25+ const testCases = parsers . map ( ( parser ) => getTestCase ( fixture , parser ) ) ;
26+
27+ for ( const testCase of testCases ) {
28+ const testTitle =
29+ testCase . expectFail || testCase . formatOptions . parser !== testCase . parser
30+ ? `[${ testCase . parser } ] format`
31+ : "format" ;
32+
33+ test ( testTitle , async ( ) => {
34+ let { code, expectedOutput, parser, formatOptions } = testCase ;
35+ let formatResult = await testCase . runFormat ( ) ;
36+
37+ // Verify parsers or error tests
38+ if ( formatOptions . parser !== parser ) {
39+ const runFormat = ( ) => format ( code , formatOptions ) ;
40+
41+ if ( shouldThrowOnFormat ( name , formatOptions ) ) {
42+ await expect ( runFormat ( ) ) . rejects . toThrowErrorMatchingSnapshot ( ) ;
43+ return ;
44+ }
45+
46+ // Verify parsers format result should be the same as main parser
47+ output = formatResult . outputWithCursor ;
48+ formatResult = await runFormat ( ) ;
49+ }
50+
51+ // Make sure output has consistent EOL
52+ expect ( formatResult . eolVisualizedOutput ) . toEqual (
53+ visualizeEndOfLine (
54+ consistentEndOfLine ( formatResult . outputWithCursor ) ,
55+ ) ,
56+ ) ;
57+
58+ // The result is assert to equals to `expectedOutput`
59+ if ( typeof expectedOutput === "string" ) {
60+ expect ( formatResult . eolVisualizedOutput ) . toEqual (
61+ visualizeEndOfLine ( expectedOutput ) ,
62+ ) ;
63+ return ;
64+ }
65+
66+ // All parsers have the same result, only snapshot the result from main parser
67+ expect (
68+ createSnapshot ( formatResult , { parsers, formatOptions } ) ,
69+ ) . toMatchSnapshot ( ) ;
70+
71+ if ( ! FULL_TEST ) {
72+ return ;
73+ }
74+ await Promise . all (
75+ [
76+ testAntlrFormat . run ,
77+ testVariantCoverage . run ,
78+ testSecondFormat . run ,
79+ testAstCompare . run ,
80+ testBom . run ,
81+ testBytecodeCompare . run ,
82+ ]
83+ . map ( ( test ) => test ( code , formatResult , filepath , formatOptions ) )
84+ . join (
85+ [ "\r\n" , "\r" ] . map ( ( eol ) =>
86+ testEndOfLine . run (
87+ code ,
88+ formatResult ,
89+ filepath ,
90+ formatOptions ,
91+ eol ,
92+ ) ,
93+ ) ,
94+ ) ,
95+ ) ;
96+ } ) ;
3997 }
98+ } ) ;
99+ }
40100
41- // Verify parsers format result should be the same as main parser
42- output = mainParserFormatResult . outputWithCursor ;
43- formatResult = await runFormat ( ) ;
44- }
101+ function getTestCase ( fixture , parser ) {
102+ const { code : originalText , context, filepath } = fixture ;
45103
46- // Make sure output has consistent EOL
47- expect ( formatResult . eolVisualizedOutput ) . toEqual (
48- visualizeEndOfLine ( consistentEndOfLine ( formatResult . outputWithCursor ) ) ,
104+ const { text : code , options : formatOptions } = replacePlaceholders (
105+ originalText ,
106+ {
107+ printWidth : 80 ,
108+ ...context . options ,
109+ filepath,
110+ parser,
111+ } ,
49112 ) ;
50113
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- }
58-
59- // All parsers have the same result, only snapshot the result from main parser
60- expect (
61- createSnapshot ( formatResult , { parsers, formatOptions } ) ,
62- ) . toMatchSnapshot ( ) ;
63-
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- ) ;
114+ const expectFail = shouldThrowOnFormat ( fixture , formatOptions ) ;
115+
116+ let promise ;
117+
118+ return {
119+ context,
120+ parser,
121+ filepath,
122+ originalText,
123+ code,
124+ formatOptions,
125+ expectFail,
126+ expectedOutput : fixture . output ,
127+ isEmpty : code . trim ( ) === "" ,
128+ runFormat : ( ) =>
129+ promise === undefined ? ( promise = format ( code , formatOptions ) ) : promise ,
130+ } ;
83131}
84132
85- export { runTest } ;
133+ export { testFixture } ;
0 commit comments