metamon

Property-based and metamorphic testing.

forall runs a property on many generated inputs and shrinks the first input that makes it panic. forall_morph checks metamorphic relations: how the output of a function changes when its input is transformed. Generators live in metamon/generator.

import gleam/list
import metamon
import metamon/generator

pub fn reverse_twice_test() {
  let lists =
    generator.list_of(
      generator.int(generator.range(from: -100, to: 100)),
      length: generator.range(from: 0, to: 20),
    )
  metamon.forall(lists, fn(items) {
    assert list.reverse(list.reverse(items)) == items
  })
}

Types

How a runner explores inputs and reports a failure. Build one from default_config and override the fields you need:

let config = metamon.Config(..metamon.default_config(), runs: 300)

The runners check the fields when they start; see forall_with for the values they reject.

pub type Config {
  Config(
    runs: Int,
    seed: option.Option(Int),
    max_size: Int,
    shrink_limit: Int,
    max_edges: Int,
    regression_file: option.Option(String),
    output: Output,
  )
}

Constructors

  • Config(
      runs: Int,
      seed: option.Option(Int),
      max_size: Int,
      shrink_limit: Int,
      max_edges: Int,
      regression_file: option.Option(String),
      output: Output,
    )

    Arguments

    runs

    The number of inputs to try, edge values included: the edge values come first, and fewer runs than edge values try only the first ones. METAMON_RUNS_MULTIPLIER, when set to a positive integer, multiplies it.

    seed

    The seed of the random inputs. None draws a new seed for every property; Some(n) replays the same inputs on every run.

    max_size

    The largest size given to the generators. Sizes grow from 0 on the first random input, after the edge values, to max_size on the last.

    shrink_limit

    The largest number of shrink steps taken on one failure.

    max_edges

    The largest number of edge values tried before the random inputs.

    regression_file

    A file that records every failing input and replays the recorded inputs first on the next run. None turns recording off. Several properties can share a file: an entry is replayed only when the generator reproduces the input it recorded, compared as rendered by string.inspect, so an entry of another property, or of a generator that has since changed, is skipped. Floats render differently on Erlang and JavaScript, so a file recorded on one target may skip such entries on the other.

    output

    The format of the failure report.

A metamorphic relation of a function f from a to b. The name identifies the morph in failure reports and in the regression file.

pub type Morph(a, b) {
  Morph(
    name: String,
    transform: Transform(a),
    relation: Relation(b),
  )
  Equivariant(
    name: String,
    input: Transform(a),
    output: Transform(b),
    relation: Relation(b),
  )
}

Constructors

  • Morph(
      name: String,
      transform: Transform(a),
      relation: Relation(b),
    )

    f(x) and f(transform.apply(x)) satisfy relation.

  • Equivariant(
      name: String,
      input: Transform(a),
      output: Transform(b),
      relation: Relation(b),
    )

    output.apply(f(x)) and f(input.apply(x)) satisfy relation: transforming the input transforms the output the same way.

The format of a failure report.

pub type Output {
  Text
  Json
}

Constructors

  • Text

    A multi-line report for people (the default).

  • Json

    A single-line JSON object for CI and other tools.

A named check between two outputs, such as equality. The name appears in failure reports.

pub type Relation(b) {
  Relation(name: String, holds: fn(b, b) -> Bool)
}

Constructors

  • Relation(name: String, holds: fn(b, b) -> Bool)

A named transformation of an input, such as reversing a list. The name appears in failure reports. apply must be pure.

pub type Transform(a) {
  Transform(name: String, apply: fn(a) -> a)
}

Constructors

  • Transform(name: String, apply: fn(a) -> a)

Values

pub fn annotate(label label: String, value value: a) -> Nil

Record value under label for the current run. The failure report shows the values recorded in the run of the reported input, rendered with string.inspect; a passing run discards them.

metamon.forall(ints, fn(n) {
  let doubled = n * 2
  metamon.annotate(label: "doubled", value: doubled)
  assert doubled >= n
})
pub fn approximately(within epsilon: Float) -> Relation(Float)

Return the relation that holds when two floats differ by at most epsilon. Equal values, infinities included, always hold.

Panics

Panics when epsilon is negative or NaN, since no pair of floats would then satisfy the relation.

pub fn assert_morph(
  input: a,
  morph: Morph(a, b),
  function: fn(a) -> b,
) -> Nil

Check morph against function on the single input input, for example a shrunk input copied from a failure report. Panics with a report when the relation does not hold.

pub fn classify(label label: String, when condition: Bool) -> Nil

