From b4cd6af1fdbd1ab84ea261ff680b17d069a922b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4fer?= Date: Mon, 9 Mar 2015 07:15:19 +0100 Subject: [PATCH] add method $ to StageBase for creating ad-hoc steps using lambdas --- .../main/java/com/tngtech/jgiven/Stage.java | 1 + .../java/com/tngtech/jgiven/StepFunction.java | 12 +++++++++++ .../com/tngtech/jgiven/base/StageBase.java | 21 +++++++++++++++++++ .../jgiven/report/json/GivenJsonReports.java | 3 ++- 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 jgiven-core/src/main/java/com/tngtech/jgiven/StepFunction.java diff --git a/jgiven-core/src/main/java/com/tngtech/jgiven/Stage.java b/jgiven-core/src/main/java/com/tngtech/jgiven/Stage.java index 837a4032a7..f35f5a9796 100644 --- a/jgiven-core/src/main/java/com/tngtech/jgiven/Stage.java +++ b/jgiven-core/src/main/java/com/tngtech/jgiven/Stage.java @@ -41,4 +41,5 @@ public SELF with() { public SELF but() { return self(); } + } \ No newline at end of file diff --git a/jgiven-core/src/main/java/com/tngtech/jgiven/StepFunction.java b/jgiven-core/src/main/java/com/tngtech/jgiven/StepFunction.java new file mode 100644 index 0000000000..123e1fa1b1 --- /dev/null +++ b/jgiven-core/src/main/java/com/tngtech/jgiven/StepFunction.java @@ -0,0 +1,12 @@ +package com.tngtech.jgiven; + +/** + * A functional interface for defining ad-hoc steps. + * + * @see Stage#$(String, StepFunction) + * @param the stage in which this step is executed + */ +public interface StepFunction { + + public void apply( STAGE stage ) throws Exception; +} diff --git a/jgiven-core/src/main/java/com/tngtech/jgiven/base/StageBase.java b/jgiven-core/src/main/java/com/tngtech/jgiven/base/StageBase.java index ebf9b6dd68..7e589ce502 100644 --- a/jgiven-core/src/main/java/com/tngtech/jgiven/base/StageBase.java +++ b/jgiven-core/src/main/java/com/tngtech/jgiven/base/StageBase.java @@ -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. @@ -20,4 +23,22 @@ public SELF self() { return (SELF) this; } + /** + * A step method for creating ad-hoc steps using lambdas. + * + *

Example Usage

+ *
+     *     given().$( "Two negative arguments", stage -> {
+     *        stage.given().argument( -5 )
+     *             .and().argument( -6 );
+     *     });
+     * 
+ * + * @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 function ) throws Exception { + function.apply( self() ); + return self(); + } } diff --git a/jgiven-tests/src/test/java/com/tngtech/jgiven/report/json/GivenJsonReports.java b/jgiven-tests/src/test/java/com/tngtech/jgiven/report/json/GivenJsonReports.java index 326a35a3b8..f200e6d99b 100644 --- a/jgiven-tests/src/test/java/com/tngtech/jgiven/report/json/GivenJsonReports.java +++ b/jgiven-tests/src/test/java/com/tngtech/jgiven/report/json/GivenJsonReports.java @@ -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(); } }