assert Golden(path: string, actual: string) - > ()- Module
- std.
test - Receiver
- Not applicable
- Minimum arity
- 2
- Maximum arity
- 2
Declaration and parameters
LinkAvailability
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
- compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1962 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 71 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 448 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1962 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
b48527d0a174· 273 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1973 - compiler/crates/topaz_self_frontend/topaz/checker/call_facts.tpz at
b48527d0a174· 978 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1265
Receiver
Parameter constraints
assert 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.
Related rule: Compare existing golden text without updating itResult
The declared result type is ().
Effects
Failures
Result and effects
LinkCompare 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
LinkCompare existing golden text without updating it
LinkCompare existing golden text without updating it
assert 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
- compiler/crates/topaz_value/src/value/builtins/test.rs at
912a5e86e1f9· 134 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 98 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 192 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 214 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 238 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 163 - compiler/crates/topaz_self_frontend/topaz/emitter/emit.tpz at
912a5e86e1f9· 1004 -
topaz.stdlib.contract.test-golden-textatworking-tree - topaz.decision.test-assertions-and-golden.1 at
topaz.decisi
Match exact line endings and an empty golden file
Linkimport std.testimport std.iotest.assertGolden("expected.txt", "alpha\r\nβ\n")test.assertGolden("empty.txt", "")io.writeLine("golden text matched")
golden text matched
alphaβ
Expected result
Expected result
Execution completes. Exit code 0.
golden text matched
Source evidence
- compiler/crates/topaz_value/src/value/builtins/test.rs at
912a5e86e1f9· 134 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 98 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 192 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 214 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 238 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 163 - compiler/crates/topaz_self_frontend/topaz/emitter/emit.tpz at
912a5e86e1f9· 1004 -
topaz.stdlib.contract.test-golden-textatworking-tree - topaz.decision.test-assertions-and-golden.1 at
topaz.decisi
Resolve a golden path from the working directory
Linkimport std.testimport std.iotest.assertGolden("expected.txt", "working directory\n")io.writeLine("working directory matched")
working directory matched
working directory
source directory
Expected result
Expected result
Execution completes. Exit code 0.
working directory matched
Source evidence
- compiler/crates/topaz_value/src/value/builtins/test.rs at
912a5e86e1f9· 134 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 98 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 192 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 214 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 238 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 163 - compiler/crates/topaz_self_frontend/topaz/emitter/emit.tpz at
912a5e86e1f9· 1004 -
topaz.stdlib.contract.test-golden-textatworking-tree - topaz.decision.test-assertions-and-golden.1 at
topaz.decisi
Fail when the final newline is missing from actual text
Linkimport std.testimport std.ioio.writeLine("before")test.assertGolden("expected.txt", "line")io.writeLine("after")
before
line
Expected result
Expected result
Rejection during run. Nonzero exit.
TPZ4007stdlib-assert-golden-mismatch atmain.tpz:5:1
before
Source evidence
- compiler/crates/topaz_value/src/value/builtins/test.rs at
912a5e86e1f9· 134 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 98 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 192 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 214 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 238 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 163 - compiler/crates/topaz_self_frontend/topaz/emitter/emit.tpz at
912a5e86e1f9· 1004 -
topaz.stdlib.contract.test-golden-textatworking-tree - topaz.decision.test-assertions-and-golden.1 at
topaz.decisi
Fail instead of creating a missing golden file
Linkimport std.testimport std.ioio.writeLine("before")test.assertGolden("missing.txt", "new text")io.writeLine("after")
before
Expected result
Expected result
Rejection during run. Nonzero exit.
TPZ4007stdlib-assert-golden-missing atmain.tpz:5:1
before
Source evidence
- compiler/crates/topaz_value/src/value/builtins/test.rs at
912a5e86e1f9· 134 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 98 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 192 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 214 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 238 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 163 - compiler/crates/topaz_self_frontend/topaz/emitter/emit.tpz at
912a5e86e1f9· 1004 -
topaz.stdlib.contract.test-golden-textatworking-tree - topaz.decision.test-assertions-and-golden.1 at
topaz.decisi
Fail instead of replacing invalid UTF-8 with a character
Linkimport std.bytes { Bytes }import std.fsimport std.testimport std.iolet bytes = test.assertOk(Bytes.fromArray([255]))test.assertOk(fs.writeBytes("invalid.txt", bytes))io.writeLine("before")test.assertGolden("invalid.txt", "�")io.writeLine("after")
before
Expected result
Expected result
Rejection during run. Nonzero exit.
TPZ4007stdlib-assert-golden-invalid-utf8 atmain.tpz:9:1
before
Source evidence
- compiler/crates/topaz_value/src/value/builtins/test.rs at
912a5e86e1f9· 134 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 98 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 192 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 214 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 238 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 163 - compiler/crates/topaz_self_frontend/topaz/emitter/emit.tpz at
912a5e86e1f9· 1004 -
topaz.stdlib.contract.test-golden-textatworking-tree - topaz.decision.test-assertions-and-golden.1 at
topaz.decisi