Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Test

Test assertions and per-type value generators for property testing.

fail_with, expect, expect_equal, and expect_text fail the running test fn with a structured payload: a message, and for expect_equal the expected and actual values with a line diff between them. The payload travels to prism test as the versioned prism-test-failure-v1 envelope, written as the test’s last output line just before the failure fires, and the runner reports its fields in place of a bare test failed.

A generator is the unwrapped Quickcheck.Gen: a size-bounded draw from the ambient Random capability. That makes a run deterministic under a seed (the Quickcheck runner serves Random from a seeded SplitMix64 stream), and lets a derived instance recurse through its fields’ arbitrary while composing cleanly with the Gen combinators. The primitive instances delegate to Quickcheck’s canonical generators, so a derived type draws from the same distributions the hand-written harness does.

Wrap an instance back into a Gen with arb_gen to feed it to quickcheck.

Type Classes

Arbitrary

class Arbitrary(a)
  arbitrary : (Int) -> a ! {Random | e}

A structural generator, sized by a fuel budget. deriving (Arbitrary) spends the budget on depth: at each recursive constructor it draws its fields a size smaller, and once the budget runs out it restricts to non-recursive constructors, so generation always terminates.

Instances

arbitraryInt

instance arbitraryInt : Arbitrary(Int)

arbitraryI64

instance arbitraryI64 : Arbitrary(I64)

arbitraryU64

instance arbitraryU64 : Arbitrary(U64)

arbitraryBool

instance arbitraryBool : Arbitrary(Bool)

arbitraryUnit

instance arbitraryUnit : Arbitrary(Unit)

arbitraryChar

instance arbitraryChar : Arbitrary(Char)

arbitraryFloat

instance arbitraryFloat : Arbitrary(Float)

arbitraryString

instance arbitraryString : Arbitrary(String)

arbitraryOption

instance arbitraryOption : Arbitrary(Option(a))

arbitraryList

instance arbitraryList : Arbitrary(List(a))

Functions and Values

fail_with

fail_with : forall a. (String) -> a ! {Fail, Output}

Fail the running test with message.

test fn parses() =
  match parse_int("12") of
    Some(_) => ()
    None => fail_with("`12` did not parse")

expect

expect : (Bool, String) -> Unit ! {Fail, Output}

Fail the running test with message unless cond holds.

expect_equal

expect_equal : forall a. (a, a) -> Unit ! {Fail, Output} given Eq(a), Show(a)

Fail the running test unless actual equals expected, reporting both values and a line diff between their renderings.

test fn adds() = expect_equal(4, 2 + 2)

expect_text

expect_text : (String, String) -> Unit ! {Fail, Output}

Fail the running test unless the text actual equals expected, reporting a line diff of the raw text. Prefer this to expect_equal for multi-line strings, whose Show rendering escapes the line breaks a diff needs.

arb_gen

arb_gen : forall a. () -> Quickcheck.Gen(a) given Test.Arbitrary(a)

The generator of an Arbitrary type, as a Quickcheck.Gen ready for quickcheck/gen_at.

fn prop(xs : List(Int)) : Bool = reverse(reverse(xs)) == xs

fn main() = println(passed(quickcheck(arb_gen(), prop)))