std: arbitrary<T>() test data from types, and check() with shrinking #15
Loading…
Reference in a new issue
No description provided.
Delete branch "std-arbitrary"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What
arbitrary<T>()from a new std module,@evobug/safe-ts-std/arbitrary, is rewritten at build time into a generator forT. It uses the type modelparseJson<T>already has (crates/safe_ts_core/src/json.rs).check(arb, property, { runs, seed, maxShrinks })runs a property and returns aResult:{ runs, seed }, or anErrwith the first failing value (shrunk), the original value, the seed, the run, the cause and a readablemessageforassert.fail.parseJson/validate.arbitrary<User>()becomes__arbitraryWith<User>(__g0), and the generator functions are added at the end of the file. As with validators, line numbers don't move, the type argument is kept (so tsc checks that the generated type fits), and the__Ttypes are shared when one file uses both.json-typeaccepts: primitives, literals, objects/interfaces (includingextends), optional keys, readonly arrays and tuples, unions,Option,Enum(every variant),SafeInt,bigint, and generic and recursive aliases. Unsupported types get the samejson-typediagnostics with an`arbitrary<T>`:prefix. One new error: a type whose every value contains another one (type Loop = { next: Loop })."", 4000-character strings, non-ASCII, astral and ZWJ characters,0,-0, ±MAX_SAFE_INTEGER,MAX_VALUE/MIN_VALUE, 64-bit bigint limits, empty arrays, missing optional keys. Numbers are always finite. Strings never contain lone surrogates. An optional key is either absent or holds a value, as inparseJsonoutput.Rng.seeded(mulberry32). If no seed is given,checkpicks a random one and reports it.maxShrinksproperty calls (default 1000), and the choice list only gets shortlex-smaller.Tests
packages/std/test/arbitrary.test.ts(14 tests):parseJson<T>for the sameTand come back equal (-0normalised), forPerson,Admin,Shape,Tree,Page<Point>,Contact, a largeAccounttype and a recursive JSON-likeEnum.10,-3,1e6,10n**20n,"aaaaa",[true],{ tag: "Rect", w: 0, h: 0 }, and the simplestAccount.Errand async properties;maxShrinks: 0; the code-point rank bijection.crates/safe_ts_core/tests/transform.rs: the exact generated code for a small recursive type, union ordering, sharing types withparseJson, and all the rejections.tests/fixtures/json/main.ts:arbitrarylines for the checker path, including that a discardedarbitrary<T>()is notmust-use-result.typecheck:emitnow also emitsarbitrary.test.tsand runs tsc on the generated code.ts goodexamples in the new guide as realnode:testfiles. The buggyaveragefails as the page describes, and the others pass.Results, after rebasing onto main at
1fe2d1d(head below):cargo nextest run --workspace: 108/108 pass.npm run test:std: 145/145 pass. That includesarbitrary.test.tsunder the Node loader's dev checks (on by default), and the new dev-checks test.arbitrary.test.tsalso passes 14/14 withSAFE_TS_DEV_CHECKS=1and=0.npm run test:vite,npm run typecheck,npm run test:docs(docs examples, wasm, VitePress build): pass.node scripts/verify-packages.mjs(packs.release/, installs the tarballs into a fresh project from the npm registry): all checks pass, including the new arbitrary ones.npm run bench:check: passed in 2 of 4 runs. In the other two,bench: match / switch, through the transformwas 2.01x against an allowed 1.92x. That ratio measuresmatchrewriting, which this PR doesn't touch: the new code only runs whenarbitraryis imported from the arbitrary module, andmatch.bench.tsdoesn't import it. The machine was under heavy load at the time (load average 37–46, other builds running). I didn't update the baseline.What changed in the rebase
packages/std/package.json,./arbitrarywas added after./arraynext to #12'sfiles/publishConfig. Intests/transform.rs, #10's dev-check tests and the arbitrary tests are now both kept.scripts/build-packages.mjscompiles std from itsexports, so./arbitraryis built into.release/std/dist/arbitrary.{js,d.ts}with no change to the script.scripts/verify-packages.mjsnow checks arbitrary the way a user gets it:check()runs, and an untransformedarbitrary<T>()throws "was not compiled";check: "error": the bundle contains no "was not compiled", andcheck(arbitrary<Status>(), ...)runs;q,matchor comparators). A dev check that fails inside a property is caught bycheck()like any throw, so it becomes a counterexample and is shrunk. A new test inpackages/std/test/dev-checks.test.tscovers this: a comparator that returnsNaNfor a missingpricefailscheck()across 5 seeds and shrinks to[{ name: "" }, { name: "" }]. WithSAFE_TS_DEV_CHECKS=0the same property passes. The testing guide now says so.docs/guide/std-helpers.md(new on main with #6) now lists the module, linking to the testing guide.Docs
docs/guide/testing.md, with checked examples (good,bad must-use-result,bad json-type), in the sidebar under "Everyday code" and indocs/README.md.docs/rules/json-type.mdanddocs/guide/json.mdlink to the new guide.Left out / uncertain
json-typedoesn't acceptRecordor index signatures yet ("index signatures are not supported yet"), soarbitrarydoesn't either. It will follow whenparseJsongains them.map/filter/oneOf), no custom generators, no async properties (a property that returns a promise fails with a message saying so), and no stateful testing.awould change its UTF-16 length and make the property pass. The guide says so.seedandruns, because the size of each run depends onruns.arbitraryis not in the prelude.🤖 Generated with Claude Code
d15ebdf77dtoab18627dd2