Skip to content

Commit bc9a584

Browse files
authored
moving failed format into it's own file (#1455)
1 parent 5edce22 commit bc9a584

File tree

5 files changed

+85
-82
lines changed

5 files changed

+85
-82
lines changed

tests/config/constants.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import path from "node:path";
2+
import createEsmUtils from "esm-utils";
3+
import { normalizeDirectory } from "./utilities.js";
4+
5+
const { __dirname } = createEsmUtils(import.meta);
6+
7+
export const FORMAT_TEST_DIRECTORY = normalizeDirectory(
8+
path.join(__dirname, "../format/"),
9+
);
10+
11+
export const CURSOR_PLACEHOLDER = "<|>";
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import path from "node:path";
2+
import { FORMAT_TEST_DIRECTORY } from "./constants.js";
3+
4+
// Here we add files that will not be the same when formatting a second time.
5+
const unstableTests = new Map(
6+
[].map((fixture) => {
7+
const [file, isUnstable = () => true] = Array.isArray(fixture)
8+
? fixture
9+
: [fixture];
10+
return [path.join(FORMAT_TEST_DIRECTORY, file), isUnstable];
11+
}),
12+
);
13+
14+
// Here we add files that will not have the same AST after being formatted.
15+
const unstableAstTests = new Map(
16+
[
17+
// `: =` and `= :` are syntactically the same as `:=` and `=:`, but the ast
18+
// changes from `YulColonAndEqual` and `YulEqualAndColon` to `ColonEqual`
19+
// and `EqualColon`, which is expected but the workaround to keep the test
20+
// stable is too much, so we just put it in this list.
21+
"AssemblyV0.4.26/Assembly.sol",
22+
].map((fixture) => {
23+
const [file, isUnstable = () => true] = Array.isArray(fixture)
24+
? fixture
25+
: [fixture];
26+
return [path.join(FORMAT_TEST_DIRECTORY, file), isUnstable];
27+
}),
28+
);
29+
30+
const antlrMismatchTests = new Map(
31+
[
32+
// Better placement of comments in Slang.
33+
"BasicIterator/BasicIterator.sol",
34+
"Comments/Comments.sol",
35+
"IndexOf/IndexOf.sol",
36+
// Syntax for `pragma solidity 0.5.0 - 0.6.0;` not supported by ANTLR
37+
"Pragma/Pragma.sol",
38+
// ANTLR doesn't support assembly assignment operators separated by a space
39+
// like `: =` or `= :`
40+
"AssemblyV0.4.26/Assembly.sol",
41+
// ANTLR doesn't support UntypedTupleMember with a storage location, which
42+
// is valid Slang, but not in Solidity.
43+
"AllSolidityFeaturesV0.4.26/AllSolidityFeatures.sol",
44+
// TODO Review how ANTLR is formatting chained assignments
45+
"Assignments/Assignments.sol",
46+
].map((fixture) => {
47+
const [file, isUnstable = () => true] = Array.isArray(fixture)
48+
? fixture
49+
: [fixture];
50+
return [path.join(FORMAT_TEST_DIRECTORY, file), isUnstable];
51+
}),
52+
);
53+
54+
const isUnstable = (filepath, options) =>
55+
unstableTests.get(filepath)?.(options);
56+
57+
const isAstUnstable = (filepath, options) =>
58+
unstableAstTests.get(filepath)?.(options);
59+
60+
const isAntlrMismatch = (filepath, options) =>
61+
antlrMismatchTests.get(filepath)?.(options);
62+
63+
export { isAstUnstable, isUnstable, isAntlrMismatch };

tests/config/run-format-test.js

Lines changed: 5 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import consistentEndOfLine from "./utils/consistent-end-of-line.js";
1111
import createSnapshot from "./utils/create-snapshot.js";
1212
import { stringifyOptionsForTitle } from "./utils/stringify-options-for-title.js";
1313
import visualizeEndOfLine from "./utils/visualize-end-of-line.js";
14+
import {
15+
isAntlrMismatch,
16+
isAstUnstable,
17+
isUnstable,
18+
} from "./failed-format-tests.js";
1419

1520
const { __dirname } = createEsmUtils(import.meta);
1621

