---
title: "5.7 Testing it"
url: https://una-lang.org/en/learn/contracts/testing-it/
release: v0.4.0
edition: "0.2"
---

# 5.7 Testing it

## 5.7.1 What the examples pin down

A proof covers every input, so why write examples? Because a contract states only what you wrote in it. `discount` promises `result <= amount`, and a body that always returns 0 keeps that promise too. The examples pin down the values the business expects: 10% of 188.85 is 18.88, rounded down; credit pays before the card; a refund of more than was paid is refused, in these words.

```una include="day-to-day/src/billing.una#L117-L148" title="day-to-day/src/billing.una" caption="the examples"
example "two lines add up"
  do subtotal(Line(sku: "LAMP-01", unit_cents: 1990, quantity: 2), second: Line(sku: "DESK-02", unit_cents: 14900, quantity: 1))
  expect result == 18880

example "a coupon worth more than the invoice takes the whole invoice"
  do discount(1000, coupon: CentsOff(amount: 1500))
  expect result == 1000

example "ten percent is rounded down"
  do discount(18885, coupon: PercentOff(rate: 10))
  expect result == 1888

example "store credit pays first"
  do pay(16992, credit: 5000)
  expect result.credit == 5000
  expect result.card == 11992

example "credit larger than the total leaves nothing for the card"
  do pay(1990, credit: 5000)
  expect result.card == 0

example "a refund within what was paid pays back what was asked"
  do refund(16992, asked: 3980)
  expect result == 3980

example "a refund of more than was paid is refused"
  do refund_line(16992, asked: 20000)
  expect result == "refund 200.00: refused, only 169.92 was paid"

example "an amount below 10 cents keeps its zero"
  do money(1905)
  expect result == "19.05"
```

Each example is written as in [chapter 1](https://una-lang.org/en/learn/hello/testing-it/#s1-7): `do` makes one call, and `expect` states what must hold of `result`. An example may have several `expect` lines, as "store credit pays first" does, and every one must hold. [Chapter 14](https://una-lang.org/en/learn/testing/examples-do-and-expect/#s14-1) covers examples in full.

## 5.7.2 Examples are checked like code

The examples are verified like the rest of the code: a `do` line that calls `pay` with `credit: -5`, or `discount` with a rate of 110, does not check, since the argument does not fit its kind. An example cannot test a case that the contracts rule out, and it needs no such test, since the verifier has proven that no code can make that call.

## 5.7.3 Examples and proofs together

Two habits make examples and proofs work together:

- **Turn each counterexample into an example**, as in [section 5.2](https://una-lang.org/en/learn/contracts/postconditions-ensures-and-counterexamples/#s5-2). The case that broke the code once is the case a later change is most likely to break again.
- **Test the exact values; prove the bounds.** A contract states a range or a relation that holds for every input. An example states one exact answer that a person can check by hand.

## 5.7.4 Running the examples

`una test .` prints:

```text include="day-to-day/expected/commands/test.txt#L2-L11"
example billing "two lines add up" ... ok
example billing "a coupon worth more than the invoice takes the whole invoice" ... ok
example billing "ten percent is rounded down" ... ok
example billing "store credit pays first" ... ok
example billing "credit larger than the total leaves nothing for the card" ... ok
example billing "a refund within what was paid pays back what was asked" ... ok
example billing "a refund of more than was paid is refused" ... ok
example billing "an amount below 10 cents keeps its zero" ... ok

8 examples: 8 passed, 0 failed
```
