渲染zh-Hans

断言

Translation status: 简体中文 reader-locale proof. Term names and code fences follow the zh-Hans 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"
}