Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve line separators for input runtime tests data #3483

Merged
merged 3 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# This rule applies to all files which don't match another line below
* text=auto
LineSeparator_LF.txt eol=lf
LineSeparator_CRLF.txt eol=crlf
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ X : 'x';
[output]
[@0,0:0='x',<2>,1:0]
[@1,1:1='\n',<1>,1:1]
[@2,2:1='<EOF>',<-1>,2:0]
[@2,2:1='<EOF>',<-1>,2:0]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[type]
Lexer

[grammar]
lexer grammar L;
T: ~'\r'+;
SEPARATOR: '\r\n';

[input]
"""1
2
3"""

[output]
[@0,0:0='1',<1>,1:0]
[@1,1:2='\r\n',<2>,1:1]
[@2,3:3='2',<1>,2:0]
[@3,4:5='\r\n',<2>,2:1]
[@4,6:6='3',<1>,3:0]
[@5,7:6='<EOF>',<-1>,3:1]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[type]
Lexer

[grammar]
lexer grammar L;
T: ~'\n'+;
SEPARATOR: '\n';

[input]
"""1
2
3"""

[output]
[@0,0:0='1',<1>,1:0]
[@1,1:1='\n',<2>,1:1]
[@2,2:2='2',<1>,2:0]
[@3,3:3='\n',<2>,2:1]
[@4,4:4='3',<1>,3:0]
[@5,5:4='<EOF>',<-1>,3:1]
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Parser
grammar G;

root
: {0==0}? continue+ {<writeln("$text")>}
: {0==0}? continue+ {<write("$text")>}
;

continue returns [int return]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static junit.framework.TestCase.fail;
import static junit.framework.TestCase.failNotEquals;
Expand All @@ -42,18 +44,10 @@
* @since 4.6.
*/
public abstract class BaseRuntimeTest {

public final static String[] Targets = {
"Cpp",
"CSharp",
"Dart",
"Go",
"Java",
"Node",
"PHP",
"Python2", "Python3",
"Swift"
};
final static Set<String> sections = new HashSet<>(Arrays.asList(
"notes", "type", "grammar", "slaveGrammar", "start", "input", "output", "errors", "flags", "skip"
));
final static Pattern linePattern = Pattern.compile("([^\\r\\n]*)(\r?\n)");
parrt marked this conversation as resolved.
Show resolved Hide resolved

@BeforeClass
public static void startHeartbeatToAvoidTimeout() {
Expand Down Expand Up @@ -426,35 +420,39 @@ public static RuntimeTestDescriptor[] getRuntimeTestDescriptors(String group, St
public static UniversalRuntimeTestDescriptor readDescriptor(String dtext)
throws RuntimeException
{
String[] fileSections = {
"notes","type","grammar","slaveGrammar","start","input","output","errors",
"flags","skip"};
Set<String> sections = new HashSet<>(Arrays.asList(fileSections));
String currentField = null;
String currentValue = null;
StringBuilder currentValue = new StringBuilder();

List<Pair<String, String>> pairs = new ArrayList<>();
String[] lines = dtext.split("\r?\n");
for (String line : lines) {
if ( line.startsWith("[") && line.length()>2 &&
sections.contains(line.substring(1, line.length() - 1)) ) {
if ( currentField!=null ) {
pairs.add(new Pair<>(currentField,currentValue));

Matcher matcher = linePattern.matcher(dtext);
parrt marked this conversation as resolved.
Show resolved Hide resolved
boolean preserveSeparator = false;
int lastEnd = 0;
while (matcher.find()) {
String line = matcher.group(1);
lastEnd = matcher.end();
boolean newSection = false;
String sectionName = null;
if (line.startsWith("[") && line.length() > 2) {
sectionName = line.substring(1, line.length() - 1);
newSection = sections.contains(sectionName);
}

if (newSection) {
if (currentField!=null) {
pairs.add(new Pair<>(currentField, currentValue.toString()));
}
currentField = line.substring(1, line.length() - 1);
currentValue = null;
preserveSeparator = sectionName.equals("input");
parrt marked this conversation as resolved.
Show resolved Hide resolved
currentField = sectionName;
currentValue.setLength(0);
}
else {
if ( currentValue==null ) {
currentValue = "";
}
currentValue += line + "\n";
currentValue.append(line);
currentValue.append(preserveSeparator ? matcher.group(2) : "\n");
}
}
// save last section
pairs.add(new Pair<>(currentField,currentValue));

// System.out.println(pairs);
currentValue.append(dtext.substring(lastEnd));
pairs.add(new Pair<>(currentField, currentValue.toString()));

UniversalRuntimeTestDescriptor d = new UniversalRuntimeTestDescriptor();
for (Pair<String,String> p : pairs) {
Expand Down