Skip to content

Commit

Permalink
fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
emilybache committed Feb 1, 2024
1 parent 29c0d9d commit d85214b
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,21 @@ public void printBDDScenario() throws Exception
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)));
story.when("Add apples to the cart", () -> shoppingCart.add("Apples", 3, BigDecimal.valueOf(4)));
story.when("Add apples to the cart", () -> shoppingCart.add("Apples", 3, BigDecimal.valueOf(4)));
Approvals.verify(story);
}
// end-snippet

@Test
public void bddScenarioThatThrows() throws Exception
{
PrintableScenario story = new PrintableScenario("Shoplifting", "We expect an exception");
User currentUser = new User();
ShoppingCart shoppingCart = new ShoppingCart(currentUser);
story.given(new Printable<>(currentUser, UserPrinter::print),
new Printable<>(shoppingCart, ShoppingCartPrinter::print));
story.when("Something illegal", () -> {throw new RuntimeException("shoplifting");});
new Printable<>(shoppingCart, ShoppingCartPrinter::print));
story.when("Something illegal", () -> {
throw new RuntimeException("shoplifting");
});
Approvals.verify(story);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,82 +6,86 @@
import java.util.Arrays;
import java.util.Collections;

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 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(printAll());
}
public String makeHeading(String heading, String summary, String underlineCharacter)
{
int count = heading.length();
// create a string made up of n copies of underlineCharacter
String underlineHeading = String.join("", Collections.nCopies(count, underlineCharacter));
String result = heading + "\n" + underlineHeading + "\n";
if (summary != null && !summary.isEmpty())
{
result += summary + "\n\n";
}

public void given(Printable... printables) {
arrange(printables);
return result;
}
public String printAll()
{
StringBuilder result = new StringBuilder();
for (Printable printable : this.printables)
{
result.append(printable.toString());
result.append("\n");
}

public void arrange(Printable... printables) {
this.printables.addAll(Arrays.asList(printables));
toVerify.append(makeHeading(name, description, "="));
toVerify.append(printAll());
return result.toString();
}
public void when(String action, Action0WithException function)
{
try
{
function.call();
when(action);
}

public String makeHeading(String heading, String summary, String underlineCharacter) {
int count = heading.length();
// create a string made up of n copies of underlineCharacter
String underlineHeading = String.join("", Collections.nCopies(count, underlineCharacter));

String result = heading + "\n" + underlineHeading + "\n";
if (summary != null && !summary.isEmpty()) {
result += summary + "\n\n";
}
return result;
}

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, Action0WithException function) {
try {
function.call();
when(action);
} catch (Throwable e) {
toVerify.append(makeHeading(action,
"This action threw an exception: " + e, "-"));
toVerify.append(printAll());
}
}

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();
}

@Override
public String toString() {
return print();
catch (Throwable e)
{
toVerify.append(makeHeading(action, "This action threw an exception: " + e, "-"));
toVerify.append(printAll());
}
}
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();
}
@Override
public String toString()
{
return print();
}
}

0 comments on commit d85214b

Please sign in to comment.