Skip to content

Commit

Permalink
support to specify a test name formatter on @dataProvider annotation (#…
Browse files Browse the repository at this point in the history
…101)

Signed-off-by: Andreas Schmid <service@aaschmid.de>
  • Loading branch information
aaschmid committed Mar 30, 2018
1 parent dae9931 commit 84cf75d
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,50 @@

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

import java.lang.reflect.Method;
import java.util.List;

import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;

import com.tngtech.junit.dataprovider.DataProvider;
import com.tngtech.junit.dataprovider.UseDataProvider;
import com.tngtech.junit.dataprovider.UseDataProviderExtension;
import com.tngtech.junit.dataprovider.format.DataProviderTestNameFormatter;

@ExtendWith(UseDataProviderExtension.class)
class FormatAcceptanceTest {

static class PlusTestNameFormatter implements DataProviderTestNameFormatter {
@Override
public String format(Method testMethod, int invocationIndex, List<Object> arguments) {
return String.format("%s: %2d + %2d = %2d", testMethod.getName(), arguments.get(0), arguments.get(1),
arguments.get(2));
}
}

@DataProvider(format = "%a[0] * %a[1] == %a[2]", formatter = PlusTestNameFormatter.class)
static Object[][] dataProviderPlus() {
// @formatter:off
return new Object[][] {
{ 0, 0, 0 },
{ -1, 0, -1 },
{ 0, 1, 1 },
{ 1, 1, 2 },
{ 1, -1, 0 },
{ -1, -1, -2 },
};
// @formatter:on
}

@TestTemplate
@UseDataProvider
void testPlus(int a, int b, int expected) {
// Expect:
assertThat(a + b).isEqualTo(expected);
}

@DataProvider(format = "%a[0] * %a[1] == %a[2]")
static Object[][] dataProviderMultiply() {
// @formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.tngtech.junit.dataprovider.convert.ObjectArrayConverter;
import com.tngtech.junit.dataprovider.convert.SingleArgConverter;
import com.tngtech.junit.dataprovider.convert.StringConverter;
import com.tngtech.junit.dataprovider.format.DataProviderPlaceholderFormatter;
import com.tngtech.junit.dataprovider.format.DataProviderTestNameFormatter;

/**
* Mark a method as a dataprovider used by a test method or use it directly at the test method and provide data via {@link #value()}
Expand Down Expand Up @@ -199,6 +201,14 @@
*/
String format() default DEFAULT_FORMAT;

/**
* Formatter to be used to generate test method description. The placeholder approach which formats according to
* {@link #format()} is used by default.
*
* @return a formatter used to generate test names.
*/
Class<? extends DataProviderTestNameFormatter> formatter() default DataProviderPlaceholderFormatter.class;

/**
* @return a custom converter converting {@link Object}{@code []} data to proper arguments
* @see ObjectArrayConverter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ protected ConverterContext getConverterContext(DataProvider dataProvider) {

@Override
protected DisplayNameContext getDisplayNameContext(DataProvider dataProvider) {
return new DisplayNameContext(dataProvider.format(), getDefaultPlaceholders());
return new DisplayNameContext(dataProvider.formatter(), dataProvider.format(), getDefaultPlaceholders());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
import org.junit.platform.commons.util.ReflectionUtils;

import com.tngtech.junit.dataprovider.placeholder.BasePlaceholder;
import com.tngtech.junit.dataprovider.placeholder.ReplacementData;
import com.tngtech.junit.dataprovider.format.DataProviderPlaceholderFormatter;

class DataProviderInvocationContext implements TestTemplateInvocationContext {

Expand All @@ -27,13 +27,14 @@ class DataProviderInvocationContext implements TestTemplateInvocationContext {

@Override
public String getDisplayName(int invocationIndex) {
ReplacementData data = ReplacementData.of(testMethod, invocationIndex, arguments);

String result = displayNameContext.getFormat();
for (BasePlaceholder placeHolder : displayNameContext.getPlaceholders()) {
result = placeHolder.process(data, result);
if (displayNameContext.getFormatter() == null
|| DataProviderPlaceholderFormatter.class.equals(displayNameContext.getFormatter())) {
return new DataProviderPlaceholderFormatter(displayNameContext.getFormat(),
displayNameContext.getPlaceholders()).format(testMethod, invocationIndex, arguments);
}
return result;
return ReflectionUtils.newInstance(displayNameContext.getFormatter()).format(testMethod, invocationIndex,
arguments);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,30 @@
import java.util.ArrayList;
import java.util.List;

import com.tngtech.junit.dataprovider.format.DataProviderTestNameFormatter;
import com.tngtech.junit.dataprovider.placeholder.BasePlaceholder;

public class DisplayNameContext {

private final Class<? extends DataProviderTestNameFormatter> formatter;
private final String format;
private final List<? extends BasePlaceholder> placeholders;

public DisplayNameContext(String format, List<? extends BasePlaceholder> placeholders) {
this(null, format, placeholders);
}

public DisplayNameContext(Class<? extends DataProviderTestNameFormatter> formatter, String format,
List<? extends BasePlaceholder> placeholders) {
this.formatter = formatter;
this.format = checkNotNull(format, "'format' must not be null");
this.placeholders = new ArrayList<>(checkNotNull(placeholders, "'placeholders' must not be null"));
}

public Class<? extends DataProviderTestNameFormatter> getFormatter() {
return formatter;
}

public String getFormat() {
return format;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ protected boolean cacheDataProviderResult(DataProvider dataProviderAnnotation) {

@Override
protected DisplayNameContext getDisplayNameContext(DataProvider dataProvider) {
return new DisplayNameContext(dataProvider.format(), getDefaultPlaceholders());
return new DisplayNameContext(dataProvider.formatter(), dataProvider.format(), getDefaultPlaceholders());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.tngtech.junit.dataprovider.convert.ConverterContext;
import com.tngtech.junit.dataprovider.convert.DataConverter;
import com.tngtech.junit.dataprovider.format.DataProviderTestNameFormatter;
import com.tngtech.junit.dataprovider.resolver.DataProviderResolverContext;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.tngtech.junit.dataprovider.format;

import java.lang.reflect.Method;
import java.util.List;

import com.tngtech.junit.dataprovider.placeholder.BasePlaceholder;
import com.tngtech.junit.dataprovider.placeholder.ReplacementData;

public class DataProviderPlaceholderFormatter implements DataProviderTestNameFormatter {

private final String format;
private final List<? extends BasePlaceholder> placeholders;

public DataProviderPlaceholderFormatter(String format, List<? extends BasePlaceholder> placeholders) {
this.format = format;
this.placeholders = placeholders;
}

@Override
public String format(Method testMethod, int invocationIndex, List<Object> arguments) {
ReplacementData data = ReplacementData.of(testMethod, invocationIndex, arguments);

String result = format;
for (BasePlaceholder placeHolder : placeholders) {
result = placeHolder.process(data, result);
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.tngtech.junit.dataprovider.format;

import java.lang.reflect.Method;
import java.util.List;

public interface DataProviderTestNameFormatter {

String format(Method testMethod, int invocationIndex, List<Object> arguments);
}

0 comments on commit 84cf75d

Please sign in to comment.