---
title: Tests
description: Examples, test elements, checked values and givens. How Stoic turns a spec into tests that report what they saw, and why every rule needs a witness.
kicker: Write
---

# Tests

A spec without tests is a wish. In Stoic the spec defines the tests, the implementer writes them in whatever language the project uses, and verify checks the run against the lock. There are two ways to define one: **examples** on a field, and **test elements**.

## Examples

`+"…"` on a field must be accepted and `-"…"` rejected. Each one becomes a test with a stable ID. A `^"…"` among the field's arguments says how each example is tried.

```stoic
<form:signup
  #username(
    ^"submit the form with value",
    >=3,
    <=20,
    !" ",
    +"ada",
    -"ad",
    -"a b",
    +"twenty_chars_exactly",
    -"twenty_one_characters",
    +=>="'ok'",
    -=>="'INVALID'"
  )
/>
```

Text and values are different things: `+"1"` is the text "1" and `+=1` is the number. The kind is part of the test.

## Test elements

Any element with expectations is a test. It's bound to what it tests (`=signup`), takes inputs as fields, has exactly one action, and says what must and must not happen.

```stoic
<form:signup #username(>=3) #email("email") @submit="create account" />
<test:ada_signs_up=signup
  #username="ada"
  #email="ada@acme.com"
  @submit
  =>"welcome email queued"
  =>account="'created'"
/>
```

## Checked values

This is the distinction the rest of Stoic is built on. An expectation like `=>"welcome email queued"` is guidance: verify only ever sees whether the test passed, so a test body that asserts the wrong thing still passes. Stoic says so wherever it prints one: *(not checked)*.

A **checked value**, `=>="…"` or `=>label="…"`, is different. The test reports what it observed (`pass fee600 fee = 53`), and verify compares that with the value in the spec, as a value:

```sh
WRONG    fee600 fee: expected 52, observed 53
```

So the test body no longer decides what's right. If someone edits it to agree with a bug, the observation still disagrees with the lock. A test element with nothing checked gets a warning, and `stoic verify --strict` makes it an error. One checked value is enough.

```stoic
<method:checkout @call="checkout(cart)" />
<test:spring_sale=checkout
  ^"SPRING20 applied to a 9000 cart"
  @call
  =>total="7200"
  =>"shows the discount line"
/>
```

## Givens

`^"…"` on a test is a step that has happened before its action. Use as many as it takes, in order. They're part of the test: change one and the test changed.

```stoic
<method:refund @call="refund(paymentId, amount)" />
<test:no_double_refund=refund
  ^"a payment of 5000"
  ^"refund(p, 5000) succeeded"
  @call="p, 1"
  =>="{ ok: false, error: 'ALREADY_REFUNDED' }"
/>
```

## Or-values

Sometimes a field takes one value its limits don't describe: `null`, `-1`, `"auto"`. Say it after `|`, and give it an example.

```stoic
<config:job #retries(>=1, <=5, |null, +=1, +=5, +=null, -=0, -=6, +=>="true", -=>="false") />
```

## No rule without a witness

A rule is enforced only if dropping it would fail a test. So each formal rule on a field with examples needs examples at its edge: `>=1` needs an accepted 1 (or anything at or inside the bound) and a rejected 0, *just* outside. `!" "` needs a rejected example containing a space. The compiler names the one to add:

```stoic
<form:f #qty(>=1, <=99, +=1, -=0, +=>="true", -=>="false") />
```

That's a warning while you write and an error under `stoic verify --strict`, the check CI runs. It's the difference between "we have a rule" and "we'd notice if it broke".

## How many tests

Write the fewest that pin the behaviour down. One per rule, and one per boundary on each side: 99 accepted and 100 rejected, not every number between. If two tests would fail for the same reason, keep one. The [writing guide](/docs/writing/) has more.
