From 98ec083011401b0ab3e9fa4cd1a486aa7e263a76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4fer?= Date: Sat, 5 Sep 2015 19:33:44 +0200 Subject: [PATCH] enable formatters for arguments of test methods (fixes #114) --- CHANGELOG.md | 4 +++ .../report/model/ReportModelBuilder.java | 25 +++++++++++++++---- .../jgiven/junit/DataProviderTest.java | 19 +++++++++++++- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e15a8e5a2d..5b1f9034ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # v0.8.1 +## New Features + +* Parameters of test methods can now also be formatted with formatters like step parameters. This is only relevant, however, when you have scenarios with multiple cases that do not generate a data table, but multiple cases [#114](https://github.com/TNG/JGiven/pull/114) + ## Fixed Issues * Fixed the issue that test classes had to be compiled with the -parameters option of javac when using Java 8. This was fixed by upgrading to the newest version of the Paranamer library that now fully supports Java 8 [#106](https://github.com/TNG/JGiven/pull/106) diff --git a/jgiven-core/src/main/java/com/tngtech/jgiven/report/model/ReportModelBuilder.java b/jgiven-core/src/main/java/com/tngtech/jgiven/report/model/ReportModelBuilder.java index b8835e73f8..894af3014d 100644 --- a/jgiven-core/src/main/java/com/tngtech/jgiven/report/model/ReportModelBuilder.java +++ b/jgiven-core/src/main/java/com/tngtech/jgiven/report/model/ReportModelBuilder.java @@ -2,7 +2,10 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Method; -import java.util.*; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,6 +35,7 @@ */ public class ReportModelBuilder implements ScenarioListener { private static final Logger log = LoggerFactory.getLogger( ReportModelBuilder.class ); + private static final Formatting DEFAULT_FORMATTING = new Formatting( new DefaultFormatter() ); private ScenarioModel currentScenarioModel; private ScenarioCaseModel currentScenarioCase; @@ -176,6 +180,7 @@ private Formatting getFormatting( Annotation[] annotations ) { /** * Recursively searches for formatting annotations. + * * @param visitedTypes used to prevent an endless loop */ private Formatting getFormatting( Annotation[] annotations, Set> visitedTypes, Annotation originalAnnotation ) { @@ -288,7 +293,9 @@ public void scenarioStarted( Method method, List namedArguments ) // must come at last setMethodName( method.getName() ); - setArguments( toStringList( getValues( namedArguments ) ) ); + + List> formatters = getFormatters( method.getParameterAnnotations() ); + setArguments( toStringList( formatters, getValues( namedArguments ) ) ); } private List getValues( List namedArguments ) { @@ -311,14 +318,22 @@ private void readConfiguration( Class testClass ) { configuration = ConfigurationUtil.getConfiguration( testClass ); } - private List toStringList( Collection arguments ) { + private List toStringList( List> formatters, List arguments ) { List result = Lists.newArrayList(); - for( Object o : arguments ) { - result.add( new DefaultFormatter().format( o ) ); + for( int i = 0; i < arguments.size(); i++ ) { + Formatting formatting = DEFAULT_FORMATTING; + if( i < formatters.size() && formatters.get( i ) != null ) { + formatting = formatters.get( i ); + } + result.add( formatUsingFormatterOrDefault( formatting, arguments.get( i ) ) ); } return result; } + private String formatUsingFormatterOrDefault( Formatting formatting, Object o ) { + return formatting.format( (T) o ); + } + private void readAnnotations( Method method ) { String scenarioDescription = method.getName(); diff --git a/jgiven-junit/src/test/java/com/tngtech/jgiven/junit/DataProviderTest.java b/jgiven-junit/src/test/java/com/tngtech/jgiven/junit/DataProviderTest.java index f1cf23bc8a..ef0ad40c21 100644 --- a/jgiven-junit/src/test/java/com/tngtech/jgiven/junit/DataProviderTest.java +++ b/jgiven-junit/src/test/java/com/tngtech/jgiven/junit/DataProviderTest.java @@ -1,6 +1,6 @@ package com.tngtech.jgiven.junit; -import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; import java.util.List; @@ -10,6 +10,8 @@ import com.tngtech.java.junit.dataprovider.DataProvider; import com.tngtech.java.junit.dataprovider.DataProviderRunner; import com.tngtech.java.junit.dataprovider.UseDataProvider; +import com.tngtech.jgiven.annotation.Format; +import com.tngtech.jgiven.format.BooleanFormatter; import com.tngtech.jgiven.junit.test.GivenTestStep; import com.tngtech.jgiven.junit.test.ThenTestStep; import com.tngtech.jgiven.junit.test.WhenTestStep; @@ -94,4 +96,19 @@ public void derived_parameters_work( Integer arg ) { } } + @Test + @DataProvider( { "true", "false" } ) + public void parameters_of_methods_can_be_formatted( @Format( value = BooleanFormatter.class, args = { "foo", "bar" } ) boolean b ) + throws Throwable { + given().some_boolean_value( b ); + if( b ) { + when().something(); + } + + getScenario().finished(); + + List cases = getScenario().getModel().getLastScenarioModel().getScenarioCases(); + assertThat( cases.get( cases.size() - 1 ).getExplicitArguments() ).containsExactly( b ? "foo" : "bar" ); + } + }