Skip to content

Commit cba0a36

Browse files
committed
[GR-40749] [GR-40316] AST validator
PullRequest: graalpython/2421
2 parents 720c7c4 + 8da1abc commit cba0a36

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1558
-73
lines changed

graalpython/com.oracle.graal.python.pegparser.test/src/com/oracle/graal/python/pegparser/BasicTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,11 @@ public void multipleStatements() {
10331033
checkSyntaxErrorMessage("a = 1\nb = 1", "multiple statements found while compiling a single statement", InputType.SINGLE);
10341034
}
10351035

1036+
@Test
1037+
public void lineContinuationInIndent() {
1038+
checkIndentationErrorMessage("\n \\\n (\\\n \\ ", "unexpected indent");
1039+
}
1040+
10361041
private void checkScopeAndTree() throws Exception {
10371042
File testFile = getTestFileFromTestAndTestMethod();
10381043
checkScopeFromFile(testFile, false);

graalpython/com.oracle.graal.python.pegparser.test/src/com/oracle/graal/python/pegparser/ParserTestBase.java

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public void checkSyntaxErrorMessage(String source, String expectedMessage, Input
148148
assertTrue("Expected Error.", errorCallback.hasErrors());
149149
TestErrorCallbackImpl.Error error = errorCallback.getErrors().get(0);
150150
assertSame("Expected SyntaxError not " + error.getType(), error.getType(), ErrorCallback.ErrorType.Syntax);
151-
assertEquals("The expected message:\n\"" + expectedMessage + "\"\n was not found. The message is: \n\"" + error.getMessage() + "\"", error.getMessage(), expectedMessage);
151+
assertEquals(expectedMessage, error.getMessage());
152152
}
153153

154154
public void checkDeprecationWarning(String source, String expectedMessage) {
@@ -177,7 +177,7 @@ public void checkIndentationErrorMessage(String source, String expectedMessage)
177177
assertTrue("Expected Error.", errorCallback.hasErrors());
178178
TestErrorCallbackImpl.Error error = errorCallback.getErrors().get(0);
179179
assertSame("Expected IndentationError not " + error.getType(), error.getType(), ErrorCallback.ErrorType.Indentation);
180-
assertEquals("The expected message:\n\"" + expectedMessage + "\"\n was not found. The message is: \n\"" + error.getMessage() + "\"", error.getMessage(), expectedMessage);
180+
assertEquals(expectedMessage, error.getMessage());
181181
}
182182

183183
public void checkTreeFromFile(File testFile, boolean goldenFileNextToTestFile) throws Exception {
@@ -188,11 +188,7 @@ public void checkTreeFromFile(File testFile, boolean goldenFileNextToTestFile) t
188188
File goldenFile = goldenFileNextToTestFile
189189
? new File(testFile.getParentFile(), getFileName(testFile) + GOLDEN_FILE_EXT)
190190
: getGoldenFile(GOLDEN_FILE_EXT);
191-
if (REGENERATE_TREE || !goldenFile.exists()) {
192-
try (FileWriter fw = new FileWriter(goldenFile)) {
193-
fw.write(tree);
194-
}
195-
}
191+
writeGoldenFileIfMissing(goldenFile, tree);
196192
assertDescriptionMatches(tree, goldenFile);
197193
}
198194

@@ -204,24 +200,15 @@ public void checkScopeFromFile(File testFile, boolean goldenFileNextToTestFile)
204200
File goldenScopeFile = goldenFileNextToTestFile
205201
? new File(testFile.getParentFile(), getFileName(testFile) + SCOPE_FILE_EXT)
206202
: getGoldenFile(SCOPE_FILE_EXT);
207-
if (REGENERATE_TREE || !goldenScopeFile.exists()) {
208-
try (FileWriter fw = new FileWriter(goldenScopeFile)) {
209-
fw.write(env.toString());
210-
}
211-
}
203+
writeGoldenFileIfMissing(goldenScopeFile, env.toString());
212204
assertDescriptionMatches(env.toString(), goldenScopeFile);
213205
}
214206

215207
public void checkTreeResult(String source, InputType inputType/* , Frame frame */) throws Exception {
216208
SSTNode resultNew = parse(source, getFileName(), inputType/* , frame */);
217209
String tree = printTreeToString(resultNew);
218210
File goldenFile = getGoldenFile(GOLDEN_FILE_EXT);
219-
if (REGENERATE_TREE || !goldenFile.exists()) {
220-
try (FileWriter fw = new FileWriter(goldenFile)) {
221-
fw.write(tree);
222-
}
223-
224-
}
211+
writeGoldenFileIfMissing(goldenFile, tree);
225212
assertDescriptionMatches(tree, goldenFile);
226213
}
227214

