Testable

Things we can compare in tests

This module contains the Testable<A> abstraction, which bundles toText and equals for a type A so we can use them as "expected" values in tests. It also contains a few helpers to build Testable's for compound types like Arrays and Optionals. If you want to test your own objects or control how things are printed and compared in your own tests you'll need to create your own Testable's.

import T "mo:matchers/Testable";

type Person = { name : Text, surname : ?Text };
// Helper
let optText : Testable<(?Text)> = T.optionalTestable(T.textTestable)
let testablePerson : Testable<Person> = {
   display = func (person : Person) : Text =
       person.name # " " #
       optText.display(person.surname)
   equals = func (person1 : Person, person2 : Person) : Bool =
       person1.name == person2.name and
       optText.equals(person1.surname, person2.surname)
}

type Testable<A> = { display : A -> Text; equals : (A, A) -> Bool }

Packs up all the functions we need to compare and display values under test

type TestableItem<A> = { display : A -> Text; equals : (A, A) -> Bool; item : A }

A value combined with its Testable

public let textTestable : Testable<Text>

public func text(t : Text) : TestableItem<Text>

public let natTestable : Testable<Nat>

public func nat(n : Nat) : TestableItem<Nat>

public let nat8Testable : Testable<Nat8>

public func nat8(n : Nat8) : TestableItem<Nat8>

public let nat16Testable : Testable<Nat16>

public func nat16(n : Nat16) : TestableItem<Nat16>

public let nat32Testable : Testable<Nat32>

public func nat32(n : Nat32) : TestableItem<Nat32>

public let nat64Testable : Testable<Nat64>

public func nat64(n : Nat64) : TestableItem<Nat64>

public let intTestable : Testable<Int>

public func int(n : Int) : TestableItem<Int>

public let boolTestable : Testable<Bool>

public func bool(n : Bool) : TestableItem<Bool>

public let charTestable : Testable<Char>

public func char(n : Char) : TestableItem<Char>

public func arrayTestable<A>(testableA : Testable<A>) : Testable<[A]>

public func array<A>(testableA : Testable<A>, xs : [A]) : TestableItem<[A]>

public func listTestable<A>(testableA : Testable<A>) : Testable<List.List<A>>

public func list<A>(testableA : Testable<A>, xs : List.List<A>) : TestableItem<List.List<A>>

public func optionalTestable<A>(testableA : Testable<A>) : Testable<?A>

public func optional<A>(testableA : Testable<A>, x : ?A) : TestableItem<?A>

public func resultTestable<R, E>(rTestable : Testable<R>, eTestable : Testable<E>) : Testable<Result.Result<R, E>>

public func result<R, E>(
  rTestable : Testable<R>,
  eTestable : Testable<E>,
  x : Result.Result<R, E>
) : TestableItem<Result.Result<R, E>>

public func tuple2Testable<A, B>(ta : Testable<A>, tb : Testable<B>) : Testable<(A, B)>

public func tuple2<A, B>(
  ta : Testable<A>,
  tb : Testable<B>,
  x : (A, B)
) : TestableItem<(A, B)>