-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor argument formatter for general use (#101)
- Loading branch information
Showing
20 changed files
with
363 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...n/java/com/tngtech/junit/dataprovider/placeholder/AnnotationBasedArgumentPlaceholder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.tngtech.junit.dataprovider.placeholder; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Method; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class AnnotationBasedArgumentPlaceholder extends AbstractArgumentPlaceholder { | ||
|
||
public AnnotationBasedArgumentPlaceholder() { | ||
super("%aa\\[(-?[0-9]+|-?[0-9]+\\.\\.-?[0-9]+)\\]"); | ||
} | ||
|
||
@Override | ||
protected String getReplacementFor(String placeholder, ReplacementData data) { | ||
String subscript = placeholder.substring(4, placeholder.length() - 1); | ||
|
||
int from = Integer.MAX_VALUE; | ||
int to = Integer.MIN_VALUE; | ||
if (subscript.contains("..")) { | ||
String[] split = subscript.split("\\.\\."); | ||
|
||
from = Integer.parseInt(split[0]); | ||
to = Integer.parseInt(split[1]); | ||
} else { | ||
from = Integer.parseInt(subscript); | ||
to = from; | ||
} | ||
|
||
List<Object> arguments = data.getArguments(); | ||
from = (from >= 0) ? from : arguments.size() + from; | ||
to = (to >= 0) ? to + 1 : arguments.size() + to + 1; | ||
return formatAll(getParametersAndArguments(data.getTestMethod(), arguments, from, to)); | ||
} | ||
|
||
private List<ParameterAndArgument> getParametersAndArguments(Method testMethod, List<Object> arguments, int from, int to) { | ||
Annotation[][] parameterAnnotations = testMethod.getParameterAnnotations(); | ||
Class<?>[] parameterTypes = testMethod.getParameterTypes(); | ||
|
||
List<ParameterAndArgument> result = new ArrayList<ParameterAndArgument>(); | ||
for (int idx = 0; idx < arguments.size() && idx < parameterTypes.length; idx++) { // TODO test! | ||
result.add(new ParameterAndArgument(parameterTypes[idx], parameterAnnotations[idx], arguments.get(idx))); | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* Formats the given parameters and arguments to a comma-separated list of {@code $parameterName=$argumentName}. | ||
* Arguments {@link String} representation are therefore treated specially. | ||
* | ||
* @param parameterAndArguments parameter types and annotation as well as arguments to be formatted | ||
* @return the formatted {@link String} of the given parameters and arguments | ||
*/ | ||
protected String formatAll(List<ParameterAndArgument> parameterAndArguments) { | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
for (int idx = 0; idx < parameterAndArguments.size(); idx++) { | ||
ParameterAndArgument parameterAndArgument = parameterAndArguments.get(idx); | ||
|
||
stringBuilder.append(format(parameterAndArgument)); | ||
|
||
if (idx < parameterAndArguments.size() - 1) { | ||
stringBuilder.append(", "); | ||
} | ||
} | ||
return stringBuilder.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
core/src/main/java/com/tngtech/junit/dataprovider/placeholder/argformat/ArgumentFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.tngtech.junit.dataprovider.placeholder.argformat; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Annotate a test method parameter to provide a custom formatter for it. | ||
*/ | ||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ ElementType.ANNOTATION_TYPE, ElementType.PARAMETER }) | ||
public @interface ArgumentFormat { | ||
Class<? extends ArgumentFormatter<?>> value(); | ||
} |
10 changes: 10 additions & 0 deletions
10
...src/main/java/com/tngtech/junit/dataprovider/placeholder/argformat/ArgumentFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.tngtech.junit.dataprovider.placeholder.argformat; | ||
|
||
/** | ||
* Interface for defining custom argument formatter using the {@link ArgumentFormat} annotation. | ||
* | ||
* @param <T> the type of the object to format | ||
*/ | ||
public interface ArgumentFormatter<T> { | ||
String format(T argument); | ||
} |
Oops, something went wrong.