Skip to content
Topaz6.0 · Before release
Download

Search documentation

Specification 6.0 · Not released · Tested product 5.21.3

Use arrow keys to choose a result. Press Enter to open.

Type an API name, diagnostic number, rule, or project topic.

    Library

    std.test

    Assert ordinary value conditions and compare output with existing golden text.

    APIs in this module

    9 APIs

    NameModule
    std.test.assertstd.test
    std.test.assertContainsstd.test
    std.test.assertEqstd.test
    std.test.assertErrstd.test
    std.test.assertGoldenstd.test
    std.test.assertNestd.test
    std.test.assertNonestd.test
    std.test.assertOkstd.test
    std.test.assertSomestd.test

    Module

    Link

    Successful assertions return unit or the requested payload without printing. Failed conditions raise TPZ4007 at the call and stop the current test. Golden assertions compare existing text without updating it. Use the language for loop to repeat assertions.

    Why

    Link

    Assertions use the same comparisons and constructors as ordinary programs. A test failure identifies the expectation without changing the data used to judge it.

    Availability

    Link

    Import std.test in language mode 5.21. The current compiler selects std/test.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.

    Public API

    Link

    std.test.assert

    std.test.assertEq

    std.test.assertNe

    std.test.assertContains

    std.test.assertOk

    std.test.assertErr

    std.test.assertSome

    std.test.assertNone

    std.test.assertGolden

    Pending contracts

    Link

    Repeat assertions with for

    Link
    Ruletopaz.stdlib.contract.test-for-all.rule

    Repeat assertions with for

    Test programs iterate supplied values with the ordinary for statement. The same statement applies to integers, booleans, strings, and other iterable element types.

    std.test does not export forAllInt or forAllBool. Assertions inside a for body retain the same assertion behavior as a direct call.

    Why

    The removed helpers repeated an existing loop and added no test meaning. Ordinary iteration works for every element type and keeps loop control and assertion order visible.

    Source evidence

    Use one loop for every element type

    Link
    main.tpzUse one loop for every element typeOpen in Playground
    1. import std.test
    2. import std.io
    3. let mut sum = 0
    4. for value in ([2, 3, 5]) {
    5. sum += value
    6. }
    7. let mut trueCount = 0
    8. for value in ([true, false, true]) {
    9. if (value) { trueCount += 1 }
    10. }
    11. let mut labels = ""
    12. for value in (["A", "B", "C"]) {
    13. labels = labels + value
    14. }
    15. test.assertEq(sum, 10)
    16. test.assertEq(trueCount, 2)
    17. test.assertEq(labels, "ABC")
    18. io.writeLine("all values checked")
    Expected outputexit 0
    all values checked
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. all values checked
    Source evidence

    Reject the removed forAllInt helper

    Link
    main.tpzReject the removed forAllInt helperOpen in Playground
    1. import std.test { forAllInt }
    Expected outputNonzero exit
    No stdout.
    $ topaz check main.tpz --format json · target product 5.21.3
    Expected result

    Expected result

    Rejection during resolve. Nonzero exit.

    • TPZ3009 selected-name-not-exported at main.tpz:1:19
    Source evidence

    Reject the removed forAllBool helper

    Link
    main.tpzReject the removed forAllBool helperOpen in Playground
    1. import std.test { forAllBool }
    Expected outputNonzero exit
    No stdout.
    $ topaz check main.tpz --format json · target product 5.21.3
    Expected result

    Expected result

    Rejection during resolve. Nonzero exit.

    • TPZ3009 selected-name-not-exported at main.tpz:1:19
    Source evidence

    Diagnostics

    Link

    Name is not exported

    Assert inequality and text containment

    Link
    Ruletopaz.stdlib.contract.test-comparisons.rule

    Assert inequality and text containment

    assertNe(actual, expected) returns unit when ordinary value equality says the operands differ. It performs no string conversion before comparison.

    The operands must have a common comparable type under the ordinary equality rules. Known noncomparable values produce TPZ5007 during checking. A comparison fault propagates with its own diagnostic.

    assertContains(text, needle) returns unit when the exact needle occurs as a contiguous substring. Matching is case sensitive and preserves Unicode spelling. An empty needle succeeds even when the text is empty.

    Arguments are evaluated once from left to right before testing the condition. Successful calls have no further side effects and print nothing.

    A false assertion raises TPZ4007 at the assertion call. It returns no value and stops the current test before the next statement. Earlier argument side effects remain visible. Test runner handling of later test cases follows the test execution contract.

    The inequality failure message uses the ordinary rendering of the shared value in this form.

    ```text assertNe failed: both values are {value} ```

    The containment failure message includes the original text and needle in this form.

    ```text assertContains failed: {text} does not contain {needle} ```

    These assertions add test failure reporting to ordinary equality and string containment. They do not define a separate comparison relation.

    Why

    Tests should disagree with a program value only when the same ordinary comparison disagrees. Silent success keeps output useful, and immediate failure prevents a false premise from affecting later assertions.

    Source evidence

    Compare values and exact substrings

    Link
    main.tpzCompare values and exact substringsOpen in Playground
    1. import std.test
    2. import std.io
    3. test.assertNe([1, 2], [1, 3])
    4. let present: Option<int> = Some(1)
    5. let absent: Option<int> = None()
    6. test.assertNe(present, absent)
    7. test.assertContains("Topaz", "opa")
    8. test.assertContains("é", "é")
    9. test.assertContains("text", "")
    10. test.assertContains("", "")
    11. io.writeLine("comparisons passed")
    Expected outputexit 0
    comparisons passed
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. comparisons passed
    Source evidence

    Evaluate each operand once in source order

    Link
    main.tpzEvaluate each operand once in source orderOpen in Playground
    1. import std.test
    2. import std.io
    3. function operand(label: string, value: int) -> int {
    4. io.writeLine(label)
    5. return value
    6. }
    7. let result: () = test.assertNe(operand("actual", 3), operand("expected", 4))
    8. io.writeLine("{result}")
    Expected outputexit 0
    actual
    expected
    ()
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. actual
    2. expected
    3. ()
    Source evidence

    Stop after equal operands fail assertNe

    Link
    main.tpzStop after equal operands fail assertNeOpen in Playground
    1. import std.test
    2. import std.io
    3. function operand(label: string, value: int) -> int {
    4. io.writeLine(label)
    5. return value
    6. }
    7. test.assertNe(operand("actual", 7), operand("expected", 7))
    8. io.writeLine("after")
    Expected outputNonzero exit
    actual
    expected
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Rejection during run. Nonzero exit.

    • TPZ4007 stdlib-assert-ne-equal at main.tpz:9:1
    Expected stdout
    1. actual
    2. expected
    Source evidence

    Keep substring matching case sensitive

    Link
    main.tpzKeep substring matching case sensitiveOpen in Playground
    1. import std.test
    2. import std.io
    3. io.writeLine("before")
    4. test.assertContains("Topaz", "topaz")
    5. io.writeLine("after")
    Expected outputNonzero exit
    before
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Rejection during run. Nonzero exit.

    • TPZ4007 stdlib-assert-contains-case at main.tpz:5:1
    Expected stdout
    1. before
    Source evidence

    Keep composed and decomposed spellings distinct

    Link
    main.tpzKeep composed and decomposed spellings distinctOpen in Playground
    1. import std.test
    2. import std.io
    3. io.writeLine("before")
    4. test.assertContains("é", "é")
    5. io.writeLine("after")
    Expected outputNonzero exit
    before
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Rejection during run. Nonzero exit.

    • TPZ4007 stdlib-assert-contains-spelling at main.tpz:5:1
    Expected stdout
    1. before
    Source evidence

    Reject values that ordinary equality cannot compare

    Link
    main.tpzReject values that ordinary equality cannot compareOpen in Playground
    1. import std.test
    2. let value = () => 1
    3. test.assertNe(value, value)
    Expected outputNonzero exit
    No stdout.
    $ topaz check main.tpz --format json · target product 5.21.3
    Expected result

    Expected result

    Rejection during check. Nonzero exit.

    • TPZ5007 stdlib-assert-ne-non-comparable at main.tpz:4:1
    Source evidence

    Accept exact substrings and an empty needle

    Link
    main.tpzAccept exact substrings and an empty needleOpen in Playground
    1. import std.test
    2. import std.io
    3. test.assertContains("Topaz", "opa")
    4. test.assertContains("é", "é")
    5. test.assertContains("text", "")
    6. test.assertContains("", "")
    7. io.writeLine("text contained")
    Expected outputexit 0
    text contained
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. text contained
    Source evidence

    Diagnostics

    Link

    Assertion failed

    Value is not comparable or keyable

    Assert a Result constructor and return its payload

    Link
    Ruletopaz.stdlib.contract.test-result-variants.rule

    Assert a Result constructor and return its payload

    assertOk accepts a Result<T, E>. It returns the T payload of Ok(value). An Err(error) raises TPZ4007.

    assertErr accepts a Result<T, E>. It returns the E payload of Err(error). An Ok(value) raises TPZ4007.

    The returned payload is unchanged. No conversion, recursive unwrapping, or equality comparison occurs. A nested Err inside an outer Ok still satisfies assertOk.

    The argument is evaluated once before its outer constructor is inspected. Success prints nothing. Failure reports the assertion call and stops the current test without running its next statement. Earlier effects remain visible.

    The failure messages identify the unexpected constructor and its ordinarily rendered payload.

    ```text assertOk failed: got Err({error}) assertErr failed: got Ok({value}) ```

    These helpers express a test expectation. Match the Result in ordinary code when either constructor is part of normal control flow.

    Why

    Inspecting the outer constructor preserves the distinction between an expected failure value and a failed test. Returning the payload lets later assertions examine its contents without repeating the match.

    Source evidence

    Extract both Result payloads and preserve a nested Err

    Link
    main.tpzExtract both Result payloads and preserve a nested ErrOpen in Playground
    1. import std.test
    2. import std.io
    3. let okValue: Result<int, string> = Ok(7)
    4. let errValue: Result<int, string> = Err("bad")
    5. let nested: Result<Result<int, string>, string> = Ok(Err("inner"))
    6. let value = test.assertOk(okValue)
    7. let error = test.assertErr(errValue)
    8. let inner: Result<int, string> = test.assertOk(nested)
    9. let nestedError = test.assertErr(inner)
    10. io.writeLine("{value}/{error}/{nestedError}")
    Expected outputexit 0
    7/bad/inner
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. 7/bad/inner
    Source evidence

    Fail assertOk with the rendered error

    Link
    main.tpzFail assertOk with the rendered errorOpen in Playground
    1. import std.test
    2. import std.io
    3. let value: Result<int, string> = Err("bad")
    4. io.writeLine("before")
    5. test.assertOk(value)
    6. io.writeLine("after")
    Expected outputNonzero exit
    before
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Rejection during run. Nonzero exit.

    • TPZ4007 stdlib-assert-ok-err at main.tpz:6:1
    Expected stdout
    1. before
    Source evidence

    Fail assertErr with the rendered value

    Link
    main.tpzFail assertErr with the rendered valueOpen in Playground
    1. import std.test
    2. import std.io
    3. let value: Result<int, string> = Ok(7)
    4. io.writeLine("before")
    5. test.assertErr(value)
    6. io.writeLine("after")
    Expected outputNonzero exit
    before
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Rejection during run. Nonzero exit.

    • TPZ4007 stdlib-assert-err-ok at main.tpz:6:1
    Expected stdout
    1. before
    Source evidence

    Assert presence or absence

    Link
    Ruletopaz.stdlib.contract.test-option-variants.rule

    Assert presence or absence

    assertSome accepts an Option<T> and returns the T payload of Some(value). Passing None() raises TPZ4007.

    assertNone accepts an Option<T> and returns unit for None(). Passing Some(value) raises TPZ4007.

    Presence depends on the outer constructor. Some(None()), Some(false), and Some(0) are present values. assertSome returns their payloads unchanged.

    Each argument is evaluated once. A successful assertion prints nothing and performs no additional effects. Failure reports the assertion call and stops the current test before its next statement.

    The failure messages identify the unexpected constructor. A present payload uses ordinary value rendering.

    ```text assertSome failed: got None assertNone failed: got Some({value}) ```

    Use a match to handle absence as normal program behavior. These assertions report a failed test when the chosen presence expectation is false.

    Why

    Outer constructor checks keep absence separate from a stored false, zero, or absent nested value. They give tests the same Option meaning as ordinary pattern matching.

    Source evidence

    Distinguish nested absence from an absent outer value

    Link
    main.tpzDistinguish nested absence from an absent outer valueOpen in Playground
    1. import std.test
    2. import std.io
    3. let present: Option<int> = Some(7)
    4. let absent: Option<int> = None()
    5. let nested: Option<Option<int>> = Some(None())
    6. let value = test.assertSome(present)
    7. let result: () = test.assertNone(absent)
    8. test.assertNone(test.assertSome(nested))
    9. io.writeLine("{value}/{result}")
    Expected outputexit 0
    7/()
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. 7/()
    Source evidence

    Fail assertSome on an absent value

    Link
    main.tpzFail assertSome on an absent valueOpen in Playground
    1. import std.test
    2. import std.io
    3. let value: Option<int> = None()
    4. io.writeLine("before")
    5. test.assertSome(value)
    6. io.writeLine("after")
    Expected outputNonzero exit
    before
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Rejection during run. Nonzero exit.

    • TPZ4007 stdlib-assert-some-none at main.tpz:6:1
    Expected stdout
    1. before
    Source evidence

    Fail assertNone with the present payload

    Link
    main.tpzFail assertNone with the present payloadOpen in Playground
    1. import std.test
    2. import std.io
    3. let value: Option<int> = Some(7)
    4. io.writeLine("before")
    5. test.assertNone(value)
    6. io.writeLine("after")
    Expected outputNonzero exit
    before
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Rejection during run. Nonzero exit.

    • TPZ4007 stdlib-assert-none-some at main.tpz:6:1
    Expected stdout
    1. before
    Source evidence

    Compare existing golden text without updating it

    Link
    Ruletopaz.stdlib.contract.test-golden-text.rule

    Compare existing golden text without updating it

    assertGolden(path, actual) accepts two strings. Read the existing file as UTF-8 and compare the entire decoded text with actual. UTF-8 validity and text preservation follow UTF-8 decoding. Equal text returns unit without printing.

    Resolve the path using the same host path and read permission rules as filesystem text reads. For an ordinary installed CLI program, a relative path starts at the process working directory. The source file directory does not replace that directory.

    Comparison preserves every scalar and line ending. CRLF and LF differ. A final newline, a byte order mark, Unicode spelling, and an empty file remain significant. Invalid UTF-8 is a read failure.

    Evaluate the path expression and then the actual expression, once each, before reading the file. Open and read the expected text once. Release any opened resource before returning or reporting a read or comparison failure.

    The assertion needs read access. It never creates a missing golden file, replaces its contents, or enables an update mode. Write a chosen expected result explicitly through the filesystem API.

    Missing files, directories that cannot be read as text, permission denial, and invalid UTF-8 raise TPZ4007 at the assertion call. File failures identify the path and whether opening or reading failed. The host detail explains the cause and may vary by platform.

    The file failure messages have these forms.

    ```text assertGolden failed: could not open {path}: {detail} assertGolden failed: could not read {path}: {detail} ```

    Unequal text raises TPZ4007 and stops the current test before its next statement. The message has this form.

    ```text assertGolden failed: {path} did not match actual output ```

    Earlier argument effects remain visible after failure. Reading does not promise a snapshot across concurrent host writes. Keep the expected file unchanged while the assertion reads it.

    Why

    An existing expected file gives the test an independent reference. Exact comparison preserves document bytes that matter to generated text, and explicit writes keep failed tests from replacing their own expectations.

    Source evidence

    Match exact line endings and an empty golden file

    Link
    main.tpzMatch exact line endings and an empty golden fileOpen in Playground
    1. import std.test
    2. import std.io
    3. test.assertGolden("expected.txt", "alpha\r\nβ\n")
    4. test.assertGolden("empty.txt", "")
    5. io.writeLine("golden text matched")
    Expected outputexit 0
    golden text matched
    
    $ topaz run main.tpz · target product 5.21.3
    expected.txtMatch exact line endings and an empty golden fileOpen in Playground
    1. alpha
    2. β
    empty.txtMatch exact line endings and an empty golden fileOpen in Playground
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. golden text matched
    Source evidence

    Resolve a golden path from the working directory

    Link
    nested/main.tpzResolve a golden path from the working directoryOpen in Playground
    1. import std.test
    2. import std.io
    3. test.assertGolden("expected.txt", "working directory\n")
    4. io.writeLine("working directory matched")
    Expected outputexit 0
    working directory matched
    
    $ topaz run nested/main.tpz · target product 5.21.3
    expected.txtResolve a golden path from the working directoryOpen in Playground
    1. working directory
    nested/expected.txtResolve a golden path from the working directoryOpen in Playground
    1. source directory
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. working directory matched
    Source evidence

    Fail when the final newline is missing from actual text

    Link
    main.tpzFail when the final newline is missing from actual textOpen in Playground
    1. import std.test
    2. import std.io
    3. io.writeLine("before")
    4. test.assertGolden("expected.txt", "line")
    5. io.writeLine("after")
    Expected outputNonzero exit
    before
    
    $ topaz run main.tpz · target product 5.21.3
    expected.txtFail when the final newline is missing from actual textOpen in Playground
    1. line
    Expected result

    Expected result

    Rejection during run. Nonzero exit.

    • TPZ4007 stdlib-assert-golden-mismatch at main.tpz:5:1
    Expected stdout
    1. before
    Source evidence

    Fail instead of creating a missing golden file

    Link
    main.tpzFail instead of creating a missing golden fileOpen in Playground
    1. import std.test
    2. import std.io
    3. io.writeLine("before")
    4. test.assertGolden("missing.txt", "new text")
    5. io.writeLine("after")
    Expected outputNonzero exit
    before
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Rejection during run. Nonzero exit.

    • TPZ4007 stdlib-assert-golden-missing at main.tpz:5:1
    Expected stdout
    1. before
    Source evidence

    Fail instead of replacing invalid UTF-8 with a character

    Link
    main.tpzFail instead of replacing invalid UTF-8 with a characterOpen in Playground
    1. import std.bytes { Bytes }
    2. import std.fs
    3. import std.test
    4. import std.io
    5. let bytes = test.assertOk(Bytes.fromArray([255]))
    6. test.assertOk(fs.writeBytes("invalid.txt", bytes))
    7. io.writeLine("before")
    8. test.assertGolden("invalid.txt", "�")
    9. io.writeLine("after")
    Expected outputNonzero exit
    before
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Rejection during run. Nonzero exit.

    • TPZ4007 stdlib-assert-golden-invalid-utf8 at main.tpz:9:1
    Expected stdout
    1. before
    Source evidence

    Menu

    Download