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

    Compares actual text with a golden-file path.

    assertGolden(path: string, actual: string) -> ()
    Module
    std.test
    Receiver
    Not applicable
    Minimum arity
    2
    Maximum arity
    2

    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

    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.

    Result

    The declared result type is ().

    Effects

    Failures

    Result and effects

    Link

    Compare actual with existing UTF-8 file text exactly. A mismatch or file access failure raises TPZ4007. The assertion never updates the expected file.

    Related entries

    Link

    std.test

    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

    Diagnostics

    Link

    Assertion failed

    Menu

    Download