Change the test asserts so that we can see all the values.

Change the unit test asserts so that we can see all the values in the
event of a failure.  When debugging an error, it's useful to see both
the stdout and stderr for the failure.  Previously we would only see
one or the other (whichever one failed the assert).

This adds a helper function BaseRuntimeTest.assertCorrectOutput.

This also removes SpecialRuntimeTestAssert, which has not been used for
2 years.
This commit is contained in:
Ewan Mellor 2018-11-12 13:49:23 -08:00
parent 3699ea5412
commit e54607d2ae
No known key found for this signature in database
GPG Key ID: 7CE1C6BC9EC8645D
3 changed files with 67 additions and 33 deletions

View File

@ -31,6 +31,8 @@ import java.util.Collections;
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
@ -161,14 +163,7 @@ public abstract class BaseRuntimeTest {
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 {
@ -202,16 +197,7 @@ public abstract class BaseRuntimeTest {
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 */
@ -313,4 +299,66 @@ public abstract class BaseRuntimeTest {
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 +
">.");
}
}
}

View File

@ -1,14 +0,0 @@
/*
* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
package org.antlr.v4.test.runtime;
/** This interface acts like a tag on a Base*Test class that wants
* to use its own assertEquals() instead of jUnit's.
*/
public interface SpecialRuntimeTestAssert {
void assertEqualStrings(String expected, String actual);
}

View File

@ -56,7 +56,7 @@ import static org.junit.Assert.assertFalse;
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");