Sign in

    expect

    A fluent assertion library for MoonBit

    testing
    assertions
    expect
    matchers
    Download zip
    Author
    Version
    0.2.0
    License
    Apache-2.0
    Last updated
    26 days ago
    Downloads
    24K

    No dependencies

    #moonrockz/expect

    A fluent assertion library for MoonBit.

    #Install

    moon add moonrockz/expect

    Then import the package for tests in your moon.pkg:

    import {
    "moonrockz/expect",
    } for "test"

    #Usage

    test "basic assertions" {
    // Equality
    @expect.expect(1 + 1).to_equal(2)
    @expect.expect(1).to_not_equal(2)

    // Booleans
    @expect.expect(true).to_be_true()
    @expect.expect(false).to_be_false()

    // Options
    @expect.expect(Some(42)).to_be_some()
    @expect.expect((None : Int?)).to_be_none()

    // Strings
    @expect.expect("hello world").to_contain("world")
    @expect.expect("").to_be_empty_string()
    @expect.expect("abc").to_have_length_string(3)

    // Arrays
    @expect.expect([1, 2, 3]).to_contain_element(2)
    @expect.expect(([] : Array[Int])).to_be_empty()
    @expect.expect([1, 2, 3]).to_have_length(3)
    }

    All assertions raise Failure with descriptive messages on failure.

    Failure messages use the Debug trait from moonbitlang/core/debug to show values, so strings appear quoted and escaped. Custom types must derive Debug (and Eq where the matcher compares values):

    struct Point {
    x : Int
    y : Int
    } derive(Eq, Debug)

    #License

    Apache-2.0

    Expectation

    pub struct Expectation[T] {
    actual : T
    }

    Wraps a value for fluent assertion chaining.

    Expectation::to_be_empty

    fn[T :
    Debug
    ] Expectation::to_be_empty(self : Expectation[Array[T]]) -> Unit raise

    Assert an Array is empty.

    Expectation::to_be_empty_string

    fn Expectation::to_be_empty_string(self : Expectation[String]) -> Unit raise

    Assert a String is empty.

    Expectation::to_be_false

    fn Expectation::to_be_false(self : Expectation[Bool]) -> Unit raise

    Assert the actual boolean value is false.

    Expectation::to_be_none

    fn[T :
    Debug
    ] Expectation::to_be_none(self : Expectation[T?]) -> Unit raise

    Assert the actual Option value is None.

    Expectation::to_be_some

    fn[T] Expectation::to_be_some(self : Expectation[T?]) -> Unit raise

    Assert the actual Option value is Some.

    Expectation::to_be_true

    fn Expectation::to_be_true(self : Expectation[Bool]) -> Unit raise

    Assert the actual boolean value is true.

    Expectation::to_contain

    fn Expectation::to_contain(self : Expectation[String], substring : String) -> Unit raise

    Assert a String contains the given substring.

    Expectation::to_contain_element

    fn[T : Eq +
    Debug
    ] Expectation::to_contain_element(self : Expectation[Array[T]], element : T) -> Unit raise

    Assert an Array contains the given element.

    Expectation::to_equal

    fn[T : Eq +
    Debug
    ] Expectation::to_equal(self : Expectation[T], expected : T) -> Unit raise

    Assert the actual value equals the expected value.

    Expectation::to_have_length

    fn[T :
    Debug
    ] Expectation::to_have_length(self : Expectation[Array[T]], expected : Int) -> Unit raise

    Assert an Array has the given length.

    Expectation::to_have_length_string

    fn Expectation::to_have_length_string(self : Expectation[String], expected : Int) -> Unit raise

    Assert a String has the given length.

    Expectation::to_not_equal

    fn[T : Eq +
    Debug
    ] Expectation::to_not_equal(self : Expectation[T], other : T) -> Unit raise

    Assert the actual value does not equal the given value.

    expect

    fn[T] expect(actual : T) -> Expectation[T]

    Create an expectation on a value.

    Source Files

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io