| | @@ -0,0 +1,91 @@ |
| 1 | +# Testing: Rand, Check, Prop and raztest |
| 2 | + |
| 3 | +A plan for testing raz programs: three library modules and a tool, all |
| 4 | +shipped with raz, so that a public project needs nothing past raz, make |
| 5 | +and sh to use them. Each step is a commit of its own, useful alone; |
| 6 | +after each, stop for review. |
| 7 | + |
| 8 | +## Why here |
| 9 | + |
| 10 | +- Every program has the library, and a program's make already builds |
| 11 | + `tools/Pretty.raz` for `fmt`; `raztest` is found the same way. |
| 12 | +- Today ~180 `check`/`expect` helpers are written by hand across ~/r, |
| 13 | + four projects carry a PRNG of their own (rjson, pict, sauce, tasta), |
| 14 | + and each Makefile hand-rolls its test loop and summary. |
| 15 | + |
| 16 | +## The pieces |
| 17 | + |
| 18 | +| Piece | Where | Size | What | |
| 19 | +|---|---|---|---| |
| 20 | +| `Rand` | `lib/Rand.raz` | ~40 | a seeded generator, splitmix64 | |
| 21 | +| `Check` | `lib/Check.raz` | ~80 | assertions, failures on stderr, status by `Rt.atExit` | |
| 22 | +| `Prop` | `lib/Prop.raz` | ~300 | properties and fuzzing over recorded choices, with shrinking | |
| 23 | +| `raztest` | `tools/Test.raz` | ~700 | finds, builds and runs tests; colour, reports, watch | |
| 24 | + |
| 25 | +**Rand.** `init(r, seed)`, `next(r): U64`, `below(r, n)` (no bias), |
| 26 | +`range(r, lo, hi)`, `seed(): U64` (fresh, from `Sys.random`, to print |
| 27 | +and replay). No reals until a user needs them. |
| 28 | + |
| 29 | +**Check.** `ok(cond, what)`, `str(what, got, want)` (escaped), |
| 30 | +`int(what, got, want)`, `mute`/`unmute`. A failure is a block on |
| 31 | +standard error: |
| 32 | + |
| 33 | + FAIL what |
| 34 | + got: "..." |
| 35 | + want: "..." |
| 36 | + |
| 37 | +and a program with failures ends with `Check: F of N failed` and status |
| 38 | +1, by a procedure registered with `Rt.atExit` (which may call |
| 39 | +`Rt.exit` itself), so there is no `done` to forget. A passing run says |
| 40 | +nothing. The status and this text are the whole contract with |
| 41 | +`raztest`; a bare `./Mod` or a make loop works as before. |
| 42 | + |
| 43 | +**Prop.** A property is `proc(var s: Prop.Source)` that draws values |
| 44 | +(`int(s, lo, hi)`, `bool(s)`, `bytes(s, max, buf)`, `pick(s, n)`) and |
| 45 | +checks them with `Check`. A case is the bytes it drew (as Hypothesis |
| 46 | +does), so shrinking works on those bytes (cut chunks, zero, lower) and |
| 47 | +replays the property: no shrinker per type, no closures. A property |
| 48 | +reading only `bytes` is a byte fuzzer. While searching, `Check` is |
| 49 | +muted; the smallest failing case is run again aloud and saved to |
| 50 | +`test/seeds/<name>`; saved seeds are replayed first, every run. |
| 51 | +`PROP_SEED` replays one case, `PROP_TIME=60` fuzzes for a minute. |
| 52 | + |
| 53 | +**raztest.** Finds modules with `proc main*` in `src/` and `test/*.raz` |
| 54 | +(`-I`, default `src`); builds with `-Werror`; runs them, `-j n`, a |
| 55 | +timeout each, `-r pattern`; times each; shows a test's output only when |
| 56 | +it fails; colour on a terminal unless `CI` or `NO_COLOR`; a summary and |
| 57 | +the status. `--tap`, `--junit file` (a module is a case, its `FAIL` |
| 58 | +blocks the failure). `-w` reruns what a changed `.raz` reaches, by |
| 59 | +polling file times (Sys has no inotify: a gap). `--seed`, `--fuzz 60s` |
| 60 | +set `PROP_*`. Left out: `.cn`, sh and pty tests, which stay in make. A |
| 61 | +Makefile uses `raztest` when it's there and its own loop when not. |
| 62 | + |
| 63 | +## Steps |
| 64 | + |
| 65 | +1. `Rand`, with its self-test. |
| 66 | +2. `Check`; the library's self-tests use it. |
| 67 | +3. `raztest`: finding, building, running, `-j`, timeouts, colour, |
| 68 | + summary; `test/check.sh`'s library loop uses it. `TOOLS` gains |
| 69 | + `Test`; `make install` puts `raztest` on PATH; `doc/raztest.1`. |
| 70 | +4. `raztest --tap`, `--junit`. |
| 71 | +5. `raztest -w`. |
| 72 | +6. `Prop`, with its self-test. |
| 73 | +7. Volume 2: `Rand`, `Check` and `Prop` go into `doc/raz-lib.md` as |
| 74 | + standard library (not core: `conform/` is untouched), with whole |
| 75 | + examples, once the first users have settled them; one report |
| 76 | + version. `STANDARDS.md` says how a module tests itself with them. |
| 77 | +8. robbie: `robbie -w`, in its own repository. |
| 78 | + |
| 79 | +After the release that brings them: rjson, pict and sauce take `Rand` |
| 80 | +and `Prop` (rjson's `Random.raz`, JSONTestSuite as seeds; pict's fuzz; |
| 81 | +sauce's `Fuzz.raz` draws), and projects take `Check` and `raztest` as |
| 82 | +their files are touched, with no sweep. `desktop.7` names `raztest` |
| 83 | +once it is installed. |
| 84 | + |
| 85 | +## Left out |
| 86 | + |
| 87 | +Coverage-guided fuzzing (the compiler would have to count branches); |
| 88 | +tests in doc comments (no library comment holds a whole example; the |
| 89 | +report's and the tutorial's are already run); a shared pty harness |
| 90 | +(belongs with rtui or rterm); `Check` naming the failing line (`Rt` |
| 91 | +knows places but exports none: a gap). |