Skip to content

Commit 248cf84

Browse files
KvanTTTparrt
authored andcommitted
Restore \n for all input in runtime tests, add extra LexerExec tests (LineSeparatorLf, LineSeparatorCrLf)
1 parent 1b144fa commit 248cf84

File tree

5 files changed

+52
-54
lines changed

5 files changed

+52
-54
lines changed

.gitattributes

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
# This rule applies to all files which don't match another line below
2-
* text=auto
3-
LineSeparator_LF.txt eol=lf
4-
LineSeparator_CRLF.txt eol=crlf
2+
* text=auto

runtime-testsuite/resources/org/antlr/v4/test/runtime/descriptors/LexerExec/LineSeparator_CRLF.txt

-20
This file was deleted.

runtime-testsuite/resources/org/antlr/v4/test/runtime/descriptors/LexerExec/LineSeparator_LF.txt

-20
This file was deleted.

runtime-testsuite/test/org/antlr/v4/test/runtime/BaseRuntimeTest.java

+10-11
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public abstract class BaseRuntimeTest {
4747
final static Set<String> sections = new HashSet<>(Arrays.asList(
4848
"notes", "type", "grammar", "slaveGrammar", "start", "input", "output", "errors", "flags", "skip"
4949
));
50-
final static Pattern linePattern = Pattern.compile("([^\\r\\n]*)(\r?\n)");
5150

5251
@BeforeClass
5352
public static void startHeartbeatToAvoidTimeout() {
@@ -367,6 +366,12 @@ public static RuntimeTestDescriptor[] getRuntimeTestDescriptors(String group, St
367366
System.err.println("Can't read descriptor file "+fname);
368367
}
369368
}
369+
370+
if (group.equals("LexerExec")) {
371+
descriptors.add(ExtraTests.getLineSeparatorLfTest(targetName));
372+
descriptors.add(ExtraTests.getLineSeparatorCrLfTest(targetName));
373+
}
374+
370375
return descriptors.toArray(new RuntimeTestDescriptor[0]);
371376
}
372377

@@ -424,13 +429,9 @@ public static UniversalRuntimeTestDescriptor readDescriptor(String dtext)
424429
StringBuilder currentValue = new StringBuilder();
425430

426431
List<Pair<String, String>> pairs = new ArrayList<>();
432+
String[] lines = dtext.split("\r?\n");
427433

428-
Matcher matcher = linePattern.matcher(dtext);
429-
boolean preserveSeparator = false;
430-
int lastEnd = 0;
431-
while (matcher.find()) {
432-
String line = matcher.group(1);
433-
lastEnd = matcher.end();
434+
for (String line : lines) {
434435
boolean newSection = false;
435436
String sectionName = null;
436437
if (line.startsWith("[") && line.length() > 2) {
@@ -439,19 +440,17 @@ public static UniversalRuntimeTestDescriptor readDescriptor(String dtext)
439440
}
440441

441442
if (newSection) {
442-
if (currentField!=null) {
443+
if (currentField != null) {
443444
pairs.add(new Pair<>(currentField, currentValue.toString()));
444445
}
445-
preserveSeparator = sectionName.equals("input");
446446
currentField = sectionName;
447447
currentValue.setLength(0);
448448
}
449449
else {
450450
currentValue.append(line);
451-
currentValue.append(preserveSeparator ? matcher.group(2) : "\n");
451+
currentValue.append("\n");
452452
}
453453
}
454-
currentValue.append(dtext.substring(lastEnd));
455454
pairs.add(new Pair<>(currentField, currentValue.toString()));
456455

457456
UniversalRuntimeTestDescriptor d = new UniversalRuntimeTestDescriptor();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.antlr.v4.test.runtime;
2+
3+
public class ExtraTests {
4+
static RuntimeTestDescriptor getLineSeparatorLfTest(String targetName) {
5+
UniversalRuntimeTestDescriptor result = new UniversalRuntimeTestDescriptor();
6+
result.name = "LineSeparatorLf";
7+
result.targetName = targetName;
8+
result.testType = "Lexer";
9+
result.grammar = "lexer grammar L;\n" +
10+
"T: ~'\\n'+;\n" +
11+
"SEPARATOR: '\\n';";
12+
result.grammarName = "L";
13+
result.input = "1\n2\n3";
14+
result.output = "[@0,0:0='1',<1>,1:0]\n" +
15+
"[@1,1:1='\\n',<2>,1:1]\n" +
16+
"[@2,2:2='2',<1>,2:0]\n" +
17+
"[@3,3:3='\\n',<2>,2:1]\n" +
18+
"[@4,4:4='3',<1>,3:0]\n" +
19+
"[@5,5:4='<EOF>',<-1>,3:1]\n";
20+
return result;
21+
}
22+
23+
static RuntimeTestDescriptor getLineSeparatorCrLfTest(String targetName) {
24+
UniversalRuntimeTestDescriptor result = new UniversalRuntimeTestDescriptor();
25+
result.name = "LineSeparatorCrLf";
26+
result.targetName = targetName;
27+
result.testType = "Lexer";
28+
result.grammar = "lexer grammar L;\n" +
29+
"T: ~'\\r'+;\n" +
30+
"SEPARATOR: '\\r\\n';";
31+
result.grammarName = "L";
32+
result.input = "1\r\n2\r\n3";
33+
result.output = "[@0,0:0='1',<1>,1:0]\n" +
34+
"[@1,1:2='\\r\\n',<2>,1:1]\n" +
35+
"[@2,3:3='2',<1>,2:0]\n" +
36+
"[@3,4:5='\\r\\n',<2>,2:1]\n" +
37+
"[@4,6:6='3',<1>,3:0]\n" +
38+
"[@5,7:6='<EOF>',<-1>,3:1]\n";
39+
return result;
40+
}
41+
}

0 commit comments

Comments
 (0)