渲染zh-Hant

斷言

Translation status: 繁體中文 reader-locale proof. Term names and code fences follow the zh-Hant pack; supporting prose may still be English.

Asserts that a condition is true at runtime.

Aliases: assert

Syntax: 斷言 <expression> [崩潰 <expression>]

Category#

testing

Examples#

radix/corpus/adfirma/adfirma.fab (canonical · keyword)#

Asserts that a condition is true at runtime.

test "assertions validate invariants" {
    const int x ← 10
    assert x ≻ 0
    assert x ≡ 10 panic "x decem esse debet"
    const string 名稱 ← "Marcus"
    assert 名稱 ≡ "Marcus"
    assert 名稱 ≠ "" panic "nomen vacuum non sit"
    const bool viget ← true
    assert viget
    assert viget ≡ true panic "vigere debet"
}

# =============================================================================
# 斷言 — Asserts that a condition is true at runtime.
# =============================================================================
#
# What this teaches:
#   • Runtime assertions — `斷言 <condition>` validates invariants during
#     program execution
#   • Custom messages — `斷言 <condition> 崩潰 "message"` adds descriptive
#     diagnostic text on failure
#   • Testing category — 斷言 is the primary assertion keyword for test
#     and verification code
#
# Common mistakes:
#   • using `斷言` outside `測試` or `測試規格` (WARN006); use `崩潰` for invariant violations in production code or move assertions into test blocks
#
# See also: 測試, 執行, 捕捉
# =============================================================================

# 斷言 — runtime assertions
#
# 斷言 <condition>
# 斷言 <condition> 崩潰 "nuntius"
#
# GRAMMAR:
#   adfirmaStmt :← '斷言' expr ('崩潰' expr)?
#
# EXPECTED OUTPUT:
#   empty — 斷言 only (adfirma.expected)

main {
    const int x ← 10

    # Simple assertion without message
    assert x ≻ 0

    # Assertion with custom message
    assert x ≡ 10 panic "x decem esse debet"

    # Multiple assertions
    const string 名稱 ← "Marcus"
    assert 名稱 ≡ "Marcus"
    assert 名稱 ≠ "" panic "nomen vacuum non sit"

    # Boolean assertions
    const bool viget ← true
    assert viget
    assert viget ≡ true panic "vigere debet"
}