[test] Improve test method name, display test method

This commit is contained in:
Andreas Dangel
2022-04-29 12:22:44 +02:00
parent 6ec5fe1073
commit efcc583086
3 changed files with 30 additions and 1 deletions

View File

@ -240,6 +240,7 @@ public abstract class RuleTst {
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());

View File

@ -155,6 +155,7 @@ public class TestDescriptor {
+ "_"
+ getDescription()
.replaceAll("\n|\r", "_")
.replaceAll("[\\.\\(\\)]|\\s", "_");
.replaceAll("[^\\w\\d_$]", "_")
.replaceAll("\\s+", "_");
}
}

View File

@ -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();
}
}