Skip to content

Commit

Permalink
Pass testSourceUri to dynamicTest and to dynamicContainer for conveni…
Browse files Browse the repository at this point in the history
…ent navigation to tests data in IntelliJ

Signed-off-by: Ivan Kochurkin <[email protected]>
  • Loading branch information
KvanTTT committed Jun 20, 2022
1 parent 59dccba commit 17f9e9d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@

package org.antlr.v4.test.runtime;

import java.net.URI;
import java.nio.file.Paths;
import java.util.*;

public class CustomDescriptors {
public final static HashMap<String, RuntimeTestDescriptor[]> descriptors;
private final static URI uri;

static {
uri = Paths.get(RuntimeTestUtils.runtimeTestsuitePath.toString(),
"test", "org", "antlr", "v4", "test", "runtime", "CustomDescriptors.java").toUri();

descriptors = new HashMap<>();
descriptors.put("LexerExec",
new RuntimeTestDescriptor[]{
Expand Down Expand Up @@ -41,7 +47,7 @@ private static RuntimeTestDescriptor getLineSeparatorLfDescriptor() {
"@members {<setOutStream()>}\n" +
"T: ~'\\n'+;\n" +
"SEPARATOR: '\\n';",
null, false, false, null);
null, false, false, null, uri);
}

private static RuntimeTestDescriptor getLineSeparatorCrLfDescriptor() {
Expand All @@ -63,7 +69,7 @@ private static RuntimeTestDescriptor getLineSeparatorCrLfDescriptor() {
"@members {<setOutStream()>}\n" +
"T: ~'\\r'+;\n" +
"SEPARATOR: '\\r\\n';",
null, false, false, null);
null, false, false, null, uri);
}

private static RuntimeTestDescriptor getLargeLexerDescriptor() {
Expand Down Expand Up @@ -91,7 +97,7 @@ private static RuntimeTestDescriptor getLargeLexerDescriptor() {
"",
grammarName,
grammar.toString(),
null, false, false, null);
null, false, false, null, uri);
}

private static RuntimeTestDescriptor getAtnStatesSizeMoreThan65535Descriptor() {
Expand Down Expand Up @@ -142,6 +148,6 @@ private static RuntimeTestDescriptor getAtnStatesSizeMoreThan65535Descriptor() {
grammarName,
grammar.toString(),
null, false, false,
new String[] {"CSharp", "Python2", "Python3", "Go", "PHP", "Swift", "JavaScript", "Dart"});
new String[] {"CSharp", "Python2", "Python3", "Go", "PHP", "Swift", "JavaScript", "Dart"}, uri);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.antlr.v4.runtime.misc.Pair;

import java.net.URI;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -52,11 +53,14 @@ public class RuntimeTestDescriptor {

public final String[] skipTargets;

public final URI uri;

public RuntimeTestDescriptor(GrammarType testType, String name, String notes,
String input, String output, String errors,
String startRule,
String grammarName, String grammar, List<Pair<String, String>> slaveGrammars,
boolean showDFA, boolean showDiagnosticErrors, String[] skipTargets) {
boolean showDFA, boolean showDiagnosticErrors, String[] skipTargets,
URI uri) {
this.testType = testType;
this.name = name;
this.notes = notes;
Expand All @@ -70,6 +74,7 @@ public RuntimeTestDescriptor(GrammarType testType, String name, String notes,
this.showDFA = showDFA;
this.showDiagnosticErrors = showDiagnosticErrors;
this.skipTargets = skipTargets != null ? skipTargets : new String[0];
this.uri = uri;
}

/** Return true if this test should be ignored for the indicated target */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.antlr.v4.runtime.misc.Pair;

import java.net.URI;
import java.util.*;

public class RuntimeTestDescriptorParser {
Expand Down Expand Up @@ -62,7 +63,7 @@ public class RuntimeTestDescriptorParser {
a : b {<writeln("\"S.a\"")>};
b : B;
*/
public static RuntimeTestDescriptor parse(String name, String text) throws RuntimeException {
public static RuntimeTestDescriptor parse(String name, String text, URI uri) throws RuntimeException {
String currentField = null;
StringBuilder currentValue = new StringBuilder();

Expand Down Expand Up @@ -162,7 +163,7 @@ else if ( value.indexOf('\n')>=0 ) {
}
}
return new RuntimeTestDescriptor(testType, name, notes, input, output, errors, startRule, grammarName, grammar,
slaveGrammars, showDFA, showDiagnosticErrors, skipTargets);
slaveGrammars, showDFA, showDiagnosticErrors, skipTargets, uri);
}

/** Get A, B, or C from:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -71,7 +72,7 @@ public abstract class RuntimeTests {
} catch (IOException e) {
throw new RuntimeException(e);
}
descriptors.add(RuntimeTestDescriptorParser.parse(name, text));
descriptors.add(RuntimeTestDescriptorParser.parse(name, text, descriptorFile.toURI()));
}

testDescriptors.put(groupName, descriptors.toArray(new RuntimeTestDescriptor[0]));
Expand All @@ -96,17 +97,19 @@ public List<DynamicNode> runtimeTests() {
ArrayList<DynamicNode> descriptorTests = new ArrayList<>();
RuntimeTestDescriptor[] descriptors = testDescriptors.get(group);
for (RuntimeTestDescriptor descriptor : descriptors) {
descriptorTests.add(dynamicTest(descriptor.name, () -> {
descriptorTests.add(dynamicTest(descriptor.name, descriptor.uri, () -> {
try (RuntimeRunner runner = createRuntimeRunner()) {
String errorMessage = test(descriptor, runner);
if (errorMessage != null) {
runner.setSaveTestDir(true);
fail(descriptor.name + "; " + errorMessage + "\nDirectory: " + runner.getTempDirPath());
fail(descriptor.name + "; " + errorMessage + "\nTest directory: " + runner.getTempDirPath());
}
}
}));
}
result.add(dynamicContainer(group, descriptorTests));

Path descriptorGroupPath = Paths.get(RuntimeTestUtils.resourcePath.toString(), "descriptors", group);
result.add(dynamicContainer(group, descriptorGroupPath.toUri(), Arrays.stream(descriptorTests.toArray(new DynamicNode[0]))));
}

return result;
Expand Down

0 comments on commit 17f9e9d

Please sign in to comment.