Skip to content

Commit

Permalink
Some more regexes to deal with special JUnit 5 test patterns such as …
Browse files Browse the repository at this point in the history
…for parameterized tests
  • Loading branch information
august782 committed May 24, 2024
1 parent fd450b9 commit 2085249
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class Utils {
private static final Pattern junit5BasicPattern = Pattern.compile(junit5BasicRegex);
private static final String junit5NestedRegex = "\\[nested-class:([\\w.]+)\\]";
private static final Pattern junit5NestedPattern = Pattern.compile(junit5NestedRegex);
private static final String junit5ParameterizedRegex = "\\[class:([\\w.]+).*\\[test-template:([\\w().]+).*\\[test-template-invocation:#([\\w.]+)";
private static final Pattern junit5ParameterizedPattern = Pattern.compile(junit5ParameterizedRegex);
private static final String junit4Regex = "\\[test:(\\w+)\\(([\\w.]+)\\)";
private static final Pattern junit4Pattern = Pattern.compile(junit4Regex);

Expand All @@ -18,6 +20,10 @@ public class Utils {
* uniqueId: [engine:junit-jupiter]/[class:com.luojl.demo.JUnit5DemoTest]/[method:TestC()]
* full qualified name: com.luojl.demo.JUnit5DemoTest#TestC()
*
* For JUnit 5 parameterized test:
* uniqueId: [engine:junit-jupiter]/[class:com.luojl.demo.JUnit5DemoTest]/[method:TestC()][\[1\]]
* full qualified name: com.luojl.demo.JUnit5DemoTest#TestC()[1]
*
* For JUnit 5 nested test:
* uniqueId: [engine:junit-jupiter]/[class:com.luojl.demo.InheritedTest]/[nested-class:NestedTest]/[method:NestedTestB()]
* fully qualified name: com.luojl.demo.InheritedTest$NestedTest#NestedTestB()
Expand All @@ -43,8 +49,23 @@ public static String toFullyQualifiedName(String identifierUniqueId) {

sb.append("#");
sb.append(matcher.group(2)); // method

return sb.toString();
}
// try JUnit 5 parameterized pattern
matcher = junit5ParameterizedPattern.matcher(identifierUniqueId);
if (matcher.find()) {
StringBuilder sb = new StringBuilder();
sb.append(matcher.group(1)); // com.package.ClassName
sb.append("#");
sb.append(matcher.group(2)); // method
sb.append("[");
sb.append(matcher.group(3)); // parameter
sb.append("[");
sb.append("]");
return sb.toString();
}

// fall back to JUnit 4
matcher = junit4Pattern.matcher(identifierUniqueId);
if (matcher.find()) {
Expand Down

0 comments on commit 2085249

Please sign in to comment.