assert Golden(path: string, actual: string) - > ()- 수신자
- 해당 없음
- 최소 인자 수
- 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
기대 결과
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
기대 결과
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. 종료 코드가 0이 아닙니다.
stdlib-assert-golden-invalid-utf8 at
main.tpz:9:1
before