Home / Learn / 7 Results and failures

Release v0.4.0Edition 0.2

7.8 Testing it

Page 9 of 12

7.8.1 One example per path

Each way accept can end is a path: it gives back an order, or it fails with one of the variants of OrderError. Every path gets an example, and since every function here is pure, each example is one call and one expect line, with nothing to set up. Each example states one fact, so when one fails, una test . names it and says which expect line did not hold, as the exercises show.

7.8.2 The accepted line: expect result

day-to-day/src/orders.unathe accepted linesuna
example "a line in stock is accepted"
  do accept("LAMP-01, 2")
  expect result == Order(sku: "LAMP-01", quantity: 2)

example "the spaces around the SKU and the quantity do not matter"
  do parse_order(" DESK-02 , 1 ")
  expect result == Order(sku: "DESK-02", quantity: 1)

expect result == Order(…) compares the whole order that the call gave back with the one it should be: two records are equal when every field is (section 4.1). The second example calls parse_order alone, to show that the spaces around both parts are trimmed.

7.8.3 The failure paths: expect fails

day-to-day/src/orders.unathe failure pathsuna
example "a line without a comma is Malformed"
  do accept("LAMP-01")
  expect fails Malformed

example "a quantity that is not a number is BadQuantity"
  do accept("LAMP-01, two")
  expect fails BadQuantity

example "a quantity of 0 is BadQuantity"
  do accept("LAMP-01, 0")
  expect fails BadQuantity

example "a SKU the stock list does not know is UnknownSku"
  do accept("CHAIR-09, 1")
  expect fails UnknownSku

example "a shelf being counted is Unavailable"
  do accept("RUG-07, 1")
  expect fails Unavailable

example "too many units are out of stock"
  do accept("LAMP-01, 9")
  expect fails OutOfStock

expect fails OutOfStock holds when the do call failed with that variant, and the example fails when the call returned instead. Section 14.2 covers expect fails in full. There is one example for each variant of OrderError, and two for BadQuantity, since a quantity can be wrong in two ways: not a number, or below 1. A do line needs no handling clause: the example itself catches the failure.

7.8.4 The report line

day-to-day/src/orders.unathe reportuna
example "the report says how many units are left"
  do report("DESK-02, 3")
  expect result == "reject  3 of DESK-02 wanted, 1 left"

One example checks the whole text of a report line, so that a change to a message, or to the fields it uses, shows up.

7.8.5 Running the examples

una test . prints:

Output
example orders "a line in stock is accepted" ... ok
example orders "the spaces around the SKU and the quantity do not matter" ... ok
example orders "a line without a comma is Malformed" ... ok
example orders "a quantity that is not a number is BadQuantity" ... ok
example orders "a quantity of 0 is BadQuantity" ... ok
example orders "a SKU the stock list does not know is UnknownSku" ... ok
example orders "a shelf being counted is Unavailable" ... ok
example orders "too many units are out of stock" ... ok
example orders "the report says how many units are left" ... ok

9 examples: 9 passed, 0 failed

Next:7.9 Common mistakes