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 #172
  • Loading branch information
Airblader committed Jul 5, 2016
1 parent d06fb42 commit 62eb3ee
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 44 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 @@ -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
Expand Up @@ -2,6 +2,8 @@

import org.junit.Test;

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

Expand All @@ -12,9 +14,17 @@ public class DynamicTags extends SimpleScenarioTest<DynamicTags.Steps> {

@Test
public void tags_can_be_added_dynamically() {
getScenario().addTag( Issue.class, "Value" );
given().the_runtime_argument_$_to_be_added_as_a_tag( "Test" );
}

public static class Steps {}
public static class Steps {
@ScenarioState
CurrentScenario currentScenario;

Steps the_runtime_argument_$_to_be_added_as_a_tag( String argument ) {
currentScenario.addTag( Issue.class, "Value" );
return this;
}
}

}
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 62eb3ee

Please sign in to comment.