module assert

module lib/assert.mu

import "assert"

assert.mu keeps the core assertion helpers used by the lib tests and tooling so callers can document invariants and fail fast when expectations are violated.

Imports

Functions

assert#

fn assert(condition, message...)

assert halts execution when the provided condition is not truthy. An optional message slice is inspected to explain what went wrong.

Source lib/assert.mu:56
fn assert(condition, message...) {
    if truthy(condition) {
        return
    }
    msg := nil
    if len(message) > 0 {
        msg = message[0]
    }
    text := "assertion failed"
    if msg != nil {
        text = inspect(msg)
    }
    _panic_assert(text)
}

container_has#

fn container_has(container, value)

container_has is the shared implementation used by contains to test lists, strings, and maps for a presence / membership match.

Source lib/assert.mu:100
fn container_has(container, value) {
    typ := type(container)
    if typ == "LIST" {
        return list_contains(container, value)
    }
    if typ == "STRING" {
        if !typing.is_str(value) {
            _panic_assert("assert_contains string container requires string value")
        }
        return strings.contains(container, value)
    }
    if typ == "MAP" {
        return map_contains(container, value)
    }
    _panic_assert("assert_contains requires a list, map, or string container, got " + typ)
}

contains#

fn contains(container, value, message...)

contains asserts that the provided container already holds the value.

Source lib/assert.mu:90
fn contains(container, value, message...) {
    if container_has(container, value) {
        return
    }
    detail := "assert_contains failed: container " + inspect(container) + " missing " + inspect(value)
    assert(false, format_assert_message(detail, message))
}

equal#

fn equal(actual, expected, message...)

equal asserts that two values are identical.

Source lib/assert.mu:81
fn equal(actual, expected, message...) {
    if actual == expected {
        return
    }
    detail := "assert_equal failed: expected " + inspect(expected) + ", got " + inspect(actual)
    assert(false, format_assert_message(detail, message))
}

format_assert_message#

fn format_assert_message(base, message)

format_assert_message appends the first optional message to a base detail string so callers can keep structured error text.

Source lib/assert.mu:73
fn format_assert_message(base, message) {
    if len(message) == 0 {
        return base
    }
    return base + ": " + inspect(message[0])
}

list_contains#

fn list_contains(list, value)

list_contains scans a list for an exact match using the normal equality rules.

Source lib/assert.mu:118
fn list_contains(list, value) {
    i := 0
    length := len(list)
    while i < length {
        if list[i] == value {
            return true
        }
        i = i + 1
    }
    return false
}

map_contains#

fn map_contains(m, value)

map_contains walks the map keys and returns true when a key equals value.

Source lib/assert.mu:131
fn map_contains(m, value) {
    keys_list := keys(m)
    i := 0
    length := len(keys_list)
    while i < length {
        if keys_list[i] == value {
            return true
        }
        i = i + 1
    }
    return false
}

truthy#

fn truthy(value)

truthy implements the rule used by the mu REPL and runtime to decide whether a value should be treated as true inside conditionals and assertions.

Source lib/assert.mu:31
fn truthy(value) {
    typ := type(value)
    if typ == "BOOLEAN" {
        return value
    }
    if typ == "NULL" {
        return false
    }
    if typ == "INTEGER" {
        return value != 0
    }
    if typ == "STRING" {
        return len(value) != 0
    }
    if typ == "LIST" {
        return len(value) != 0
    }
    if typ == "MAP" {
        return len(value) != 0
    }
    return true
}

Internal helpers

Underscore-prefixed names are implementation detail. They are listed so the module's source reads without surprises, not as API — they may change at any time.

_panic_assert(text)
_stderr_write(text)