Home / Learn / 8 Choosing

Release v0.4.0Edition 0.2

8.6 Testing it

Page 7 of 10

8.6.1 The rules the run does not reach

Each example builds the values it needs and checks one rule. The run in main shows the common path; the examples cover the edges it does not reach: a heavy parcel abroad, a return before any delivery, and each case of refund.

day-to-day/src/tracking.unathe examplesuna
example "a heavy parcel abroad pays the zone and the weight"
  do postage(Parcel(order: "A-1003", grams: 2500, zone: Abroad))
  expect result == 1600

example "a second pickup scan changes nothing"
  do latest(After(event: PickedUp(carrier: Post, tracking: "PO-1"), earlier: After(event: PickedUp(carrier: Post, tracking: "PO-1"), earlier: Start)))
  expect not changed(result)

example "a parcel cannot come back before it was delivered"
  do status_of(After(event: CameBack(reason: Damaged, day: 2), earlier: Start))
  expect result is Packed

example "a damaged parcel is refunded in full"
  do refund(Returned(reason: Damaged, day: 5), paid: 4500)
  expect result == 4500

example "a refused parcel is refunded less the return postage"
  do refund(Returned(reason: Refused, day: 5), paid: 601)
  expect result == 1

example "a refund never goes below 0"
  do refund(Returned(reason: Refused, day: 5), paid: 400)
  expect result == 0

example "a delivered parcel is not refunded"
  do refund(Delivered(day: 3), paid: 4500)
  expect result == 0

As in section 1.7, do calls the code under test, result is what it gave, and each expect line states something that must hold. expect result is Packed checks the variant without its fields, and expect not changed(result) calls a function of the program on the result: any Bool works. Chapter 14 covers examples in full.

8.6.2 The edges of a guard

A guard has an edge, and each side of it deserves an example. "a refused parcel is refunded less the return postage" pays 601 cents, the smallest amount that takes the then branch, and gets 1 back. "a refund never goes below 0" pays 400, which takes the else branch. The verifier has already proven that the result is never negative; the examples show which number each side gives.

8.6.3 One example per rule that an else decides

An arm per variant is checked by the checker: forget one, and the program does not check. An else is not: whatever it answers, it checks. So the rules that an else decides need examples of their own. "a delivered parcel is not refunded" tests the else of refund, and "a parcel cannot come back before it was delivered" tests the else status of apply.

8.6.4 Running the examples

una test . prints:

Output
example tracking "a heavy parcel abroad pays the zone and the weight" ... ok
example tracking "a second pickup scan changes nothing" ... ok
example tracking "a parcel cannot come back before it was delivered" ... ok
example tracking "a damaged parcel is refunded in full" ... ok
example tracking "a refused parcel is refunded less the return postage" ... ok
example tracking "a refund never goes below 0" ... ok
example tracking "a delivered parcel is not refunded" ... ok

7 examples: 7 passed, 0 failed

When an example fails, una test . names it and shows the value result had, as Exercise 1 shows.

Next:8.7 Common mistakes