Skip to content

Commit

Permalink
Merge pull request #2405 from ewanmellor/testability-and-build-improv…
Browse files Browse the repository at this point in the history
…ements

Testability and build improvements
  • Loading branch information
parrt committed Nov 16, 2018
2 parents 9611faa + 443a1d2 commit f08de81
Show file tree
Hide file tree
Showing 16 changed files with 124 additions and 67 deletions.
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,9 @@ before_install:
- f="./.travis/before-install-$TRAVIS_OS_NAME-$TARGET.sh"; ! [ -x "$f" ] || "$f"

script:
- cd runtime-testsuite; travis_wait 40 ../.travis/run-tests-$TARGET.sh
- |
cd runtime-testsuite;
travis_wait 40 ../.travis/run-tests-$TARGET.sh;
rc=$?;
cat target/surefire-reports/*.dumpstream || true;
exit $rc
2 changes: 2 additions & 0 deletions .travis/run-tests-cpp.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

set -euo pipefail

if [ $GROUP == "LEXER" ]; then
mvn -q -Dgroups="org.antlr.v4.test.runtime.category.LexerTests" -Dtest=cpp.* test
elif [ $GROUP == "PARSER" ]; then
Expand Down
2 changes: 2 additions & 0 deletions .travis/run-tests-csharp.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/bin/bash

set -euo pipefail

mvn -q -Dparallel=methods -DthreadCount=4 -Dtest=csharp.* test
4 changes: 3 additions & 1 deletion .travis/run-tests-dotnet.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#!/bin/bash

set -euo pipefail

# we need to build the runtime before test run, since we used "--no-dependencies"
# when we call dotnet cli for restore and build, in order to speed up

dotnet restore ../runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Antlr4.Runtime.dotnet.csproj
dotnet build -c Release ../runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Antlr4.Runtime.dotnet.csproj
dotnet build -c Release -f netstandard1.3 ../runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Antlr4.Runtime.dotnet.csproj

# call test

Expand Down
2 changes: 2 additions & 0 deletions .travis/run-tests-go.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/bin/bash

set -euo pipefail

mvn -q -Dparallel=methods -DthreadCount=4 -Dtest=go.* test
2 changes: 2 additions & 0 deletions .travis/run-tests-java.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

set -euo pipefail

mvn -q -Dparallel=methods -DthreadCount=4 -Dtest=java.* test
cd ../tool-testsuite
mvn test
2 changes: 2 additions & 0 deletions .travis/run-tests-javascript.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/bin/bash

set -euo pipefail

mvn -q -Dparallel=methods -DthreadCount=4 -Dtest=node.* test
2 changes: 2 additions & 0 deletions .travis/run-tests-python2.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/bin/bash

set -euo pipefail

mvn -q -Dparallel=methods -DthreadCount=4 -Dtest=python2.* test
2 changes: 2 additions & 0 deletions .travis/run-tests-python3.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/bin/bash

set -euo pipefail

mvn -q -Dparallel=methods -DthreadCount=4 -Dtest=python3.* test
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@
<filesets>
<fileset>
<directory>runtime/Swift/.build</directory>
</fileset>
<fileset>
<directory>runtime/Swift/Tests/Antlr4Tests/gen</directory>
</fileset>
</filesets>
Expand Down
5 changes: 5 additions & 0 deletions runtime-testsuite/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@
</resource>
<resource>
<directory>../runtime</directory>
<excludes>
<exclude>**/.build/**</exclude>
<exclude>**/target/**</exclude>
<exclude>Swift/*.xcodeproj/**</exclude>
</excludes>
</resource>
</resources>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.List;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.fail;
import static junit.framework.TestCase.failNotEquals;
import static org.junit.Assume.assumeFalse;

/** This class represents a single runtime test. It pulls data from
Expand Down Expand Up @@ -161,14 +163,7 @@ public void testParser(RuntimeTestDescriptor descriptor) throws Exception {
descriptor.getInput(),
descriptor.showDiagnosticErrors()
);
if ( delegate instanceof SpecialRuntimeTestAssert ) {
((SpecialRuntimeTestAssert)delegate).assertEqualStrings(descriptor.getErrors(), delegate.getParseErrors());
((SpecialRuntimeTestAssert)delegate).assertEqualStrings(descriptor.getOutput(), found);
}
else {
assertEquals(descriptor.getErrors(), delegate.getParseErrors());
assertEquals(descriptor.getOutput(), found);
}
assertCorrectOutput(descriptor, delegate, found);
}

public void testLexer(RuntimeTestDescriptor descriptor) throws Exception {
Expand Down Expand Up @@ -202,16 +197,7 @@ public void testLexer(RuntimeTestDescriptor descriptor) throws Exception {
grammar = grammarST.render();

String found = delegate.execLexer(grammarName+".g4", grammar, grammarName, descriptor.getInput(), descriptor.showDFA());
if ( delegate instanceof SpecialRuntimeTestAssert ) {
((SpecialRuntimeTestAssert)delegate).assertEqualStrings(descriptor.getOutput(), found);
((SpecialRuntimeTestAssert)delegate).assertEqualStrings(descriptor.getANTLRToolErrors(), delegate.getANTLRToolErrors());
((SpecialRuntimeTestAssert)delegate).assertEqualStrings(descriptor.getErrors(), delegate.getParseErrors());
}
else {
assertEquals(descriptor.getOutput(), found);
assertEquals(descriptor.getANTLRToolErrors(), delegate.getANTLRToolErrors());
assertEquals(descriptor.getErrors(), delegate.getParseErrors());
}
assertCorrectOutput(descriptor, delegate, found);
}

/** Write a grammar to tmpdir and run antlr */
Expand Down Expand Up @@ -313,4 +299,66 @@ public static void writeFile(String dir, String fileName, String content) {
ioe.printStackTrace(System.err);
}
}