Count the current run as hitting label when condition is true. A label counts at most once per run. The counts appear in the coverage part of a failure report.

metamon.forall(lists, fn(items) {
  metamon.classify(label: "empty", when: items == [])
  assert list.reverse(list.reverse(items)) == items
})
pub fn commutativity_of(name name: String) -> Morph(#(a, a), b)

Return the morph f(#(a, b)) == f(#(b, a)) for a function of a pair: swapping the arguments does not change the output.

pub fn cover(
  at_least percent: Float,
  label label: String,
  when condition: Bool,
) -> Nil

Like classify, and require at least percent percent of the runs to hit label. When fewer do, the property fails after its last run with a coverage shortfall, even if every run passed.

Panics

Panics when percent is outside [0.0, 100.0] or NaN.

pub fn default_config() -> Config

Return the default configuration: 100 runs, a new random seed for every property, max_size 99, at most 1024 shrink steps, at most 16 edge values, no regression file and text output.

pub fn equal() -> Relation(a)

Return the relation that holds when both outputs are equal with ==.

pub fn equivalent_under(
  name name: String,
  via project: fn(b) -> c,
) -> Relation(b)

Return the relation that holds when both outputs are equal after project, such as equal ignoring case. The report names it equivalent_under(<name>).

pub fn forall(
  generator: generator.Generator(a),
  property: fn(a) -> Nil,
) -> Nil

Run property on many generated inputs: the edge values of the generator first, then random inputs. The property fails when it panics: a failing assert or let assert, a panic, or an Erlang error or JavaScript exception. On failure the input, an edge value included, is shrunk to a smaller one that still panics, and the runner panics with a report of that input and its panic message.

metamon.forall(generator.int(generator.range(from: -100, to: 100)), fn(n) {
  assert n + 0 == n
})
pub fn forall_morph(
  generator: generator.Generator(a),
  morphs: List(Morph(a, b)),
  function: fn(a) -> b,
) -> Nil

Check every morph in morphs against function on many generated inputs. Each morph runs on its own; when any fails, the input is shrunk and the runner panics with the report of every failing morph.

let lists =
  generator.list_of(
    generator.int(generator.range(from: 0, to: 9)),
    length: generator.range(from: 0, to: 10),
  )
let reverse = metamon.Transform(name: "reverse", apply: list.reverse)
metamon.forall_morph(
  lists,
  [metamon.invariant_under(name: "sum_under_reverse", under: reverse)],
  int.sum,
)

Panics

Panics when morphs is empty.

pub fn forall_morph_with(
  config: Config,
  generator: generator.Generator(a),
  morphs: List(Morph(a, b)),
  function: fn(a) -> b,
) -> Nil

Check morphs like forall_morph, with an explicit configuration.

Panics

Panics when morphs is empty, or when config is invalid as described in forall_with.

pub fn forall_round_trip(
  generator generator: generator.Generator(a),
  name name: String,
  encode encode: fn(a) -> b,
  decode decode: fn(b) -> Result(a, e),
) -> Nil

Check that decode(encode(x)) is Ok(x) for many generated x. The report is titled round_trip[<name>], and a failing input is shrunk like in forall.

metamon.forall_round_trip(
  generator: generator.int(generator.range(from: -1000, to: 1000)),
  name: "int_to_string",
  encode: int.to_string,
  decode: int.parse,
)
pub fn forall_round_trip_with(
  config config: Config,
  generator generator: generator.Generator(a),
  name name: String,
  encode encode: fn(a) -> b,
  decode decode: fn(b) -> Result(a, e),
) -> Nil

Check a round trip like forall_round_trip, with an explicit configuration.

Panics

Panics when config is invalid as described in forall_with.

pub fn forall_with(
  config: Config,
  generator: generator.Generator(a),
  property: fn(a) -> Nil,
) -> Nil

Run property like forall, with an explicit configuration.

Panics

Panics when config.runs, config.max_size, config.shrink_limit or config.max_edges is not positive, or config.regression_file is Some(""). Every runner that takes a Config checks it the same way.

pub fn idempotency_of(
  name name: String,
  of function: fn(a) -> a,
) -> Morph(a, a)

Return the morph f(f(x)) == f(x): applying function twice gives the same output as applying it once. Use it as the morph of function itself.

pub fn invariant_under(
  name name: String,
  under transform: Transform(a),
) -> Morph(a, b)

Return the morph f(transform.apply(x)) == f(x): the output does not change when the input is transformed.

pub fn permutation_of() -> Relation(List(a))

Return the relation that holds when two lists have the same elements with the same counts, in any order.

✨ Search Document