---
title: "3.6 Testing it"
url: https://una-lang.org/en/learn/text-and-conditions/testing-it/
release: v0.4.0
edition: "0.2"
---

# 3.6 Testing it

## 3.6.1 Examples for each rule

Every function of the program but `main` gives back a value and prints nothing, so each one can have examples, in the form of [section 1.7](https://una-lang.org/en/learn/hello/testing-it/#s1-7): `do` a call, then `expect` what must hold. The examples aim at the edges, where values go wrong:

```una include="day-to-day/src/labels.una#L63-L81" title="day-to-day/src/labels.una" caption="the first five examples"
example "a SKU loses its spaces and is written in capitals"
  do clean_sku("  lamp-01 ")
  expect result == "LAMP-01"

example "a SKU with a dash inside it looks right"
  do valid_sku("RUG-07")
  expect result

example "a SKU without a dash does not"
  do valid_sku("MUG 4")
  expect not result

example "a SKU that ends with its dash does not either"
  do valid_sku("LAMP-")
  expect not result

example "a long name is cut to 24 characters"
  do fit_name("Wool rug, 120 by 180 cm, natural")
  expect result.len == 24
```

- "a SKU loses its spaces and is written in capitals" gives `clean_sku` a value with spaces at both ends.
- The three examples of `valid_sku` cover a right SKU and two wrong ones, one for each part of the rule.
- "a long name is cut to 24 characters" checks the length of the result, `result.len`, not its text: the width is what matters.

The other examples cover the rest: a price whose cents are under ten, a discount over 90 percent, the rounding of a sale, a product not sold by weight, a sale's price text, an empty shelf, and a label for a SKU that does not look right.

## 3.6.2 `expect` takes a condition

The line after `expect` is a condition, a `Bool`, and the example passes when it is `true`. `expect result == "LAMP-01"` is a comparison, as in [chapter 1](https://una-lang.org/en/learn/hello/testing-it/#s1-7). When the function itself gives a `Bool`, the result is already the condition: `expect result` says that `valid_sku("RUG-07")` must be `true`, and `expect not result` that `valid_sku("MUG 4")` must be `false`. Write them this way, not `expect result == true`, as you write `domestic` and not `domestic == true`.

## 3.6.3 Running the examples

In the `day-to-day` directory, `una test .` prints:

```text include="day-to-day/expected/commands/test.txt#L2-L16"
example labels "a SKU loses its spaces and is written in capitals" ... ok
example labels "a SKU with a dash inside it looks right" ... ok
example labels "a SKU without a dash does not" ... ok
example labels "a SKU that ends with its dash does not either" ... ok
example labels "a long name is cut to 24 characters" ... ok
example labels "a short name is kept as it is" ... ok
example labels "an amount keeps the zero of its cents" ... ok
example labels "a discount over 90 percent is not a sale" ... ok
example labels "a discount rounds down to a whole cent" ... ok
example labels "a product not sold by weight has no price per kilogram" ... ok
example labels "a sale shows the old and the new price" ... ok
example labels "an empty shelf is sold out" ... ok
example labels "a SKU that does not look right asks for a check" ... ok

13 examples: 13 passed, 0 failed
```
