diff --git a/tests/js/eol/__snapshots__/jsfmt.spec.js.snap b/tests/js/eol/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..4e93e2e43 --- /dev/null +++ b/tests/js/eol/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,312 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`cursor-1.js - {"endOfLine":"auto"} format 1`] = ` +====================================options===================================== +endOfLine: "auto" +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +(function(){return <|>15})() + +=====================================output===================================== +** The EOL of output might not real when \`TEST_CRLF\` ** +(function () { + return <|>15; +})(); + +================================================================================ +`; + +exports[`cursor-1.js - {"endOfLine":"cr"} format 1`] = ` +====================================options===================================== +endOfLine: "cr" +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +(function(){return <|>15})() + +=====================================output===================================== +(function () { + return <|>15; +})(); + +================================================================================ +`; + +exports[`cursor-1.js - {"endOfLine":"crlf"} format 1`] = ` +====================================options===================================== +endOfLine: "crlf" +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +(function(){return <|>15})() + +=====================================output===================================== +(function () { + return <|>15; +})(); + +================================================================================ +`; + +exports[`cursor-1.js - {"endOfLine":"lf"} format 1`] = ` +====================================options===================================== +endOfLine: "lf" +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +(function(){return <|>15})() + +=====================================output===================================== +(function () { + return <|>15; +})(); + +================================================================================ +`; + +exports[`range-1.js - {"endOfLine":"auto"} format 1`] = ` +====================================options===================================== +endOfLine: "auto" +parsers: ["babel"] +printWidth: 80 + | | printWidth +=====================================input====================================== + 1 | + 2 | +> 3 | class a { + | ^^^^ +> 4 | b( ) {} + | ^^^^^^^^^^^ + 5 | } + 6 | + 7 | let x + 8 | +=====================================output===================================== +** The EOL of output might not real when \`TEST_CRLF\` ** + + +class a { + b() {} +} + +let x + +================================================================================ +`; + +exports[`range-1.js - {"endOfLine":"cr"} format 1`] = ` +====================================options===================================== +endOfLine: "cr" +parsers: ["babel"] +printWidth: 80 + | | printWidth +=====================================input====================================== + 1 | + 2 | +> 3 | class a { + | ^^^^ +> 4 | b( ) {} + | ^^^^^^^^^^^ + 5 | } + 6 | + 7 | let x + 8 | +=====================================output===================================== + + +class a { + b() {} +} + +let x + +================================================================================ +`; + +exports[`range-1.js - {"endOfLine":"crlf"} format 1`] = ` +====================================options===================================== +endOfLine: "crlf" +parsers: ["babel"] +printWidth: 80 + | | printWidth +=====================================input====================================== + 1 | + 2 | +> 3 | class a { + | ^^^^ +> 4 | b( ) {} + | ^^^^^^^^^^^ + 5 | } + 6 | + 7 | let x + 8 | +=====================================output===================================== + + +class a { + b() {} +} + +let x + +================================================================================ +`; + +exports[`range-1.js - {"endOfLine":"lf"} format 1`] = ` +====================================options===================================== +endOfLine: "lf" +parsers: ["babel"] +printWidth: 80 + | | printWidth +=====================================input====================================== + 1 | + 2 | +> 3 | class a { + | ^^^^ +> 4 | b( ) {} + | ^^^^^^^^^^^ + 5 | } + 6 | + 7 | let x + 8 | +=====================================output===================================== + + +class a { + b() {} +} + +let x + +================================================================================ +`; + +exports[`range-and-cursor-1.js - {"endOfLine":"auto"} format 1`] = ` +====================================options===================================== +endOfLine: "auto" +parsers: ["babel"] +printWidth: 80 + | | printWidth +=====================================input====================================== + 1 | + 2 | + 3 | +> 4 | class a { + | ^^^^ +> 5 | b( <|> ) {} + | ^^^^^^^^^^^^^^ + 6 | } + 7 | + 8 | let x + 9 | +=====================================output===================================== +** The EOL of output might not real when \`TEST_CRLF\` ** + + + +class a { + b(<|>) {} +} + +let x + +================================================================================ +`; + +exports[`range-and-cursor-1.js - {"endOfLine":"cr"} format 1`] = ` +====================================options===================================== +endOfLine: "cr" +parsers: ["babel"] +printWidth: 80 + | | printWidth +=====================================input====================================== + 1 | + 2 | + 3 | +> 4 | class a { + | ^^^^ +> 5 | b( <|> ) {} + | ^^^^^^^^^^^^^^ + 6 | } + 7 | + 8 | let x + 9 | +=====================================output===================================== + + + +class a { + b(<|>) {} +} + +let x + +================================================================================ +`; + +exports[`range-and-cursor-1.js - {"endOfLine":"crlf"} format 1`] = ` +====================================options===================================== +endOfLine: "crlf" +parsers: ["babel"] +printWidth: 80 + | | printWidth +=====================================input====================================== + 1 | + 2 | + 3 | +> 4 | class a { + | ^^^^ +> 5 | b( <|> ) {} + | ^^^^^^^^^^^^^^ + 6 | } + 7 | + 8 | let x + 9 | +=====================================output===================================== + + + +class a { + b(<|>) {} +} + +let x + +================================================================================ +`; + +exports[`range-and-cursor-1.js - {"endOfLine":"lf"} format 1`] = ` +====================================options===================================== +endOfLine: "lf" +parsers: ["babel"] +printWidth: 80 + | | printWidth +=====================================input====================================== + 1 | + 2 | + 3 | +> 4 | class a { + | ^^^^ +> 5 | b( <|> ) {} + | ^^^^^^^^^^^^^^ + 6 | } + 7 | + 8 | let x + 9 | +=====================================output===================================== + + + +class a { + b(<|>) {} +} + +let x + +================================================================================ +`; diff --git a/tests/js/eol/cursor-1.js b/tests/js/eol/cursor-1.js new file mode 100644 index 000000000..dea72dfae --- /dev/null +++ b/tests/js/eol/cursor-1.js @@ -0,0 +1 @@ +(function(){return <|>15})() diff --git a/tests/js/eol/jsfmt.spec.js b/tests/js/eol/jsfmt.spec.js new file mode 100644 index 000000000..98d86cf6f --- /dev/null +++ b/tests/js/eol/jsfmt.spec.js @@ -0,0 +1,4 @@ +run_spec(__dirname, ["babel"], { endOfLine: "lf" }); +run_spec(__dirname, ["babel"], { endOfLine: "crlf" }); +run_spec(__dirname, ["babel"], { endOfLine: "cr" }); +run_spec(__dirname, ["babel"], { endOfLine: "auto" }); diff --git a/tests/js/eol/range-1.js b/tests/js/eol/range-1.js new file mode 100644 index 000000000..0738d2dc2 --- /dev/null +++ b/tests/js/eol/range-1.js @@ -0,0 +1,7 @@ + + +class <<>> a { + b( ) {}<<>> +} + +let x diff --git a/tests/js/eol/range-and-cursor-1.js b/tests/js/eol/range-and-cursor-1.js new file mode 100644 index 000000000..53a6be379 --- /dev/null +++ b/tests/js/eol/range-and-cursor-1.js @@ -0,0 +1,8 @@ + + + +class <<>> a { + b( <|> ) {}<<>> +} + +let x diff --git a/tests_config/run_spec.js b/tests_config/run_spec.js index eec6386a9..3733b48bb 100644 --- a/tests_config/run_spec.js +++ b/tests_config/run_spec.js @@ -179,23 +179,56 @@ global.run_spec = (fixtures, parsers, options) => { .replace(RANGE_END_PLACEHOLDER, "") : source; let codeOffset = 0; + let resultForSnapshot = formattedWithCursor; if ( typeof baseOptions.rangeStart === "number" || typeof baseOptions.rangeEnd === "number" ) { + if (TEST_CRLF && hasEndOfLine) { + codeForSnapshot = codeForSnapshot.replace(/\n/g, "\r\n"); + } codeForSnapshot = visualizeRange(codeForSnapshot, baseOptions); codeOffset = codeForSnapshot.match(/^>?\s+1 \| /)[0].length; } + // When `TEST_CRLF` and `endOfLine: "auto"`, the eol is always `\r\n`, + // Replace it with guess result from original input + let resultNote = ""; + if (mainOptions.endOfLine === "auto") { + const { + guessEndOfLine, + convertEndOfLineToChars, + } = require("../src/common/end-of-line"); + const originalAutoResult = convertEndOfLineToChars( + guessEndOfLine(code) + ); + if (originalAutoResult !== "\r\n") { + resultNote += + "** The EOL of output might not real when `TEST_CRLF` **"; + + if (TEST_CRLF) { + resultForSnapshot = resultForSnapshot.replace( + /\r\n?/g, + originalAutoResult + ); + } + } + } + if (hasEndOfLine) { codeForSnapshot = visualizeEndOfLine(codeForSnapshot); + resultForSnapshot = visualizeEndOfLine(resultForSnapshot); + } + + if (resultNote) { + resultForSnapshot = `${resultNote}\n${resultForSnapshot}`; } expect( createSnapshot( codeForSnapshot, - hasEndOfLine ? visualizedOutput : formattedWithCursor, + resultForSnapshot, composeOptionsForSnapshot(baseOptions, parsers), { codeOffset } )