Skip to content

Commit

Permalink
Merge pull request #66 from TNG/feature-adoc-steps
Browse files Browse the repository at this point in the history
Add method $ to StageBase for creating ad-hoc steps using lambdas
  • Loading branch information
janschaefer committed Mar 16, 2015
2 parents 22fdb05 + b4cd6af commit 48cd87c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions jgiven-core/src/main/java/com/tngtech/jgiven/Stage.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ public SELF with() {
public SELF but() {
return self();
}

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

/**
* A functional interface for defining ad-hoc steps.
*
* @see Stage#$(String, StepFunction)
* @param <STAGE> the stage in which this step is executed
*/
public interface StepFunction<STAGE> {

public void apply( STAGE stage ) throws Exception;
}
21 changes: 21 additions & 0 deletions jgiven-core/src/main/java/com/tngtech/jgiven/base/StageBase.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.tngtech.jgiven.base;

import com.tngtech.jgiven.StepFunction;
import com.tngtech.jgiven.annotation.Hidden;

/**
* Useful base class for step definitions as it provides a {@link #self()} method
* to create fluent interfaces.
Expand All @@ -20,4 +23,22 @@ public SELF self() {
return (SELF) this;
}

/**
* A step method for creating ad-hoc steps using lambdas.
*
* <h2>Example Usage</h2>
* <pre>
* given().$( "Two negative arguments", stage -> {
* stage.given().argument( -5 )
* .and().argument( -6 );
* });
* </pre>
*
* @param description the description of the step
* @param function the implementation of the step in form of a function where the parameter is the stage the step is executed in
*/
public SELF $( String description, @Hidden StepFunction<SELF> function ) throws Exception {
function.apply( self() );
return self();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public SELF the_reports_exist_as_JSON_files() throws IOException {
return self();
}

public void a_custom_CSS_file() throws IOException {
public SELF a_custom_CSS_file() throws IOException {
customCssFile = temporaryFolderRule.newFile( "custom.css" );
return self();
}
}

0 comments on commit 48cd87c

Please sign in to comment.