Normalize captures in tree dump

This commit is contained in:
Clément Fournier
2020-08-23 20:28:09 +02:00
parent fd3de437b3
commit d5445d7ea5
4 changed files with 255 additions and 230 deletions

View File

@ -14,6 +14,9 @@ import net.sourceforge.pmd.lang.ast.test.BaseParsingHelper;
import net.sourceforge.pmd.lang.ast.test.BaseTreeDumpTest;
import net.sourceforge.pmd.lang.ast.test.RelevantAttributePrinter;
import net.sourceforge.pmd.lang.java.JavaParsingHelper;
import net.sourceforge.pmd.lang.java.ast.ASTAssignableExpr.ASTNamedReferenceExpr;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.lang.java.ast.InvocationNode;
import net.sourceforge.pmd.lang.java.ast.TypeNode;
import net.sourceforge.pmd.lang.rule.xpath.Attribute;
@ -27,7 +30,7 @@ public class TypesTreeDumpTest extends BaseTreeDumpTest {
}
@Override
public BaseParsingHelper<?, ?> getParser() {
public @NonNull BaseParsingHelper<?, ?> getParser() {
return JavaParsingHelper.WITH_PROCESSING.withResourceContext(getClass());
}
@ -36,6 +39,12 @@ public class TypesTreeDumpTest extends BaseTreeDumpTest {
doTest("IteratorBasedNStream");
}
@Override
protected @NonNull String normalize(@NonNull String str) {
return super.normalize(str)
// capture IDs are unstable from run to run
.replaceAll("capture#\\d+", "capture#...");
}
/**
* Only prints the type of type nodes
@ -47,6 +56,21 @@ public class TypesTreeDumpTest extends BaseTreeDumpTest {
if (node instanceof TypeNode) {
result.add(new AttributeInfo("TypeMirror", ((TypeNode) node).getTypeMirror().toString()));
}
if (node instanceof InvocationNode) {
InvocationNode invoc = (InvocationNode) node;
result.add(new AttributeInfo("MethodName", invoc.getMethodName()));
result.add(new AttributeInfo("VarargsCall", invoc.getOverloadSelectionInfo().isVarargsCall()));
result.add(new AttributeInfo("Unchecked", invoc.getOverloadSelectionInfo().needsUncheckedConversion()));
result.add(new AttributeInfo("Failed", invoc.getOverloadSelectionInfo().isFailed()));
result.add(new AttributeInfo("Function", TypePrettyPrint.prettyPrint(invoc.getMethodType(), true)));
}
if (node instanceof ASTNamedReferenceExpr) {
result.add(new AttributeInfo("Name", ((ASTNamedReferenceExpr) node).getName()));
}
if (node instanceof ASTVariableDeclaratorId) {
result.add(new AttributeInfo("Name", ((ASTVariableDeclaratorId) node).getName()));
}
}
@Override

View File

@ -38,7 +38,7 @@ open class RelevantAttributePrinter : BaseNodeAttributePrinter() {
*/
open class BaseNodeAttributePrinter : TextTreeRenderer(true, -1) {
data class AttributeInfo(val name: String, val value: String?)
data class AttributeInfo(val name: String, val value: Any?)
protected open fun ignoreAttribute(node: Node, attribute: Attribute): Boolean = true

View File

@ -67,6 +67,12 @@ abstract class BaseTextComparisonTest {
return sourceText
}
protected open fun String.normalize() = replace(
// \R on java 8+
regex = Regex("\\u000D\\u000A|[\\u000A\\u000B\\u000C\\u000D\\u0085\\u2028\\u2029]"),
replacement = "\n"
)
// Outputting a path makes for better error messages
private val srcTestResources = let {
// this is set from maven surefire
@ -90,11 +96,6 @@ abstract class BaseTextComparisonTest {
companion object {
const val ExpectedExt = ".txt"
fun String.normalize() = replace(
// \R on java 8+
regex = Regex("\\u000D\\u000A|[\\u000A\\u000B\\u000C\\u000D\\u0085\\u2028\\u2029]"),
replacement = "\n"
)
}
}