Skip to content

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

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

Closed
wants to merge 7 commits into from
Closed
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/main/java/org/junit/runners/Parameterized.java
Original file line number Diff line number Diff line change
@@ -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;
@@ -163,6 +165,8 @@
* @since 4.0
*/
public class Parameterized extends Suite {
private static final String INDEX_MATCHER_PATTERN = "(\\{)index([^\\}]*\\})";
Copy link
Member

Choose a reason for hiding this comment

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

I meant create a constant for the Pattern. Compiling a pattern isn't cheap


/**
* Annotation for a method which provides parameters to be injected into the
* test class constructor by <code>Parameterized</code>. The method has to
@@ -342,8 +346,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_MATCHER_PATTERN);
Matcher matcher = indexMatcherPattern.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));
Original file line number Diff line number Diff line change
@@ -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 {
@@ -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());
}