assert
Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.
Asserts that a condition is true at runtime.
Aliases: assert
Syntax: assert <expression> [panic <expression>]
Category#
testing
Related#
Examples#
radix/corpus/reference/keyword/adfirma/adfirma.fab (canonical · keyword)#
Asserts that a condition is true at runtime.
test "assertions validate invariants" {
let int x ← 10
assert x ≻ 0
assert x ≡ 10 panic "x decem esse debet"
let string name ← "Marcus"
assert name ≡ "Marcus"
assert name ≢ "" panic "nomen vacuum non sit"
let bool viget ← true
assert viget
assert viget ≡ true panic "vigere debet"
}
# =============================================================================
# assert — Asserts that a condition is true at runtime.
# =============================================================================
#
# What this teaches:
# • Runtime assertions — `assert <condition>` validates invariants during
# program execution
# • Custom messages — `assert <condition> panic "message"` adds descriptive
# diagnostic text on failure
# • Testing category — assert is the primary assertion keyword for test
# and verification code
#
# Common mistakes:
# • using `assert` outside `test` or `describe` (WARN006); use `panic` for invariant violations in production code or move assertions into test blocks
#
# See also: test, do, catch
# =============================================================================
# assert — runtime assertions
#
# assert <condition>
# assert <condition> panic "nuntius"
#
# GRAMMAR:
# adfirmaStmt :← 'assert' expr ('panic' expr)?
#
# EXPECTED OUTPUT:
# empty — assert only (adfirma.expected)
main {
let int x ← 10
# Simple assertion without message
assert x ≻ 0
# Assertion with custom message
assert x ≡ 10 panic "x decem esse debet"
# Multiple assertions
let string name ← "Marcus"
assert name ≡ "Marcus"
assert name ≢ "" panic "nomen vacuum non sit"
# Boolean assertions
let bool viget ← true
assert viget
assert viget ≡ true panic "vigere debet"
}radix/corpus/reference/reject/adfirma/adfirma-else-reject.fab (supporting · reject)#
The assert statement has no else clause; the panic particle names the message.
# =============================================================================
# assert — no else clause
# =============================================================================
# An assertion has no alternate branch: it either holds or it fails. The
# failure message is introduced by the `panic` particle, so a trailing `else`
# is rejected instead of being folded into some conditional shape.
main {
assert 1 ≡ 1 else "m"
}Expected: compilation rejects this example.
radix/corpus/reference/reject/adfirma/adfirma-message-particle-reject.fab (supporting · reject)#
A message on the assert line needs its panic particle.
# =============================================================================
# assert — the message needs its panic particle
# =============================================================================
# A string literal on the same line right after the condition reads as a
# failure message that lost its particle. It is rejected rather than parsed as
# a separate statement, which would silently drop the message.
main {
assert 1 ≡ 1 "m"
}Expected: compilation rejects this example.
radix/corpus/reference/reject/adfirma/assert-condition-reject.fab (supporting · reject)#
The assert condition must be boolean; an int literal is refused.
# =============================================================================
# assert — the condition must be boolean
# =============================================================================
# `assert` validates an invariant, so its condition is a `bool` expression. A
# value of any other type — here an int literal — is a check error rather than
# an implicit truthiness test.
main {
assert 5
}Expected: compilation rejects this example.