assert Golden(path: string, actual: string) - > ()- Модуль
- std.
test → - Получатель
- Неприменимо
- Минимальное число аргументов
- 2
- Максимальное число аргументов
- 2
Declaration and parameters
СсылкаДоступность
Включено по умолчанию · Не определено
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.
Получатель
Ограничения параметров
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.
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.
assertGolden failed: `{path}` did not match actual outputEarlier 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.
Связанное правило · Compare existing golden text without updating it →Результат
The declared result type is ().
Эффекты
Ошибки
Result and effects
Ссылка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
СсылкаCompare existing golden text without updating it
СсылкаCompare 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.
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.
assertGolden failed: `{path}` did not match actual outputEarlier 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.
Причина
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.
Match exact line endings and an empty golden file
Ссылкаimport std.testimport std.iotest.assertGolden("expected.txt", "alpha\r\nβ\n")test.assertGolden("empty.txt", "")io.writeLine("golden text matched")
golden text matched
alphaβ
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
golden text matched
Resolve a golden path from the working directory
Ссылкаimport std.testimport std.iotest.assertGolden("expected.txt", "working directory\n")io.writeLine("working directory matched")
working directory matched
working directory
source directory
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
working directory matched
Fail when the final newline is missing from actual text
Ссылкаimport std.testimport std.ioio.writeLine("before")test.assertGolden("expected.txt", "line")io.writeLine("after")
before
line
Ожидаемый результат
Ожидаемый результат
Этап отклонения run. Ненулевой код завершения.
stdlib-assert-golden-mismatch at
main.tpz:5:1
before
Fail instead of creating a missing golden file
Ссылкаimport std.testimport std.ioio.writeLine("before")test.assertGolden("missing.txt", "new text")io.writeLine("after")
before
Ожидаемый результат
Ожидаемый результат
Этап отклонения run. Ненулевой код завершения.
stdlib-assert-golden-missing at
main.tpz:5:1
before
Fail instead of replacing invalid UTF-8 with a character
Ссылкаimport 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
Ожидаемый результат
Ожидаемый результат
Этап отклонения run. Ненулевой код завершения.
stdlib-assert-golden-invalid-utf8 at
main.tpz:9:1
before