Tests

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.

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'"
  )
/>
The compiler reads it back

A form signup. Field username must be at least 3 and at most 20, must not contain spaces, accepts "ada" and "twenty_chars_exactly", and rejects "ad", "a b", and "twenty_one_characters". Each example is tried given submit the form with value. Each accepted example reports exactly "'ok'"; each rejected one, exactly "'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'"
/>
The compiler reads it back

A form signup that does "create account" on submit. Field username must be at least 3. Field email must match "email".

A test ada_signs_up of signup: given username = "ada" and email = "ada@acme.com", on submit, expect "welcome email queued" and expect account exactly "'created'" (reported back and compared).

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:

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"
/>
The compiler reads it back

A method checkout that does "checkout(cart)" on call.

A test spring_sale of checkout: given SPRING20 applied to a 9000 cart, on call, expect total exactly "7200" (reported back and compared) and expect "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' }"
/>
The compiler reads it back

A method refund that does "refund(paymentId, amount)" on call.

A test no_double_refund of refund: given a payment of 5000 and refund(p, 5000) succeeded, on call with "p, 1", expect exactly "{ ok: false, error: 'ALREADY_REFUNDED' }" (reported back and compared).

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") />
The compiler reads it back

A config job. Field retries must be at least 1 and at most 5, or null, accepts 1, 5, and null, and rejects 0 and 6. Each accepted example reports exactly "true"; each rejected one, exactly "false". A quoted example is text; a bare one is that value.

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") />
The compiler reads it back

A form f. Field qty must be at least 1 and at most 99, accepts 1, and rejects 0. Each accepted example reports exactly "true"; each rejected one, exactly "false". A quoted example is text; a bare one is that value.

warning: untested-rule: #qty's rule `<= 99` has no rejected example just above it; add -=100
 --> <stdin>:1:9
  |
1 | <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 has more.

All 6 examples on this page were compiled by Stoic at build time.