Skip to content

Commit

Permalink
Made example a bit powerful
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Deryabin committed Oct 11, 2019
1 parent a25ec93 commit d22ec48
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

import java.math.BigDecimal;
import java.util.Currency;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -27,9 +29,10 @@ public Grocery defineGrocery(Map<String, String> entry) {
return new Grocery(entry.get("name"), ShoppingSteps.Price.fromString(entry.get("price")));
}

@ParameterType(name = "price", value = "\\d+")
public Price definePrice(String value) {
return Price.fromString(value);
@ParameterType(name = "amount", value = "\\d+\\.\\d+\\s[a-zA-Z]+")
public Amount defineAmount(String value) {
String [] arr = value.split("\\s");
return new Amount(new BigDecimal(arr[0]), Currency.getInstance(arr[1]));
}

@DocStringType(contentType = "shopping_list")
Expand All @@ -45,9 +48,9 @@ public void the_following_groceries(List<Grocery> groceries) {
}
}

@When("I pay {price}")
public void i_pay(Price amount) {
calc.push(amount.value);
@When("I pay {amount}")
public void i_pay(Amount amount) {
calc.push(amount.price);
calc.push("-");
}

Expand Down Expand Up @@ -134,4 +137,14 @@ static Price fromString(String value) {
}

}

static final class Amount {
private final BigDecimal price;
private final Currency currency;

public Amount(BigDecimal price, Currency currency) {
this.price = price;
this.currency = currency;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Feature: Shopping
| milk | 9 |
| bread | 7 |
| soap | 5 |
When I pay 25
When I pay 25.0 USD
Then my change should be 4

Scenario: Count shopping list cost
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import io.cucumber.datatable.DataTable;
import io.cucumber.java8.En;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Currency;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -28,8 +30,8 @@ public ShoppingSteps() {
}
});

When("I pay {price}", (Price price) -> {
calc.push(price.value);
When("I pay {amount}", (Amount amount) -> {
calc.push(amount.price);
calc.push("-");
});

Expand Down Expand Up @@ -60,7 +62,10 @@ public ShoppingSteps() {
ShoppingSteps.Price.fromString(row.get("price"))
));

ParameterType("price", "\\d+", Price::fromString);
ParameterType("amount", "\\d+\\.\\d+\\s[a-zA-Z]+", (String value) -> {
String [] arr = value.split("\\s");
return new Amount(new BigDecimal(arr[0]), Currency.getInstance(arr[1]));
});

DocStringType("shopping_list", (String docstring) -> {
return Stream.of(docstring.split("\\s"))
Expand Down Expand Up @@ -104,4 +109,14 @@ static Price fromString(String value) {
}

}

static final class Amount {
private final BigDecimal price;
private final Currency currency;

public Amount(BigDecimal price, Currency currency) {
this.price = price;
this.currency = currency;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Feature: Shopping
| milk | 9 |
| bread | 7 |
| soap | 5 |
When I pay 25
When I pay 25.0 USD
Then my change should be 4

Scenario: Count shopping list cost
Expand Down

0 comments on commit d22ec48

Please sign in to comment.