Super simple Gherkin features to ExUnit tests translator.
Inspired by Cabbage, but with slightly different API and few ideas of my own to simplify working with the library.
The package can be installed by adding watermelon to your list of dependencies
in mix.exs:
defdepsdo[{:watermelon,"~> 0.1.0",only: [:test]}]endThe docs can be found at https://hexdocs.pm/watermelon.
Define file tests/feature/coffe.feature:
Feature: Serve coffee
Coffee should not be served until paid for
Coffee should not be served until the button has been pressed
If there is no coffee left then money should be refunded
Scenario: Buy last coffeeGiven there are 1 coffees left in the machine
And I have deposited £1
When I press the coffee button
Then I should be served a coffee
Scenario Outline: Coffee countGiven there are <start> coffees left in the machine
And I have deposited £<money>When I press the coffee button <orders> times
Then I should have <left> coffees
Examples:
| start | money | orders | left | | 12 | 5 | 5 | 7 | | 20 | 5 | 5 | 15 |In your test module:
defmoduleMyTestdouseExUnit.CaseuseWatermelon.Casefeature_file("coffee.feature")defgivenmatch(number)when"there are {int} coffee(s) left in the machine"do{:ok,%{machine: Machine.put_coffee(Machine.new(),number)}}enddefgivenmatch(number)when"I have deposited £{int}",context: %{machine: machine}do{:ok,%{machine: Machine.deposit(machine,nil,number)}}enddefwhenmatchwhen"I press the coffee button",context: %{machine: machine}doassert{:ok,machine}=Machine.press_coffee(machine){:ok,machine: machine}enddefwhenmatch(n)when"I press the coffee button {int} times",context: %{machine: machine}domachine=for_<-1..n,reduce: machinedomachine->assert{:ok,machine}=Machine.press_coffee(machine)machineend{:ok,machine: machine}enddefthenmatchwhen"I should be served a coffee",context: statedoassert{:coffee,_}=Machine.take_drink(state.machine):okenddefthenmatch(num)when"I should have {int} coffee(s)",context: %{machine: machine}doassertmachine.coffees==num:okendendMozilla Public License 2.0, see LICENSE.