---
title: "13.5 Testing it"
url: https://una-lang.org/en/learn/authority/testing-it/
release: v0.4.0
edition: "0.2, 0.3"
---

# 13.5 Testing it

## 13.5.1 The price rule, at its edges

`sale_price` is pure, so its tests are the simple ones of [chapter 1](https://una-lang.org/en/learn/hello/testing-it/#s1-7), written `test` in edition `0.3`: a call and its result. They cover an ordinary discount, the two ends of `Percent`, and a discount that is not a whole number of cents:

```una include="day-to-day/src/store/sale.una#L54-L68" title="day-to-day/src/store/sale.una" caption="the price rule"
test "20 percent off a desk lamp"
  do sale_price(1990, percent: 20)
  expect result == 1592

test "0 percent off keeps the price"
  do sale_price(4500, percent: 0)
  expect result == 4500

test "100 percent off gives the item away"
  do sale_price(4500, percent: 100)
  expect result == 0

test "a discount that is not whole cents is rounded down"
  do sale_price(999, percent: 15)
  expect result == 850
```

## 13.5.2 The dry run, with a stand-in price list

`apply_sale` uses the price list, so a test of it needs a price list to call. `given @prices = ignored()` installs the handler `ignored`, which keeps nothing, for one test, as in [chapter 12](https://una-lang.org/en/learn/capabilities/testing-it/#s12-6). Both tests check the price that comes back, in a dry run and in a real one:

```una include="day-to-day/src/store/sale.una#L70-L78" title="day-to-day/src/store/sale.una" caption="a dry run and a real run"
test "a dry run gives the sale price"
  given @prices = ignored()
  do apply_sale(Item(sku: "LAMP-01", name: "Desk lamp", cents: 1990), percent: 20, dry_run: true)
  expect result == 1592

test "a real run gives the same sale price"
  given @prices = ignored()
  do apply_sale(Item(sku: "LAMP-01", name: "Desk lamp", cents: 1990), percent: 20, dry_run: false)
  expect result == 1592
```

The tests pin down the price itself, which no condition states: a dry run must report the same price that the real run sets.

## 13.5.3 What must not happen: proved, not tested

That a dry run sets no price is not a test's job here. The condition on `set_price` puts the rule in the header, and the verifier proves it on every path, for every item and every percent, before any test runs. If the guard goes, `una check .` reports U5001, as [Setting a price without the guard](https://una-lang.org/en/learn/authority/common-mistakes/#setting-a-price-without-the-guard-u5001) shows, and `una test .` fails with it. A test can also check which operations a call performed, a tool of [section 14.3](https://una-lang.org/en/learn/testing/fakes-given-expect-performed-and-expect-not-performed/#s14-3).

## 13.5.4 What the tests leave out: `run_sale` and the tags

`run_sale` prints, and its tests would need a stand-in for the console too ([chapter 14](https://una-lang.org/en/learn/testing/fakes-given-expect-performed-and-expect-not-performed/#s14-3)). A test outside the boundary also cannot call `print_tag`: a test's `do` line takes no `with`, so it can call into a boundary only a function with no `uses`. The text that a dry run prints for each item is the pure `planned_line`, which has its own test:

```una include="day-to-day/src/store/sale.una#L80-L82" title="day-to-day/src/store/sale.una" caption="the line of a dry run"
test "the line of a dry run names the old and the new price"
  do planned_line(Item(sku: "RUG-07", name: "Wool rug", cents: 4500), sale: 3600)
  expect result == "  RUG-07 would go from 4500 to 3600"
```

## 13.5.5 Running the tests

```text include="day-to-day/expected/commands/test.txt#L2-L10"
test store.sale "20 percent off a desk lamp" ... ok
test store.sale "0 percent off keeps the price" ... ok
test store.sale "100 percent off gives the item away" ... ok
test store.sale "a discount that is not whole cents is rounded down" ... ok
test store.sale "a dry run gives the sale price" ... ok
test store.sale "a real run gives the same sale price" ... ok
test store.sale "the line of a dry run names the old and the new price" ... ok

7 tests: 7 passed, 0 failed
```
