Skip to content

Commit b2ecc43

Browse files
committed
moving test end of line to its own file
1 parent 93f371e commit b2ecc43

2 files changed

Lines changed: 50 additions & 37 deletions

File tree

tests/config/run-test.js

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import consistentEndOfLine from "./utils/consistent-end-of-line.js";
77
import createSnapshot from "./utils/create-snapshot.js";
88
import visualizeEndOfLine from "./utils/visualize-end-of-line.js";
99
import * as testAstCompare from "./test-ast-compare.js";
10+
import * as testEndOfLine from "./test-end-of-line.js";
1011
import * as testSecondFormat from "./test-second-format.js";
1112
import { shouldThrowOnFormat } from "./utilities.js";
1213
import getPrettier from "./get-prettier.js";
@@ -129,24 +130,8 @@ async function runTest({
129130

130131
await testSecondFormat.run(code, formatResult, filename, formatOptions);
131132
await testAstCompare.run(code, formatResult, filename, formatOptions);
132-
133-
if (!shouldSkipEolTest(code, formatResult.options)) {
134-
for (const eol of ["\r\n", "\r"]) {
135-
const { eolVisualizedOutput: output } = await format(
136-
code.replace(/\n/gu, eol),
137-
formatOptions,
138-
);
139-
// Only if `endOfLine: "auto"` the result will be different
140-
const expected =
141-
formatOptions.endOfLine === "auto"
142-
? visualizeEndOfLine(
143-
// All `code` use `LF`, so the `eol` of result is always `LF`
144-
formatResult.outputWithCursor.replace(/\n/gu, eol),
145-
)
146-
: formatResult.eolVisualizedOutput;
147-
expect(output).toEqual(expected);
148-
}
149-
}
133+
await testEndOfLine.run(code, formatResult, filename, formatOptions, "\r\n");
134+
await testEndOfLine.run(code, formatResult, filename, formatOptions, "\r");
150135

151136
if (code.charAt(0) !== BOM) {
152137
const { eolVisualizedOutput: output } = await format(
@@ -164,23 +149,4 @@ async function runTest({
164149
}
165150
}
166151

167-
function shouldSkipEolTest(code, options) {
168-
if (code.includes("\r")) {
169-
return true;
170-
}
171-
const { requirePragma, rangeStart, rangeEnd } = options;
172-
if (requirePragma) {
173-
return true;
174-
}
175-
176-
if (
177-
typeof rangeStart === "number" &&
178-
typeof rangeEnd === "number" &&
179-
rangeStart >= rangeEnd
180-
) {
181-
return true;
182-
}
183-
return false;
184-
}
185-
186152
export { runTest };

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { format } from "./run-prettier.js";
2+
import visualizeEndOfLine from "./utils/visualize-end-of-line.js";
3+
4+
async function testEndOfLine(
5+
source,
6+
formatResult,
7+
_filename,
8+
formatOptions,
9+
eol,
10+
) {
11+
if (!shouldSkipEolTest(source, formatResult.options)) {
12+
const { eolVisualizedOutput: output } = await format(
13+
source.replace(/\n/gu, eol),
14+
formatOptions,
15+
);
16+
// Only if `endOfLine: "auto"` the result will be different
17+
const expected =
18+
formatOptions.endOfLine === "auto"
19+
? visualizeEndOfLine(
20+
// All `code` use `LF`, so the `eol` of result is always `LF`
21+
formatResult.outputWithCursor.replace(/\n/gu, eol),
22+
)
23+
: formatResult.eolVisualizedOutput;
24+
expect(output).toEqual(expected);
25+
}
26+
}
27+
28+
function shouldSkipEolTest(source, options) {
29+
if (source.includes("\r")) {
30+
return true;
31+
}
32+
const { requirePragma, rangeStart, rangeEnd } = options;
33+
if (requirePragma) {
34+
return true;
35+
}
36+
37+
if (
38+
typeof rangeStart === "number" &&
39+
typeof rangeEnd === "number" &&
40+
rangeStart >= rangeEnd
41+
) {
42+
return true;
43+
}
44+
return false;
45+
}
46+
47+
export { testEndOfLine as run };

0 commit comments

Comments
 (0)