Skip to content

Commit

Permalink
Introduced CurrentScenario.
Browse files Browse the repository at this point in the history
We introduce a new interface CurrentScenario similar to CurrentStep which
can be injected into stages. The previously introduced addTag() method is
moved from ScenarioBase to this new interface.

relates to TNG#172
  • Loading branch information
Airblader committed Jul 5, 2016
1 parent 6e74e63 commit b928350
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 47 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* Added a new comment() method to provide further information on specific step method invocations, see [#50](https://github.com/TNG/JGiven/issues/50).
* Steps can now have multiple attachments [#194](https://github.com/TNG/JGiven/issues/194).
* Tags can now be hidden from the navigation bar in the HTML report by setting the `showInNavigation` attribute to `false` [#211](https://github.com/TNG/JGiven/issues/211).
* Added addTag() to ScenarioBase to allow adding tags dynamically, see [#172](https://github.com/TNG/JGiven/issues/172).
* Added a new CurrentScenario interface similar to CurrentStep.
* The CurrentScenario interface allows adding tags programmatically, see [#172](https://github.com/TNG/JGiven/issues/172).

## Breaking Changes in the JSON model

Expand Down
23 changes: 23 additions & 0 deletions jgiven-core/src/main/java/com/tngtech/jgiven/CurrentScenario.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.tngtech.jgiven;

import java.lang.annotation.Annotation;

import com.tngtech.jgiven.annotation.ScenarioState;

/**
* This interface can be injected into a stage using the {@link ScenarioState} annotation.
* It provided programmatic access to the current scenario.
*
* @since 0.12.0
*/
public interface CurrentScenario {

/**
* Dynamically add a tag to the scenario.
* @param annotationClass The tag annotation class to use.
* @param values List of custom values.
* @since 0.12.0
*/
void addTag( Class<? extends Annotation> annotationClass, String... values );

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.tngtech.jgiven.impl;

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

Expand Down Expand Up @@ -115,14 +114,4 @@ public void section( String sectionTitle ) {
executor.addSection( sectionTitle );
}

/**
* Dynamically add a tag to the scenario.
* @param annotationClass The tag annotation class to use.
* @param values List of custom values. This must be supported by the given tag.
* @since 0.12.0
*/
public void addTag( Class<? extends Annotation> annotationClass, String... values ) {
executor.addTag( annotationClass, values );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public enum State {

void addSection( String sectionTitle );

void addTag( Class<? extends Annotation> annotationClass, String... values );

void readScenarioState( Object object );

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.tngtech.jgiven.CurrentScenario;
import com.tngtech.jgiven.CurrentStep;
import com.tngtech.jgiven.annotation.AfterScenario;
import com.tngtech.jgiven.annotation.AfterStage;
Expand Down Expand Up @@ -80,6 +81,7 @@ public class StandaloneScenarioExecutor implements ScenarioExecutor {
public StandaloneScenarioExecutor() {
injector.injectValueByType( StandaloneScenarioExecutor.class, this );
injector.injectValueByType( CurrentStep.class, new StepAccessImpl() );
injector.injectValueByType( CurrentScenario.class, new ScenarioAccessImpl() );
}

protected static class StageState {
Expand All @@ -106,6 +108,15 @@ public void setExtendedDescription( String extendedDescription ) {
}
}

class ScenarioAccessImpl implements CurrentScenario {

@Override
public void addTag( Class<? extends Annotation> annotationClass, String... values ) {
listener.tagAdded( annotationClass, values );
}

}

class MethodHandler implements StepMethodHandler {
@Override
public void handleMethod( Object stageInstance, Method paramMethod, Object[] arguments, InvocationMode mode,
Expand Down Expand Up @@ -533,9 +544,4 @@ public void addSection( String sectionTitle ) {
listener.sectionAdded( sectionTitle );
}

@Override
public void addTag( Class<? extends Annotation> annotationClass, String... values ) {
listener.tagAdded( annotationClass, values );
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.tngtech.jgiven.examples.tags;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import org.junit.Test;

import com.tngtech.jgiven.CurrentScenario;
import com.tngtech.jgiven.annotation.IsTag;
import com.tngtech.jgiven.annotation.ScenarioState;
import com.tngtech.jgiven.junit.SimpleScenarioTest;
import com.tngtech.jgiven.tags.Issue;

/**
* This example shows how tags can be added dynamically at runtime.
Expand All @@ -12,9 +17,46 @@ public class DynamicTags extends SimpleScenarioTest<DynamicTags.Steps> {

@Test
public void tags_can_be_added_dynamically() {
getScenario().addTag( Issue.class, "Value" );
given().an_order_for_a_$_car( CarType.BMW );
when().the_order_is_processed();
then().the_car_is_sent_to_manufacturing();
}

public static class Steps {
@ScenarioState
CurrentScenario currentScenario;

Steps an_order_for_a_$_car( CarType type ) {
/*
* Dynamically add a tag with a value depending on the passed
* argument.
*/
currentScenario.addTag( CarOrder.class, type.name );
return this;
}

Steps the_order_is_processed() {
return this;
}

Steps the_car_is_sent_to_manufacturing() {
return this;
}
}

public static class Steps {}
@IsTag
@Retention( RetentionPolicy.RUNTIME )
@interface CarOrder {}

private static enum CarType {
AUDI( "Audi" ),
BMW( "BMW" );

final String name;

private CarType( String name ) {
this.name = name;
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
package com.tngtech.jgiven.junit;

import static com.tngtech.jgiven.annotation.ScenarioState.Resolution.NAME;
import static org.assertj.core.api.Assertions.assertThat;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;

import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.jgiven.CurrentScenario;
import com.tngtech.jgiven.CurrentStep;
import com.tngtech.jgiven.Stage;
import com.tngtech.jgiven.annotation.*;
Expand All @@ -12,13 +23,6 @@
import com.tngtech.jgiven.junit.test.ThenTestStep;
import com.tngtech.jgiven.junit.test.WhenTestStep;
import com.tngtech.jgiven.report.model.AttachmentModel;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.List;

import static com.tngtech.jgiven.annotation.ScenarioState.Resolution.NAME;
import static org.assertj.core.api.Assertions.assertThat;

@RunWith( DataProviderRunner.class )
@JGivenConfiguration( TestConfiguration.class )
Expand Down Expand Up @@ -73,8 +77,7 @@ public void an_exception_is_thrown_when_stages_have_ambiguous_fields() {
stage.something();
}

static class SomeType {
}
static class SomeType {}

public static class TestStageWithAmbiguousFields {
@ScenarioState
Expand All @@ -83,8 +86,7 @@ public static class TestStageWithAmbiguousFields {
@ScenarioState
SomeType secondType;

public void something() {
}
public void something() {}
}

@Test
Expand All @@ -101,8 +103,7 @@ public static class TestStageWithAmbiguousFieldsButResolutionByName {
@ScenarioState( resolution = NAME )
SomeType secondType;

public void something() {
}
public void something() {}
}

@Test( expected = IllegalStateException.class )
Expand Down Expand Up @@ -168,17 +169,15 @@ public void after() {
}

@SuppressWarnings( "serial" )
static class SomeExceptionInAfterStage extends RuntimeException {
}
static class SomeExceptionInAfterStage extends RuntimeException {}

static class AssertionInAfterStage extends Stage<AssertionInAfterStage> {
@AfterStage
public void after() {
throw new SomeExceptionInAfterStage();
}

public void something() {
}
public void something() {}
}

@Test( expected = SomeExceptionInAfterStage.class )
Expand Down Expand Up @@ -233,8 +232,7 @@ static class SomeStageProvidingAString {
@ProvidedScenarioState
String someString = "test";

public void something() {
}
public void something() {}
}

static class SomeStageWithAHiddenMethod {
Expand All @@ -254,7 +252,6 @@ public void hidden_steps_see_injected_values() {

stage1.something();
stage2.someHiddenStep();

}

static class SomeStageWithABeforeMethod {
Expand All @@ -266,8 +263,7 @@ public void someHiddenStep() {
assertThat( someString ).isNotNull();
}

public void something() {
}
public void something() {}
}

@Test
Expand All @@ -277,7 +273,6 @@ public void before_stage_methods_see_injected_values() {

stage1.something();
stage2.something();

}

static class AttachmentStepClass {
Expand Down Expand Up @@ -317,14 +312,37 @@ public void extended_descriptions_can_be_set_using_the_current_step() {
assertThat( description ).isEqualTo( "An extended description" );
}

@IsTag
@Retention( RetentionPolicy.RUNTIME )
@interface DynamicTag {}

static class CurrentScenarioStage {
@ScenarioState
CurrentScenario currentScenario;

public void add_tag() {
currentScenario.addTag( DynamicTag.class, "value" );
}
}

@Test
public void tags_can_be_added_using_the_current_scenario() {
CurrentScenarioStage steps = addStage( CurrentScenarioStage.class );

steps.add_tag();

List<String> tagIds = getScenario().getScenarioModel().getTagIds();
assertThat( tagIds ).hasSize( 1 );
assertThat( tagIds.get( 0 ) ).isEqualTo( "DynamicTag-value" );
}

static abstract class AbstractStage {
public abstract void abstract_step();
}

static class ConcreteStage extends AbstractStage {
@Override
public void abstract_step() {
}
public void abstract_step() {}
}

@Test
Expand Down

0 comments on commit b928350

Please sign in to comment.