Files
sisi/verifiers/many-inputs/go-rapid.md
T
swaitsandClaude Fable 5.2 2fc1fe9c2d
ci / tests (macos-latest, python 3.11) (push) Canceled after 0s
ci / tests (macos-latest, python 3.13) (push) Canceled after 0s
ci / tests (macos-latest, python 3.9) (push) Canceled after 0s
ci / tests (ubuntu-latest, python 3.11) (push) Canceled after 0s
ci / tests (ubuntu-latest, python 3.13) (push) Canceled after 0s
ci / tests (ubuntu-latest, python 3.9) (push) Canceled after 0s
sisi 0.1.0: a shared rulebook and verification tool for AI coding agents
Contracts with a ladder of checks, an end-of-turn gate, retrospectives and
shared lessons, time-to-delivery measurement, a library of 140 verification
techniques and 20 recipes, personal data kept outside the clone, and
releases with rollback. Standard library only.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-22 22:29:02 -07:00

73 lines
2.6 KiB
Markdown

---
id: go-rapid
title: Property testing in Go with rapid
answers: Does this Go code hold its law for generated inputs, and what is the smallest failing case?
rung: 2
section: many-inputs
languages: go
kinds: general, parsing, money, state-machine
cost: low
detect: go version
ran_here: no
verified: docs
---
## Use when
- Go code has a law (round trip, sorted output, idempotence, agreement with a model) and you want shrinking.
- You want a stateful model test using `T.Repeat` (see `stateful-model-test`).
## Do not use when
- You want coverage-guided search: use `go-fuzz`. rapid can be wired into it with `rapid.MakeFuzz`, but the fuzzing engine then does the guiding, not rapid.
- A table of examples already covers the cases that matter (see the `test-quality` section).
## Set up
go get pgregory.net/rapid
# in a *_test.go file: import "pgregory.net/rapid"
## Minimal example
package hexcodec
import (
"bytes"
"encoding/hex"
"testing"
"pgregory.net/rapid"
)
func TestRoundTrip(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
data := rapid.SliceOf(rapid.Byte()).Draw(t, "data")
got, err := hex.DecodeString(hex.EncodeToString(data))
if err != nil || !bytes.Equal(got, data) {
t.Fatalf("round trip failed for %v: %v, %v", data, got, err)
}
})
}
## Run
go test -run TestRoundTrip ./...
go test -rapid.checks=10000 -run TestRoundTrip ./...
Passing looks like: `ok <package>` and exit 0.
Failure shows the minimal failing draw (`data = []byte{...}`) and `FAIL`, exit 1.
rapid saves minimised failing cases and re-runs them automatically; the
directory was not confirmed against the docs here, so look for a new
`testdata/` subdirectory after a failure and commit it. Each flag also has a
`RAPID_` environment variable (for example `RAPID_CHECKS`).
## As a contract check
{"id": "prop-round-trip-go", "claim": "hex decode of encode returns the input for generated byte slices", "kind": "parsing", "rung": 2, "cmd": "go test -run TestRoundTrip ./...", "expect": {"exit": 0}, "tier": "deep", "timeout_s": 300}
## What a pass does not prove
- 100 checks by default; it samples. Raise `-rapid.checks` for important laws.
- `t.Skip` in a property discards the case; if most cases skip, the pass is nearly empty.
- Not run here (Go is not installed on this machine). The example, `-rapid.checks`, the `RAPID_` variables, `T.Repeat` and `MakeFuzz` come from the package documentation; the failure-file location and `-rapid.seed` could not be confirmed. Break the code on purpose to see the check fail.