@@ -261,12 +248,20 @@ public void checkScopeResult(String source, InputType inputType) throws Exceptio
261248
ModTy mod = parse(source, "<module>", inputType);
262249
File goldenScopeFile = getGoldenFile(SCOPE_FILE_EXT);
263250
ScopeEnvironment env = ScopeEnvironment.analyze(mod, errorCallback, EMPTY_FUTURE);
264-
if (REGENERATE_TREE || !goldenScopeFile.exists()) {
265-
try (FileWriter fw = new FileWriter(goldenScopeFile)) {
266-
fw.write(env.toString());
251+
writeGoldenFileIfMissing(goldenScopeFile, env.toString());
252+
assertDescriptionMatches(env.toString(), goldenScopeFile);
253+
}
254+
255+
private static void writeGoldenFileIfMissing(File goldenFile, String contents) throws IOException {
256+
if (REGENERATE_TREE || !goldenFile.exists()) {
257+
if (System.getenv("CI") != null) {
258+
fail("Missing golden file " + goldenFile);
259+
} else {
260+
try (FileWriter fw = new FileWriter(goldenFile)) {
261+
fw.write(contents);
262+
}
267263
}
268264
}
269-
assertDescriptionMatches(env.toString(), goldenScopeFile);
270265
}
271266

272267
// public void checkSSTNodeOffsets(SSTNode node) {
@@ -281,17 +276,7 @@ protected String printTreeToString(SSTNode node) {
281276
}
282277

283278
protected void assertDescriptionMatches(String actual, File goldenFile) throws Exception {
284-
if (!goldenFile.exists()) {
285-
if (!goldenFile.createNewFile()) {
286-
fail("Cannot create file " + goldenFile.getAbsolutePath());
287-
}
288-
try (FileWriter fw = new FileWriter(goldenFile)) {
289-
fw.write(actual);
290-
}
291-
fail("Created generated golden file " + goldenFile.getAbsolutePath() + "\nPlease re-run the test.");
292-
}
293279
String expected = readFile(goldenFile);
294-
295280
assertDescriptionMatches(actual, expected, goldenFile.getName());
296281
}
297282

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Module[0, 4]
2+
BYTES[0, 4] Value: <unprintable value>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Module[0, 6]
2+
Dict[0, 6]
3+
Values:
4+
Key: LONG[1, 2] Value: 1
5+
Val: LONG[4, 5] Value: 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Module[0, 23]
2+
DictComp[0, 23]
3+
Key: Tuple[1, 5]
4+
Values:
5+
Name[2, 3] Value: "a"
6+
Value: Name[7, 8] Value: "b"
7+
ComprehensionTy[10, 22]
8+
Target:
9+
Tuple[14, 17] Context: Store
10+
Values:
11+
Name[14, 15] Value: "a" Store
12+
Name[16, 17] Value: "b" Store
13+
Iterator: Name[21, 22] Value: "y"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Module[0, 3]
2+
ELLIPSIS[0, 3] Value: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Module[0, 9]
2+
JoinedStr[0, 9]
3+
Values:
4+
RAW[2, 3] Value: "a"
5+
FormattedValue[4, 7]
6+
Value: Name[1, 2] Value: "b"
7+
Conversion: 114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Module[0, 45]
2+
JoinedStr[0, 45]
3+
Values:
4+
RAW[4, 15] Value: "First line."
5+
FormattedValue[16, 27]
6+
Value:
7+
BinOp[5, 12]
8+
Op: Add
9+
LHS: Name[5, 7] Value: "v1"
10+
RHS: Name[10, 12] Value: "v2"
11+
Conversion: -1
12+
RAW[28, 42] Value: "\nAnother line."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Module[0, 5]
2+
BOOLEAN[0, 5] Value: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Module[0, 17]
2+
GeneratorExp[0, 17]
3+
Element:
4+
Tuple[1, 5]
5+
Values:
6+
Name[2, 3] Value: "a"
7+
ComprehensionTy[6, 16]
8+
Target: Name[10, 11] Value: "a" Store
9+
Iterator: Name[15, 16] Value: "b"

0 commit comments

Comments
 (0)