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

# 16.6 Testing it

## 16.6.1 Reaching `apply` through a copy

An example's `do` line cannot pass `edit`: it is not a statement, and it has no editable binding of its own. So the examples reach `apply` through a small private function, `applied`, which copies the ledger into a `let edit` binding, applies the movement and returns the copy. `lamps` builds the ledger the examples start from.

```una include="day-to-day/src/stockcount.una#L104-L132" title="day-to-day/src/stockcount.una" caption="the examples"
--- A north ledger that counts `units` of LAMP-01 and nothing else, for the examples.
lamps(units: Int) -> Ledger = Ledger(site: "north", counts: empty_map().with(key: "LAMP-01", value: units), applied: 0)

--- `apply` through a copy, since a `do` line cannot pass `edit`.
applied(ledger: Ledger, movement: Movement) -> Ledger
  fails CountError
=
  let edit copy = ledger
  let receipt = apply(edit copy, movement: movement)
    if error pass
  copy

example "a sale lowers the count of its SKU"
  do applied(lamps(4), movement: sale("LAMP-01", units: 1))
  expect result.counts.get(key: "LAMP-01") == Some(3)
  expect result.applied == 1

example "selling more than is counted is Oversold"
  do applied(lamps(4), movement: sale("LAMP-01", units: 9))
  expect fails Oversold

example "a rejected movement leaves the counts as they were"
  do close_day(Day(site: "south", opening: empty_map().with(key: "LAMP-01", value: 2), movements: [sale("LAMP-01", units: 5)]))
  expect result.closing == empty_map().with(key: "LAMP-01", value: 2)
  expect result.summary == "south: 0 applied, 1 rejected"

example "closing every day at once gives the reports in site order"
  do close_all([north_day(), south_day()])
  expect result == [close_day(north_day()), close_day(south_day())]
```

## 16.6.2 What each example checks

Each example checks one fact of the chapter:

- **"a sale lowers the count of its SKU"** checks `apply` through `applied`: the count and the number of applied movements.
- **"selling more than is counted is Oversold"** expects the failure with `expect fails Oversold`. `applied` passes the failure on with `if error pass`, so the example sees it.
- **"a rejected movement leaves the counts as they were"** closes a day whose only movement fails, and checks that the closing counts are the opening counts.
- **"closing every day at once gives the reports in site order"** compares `close_all` with closing the days one after another. This is the guarantee of `together for` ([section 15.3](https://una-lang.org/en/learn/concurrency/what-the-result-can-depend-on-failures-and-order/#s15-3)), stated as an example.

## 16.6.3 No fakes needed

None of the examples needs a fake service: the program has no capability but the console, and the console is used only in `main`. Every function under test takes its input as values and returns its result, which is what makes each one easy to check on its own.

## 16.6.4 Running the examples

`una test .` prints:

```text include="day-to-day/expected/commands/test.txt#L2-L7"
example stockcount "a sale lowers the count of its SKU" ... ok
example stockcount "selling more than is counted is Oversold" ... ok
example stockcount "a rejected movement leaves the counts as they were" ... ok
example stockcount "closing every day at once gives the reports in site order" ... ok

4 examples: 4 passed, 0 failed
```
