Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method $ to StageBase for creating ad-hoc steps using lambdas #66

Merged
merged 1 commit into from
Mar 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}