Cleanup tests

This commit is contained in:
Clément Fournier
2020-12-12 23:28:51 +01:00
parent 163d7af6c2
commit 084a81265c
5 changed files with 32 additions and 60 deletions

View File

@ -4,6 +4,7 @@
package net.sourceforge.pmd.lang.apex.ast;
import static net.sourceforge.pmd.lang.ast.test.TestUtilsKt.assertPosition;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertEquals;
@ -194,13 +195,4 @@ public class ApexParserTest extends ApexParserTestBase {
}
return result;
}
// TEST HELPER
private static void assertPosition(Node node, int beginLine, int beginColumn, int endLine, int endColumn) {
assertEquals("Wrong begin line", beginLine, node.getBeginLine());
assertEquals("Wrong begin column", beginColumn, node.getBeginColumn());
assertEquals("Wrong end line", endLine, node.getEndLine());
assertEquals("Wrong end column", endColumn, node.getEndColumn());
}
}

View File

@ -16,6 +16,7 @@ import org.junit.Ignore;
import org.junit.Test;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.ast.test.TestUtilsKt;
import net.sourceforge.pmd.lang.java.JavaParsingHelper;
@Ignore("This test is Java specific even though parts of it should apply to any language implementation")
@ -26,19 +27,19 @@ public class SimpleNodeTest extends BaseParserTest {
@Test
public void testMethodDiffLines() {
List<ASTMethodDeclaration> methods = java.getNodes(ASTMethodDeclaration.class, METHOD_DIFF_LINES);
verifyNode(methods.iterator().next(), 2, 9, 4, 3);
TestUtilsKt.assertPosition(methods.iterator().next(), 2, 9, 4, 3);
}
@Test
public void testMethodSameLine() {
List<ASTMethodDeclaration> methods = java.getNodes(ASTMethodDeclaration.class, METHOD_SAME_LINE);
verifyNode(methods.iterator().next(), 2, 9, 2, 22);
TestUtilsKt.assertPosition(methods.iterator().next(), 2, 9, 2, 22);
}
@Test
public void testNoLookahead() {
List<ASTClassOrInterfaceDeclaration> uCD = java.getNodes(ASTClassOrInterfaceDeclaration.class, NO_LOOKAHEAD);
verifyNode(uCD.iterator().next(), 1, 8, 1, 21);
TestUtilsKt.assertPosition(uCD.iterator().next(), 1, 8, 1, 21);
}
@Test
@ -69,7 +70,7 @@ public class SimpleNodeTest extends BaseParserTest {
public void testColumnsOnQualifiedName() {
for (Node node : java.getNodes(ASTName.class, QUALIFIED_NAME)) {
if (node.getImage().equals("java.io.File")) {
verifyNode(node, 1, 8, 1, 20);
TestUtilsKt.assertPosition(node, 1, 8, 1, 20);
}
}
}
@ -78,10 +79,10 @@ public class SimpleNodeTest extends BaseParserTest {
public void testLineNumbersForNameSplitOverTwoLines() {
for (Node node : java.getNodes(ASTName.class, BROKEN_LINE_IN_NAME)) {
if (node.getImage().equals("java.io.File")) {
verifyNode(node, 1, 8, 2, 5);
TestUtilsKt.assertPosition(node, 1, 8, 2, 5);
}
if (node.getImage().equals("Foo")) {
verifyNode(node, 2, 15, 2, 19);
TestUtilsKt.assertPosition(node, 2, 15, 2, 19);
}
}
}
@ -256,13 +257,6 @@ public class SimpleNodeTest extends BaseParserTest {
assertTrue(nodes.get(0) instanceof ASTFieldDeclaration);
}
private void verifyNode(Node node, int beginLine, int beginCol, int endLine, int endCol) {
assertEquals("Unexpected beginning line: ", beginLine, node.getBeginLine());
assertEquals("Unexpected beginning column: ", beginCol, node.getBeginColumn());
assertEquals("Unexpected ending line:", endLine, node.getEndLine());
assertEquals("Unexpected ending column:", endCol, node.getEndColumn());
}
private static final String HAS_EXPLICIT_EXTENDS = "public class Test extends Foo {}";
private static final String NO_EXPLICIT_EXTENDS = "public class Test {}";

View File

@ -9,6 +9,7 @@ import io.kotest.matchers.equalityMatcher
import io.kotest.matchers.should
import net.sourceforge.pmd.Report
import net.sourceforge.pmd.RuleViolation
import net.sourceforge.pmd.lang.ast.Node
import kotlin.reflect.KCallable
import kotlin.reflect.jvm.isAccessible
import kotlin.test.assertEquals
@ -91,3 +92,11 @@ fun assertSuppressed(report: Report, size: Int): List<Report.SuppressedViolation
assertEquals(size, report.suppressedViolations.size, message = "Wrong number of suppressed violations!")
return report.suppressedViolations
}
/** Checks the coordinates of this node. */
fun Node.assertPosition(bline: Int, bcol: Int, eline: Int, ecol: Int) {
this::getBeginLine shouldBe bline
this::getBeginColumn shouldBe bcol
this::getEndLine shouldBe eline
this::getEndColumn shouldBe ecol
}

View File

@ -6,9 +6,8 @@ package net.sourceforge.pmd.lang.scala.ast
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.should
import net.sourceforge.pmd.lang.LanguageRegistry
import net.sourceforge.pmd.lang.ast.Node
import net.sourceforge.pmd.lang.ast.test.matchNode
import net.sourceforge.pmd.lang.ast.test.assertPosition
import net.sourceforge.pmd.lang.ast.test.shouldBe
class ScalaTreeTests : FunSpec({
@ -23,54 +22,54 @@ class Foo {
""".trim().parseScala() should matchNode<ASTSource> {
child<ASTDefnClass> {
it.assertBounds(bline = 1, bcol = 1, eline = 3, ecol = 2)
it.assertPosition(bline = 1, bcol = 1, eline = 3, ecol = 2)
it::isImplicit shouldBe false
child<ASTTypeName> {
it.assertBounds(bline = 1, bcol = 7, eline = 1, ecol = 10)
it.assertPosition(bline = 1, bcol = 7, eline = 1, ecol = 10)
it::isImplicit shouldBe false
}
child<ASTCtorPrimary> {
it.assertBounds(bline = 1, bcol = 11, eline = 1, ecol = 11) // node has zero length
it.assertPosition(bline = 1, bcol = 11, eline = 1, ecol = 11) // node has zero length
it::isImplicit shouldBe true
child<ASTNameAnonymous> {
it.assertBounds(bline = 1, bcol = 11, eline = 1, ecol = 11) // node has zero length
it.assertPosition(bline = 1, bcol = 11, eline = 1, ecol = 11) // node has zero length
it::isImplicit shouldBe true
}
}
child<ASTTemplate> {
it.assertBounds(bline = 1, bcol = 11, eline = 3, ecol = 2)
it.assertPosition(bline = 1, bcol = 11, eline = 3, ecol = 2)
it::isImplicit shouldBe false
child<ASTSelf> {
it.assertBounds(bline = 2, bcol = 2, eline = 2, ecol = 2) // node has zero length
it.assertPosition(bline = 2, bcol = 2, eline = 2, ecol = 2) // node has zero length
it::isImplicit shouldBe true
child<ASTNameAnonymous> {
it.assertBounds(bline = 2, bcol = 2, eline = 2, ecol = 2) // node has zero length
it.assertPosition(bline = 2, bcol = 2, eline = 2, ecol = 2) // node has zero length
it::isImplicit shouldBe true
}
}
child<ASTDefnVal> {
it.assertBounds(bline = 2, bcol = 2, eline = 2, ecol = 12)
it.assertPosition(bline = 2, bcol = 2, eline = 2, ecol = 12)
it::isImplicit shouldBe false
child<ASTPatVar> {
it.assertBounds(bline = 2, bcol = 6, eline = 2, ecol = 7)
it.assertPosition(bline = 2, bcol = 6, eline = 2, ecol = 7)
it::isImplicit shouldBe false
child<ASTTermName> {
it.assertBounds(bline = 2, bcol = 6, eline = 2, ecol = 7)
it.assertPosition(bline = 2, bcol = 6, eline = 2, ecol = 7)
it::isImplicit shouldBe false
}
}
child<ASTLitString> {
it.assertBounds(bline = 2, bcol = 10, eline = 2, ecol = 12)
it.assertPosition(bline = 2, bcol = 10, eline = 2, ecol = 12)
}
}
}
@ -80,10 +79,3 @@ class Foo {
})
fun String.parseScala(): ASTSource = ScalaParsingHelper.DEFAULT.parse(this)
fun Node.assertBounds(bline: Int, bcol: Int, eline: Int, ecol: Int) {
this::getBeginLine shouldBe bline
this::getBeginColumn shouldBe bcol
this::getEndLine shouldBe eline
this::getEndColumn shouldBe ecol
}

View File

@ -4,11 +4,11 @@
package net.sourceforge.pmd.lang.xml.ast;
import static net.sourceforge.pmd.lang.ast.test.TestUtilsKt.assertPosition;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.junit.Assert;
import org.junit.Test;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.ast.test.BaseParsingHelper;
import net.sourceforge.pmd.lang.ast.test.BaseTreeDumpTest;
import net.sourceforge.pmd.lang.ast.test.CoordinatesPrinter;
@ -37,22 +37,7 @@ public class XmlCoordinatesTest extends BaseTreeDumpTest {
@Test
public void testAutoclosingElementLength() {
final String xml = "<elementName att1='foo' att2='bar' att3='other' />";
assertLineNumbers(XmlParsingHelper.XML.parse(xml), 1, 1, 1, xml.length());
assertPosition(XmlParsingHelper.XML.parse(xml), 1, 1, 1, xml.length());
}
/**
* Assert the line numbers of a node.
*
* @param node the node
* @param beginLine the begin line
* @param beginColumn the begin column
* @param endLine the end line
* @param endColumn the end column
*/
private void assertLineNumbers(Node node, int beginLine, int beginColumn, int endLine, int endColumn) {
Assert.assertEquals("begin line wrong", beginLine, node.getBeginLine());
Assert.assertEquals("begin column wrong", beginColumn, node.getBeginColumn());
Assert.assertEquals("end line wrong", endLine, node.getEndLine());
Assert.assertEquals("end column wrong", endColumn, node.getEndColumn());
}
}