Skip to content

Commit

Permalink
Merge pull request #142 from Pragmatists/mattmook-master
Browse files Browse the repository at this point in the history
Fix for #134 - Mattmook master with some fixes
  • Loading branch information
woprzech authored Nov 3, 2017
2 parents 58c3a1f + a02c5e4 commit 0a20971
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 7 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: java
dist: precise
install: mvn install -DskipTests=true -Dgpg.skip=true -Dmaven.javadoc.skip=true -B -V
jdk:
- oraclejdk8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,6 @@ public Description getDescriptionFor(FrameworkMethod method) {
}

private boolean wasInvoked(ParameterisedTestMethodRunner runner) {
return runner != null;
return runner != null && runner.count() != 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@

import junitparams.internal.Utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;

public class MacroSubstitutionNamingStrategy implements TestCaseNamingStrategy {
private static final String MACRO_PATTERN = "\\{[^\\}]{0,50}\\}";
// Pattern that keeps delimiters in split result
private static final Pattern MACRO_SPLIT_PATTERN = Pattern.compile(String.format("(?=%s)|(?<=%s)", MACRO_PATTERN, MACRO_PATTERN));
private static final String MACRO_START = "{";
private static final String MACRO_END = "}";
static final String DEFAULT_TEMPLATE = "{method}({params}) [{index}]";
private static final String[] DEFAULT_TEMPLATE_PARTS = MACRO_SPLIT_PATTERN.split(DEFAULT_TEMPLATE);
private static final String[] DEFAULT_TEMPLATE_PARTS = split(DEFAULT_TEMPLATE);

private String[] templateParts;
private String methodName;

public MacroSubstitutionNamingStrategy(TestCaseName testCaseName, String methodName) {
this.templateParts = MACRO_SPLIT_PATTERN.split(getTemplate(testCaseName));
this.templateParts = split(getTemplate(testCaseName));
this.methodName = methodName;
}

Expand Down Expand Up @@ -118,4 +116,48 @@ public static Macro parse(String value) {
Macro.INDEX.toString(), Macro.PARAMS.toString(), Macro.METHOD.toString())
);
}

/**
* Split keeping delimiters in split result
*/
private static String[] split(String input) {
char macroStart = MACRO_START.charAt(0);
char macroEnd = MACRO_END.charAt(0);

int startIndex = 0;
boolean inMacro = false;

List<String> list = new ArrayList<String>();

for (int endIndex = 0; endIndex < input.length(); endIndex++) {
char chr = input.charAt(endIndex);

if (!inMacro) {
if (chr == macroStart) {
String result = input.substring(startIndex, endIndex);
if (result.length() > 0) {
list.add(result);
}
inMacro = true;
startIndex = endIndex;
}
} else {
if (chr == macroEnd) {
String result = input.substring(startIndex, endIndex + 1);
if (result.length() > 0) {
list.add(result);
}
inMacro = false;
startIndex = endIndex + 1;
}
}
}

String result = input.substring(startIndex);
if (result.length() > 0) {
list.add(result);
}

return list.toArray(new String[list.size()]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package junitparams.internal;

import junitparams.Parameters;
import org.junit.Test;
import org.junit.runner.Description;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.TestClass;

import static org.assertj.core.api.Assertions.assertThat;

public class ParameterisedTestClassRunnerTest {

@Test
public void shouldGetNullDescriptionForMethodNotYetInvoked() throws Exception {
ParameterisedTestClassRunner runner = new ParameterisedTestClassRunner(new TestClass(SampleTest.class));
runner.returnListOfMethods();

Description test = runner.getDescriptionFor(new FrameworkMethod(SampleTest.class.getMethod("parametrized", String.class)));

assertThat(test).isNull();
}

static class SampleTest {
@Test
@Parameters({ "123", "456" })
@SuppressWarnings({"unused", "WeakerAccess"})
public void parametrized(String param) {
}
}
}

0 comments on commit 0a20971

Please sign in to comment.