Your first oath from scratch
In Get started on your computer we ran a scaffolded example. Now we write our own from a blank file: a tiny calculator. By the end you will have described a behaviour in prose, bound it to code, and seen the two verify each other.
This tutorial continues in the project you set up in Get started.
-
Describe the behaviour
Create
varar/calculator.md:# CalculatorThe expression `1+1` should evaluate to `2`.That’s the whole oath. It reads like documentation because it is documentation — plain prose, with concrete values. Varar calls those values cells, and they are what make the prose checkable:
1+1is the cell we act with,2is the cell we expect back. -
Bind the steps
Nothing runs yet — no step matches our sentence. Create
src/varar/calculator.steps.ts:import { steps } from '@varar/varar'const { stimulus, sensor } = steps(() => ({ result: 0 }))stimulus('expression `{int}+{int}`', (_state, op1, op2) => ({ result: op1 + op2 }))sensor('evaluate to `{int}`', (state, _expected) => state.result)Three things to notice:
stepsdeclares the state each example starts from — a fresh{ result: 0 }every run, so examples never leak into each other.- The
stimulusdrives the software. It matchesexpression `1+1`in the prose, computes, and returns the next state — the value it returns is the new state, replacing the old one rather than being merged into it. - The
sensoris the read-only observation. It returns what the software actually produced —state.result— and Varar compares that against the2written in the Markdown. You never write an assertion; the document is the assertion.
The
{int}placeholders are Cucumber Expressions: they capture the cells from the prose and hand them to your function, typed. -
Run it
Terminal window pnpm vitest run✓ varar/calculator.md (1 test) 1ms✓ varar/deep-thought.md (1 test) 1msTest Files 2 passed (2)Tests 2 passed (2)Your sentence is now an executable example.
-
Watch it fail
Make it a habit: every new example should be seen failing once. This time, break the document — claim in
calculator.mdthat1+1evaluates to3.Run Varar again. The document demands 3, the software answers 2, and the failure points at the
3in your Markdown. Revert it, run once more, and you’re green.
What you just learned
Section titled “What you just learned”- An oath is prose with concrete values; no keywords, no special file format.
- Steps come in exactly two roles, chosen by what they do: a stimulus drives the system, a sensor observes it. Setting up the starting state is a stimulus too — there is no separate role for it.
- A sensor returns the observed value instead of asserting; Varar does the comparison and anchors failures to the document.
- Check tables and doc strings — one sentence per example doesn’t scale; tables do.
- Thin steps — why step bodies should stay 2–3 lines.