Matchers

Composable assertions

This module contains functions for building and combining Matchers that can be used to build up assertions for testing.

import M "mo:matchers/Matchers";
import T "mo:matchers/Testable";

assertThat(5 + 5, M.equals(T.nat(10)));
assertThat(5 + 5, M.allOf<Nat>([M.greaterThan(8), M.lessThan(12)]));
assertThat([1, 2], M.array([M.equals(T.nat(1)), M.equals(T.nat(2))]));

type Matcher<A> = { matches : (item : A) -> Bool; describeMismatch : (item : A, description : Description) -> () }

A Matcher is a composable way of building up assertions for tests

public func assertThat<A>(item : A, matcher : Matcher<A>)

Matches an item against a matcher and traps with an error if the matcher fails. This is primarily for experimentation and one-offs. For writing an actual test suite you probably want to use the functions in Suite.

public func attempt<A>(item : A, matcher : Matcher<A>) : {#success; #fail : Text}

Matches an item against a matcher and returns null in case the match succeeds and ?errorMessage if it fails.

public func contramap<A, B>(matcher : Matcher<A>, f : B -> A) : Matcher<B>

Turns a Matcher for As into a Matcher for Bs by using f as an adapter.

class Description()

public func appendText(text : Text)

public func toText() : Text

Matchers describe match failures by inserting them into a Description.

public func anything<A>() : Matcher<A>

Always matches, useful if you don’t care what the object under test is

public func describedAs<A>(msg : Text, matcher : Matcher<A>) : Matcher<A>

Decorator that allows adding a custom failure description

public func equals<A>(expected : T.TestableItem<A>) : Matcher<A>

Matches values equal to an expected Testable

public func greaterThan<A <: Int>(expected : Int) : Matcher<A>

Matches values greater than expected

public func greaterThanOrEqual<A <: Int>(expected : Int) : Matcher<A>

Matches values greater than or equal to expected

public func lessThan<A <: Int>(expected : Int) : Matcher<A>

Matches values less than expected

public func lessThanOrEqual<A <: Int>(expected : Int) : Matcher<A>

Matches values less than or equal to expected

public func inRange<A <: Int>(lower : Int, upper : Int) : Matcher<A>

Matches values for being in inclusive range [lower .. upper]

public func allOf<A>(matchers : [Matcher<A>]) : Matcher<A>

Matches if all matchers match, short circuits (like and)

public func anyOf<A>(matchers : [Matcher<A>]) : Matcher<A>

Matches if any matchers match, short circuits (like or)

public func not_<A>(matcher : Matcher<A>) : Matcher<A>

Matches if the wrapped matcher doesn’t match and vice versa

public func array<A>(matchers : [Matcher<A>]) : Matcher<[A]>

Test an array’s elements against an array of matchers

public func isSome<A>() : Matcher<?A>

Tests that a value is not-null

public func isNull<A>() : Matcher<T.TestableItem<?A>>

Tests that a value is null