protected static void assertCorrectOutput(RuntimeTestDescriptor descriptor, RuntimeTestSupport delegate, String actualOutput) {
String actualParseErrors = delegate.getParseErrors();
String actualToolErrors = delegate.getANTLRToolErrors();
String expectedOutput = descriptor.getOutput();
String expectedParseErrors = descriptor.getErrors();
String expectedToolErrors = descriptor.getANTLRToolErrors();

if (actualOutput == null) {
actualOutput = "";
}
if (actualParseErrors == null) {
actualParseErrors = "";
}
if (actualToolErrors == null) {
actualToolErrors = "";
}
if (expectedOutput == null) {
expectedOutput = "";
}
if (expectedParseErrors == null) {
expectedParseErrors = "";
}
if (expectedToolErrors == null) {
expectedToolErrors = "";
}

if (actualOutput.equals(expectedOutput) &&
actualParseErrors.equals(expectedParseErrors) &&
actualToolErrors.equals(expectedToolErrors)) {
return;
}

if (actualOutput.equals(expectedOutput)) {
if (actualParseErrors.equals(expectedParseErrors)) {
failNotEquals("[" + descriptor.getTarget() + ":" + descriptor.getTestName() + "] " +
"Parse output and parse errors are as expected, but tool errors are incorrect",
expectedToolErrors, actualToolErrors);
}
else {
fail("[" + descriptor.getTarget() + ":" + descriptor.getTestName() + "] " +
"Parse output is as expected, but errors are not: " +
"expectedParseErrors:<" + expectedParseErrors +
">; actualParseErrors:<" + actualParseErrors +
">; expectedToolErrors:<" + expectedToolErrors +
">; actualToolErrors:<" + actualToolErrors +
">.");
}
}
else {
fail("[" + descriptor.getTarget() + ":" + descriptor.getTestName() + "] " +
"Parse output is incorrect: " +
"expectedOutput:<" + expectedOutput +
">; actualOutput:<" + actualOutput +
">; expectedParseErrors:<" + expectedParseErrors +
">; actualParseErrors:<" + actualParseErrors +
">; expectedToolErrors:<" + expectedToolErrors +
">; actualToolErrors:<" + actualToolErrors +
">.");
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class BaseCSharpTest implements RuntimeTestSupport /*, SpecialRuntimeTestAssert*/ {
public class BaseCSharpTest implements RuntimeTestSupport {
public static final String newline = System.getProperty("line.separator");
public static final String pathSep = System.getProperty("path.separator");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ private void writeParserTestFile(String parserName,
" }\n" +
"}\n" +
"\n" +
"do {\n" +
"let args = CommandLine.arguments\n" +
"let input = try ANTLRFileStream(args[1])\n" +
"let lex = <lexerName>(input)\n" +
Expand All @@ -289,12 +288,7 @@ private void writeParserTestFile(String parserName,
"<profile>\n" +
"let tree = try parser.<parserStartRuleName>()\n" +
"<if(profile)>print(profiler.getDecisionInfo().description)<endif>\n" +
"try ParseTreeWalker.DEFAULT.walk(TreeShapeListener(), tree)\n" +
"}catch ANTLRException.recognition(let e ) {\n" +
" print(\"error occur\\(e)\")\n" +
"}catch {\n" +
" print(\"error occur\")\n" +
"}\n"
"try ParseTreeWalker.DEFAULT.walk(TreeShapeListener(), tree)\n"
);
ST createParserST = new ST(" let parser = try <parserName>(tokens)\n");
if (debug) {
Expand Down Expand Up @@ -329,13 +323,7 @@ private void writeLexerTestFile(String lexerName, boolean showDFA) {
"let lex = <lexerName>(input)\n" +
"let tokens = CommonTokenStream(lex)\n" +

"do {\n" +
" try tokens.fill()\n" +
"} catch ANTLRException.recognition(let e ) {\n" +
" print(\"error occur\\(e)\")\n" +
"} catch {\n" +
" print(\"error occur\")\n" +
"}\n" +
"try tokens.fill()\n" +

"for t in tokens.getTokens() {\n" +
" print(t)\n" +
Expand Down
43 changes: 25 additions & 18 deletions runtime/Swift/boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import argparse
import fnmatch
import os.path
import subprocess
import sys
import time
from subprocess import call
from subprocess import check_call


# ANTLR Version, here we only care about major version.
Expand Down Expand Up @@ -83,17 +85,17 @@ def gen_parser(grammar, a4):
antlr_complains("Cannot find java. Check your JAVA_HOME setting.")
return

call([java, "-jar", a4,
"-Dlanguage=Swift", grammar, "-visitor",
"-o", grammar_folder + "/gen"])
check_call([java, "-jar", a4,
"-Dlanguage=Swift", grammar, "-visitor",
"-o", grammar_folder + "/gen"])


def swift_test():
"""
Run unit tests.
"""
generate_parser()
call(["swift", "test"])
check_call(["swift", "test"])


def get_argument_parser():
Expand Down Expand Up @@ -141,10 +143,10 @@ def generate_spm_module(in_folder=TMP_FOLDER):
shutil.copy("Package.swift", tmp_antlr_folder)

os.chdir(tmp_antlr_folder)
call(["git", "init"])
call(["git", "add", "*"])
call(["git", "commit", "-m", "Initial commit."])
call(["git", "tag", "{}.0.0".format(MAJOR_VERSION)])
check_call(["git", "init"])
check_call(["git", "add", "*"])
check_call(["git", "commit", "-m", "Initial commit."])
check_call(["git", "tag", "{}.0.0".format(MAJOR_VERSION)])

antlr_says("Created local repository.")
antlr_says("(swift-tools-version:3.0) "
Expand All @@ -163,7 +165,7 @@ def generate_xcodeproj():
:return:
"""
generate_parser()
call(["swift", "package", "generate-xcodeproj"])
check_call(["swift", "package", "generate-xcodeproj"])


def generate_parser():
Expand All @@ -186,11 +188,16 @@ def antlr_complains(msg):
if __name__ == "__main__":
parser = get_argument_parser()
args = parser.parse_args()
if args.gen_spm_module:
generate_spm_module()
elif args.gen_xcodeproj:
generate_xcodeproj()
elif args.test:
swift_test()
else:
parser.print_help()
try:
if args.gen_spm_module:
generate_spm_module()
elif args.gen_xcodeproj:
generate_xcodeproj()
elif args.test:
swift_test()
else:
parser.print_help()
except subprocess.CalledProcessError as err:
print >>sys.stderr, ("Error: command '%s' exited with status %d" %
(' '.join(err.cmd), err.returncode))
sys.exit(err.returncode)

0 comments on commit f08de81

Please sign in to comment.