p

pbt

be6f64dlibrary

No description provided.

0 stars0 forksUnknownupdated 3 weeks ago
Open repo

pbt

Property-based testing for Odin.

Write a property, draw generated inputs, and run it many times with deterministic replay and shrinking. pbt works with Odin libraries, CLI programs, HTTP services, stateful models, and Gransk-style external runners.

Example

package cart_test

import "core:testing"

import pbt "pbt"

cart_total :: proc(qty, price_cents: int) -> int {
  return qty * price_cents
}

cart_total_is_stable :: proc(t: ^pbt.T) -> pbt.Result {
  qty := pbt.draw(t, pbt.int_range(0, 100))
  price := pbt.draw(t, pbt.int_range(0, 10_000))

  return pbt.equal(cart_total(qty, price), qty * price)
}

@(test)
test_cart_total :: proc(t: ^testing.T) {
  result := pbt.check("cart total is stable", cart_total_is_stable, {
    num_tests = 1_000,
    seed = 123,
  })
  defer pbt.destroy_check_result(&result)

  pbt.require_pass(t, result)
}

draw records the choices needed for replay and shrinking. check runs the property many times. A failing result keeps the seed, choices, shrunk case, notes, labels, and event trace needed to reproduce the failure.

Features

  • Deterministic seeds and strict replay
  • Default-on shrinking from recorded choice streams
  • Integer, float, bool, string, byte, UUID, email, date, identifier, CLI, URL, HTTP, JSON, array, dictionary, tuple, enum, optional, and unique generators
  • Generator combinators such as map_gen, bind, one_of, frequency, sized, resize, scale, and such_that
  • pass, fail, discard, error, assert, equal, and counterexample
  • Notes, labels, classification, collection, and coverage requirements
  • Stateful model testing
  • Process, stdin, persistent line-protocol, HTTP, and statechart adapters
  • JSON/text result output and Gransk runner support

Runner Mode

For command-line or Gransk-compatible runners, collect properties and use the CLI helpers for listing, tags, replay, JSON output, exit codes, and --fail-fast:

properties := [?]pbt.Property_Case{
  {name = "cart total", property = cart_total_is_stable, tags = []string{"cart"}},
}

pbt.run_cli(properties[:], os.args[1:], {num_tests = 1_000})

Start with docs/runner-starter.md for a runner template.

Layout

  • pbt/ - core package
  • pbt_statechart/ - adapter helpers for statechart model tests
  • examples/ - runnable runner and adapter examples
  • docs/public-api.md - full API notes
  • docs/gransk-runner-contract.md - machine-readable runner contract
  • BENCHMARKS.md - benchmark commands and current measurements

Useful Docs

Commands

odin test ./pbt
odin test ./pbt_statechart

odin run examples/gransk_runner -collection:pbt=. -- --list-properties
odin run examples/runner_starter -collection:pbt=.

odin run benchmarks/check_bench.odin -file -collection:pbt=. -o:speed
Package Info
Version
be6f64d
License
Unknown
Author
@flakstad
Type
library
Forks
0
Created
2 months ago
Updated
3 weeks ago
Health
maintained
has releases
has readme
has license
Activity
last 56 weeks