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
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 19 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 @@ -67,6 +69,14 @@
* <dt>...</dt>
* <dd>...</dd>
* </dl>
* To format the placeholder you can use patterns like {@link MessageFormat}.<br>
* For Example if you:
* <ul>
* <li>want specified leading zero for the {index} placeholder, you can use:
* <code>{index,number,0000}</code></li>
* <li>want format a parameter (placeholder with index 2) of type date, you can
* use: <code>{2,date,full}</code></li>
* </ul>
* <p>
* In the example given above, the <code>Parameterized</code> runner creates
* names like <code>[1: fib(3)=2]</code>. If you don't use the name parameter,
Expand Down Expand Up @@ -163,6 +173,7 @@
* @since 4.0
*/
public class Parameterized extends Suite {

/**
* Annotation for a method which provides parameters to be injected into the
* test class constructor by <code>Parameterized</code>. The method has to
Expand Down Expand Up @@ -234,6 +245,8 @@ public class Parameterized extends Suite {

private static final List<Runner> NO_RUNNERS = Collections.<Runner>emptyList();

private static final Pattern INDEX_MATCHER_PATTERN = Pattern.compile("(\\{)index([^\\}]*\\})");

private final List<Runner> runners;

/**
Expand Down Expand Up @@ -342,8 +355,12 @@ 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;
Matcher matcher = INDEX_MATCHER_PATTERN.matcher(pattern);
while (matcher.find()) {
String idxPattern = matcher.group(1) + "0" + matcher.group(2);
finalPattern = finalPattern.replace(matcher.group(), MessageFormat.format(idxPattern, index));
}
String name = MessageFormat.format(finalPattern, parameters);
return new TestWithParameters("[" + name + "]", testClass,
Arrays.asList(parameters));
Expand Down
Expand Up @@ -86,6 +86,62 @@ public void plansNamedCorrectly() throws Exception {
assertEquals("[0: fib(0)=0]", description.getChildren().get(0)
.getDisplayName());
}

@RunWith(Parameterized.class)
static public class ParameterizedWithSpecialTestname {
@Parameters(name = "{index,number,0000}: param 1: {0} on test#: {index} with expected result: {1} - {index}")
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][]{{0, 0}, {1, 1}, {2, 1},
{3, 2}, {4, 3}, {5, 5}, {6, 8}});
}

private final int fInput;

private final int fExpected;

public ParameterizedWithSpecialTestname(int input, int expected) {
fInput = input;
fExpected = expected;
}

@Test
public void test() {
assertEquals(fExpected, fib(fInput));
}

private int fib(int x) {
return 0;
}
}

@Test
public void countWithSpecialTestname() {
Result result = JUnitCore.runClasses(ParameterizedWithSpecialTestname.class);
assertEquals(7, result.getRunCount());
assertEquals(6, result.getFailureCount());
}

@Test
public void failuresNamedCorrectlyWithSpecialTestname() {
Result result = JUnitCore.runClasses(ParameterizedWithSpecialTestname.class);
assertEquals(
"test[0001: param 1: 1 on test#: 1 with expected result: 1 - 1](" + ParameterizedWithSpecialTestname.class.getName() + ")",
result.getFailures().get(0).getTestHeader());
}

@Test
public void countBeforeRunWithSpecialTestname() throws Exception {
Runner runner = Request.aClass(ParameterizedWithSpecialTestname.class).getRunner();
assertEquals(7, runner.testCount());
}

@Test
public void plansNamedCorrectlyWithSpecialTestname() throws Exception {
Runner runner = Request.aClass(ParameterizedWithSpecialTestname.class).getRunner();
Description description = runner.getDescription();
assertEquals("[0000: param 1: 0 on test#: 0 with expected result: 0 - 0]", description.getChildren().get(0)
.getDisplayName());
}

@RunWith(Parameterized.class)
public static class ParameterizedWithoutSpecialTestname {
Expand Down Expand Up @@ -385,7 +441,7 @@ private int fib(int x) {

@Test
public void runsEveryTestOfArray() {
Result result= JUnitCore.runClasses(FibonacciTestWithArray.class);
Result result = JUnitCore.runClasses(FibonacciTestWithArray.class);
assertEquals(7, result.getRunCount());
}

Expand Down