-
Notifications
You must be signed in to change notification settings - Fork 74
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
F!! Printable scenario - to better support BDD #460
Open
emilybache
wants to merge
9
commits into
master
Choose a base branch
from
printable_scenario
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
31c3825
Added PrintableScenario class for creating BDD scenarios with Approvals.
emilybache 9426697
a reformat code
actions-user 2e7293d
Actually have the User class do something
emilybache bce1638
New checked exception in the PrintableScenario class.
emilybache a308a2c
Improve docs with snippet
emilybache 5ff570e
Better support if an exception is thrown by the system under test
emilybache 86286b8
tweak docs
emilybache 29c0d9d
PrintableScenario no longer throws a checked exception
emilybache d85214b
fix formatting
emilybache File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
...tests/src/test/java/org/approvaltests/CheckedExceptionsTest.testTheVerifyApi.approved.txt
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
110 changes: 110 additions & 0 deletions
110
approvaltests-tests/src/test/java/org/approvaltests/bdd/PrintableScenarioTest.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,110 @@ | ||
package org.approvaltests.bdd; | ||
|
||
import org.approvaltests.Approvals; | ||
import org.approvaltests.strings.Printable; | ||
import org.approvaltests.strings.PrintableScenario; | ||
import org.junit.Test; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
public class PrintableScenarioTest | ||
{ | ||
@Test | ||
public void printBDDScenario() throws Exception | ||
{ | ||
PrintableScenario story = new PrintableScenario("Buy Groceries", "Bob puts two items in the shopping cart"); | ||
User currentUser = new User(); | ||
ShoppingCart shoppingCart = new ShoppingCart(currentUser); | ||
story.given(new Printable<>(currentUser, UserPrinter::print), | ||
new Printable<>(shoppingCart, ShoppingCartPrinter::print)); | ||
story.when("Add oranges to the cart", () -> { | ||
shoppingCart.add("Oranges", 1, BigDecimal.valueOf(5)); | ||
return null; | ||
}); | ||
story.when("Add apples to the cart", () -> { | ||
shoppingCart.add("Apples", 3, BigDecimal.valueOf(4)); | ||
return null; | ||
}); | ||
Approvals.verify(story.then()); | ||
} | ||
} | ||
|
||
class User | ||
{ | ||
public String name = "Bob"; | ||
} | ||
|
||
class Item | ||
{ | ||
public String name; | ||
public Integer quantity; | ||
public BigDecimal price; | ||
public BigDecimal amount() | ||
{ | ||
return price.multiply(BigDecimal.valueOf(quantity)); | ||
} | ||
public Item(String itemName, int quantity, BigDecimal price) | ||
{ | ||
this.name = itemName; | ||
this.quantity = quantity; | ||
this.price = price; | ||
} | ||
} | ||
|
||
class ShoppingCart | ||
{ | ||
private final User user; | ||
public List<Item> articles = new ArrayList<>(); | ||
public ShoppingCart(User currentUser) | ||
{ | ||
this.user = currentUser; | ||
} | ||
public BigDecimal subtotal() | ||
{ | ||
return articles.stream().map(Item::amount).reduce(BigDecimal.ZERO, BigDecimal::add); | ||
} | ||
public BigDecimal shipping() | ||
{ | ||
if (Objects.equals(user.name, "Bob")) | ||
{ return BigDecimal.ONE; } | ||
return BigDecimal.ZERO; | ||
} | ||
public BigDecimal total() | ||
{ | ||
return this.subtotal().add(this.shipping()); | ||
} | ||
public void add(String itemName, int quantity, BigDecimal price) | ||
{ | ||
this.articles.add(new Item(itemName, quantity, price)); | ||
} | ||
} | ||
|
||
class ShoppingCartPrinter | ||
{ | ||
public static String print(ShoppingCart shoppingCart) | ||
{ | ||
StringBuilder result = new StringBuilder(); | ||
result.append("ShoppingCart:\n"); | ||
result.append("Articles:\n"); | ||
result.append(shoppingCart.articles.stream() | ||
.map(article -> " " + article.name + " price: " + article.price + " quantity: " + article.quantity) | ||
.collect(Collectors.joining("\n"))); | ||
result.append("\n"); | ||
result.append("Subtotal:" + shoppingCart.subtotal() + "\n"); | ||
result.append("Shipping:" + shoppingCart.shipping() + "\n"); | ||
result.append("Total:" + shoppingCart.total() + "\n"); | ||
return result.toString(); | ||
} | ||
} | ||
|
||
class UserPrinter | ||
{ | ||
public static String print(User user) | ||
{ | ||
return "User: " + user.name + "\n"; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...s/src/test/java/org/approvaltests/bdd/PrintableScenarioTest.printBDDScenario.approved.txt
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,38 @@ | ||
Buy Groceries | ||
============= | ||
Bob puts two items in the shopping cart | ||
|
||
User: Bob | ||
|
||
ShoppingCart: | ||
Articles: | ||
|
||
Subtotal:0 | ||
Shipping:1 | ||
Total:1 | ||
|
||
Add oranges to the cart | ||
----------------------- | ||
|
||
User: Bob | ||
|
||
ShoppingCart: | ||
Articles: | ||
Oranges price: 5 quantity: 1 | ||
Subtotal:5 | ||
Shipping:1 | ||
Total:6 | ||
|
||
Add apples to the cart | ||
---------------------- | ||
|
||
User: Bob | ||
|
||
ShoppingCart: | ||
Articles: | ||
Oranges price: 5 quantity: 1 | ||
Apples price: 4 quantity: 3 | ||
Subtotal:17 | ||
Shipping:1 | ||
Total:18 | ||
|
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,27 @@ | ||
<a id="top"></a> | ||
|
||
# How to make a BDD Scenario using Approvals | ||
<!-- toc --> | ||
## Contents | ||
|
||
* [Introduction](#introduction) | ||
* [Sample Code](#sample-code)<!-- endToc --> | ||
|
||
## Introduction | ||
The idea is that you will end up with a descriptive approved file that reads like a BDD Scenario. You can show this to your less-technical collaborators, and they should be able to understand what scenario is being tested without needing to read the test sourcecode. | ||
|
||
It doesn't use Gherkin strictly, but you still get a scenario with a name, description, and Given, When, Then steps. | ||
|
||
## Sample Code | ||
|
||
I need some help with this part of the documentation. I'd like to include PrintableScenarioTest here as an example. | ||
<!-- snippet: bdd_test --> | ||
|
||
<!-- endSnippet --> | ||
|
||
* Construct your scenario at the beginning of the test with a descriptive name and summary. | ||
* When you are done with the code for the "given" step of the test, call story.given() with your list of Printables. Use one printable to wrap each object you have created which you think might change state during the test and that you want to verify. | ||
* In each "When" step of your test, call when() with a descriptive name for what the user is doing. The second argument is a lambda that contains code to actually do the when step. This is optional, you can write it in your test as normal code before the call to 'when' if you prefer. | ||
* The "Then" step is a call to then() which will verify the state of all the Printables as it changed throughout the course of the test. | ||
|
||
Note - if you don't want to use the BDD terminology you don't have to. You can equally well use 'arrange', 'act', 'print' instead of 'given' 'when' 'then' |
73 changes: 73 additions & 0 deletions
73
approvaltests/src/main/java/org/approvaltests/strings/PrintableScenario.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,73 @@ | ||
package org.approvaltests.strings; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.concurrent.Callable; | ||
|
||
public class PrintableScenario | ||
{ | ||
private final StringBuilder toVerify = new StringBuilder(); | ||
private final String name; | ||
private final String description; | ||
private final ArrayList<Printable> printables = new ArrayList<>(); | ||
public PrintableScenario(String name, String description) | ||
{ | ||
this.name = name; | ||
this.description = description; | ||
} | ||
public PrintableScenario(String name) | ||
{ | ||
this(name, ""); | ||
} | ||
public void given(Printable... printables) | ||
{ | ||
arrange(printables); | ||
} | ||
public void arrange(Printable... printables) | ||
{ | ||
this.printables.addAll(Arrays.asList(printables)); | ||
toVerify.append(makeHeading(name, description, "=")); | ||
toVerify.append("\n"); | ||
toVerify.append(printAll()); | ||
} | ||
public String makeHeading(String heading, String summary, String underlineCharacter) | ||
{ | ||
int count = heading.length(); | ||
// create a string made up of n copies of string "=" | ||
String underlineHeading = String.join("", Collections.nCopies(count, underlineCharacter)); | ||
return heading + "\n" + underlineHeading + "\n" + summary + "\n"; | ||
} | ||
public String printAll() | ||
{ | ||
StringBuilder result = new StringBuilder(); | ||
for (Printable printable : this.printables) | ||
{ | ||
result.append(printable.toString()); | ||
result.append("\n"); | ||
} | ||
return result.toString(); | ||
} | ||
public void when(String action, Callable function) throws Exception | ||
{ | ||
function.call(); | ||
when(action); | ||
} | ||
public void when(String action) | ||
{ | ||
act(action); | ||
} | ||
public void act(String action) | ||
{ | ||
toVerify.append(makeHeading(action, "", "-")); | ||
toVerify.append(printAll()); | ||
} | ||
public String then() | ||
{ | ||
return print(); | ||
} | ||
public String print() | ||
{ | ||
return toVerify.toString(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think what you need is the following. These comments in the java file:
and then you can reference it here in markdown with
snippet: MySnippetName