Skip to content

Commit

Permalink
enable formatters for arguments of test methods (fixes #114)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Schäfer committed Sep 5, 2015
1 parent 6ce16d4 commit 98ec083
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Object>( new DefaultFormatter<Object>() );

private ScenarioModel currentScenarioModel;
private ScenarioCaseModel currentScenarioCase;
Expand Down Expand Up @@ -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<Class<?>> visitedTypes, Annotation originalAnnotation ) {
Expand Down Expand Up @@ -288,7 +293,9 @@ public void scenarioStarted( Method method, List<NamedArgument> namedArguments )

// must come at last
setMethodName( method.getName() );
setArguments( toStringList( getValues( namedArguments ) ) );

List<Formatting<?>> formatters = getFormatters( method.getParameterAnnotations() );
setArguments( toStringList( formatters, getValues( namedArguments ) ) );
}

private List<Object> getValues( List<NamedArgument> namedArguments ) {
Expand All @@ -311,14 +318,22 @@ private void readConfiguration( Class<?> testClass ) {
configuration = ConfigurationUtil.getConfiguration( testClass );
}

private List<String> toStringList( Collection<?> arguments ) {
private List<String> toStringList( List<Formatting<?>> formatters, List<?> arguments ) {
List<String> result = Lists.newArrayList();
for( Object o : arguments ) {
result.add( new DefaultFormatter<Object>().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 <T> String formatUsingFormatterOrDefault( Formatting<T> formatting, Object o ) {
return formatting.format( (T) o );
}

private void readAnnotations( Method method ) {
String scenarioDescription = method.getName();

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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<ScenarioCaseModel> cases = getScenario().getModel().getLastScenarioModel().getScenarioCases();
assertThat( cases.get( cases.size() - 1 ).getExplicitArguments() ).containsExactly( b ? "foo" : "bar" );
}

}

0 comments on commit 98ec083

Please sign in to comment.