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

# 9.5 Testing it

## 9.5.1 The single steps and their edges

The functions with no loop get an example for each side of their choice:

```una include="day-to-day/src/restock.una#L126-L140" title="day-to-day/src/restock.una" caption="selling and ordering"
example "a week sells its sales from the stock"
  do sell_week(9, sales: 5)
  expect result == 4

example "a shelf cannot sell more than it holds"
  do sell_week(3, sales: 5)
  expect result == 0

example "a stock below the reorder point orders a batch"
  do order_for(lamp(), stock: 4)
  expect result == 10

example "a stock at the reorder point orders nothing"
  do order_for(lamp(), stock: 6)
  expect result == 0
```

`sell_week` is tested where the stock is enough and where it is not. `order_for` is tested just below the reorder point and exactly at it, where nothing is ordered: the edge where a `<` written as `<=` would be caught.

## 9.5.2 The loops, with no weeks and with many

A loop gets an example where it runs no turn, and examples where it runs several:

```una include="day-to-day/src/restock.una#L142-L160" title="day-to-day/src/restock.una" caption="the loops"
example "a plan of no weeks leaves the stock as it is"
  do stock_after(lamp(), weeks: 0)
  expect result == 14

example "the lamp ends four weeks with 14 units"
  do stock_after(lamp(), weeks: 4)
  expect result == 14

example "the lamp orders two batches in four weeks"
  do units_ordered(lamp(), weeks: 4)
  expect result == 20

example "the lamp is never short"
  do weeks_short(lamp(), weeks: 4)
  expect result == 0

example "the desk is short every week"
  do weeks_short(desk(), weeks: 4)
  expect result == 4
```

The plan of 0 weeks checks the first value of the accumulator, which is the whole answer when the range is empty. The others pin the results of four weeks. The verifier proves that each result is a `Units`; it does not prove that the lamp ends with 14 units, since that depends on the number of turns. An example states it. `department` gets one example with a dash and one without, so the flag is tested both ways.

## 9.5.3 Running the examples

In the `day-to-day` directory, `una test .` checks the package, then runs the examples:

```text include="day-to-day/expected/commands/test.txt#L2-L15"
example restock "a department is the part of the SKU before its dash" ... ok
example restock "a SKU without a dash is its own department" ... ok
example restock "a week sells its sales from the stock" ... ok
example restock "a shelf cannot sell more than it holds" ... ok
example restock "a stock below the reorder point orders a batch" ... ok
example restock "a stock at the reorder point orders nothing" ... ok
example restock "a plan of no weeks leaves the stock as it is" ... ok
example restock "the lamp ends four weeks with 14 units" ... ok
example restock "the lamp orders two batches in four weeks" ... ok
example restock "the lamp is never short" ... ok
example restock "the desk is short every week" ... ok
example restock "a week with an order says how many units" ... ok

12 examples: 12 passed, 0 failed
```

Each example names what it shows, so a failing line of the report says which rule broke. The examples leave out `print_plan` and `main`, which print. They print only what the tested functions give back, and an example of a function that prints needs a stand-in for the console, which [section 14.3](https://una-lang.org/en/learn/testing/fakes-given-expect-performed-and-expect-not-performed/#s14-3) shows.
