Skip to content

Commit

Permalink
Merge pull request #3939 from adangel:improved-rule-test-support
Browse files Browse the repository at this point in the history
[test] Print Test Method Name for failed rule tests #3939
  • Loading branch information
adangel committed Apr 29, 2022
2 parents 094bb13 + efcc583 commit 915b7f8
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class PMDTestRunner extends Runner implements Filterable, Sortable {
private final RuleTestRunner ruleTests;
private final ParentRunner<?> unitTests;

private final Description description;

public PMDTestRunner(final Class<? extends SimpleAggregatorTst> klass) throws InitializationError {
this.klass = klass;
ruleTests = new RuleTestRunner(klass);
Expand All @@ -51,6 +53,8 @@ public PMDTestRunner(final Class<? extends SimpleAggregatorTst> klass) throws In
} else {
unitTests = new EmptyRunner(klass);
}

this.description = createDescription();
}

@Override
Expand All @@ -76,6 +80,10 @@ public void filter(Filter filter) throws NoTestsRemainException {

@Override
public Description getDescription() {
return description;
}

private Description createDescription() {
Description description = Description.createSuiteDescription(klass);
description.addChild(createChildrenDescriptions(ruleTests, "Rule Tests"));
if (ruleTests.hasUnitTests()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ public RuleTestRunner(Class<? extends RuleTst> testClass) throws InitializationE
protected Description describeChild(TestDescriptor testCase) {
Description description = testDescriptions.get(testCase);
if (description == null) {
description = Description.createTestDescription(getTestClass().getJavaClass(),
testCase.getRule().getName() + "::"
+ testCase.getNumberInDocument() + " "
+ testCase.getDescription().replaceAll("\n|\r", " "));
description = Description.createTestDescription(getTestClass().getJavaClass().getName(), testCase.getTestMethodName());
testDescriptions.putIfAbsent(testCase, description);
}
return description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ private void printReport(TestDescriptor test, Report report) {
+ " problem(s) found.");
System.out.println(" -> Expected messages: " + test.getExpectedMessages());
System.out.println(" -> Expected line numbers: " + test.getExpectedLineNumbers());
System.out.println("Test Method Name: " + test.getTestMethodName());
System.out.println(" @org.junit.Test public void " + test.getTestMethodName() + "() {}");
System.out.println();
TextRenderer renderer = new TextRenderer();
renderer.setWriter(new StringWriter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,14 @@ public void setUseAuxClasspath(boolean useAuxClasspath) {
public boolean isUseAuxClasspath() {
return useAuxClasspath;
}

public String getTestMethodName() {
return getRule().getName() + "_"
+ getNumberInDocument()
+ "_"
+ getDescription()
.replaceAll("\n|\r", "_")
.replaceAll("[^\\w\\d_$]", "_")
.replaceAll("\\s+", "_");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/

package net.sourceforge.pmd.testframework;

import org.junit.Assert;
import org.junit.Test;

import net.sourceforge.pmd.lang.rule.MockRule;

public class TestDescriptorTest {
@Test
public void testMethodName() {
Assert.assertEquals("MockRule_1_Name", create("Name"));
Assert.assertEquals("MockRule_1_Tests_xyz", create("Tests xyz"));
Assert.assertEquals("MockRule_1_Tests_xyz__false_positive_", create("Tests xyz (false positive)"));
Assert.assertEquals("MockRule_1_Tests_xyz__123", create("Tests xyz #123"));
}

private String create(String description) {
TestDescriptor descriptor = new TestDescriptor("foo", description, 0,
new MockRule("MockRule", "desc", "msg", "ruleset"));
descriptor.setNumberInDocument(1);
return descriptor.getTestMethodName();
}
}

0 comments on commit 915b7f8

Please sign in to comment.