Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle the {index} parameter like a native MessageFormat argument. #969

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/main/java/org/junit/runners/Parameterized.java
Expand Up @@ -10,6 +10,8 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.runner.Runner;
import org.junit.runners.model.FrameworkMethod;
Expand Down Expand Up @@ -342,8 +344,13 @@ private Exception parametersMethodReturnedWrongType() throws Exception {

private static TestWithParameters createTestWithParameters(
TestClass testClass, String pattern, int index, Object[] parameters) {
String finalPattern = pattern.replaceAll("\\{index\\}",
Integer.toString(index));
String finalPattern = pattern;
Pattern indexMatcherPattern = Pattern.compile("(\\{)index([^\\}]*\\})");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you extract a constant for this Pattern?

Matcher matcher = indexMatcherPattern.matcher(pattern);
if (matcher.find()) {
String idxPattern = matcher.group(1) + 0 + matcher.group(2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use "0" instead of 0, because it is clearer.

finalPattern = pattern.replace(matcher.group(), MessageFormat.format(idxPattern, index));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this using replace and not replaceall?

}
String name = MessageFormat.format(finalPattern, parameters);
return new TestWithParameters("[" + name + "]", testClass,
Arrays.asList(parameters));
Expand Down