@@ -21,32 +26,6 @@ const CURSOR_PLACEHOLDER = "<|>";
2126
const RANGE_START_PLACEHOLDER = "<<<PRETTIER_RANGE_START>>>";
2227
const RANGE_END_PLACEHOLDER = "<<<PRETTIER_RANGE_END>>>";
2328

24-
// Here we add files that will not be the same when formatting a second time.
25-
const unstableTests = new Map(
26-
[].map((fixture) => {
27-
const [file, isUnstable = () => true] = Array.isArray(fixture)
28-
? fixture
29-
: [fixture];
30-
return [path.join(__dirname, "../format/", file), isUnstable];
31-
}),
32-
);
33-
34-
// Here we add files that will not have the same AST after being formatted.
35-
const unstableAstTests = new Map(
36-
[
37-
// `: =` and `= :` are syntactically the same as `:=` and `=:`, but the ast
38-
// changes from `YulColonAndEqual` and `YulEqualAndColon` to `ColonEqual`
39-
// and `EqualColon`, which is expected but the workaround to keep the test
40-
// stable is too much, so we just put it in this list.
41-
"AssemblyV0.4.26/Assembly.sol",
42-
].map((fixture) => {
43-
const [file, isAstUnstable = () => true] = Array.isArray(fixture)
44-
? fixture
45-
: [fixture];
46-
return [path.join(__dirname, "../format/", file), isAstUnstable];
47-
}),
48-
);
49-
5029
const testsWithAstChanges = new Map(
5130
[
5231
"Parentheses/AddNoParentheses.sol",
@@ -71,60 +50,6 @@ const testsWithAstChanges = new Map(
7150
}),
7251
);
7352

74-
const antlrMismatchTests = new Map(
75-
[
76-
// Better placement of comments in Slang.
77-
"BasicIterator/BasicIterator.sol",
78-
"Comments/Comments.sol",
79-
"IndexOf/IndexOf.sol",
80-
// Syntax for `pragma solidity 0.5.0 - 0.6.0;` not supported by ANTLR
81-
"Pragma/Pragma.sol",
82-
// ANTLR doesn't support assembly assignment operators separated by a space
83-
// like `: =` or `= :`
84-
"AssemblyV0.4.26/Assembly.sol",
85-
// ANTLR doesn't support UntypedTupleMember with a storage location, which
86-
// is valid Slang, but not in Solidity.
87-
"AllSolidityFeaturesV0.4.26/AllSolidityFeatures.sol",
88-
// TODO Review how ANTLR is formatting chained assignments
89-
"Assignments/Assignments.sol",
90-
].map((fixture) => {
91-
const [file, compareBytecode = () => true] = Array.isArray(fixture)
92-
? fixture
93-
: [fixture];
94-
return [path.join(__dirname, "../format/", file), compareBytecode];
95-
}),
96-
);
97-
98-
const isUnstable = (filename, options) => {
99-
const testFunction = unstableTests.get(filename);
100-
101-
if (!testFunction) {
102-
return false;
103-
}
104-
105-
return testFunction(options);
106-
};
107-
108-
const isAstUnstable = (filename, options) => {
109-
const testFunction = unstableAstTests.get(filename);
110-
111-
if (!testFunction) {
112-
return false;
113-
}
114-
115-
return testFunction(options);
116-
};
117-
118-
const isAntlrMismatch = (filename, options) => {
119-
const testFunction = antlrMismatchTests.get(filename);
120-
121-
if (!testFunction) {
122-
return false;
123-
}
124-
125-
return testFunction(options);
126-
};
127-
12853
const shouldCompareBytecode = (filename, options) => {
12954
const testFunction = testsWithAstChanges.get(filename);
13055

tests/config/utilities.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import path from "node:path";
2+
3+
const normalizeDirectory = (directory) => path.normalize(directory + path.sep);
4+
5+
export { normalizeDirectory };

tests/config/utils/create-snapshot.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { wrap as raw } from "jest-snapshot-serializer-raw";
2+
import { CURSOR_PLACEHOLDER } from "../constants.js";
23
import visualizeEndOfLine from "./visualize-end-of-line.js";
34
import visualizeRange from "./visualize-range.js";
45

5-
const CURSOR_PLACEHOLDER = "<|>";
6-
76
const DEFAULT_PRINT_WIDTH = 80;
87
const SEPARATOR_WIDTH = DEFAULT_PRINT_WIDTH;
98
function printSeparator(description = "") {

0 commit comments

Comments
 (0)