metamon
Property-based and metamorphic testing for Gleam, on Erlang and JavaScript. A property checks one input; a metamorphic relation checks how the outputs for two related inputs, such as f(x) and f(list.reverse(x)), must relate. metamon generates inputs, shrinks a failing one and prints a report with the seed that reproduces it.
gleam add --dev metamon@0
Properties
A property is a function that panics when it does not hold, so Gleam's assert works inside it.
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
})
}
When a property fails, metamon shrinks the input and panics with a report. For assert list.reverse(items) == items the report starts like this:
× property failed
test: forall
source: random(seed=4268083006, size=1, max_size=99)
config seed: 42
runs: 1 / 100
shrinks: 10
source input (shrunk):
[0, 1]
panic message:
Assertion failed.
Metamorphic relations
forall_morph runs a function on an input and on transformed inputs, and checks each pair of outputs with a relation. It helps when the right output is hard to state but how it must change is easy to state.
import gleam/int
import gleam/list
import metamon
import metamon/generator
pub fn sum_test() {
let lists =
generator.list_of(
generator.int(generator.range(from: -100, to: 100)),
length: generator.range(from: 0, to: 20),
)
let reverse = metamon.Transform(name: "list.reverse", apply: list.reverse)
let append_zero =
metamon.Transform(name: "append 0", apply: fn(items) {
list.append(items, [0])
})
metamon.forall_morph(
lists,
[
metamon.invariant_under(name: "order does not matter", under: reverse),
metamon.Morph(
name: "adding 0 changes nothing",
transform: append_zero,
relation: metamon.equal(),
),
],
int.sum,
)
}
pub fn sort_test() {
let lists =
generator.list_of(
generator.int(generator.range(from: -100, to: 100)),
length: generator.range(from: 0, to: 20),
)
let sort = list.sort(_, int.compare)
metamon.forall_morph(
lists,
[metamon.idempotency_of(name: "sort", of: sort)],
sort,
)
}
Equivariant relates a transform of the input to a transform of the output, and equivalent_under, approximately and permutation_of compare outputs by something other than ==. forall_round_trip checks that decode(encode(x)) == Ok(x):
import gleam/bit_array
import metamon
import metamon/generator
pub fn base64_test() {
metamon.forall_round_trip(
generator: generator.bit_array(bytes: generator.range(from: 0, to: 64)),
name: "base64",
encode: bit_array.base64_encode(_, True),
decode: bit_array.base64_decode,
)
}
Configuration and coverage
Config is a plain record. Override fields of default_config() and pass it to the _with runners. A fixed seed makes a run reproducible; regression_file records failing inputs and replays them on later runs.
import gleam/option.{Some}
import gleam/string
import metamon
import metamon/generator
pub fn trim_test() {
let config =
metamon.Config(..metamon.default_config(), runs: 300, seed: Some(42))
let words =
generator.string(
of: generator.Alphanumeric,
length: generator.range(from: 0, to: 8),
)
metamon.forall_with(config, words, fn(word) {
metamon.classify(label: "empty", when: word == "")
metamon.cover(at_least: 50.0, label: "non-empty", when: word != "")
assert string.trim(word) == word
})
}
classify counts inputs by label, cover fails the run when a label is hit less often than the given percentage, and annotate adds a labelled value to the failure report.
Further documentation can be found at https://hexdocs.pm/metamon.
Verifying releases
Each GitHub Release attaches the Hex package tarball (metamon-<version>.tar),
checksums.txt, a cosign signature bundle (checksums.txt.sigstore.json) and
build provenance (metamon-<version>.intoto.jsonl). Download the assets, then:
gh attestation verify metamon-<version>.tar --repo nao1215/metamon
cosign verify-blob \
--bundle checksums.txt.sigstore.json \
--certificate-identity-regexp '^https://github\.com/nao1215/metamon/\.github/workflows/release\.yml@refs/tags/v' \
--certificate-oidc-issuer https://token.actions.githubusercontent.com \
checksums.txt
sha256sum --check checksums.txt