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.assertOk

    Checks for an Ok result and exposes its value.

    assertOk<T, E>(value: Result<T, E>) -> T
    Module
    std.test
    Receiver
    Not applicable
    Minimum arity
    1
    Maximum arity
    1

    Declaration and parameters

    Link

    Availability

    Included by default: Pending

    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.

    Source evidence

    Receiver

    Parameter constraints

    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.

    Result

    The declared result type is T.

    Effects

    Failures

    Result and effects

    Link

    Return the payload of an outer Ok unchanged. An outer Err raises TPZ4007 with its error.

    Related entries

    Link

    std.test

    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

    Diagnostics

    Link

    Assertion failed

    Menu

    Download