Skip to content

Commit

Permalink
Added a test for using a custom AsProvider with @as.
Browse files Browse the repository at this point in the history
relates to TNG#189
  • Loading branch information
Airblader committed Jul 12, 2016
1 parent 99fe5e9 commit 9a38d8f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,10 @@ private String getDescription( Method paramMethod ) {
}

As as = paramMethod.getAnnotation( As.class );
return ReflectionUtil
.newInstance( as == null ? DefaultAsProvider.class : as.provider() )
.as( as, paramMethod );
AsProvider provider = as != null
? ReflectionUtil.newInstance( as.provider() )
: new DefaultAsProvider();
return provider.as( as, paramMethod );
}

public void setSuccess( boolean success ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
/**
* The default provider for a stage method, scenario or scenario class.
*
* @since 0.12.0
*/
public class DefaultAsProvider implements AsProvider {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.tngtech.jgiven.annotation.As;
import com.tngtech.jgiven.annotation.AsProvider;
import com.tngtech.jgiven.annotation.Description;
import com.tngtech.jgiven.impl.params.DefaultAsProvider;
import com.tngtech.jgiven.impl.util.AssertionUtil;
Expand Down Expand Up @@ -182,9 +183,10 @@ public synchronized void setTestClass( Class<?> testClass ) {
}

As as = testClass.getAnnotation( As.class );
name = ReflectionUtil
.newInstance( as == null ? DefaultAsProvider.class : as.provider() )
.as( as, testClass );
AsProvider provider = as != null
? ReflectionUtil.newInstance( as.provider() )
: new DefaultAsProvider();
name = provider.as( as, testClass );
}

public String getName() {
Expand Down
27 changes: 25 additions & 2 deletions jgiven-core/src/test/java/com/tngtech/jgiven/GivenTestStep.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.tngtech.jgiven;

import java.lang.reflect.Method;

import com.tngtech.jgiven.annotation.As;
import com.tngtech.jgiven.annotation.AsProvider;
import com.tngtech.jgiven.annotation.Format;
import com.tngtech.jgiven.annotation.Formatf;
import com.tngtech.jgiven.annotation.IntroWord;
Expand Down Expand Up @@ -89,15 +92,20 @@ public GivenTestStep a_step_with_a_bracket_after_a_dollar( int value ) {
return self();
}

public GivenTestStep a_step_with_a_printf_annotation_$( @Formatf( "%.2f" ) double d) {
@As( value = "output", provider = CustomAsProvider.class )
public GivenTestStep a_step_with_an_As_annotation_and_a_custom_provider() {
return self();
}

public GivenTestStep a_step_with_a_printf_annotation_$( @Formatf( "%.2f" ) double d ) {
return self();
}

public GivenTestStep a_step_with_a_$_parameter( String param ) {
return self();
}

public GivenTestStep a_step_with_a_boolean_$_parameter( @Format( value = BooleanFormatter.class, args = { "yes", "no" } ) boolean b) {
public GivenTestStep a_step_with_a_boolean_$_parameter( @Format( value = BooleanFormatter.class, args = { "yes", "no" } ) boolean b ) {
return self();
}

Expand All @@ -110,4 +118,19 @@ public GivenTestStep a_step_with_a_bracket_after_a_dollar( int value ) {
public GivenTestStep an_intro_word_with_an_as_annotation() {
return self();
}

public static class CustomAsProvider implements AsProvider {

@Override
public String as( As annotation, Method method ) {
return "Custom AsProvider " + annotation.value() + ": " + method.getName();
}

@Override
public String as( As annotation, Class<?> scenarioClass ) {
return null;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@
import com.tngtech.jgiven.annotation.DoNotIntercept;
import com.tngtech.jgiven.annotation.IsTag;
import com.tngtech.jgiven.base.ScenarioTestBase;
import com.tngtech.jgiven.report.model.*;
import com.tngtech.jgiven.report.model.ReportModel;
import com.tngtech.jgiven.report.model.ScenarioCaseModel;
import com.tngtech.jgiven.report.model.ScenarioModel;
import com.tngtech.jgiven.report.model.StepModel;
import com.tngtech.jgiven.report.model.Tag;
import com.tngtech.jgiven.report.model.Word;

@RunWith( DataProviderRunner.class )
public class ScenarioModelBuilderTest extends ScenarioTestBase<GivenTestStep, WhenTestStep, ThenTestStep> {
Expand Down Expand Up @@ -309,6 +314,16 @@ public void characters_are_not_dropped_when_using_the_As_annotation() throws Thr
assertThat( step.getCompleteSentence() ).isEqualTo( "Given a step with a bracket after a dollar 42 ]" );
}

@Test
public void a_custom_AsProvider_can_be_used() throws Throwable {
startScenario( "Scenario with a @As tag" );
given().a_step_with_an_As_annotation_and_a_custom_provider();
getScenario().finished();
StepModel step = getScenario().getScenarioCaseModel().getFirstStep();
assertThat( step.getCompleteSentence() )
.isEqualTo( "Given Custom AsProvider output: a_step_with_an_As_annotation_and_a_custom_provider" );
}

@Test
public void camel_case_is_supported_in_steps() throws Throwable {
startScenario( "Scenario camel case steps" );
Expand Down

0 comments on commit 9a38d8f

Please sign in to comment.