Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Actors can now perform tasks conditionally
Use the Unless class static methods and a bolean expression, e.g. ``` Unless.the(items.isEmpty(), AddTodoItems.called(items)) ``` or use a question of type Question<Boolean>: ``` Unless.the(itemsListisEmpty(), AddTodoItems.called(items)) ```
- Loading branch information
Showing
7 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
serenity-journey/src/main/java/net/serenitybdd/screenplay/ConditionalPerformable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package net.serenitybdd.screenplay; | ||
|
||
public class ConditionalPerformable implements Performable { | ||
private final Question<Boolean> shouldNotPerform; | ||
private final Performable task; | ||
|
||
public ConditionalPerformable(Question<Boolean> shouldNotPerform, Performable task) { | ||
this.shouldNotPerform = shouldNotPerform; | ||
this.task = task; | ||
} | ||
|
||
@Override | ||
public <T extends Actor> void performAs(T actor) { | ||
if (shouldNotPerform.answeredBy(actor)) { return; } | ||
|
||
task.performAs(actor); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
serenity-journey/src/main/java/net/serenitybdd/screenplay/SilentPerformable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package net.serenitybdd.screenplay; | ||
|
||
public class SilentPerformable implements Performable { | ||
@Override | ||
public <T extends Actor> void performAs(T actor) {} | ||
} |
22 changes: 22 additions & 0 deletions
22
serenity-journey/src/main/java/net/serenitybdd/screenplay/Unless.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package net.serenitybdd.screenplay; | ||
|
||
public class Unless { | ||
public static Performable the(Boolean shouldNotPerform, Performable task) { | ||
return (shouldNotPerform) ? new SilentPerformable() : task; | ||
} | ||
|
||
public static Performable the(Question<Boolean> shouldNotPerform, Performable task) { | ||
return new ConditionalPerformable(shouldNotPerform, task); | ||
} | ||
|
||
// Synonyms | ||
|
||
public static Performable it(Question<Boolean> shouldNotPerform, Performable task) { | ||
return new ConditionalPerformable(shouldNotPerform, task); | ||
} | ||
|
||
public static Performable we(Boolean shouldNotPerform, Performable task) { | ||
return the(shouldNotPerform, task); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...-journey/src/test/java/net/serenitybdd/journey/shopping/WhenTestsAreRunConditionally.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package net.serenitybdd.journey.shopping; | ||
|
||
import net.serenitybdd.journey.shopping.tasks.Purchase; | ||
import net.serenitybdd.junit.runners.SerenityRunner; | ||
import net.serenitybdd.screenplay.Actor; | ||
import net.serenitybdd.screenplay.Question; | ||
import net.serenitybdd.screenplay.Unless; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import static net.serenitybdd.journey.shopping.tasks.Purchase.purchase; | ||
import static net.serenitybdd.screenplay.GivenWhenThen.when; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@RunWith(SerenityRunner.class) | ||
public class WhenTestsAreRunConditionally { | ||
|
||
Actor dana = Actor.named("Dana"); | ||
|
||
@Test | ||
public void shouldBeAbleToBuySomethingConditionally() { | ||
int cost = 10; | ||
|
||
Purchase purchase = purchase(); | ||
|
||
purchase.theItemWasPurchased = false; | ||
when(dana).attemptsTo(Unless.the(cost < 100, purchase.aPear().thatCosts(5).dollars())); | ||
|
||
assertThat(purchase.theItemWasPurchased).isFalse(); | ||
} | ||
|
||
@Test | ||
public void shouldBeAbleToNotBuySomethingConditionally() { | ||
int cost = 10; | ||
Purchase purchase = purchase(); | ||
|
||
purchase.theItemWasPurchased = false; | ||
when(dana).attemptsTo(Unless.the(cost > 100, purchase.aPear().thatCosts(5).dollars())); | ||
|
||
assertThat(purchase.theItemWasPurchased).isTrue(); | ||
} | ||
|
||
private class IsExpensive implements Question<Boolean> { | ||
|
||
private final int cost; | ||
|
||
public IsExpensive(int cost) { | ||
this.cost = cost; | ||
} | ||
|
||
@Override | ||
public Boolean answeredBy(Actor actor) { | ||
return (cost > 10); | ||
} | ||
} | ||
|
||
|
||
@Test | ||
public void shouldBeAbleToBuySomethingConditionallyUsingAQuestion() { | ||
int cost = 5; | ||
Purchase purchase = purchase(); | ||
IsExpensive isTooExpensive = new IsExpensive(cost); | ||
|
||
purchase.theItemWasPurchased = false; | ||
when(dana).attemptsTo(Unless.it(isTooExpensive, purchase.aPear().thatCosts(5).dollars())); | ||
|
||
assertThat(purchase.theItemWasPurchased).isTrue(); | ||
} | ||
|
||
@Test | ||
public void shouldBeAbleToNotBuySomethingConditionallyUsingAQuestion() { | ||
int cost = 20; | ||
Purchase purchase = purchase(); | ||
IsExpensive isTooExpensive = new IsExpensive(cost); | ||
|
||
purchase.theItemWasPurchased = false; | ||
when(dana).attemptsTo(Unless.it(isTooExpensive, purchase.aPear().thatCosts(5).dollars())); | ||
|
||
assertThat(purchase.theItemWasPurchased).isFalse(); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters