Перейти к содержимому
Топаз6.0 · До релиза
Загрузка →

Поиск в документации

Спецификация 6.0

Выберите результат стрелками и нажмите Enter, чтобы открыть его.

Введите имя API, номер диагностики, правило или тему проекта.

    Библиотека

    std.test.assertGolden

    Compares actual text with a golden-file path.

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

    Получатель

    Ограничения параметров

    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.

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

    Результат

    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

    Ссылка

    std.test →

    Compare existing golden text without updating it

    Ссылка
    Правилоtopaz.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.

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

    Причина

    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

    Ссылка
    main.tpzMatch exact line endings and an empty golden fileОткрыть песочницу →
    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")
    Ожидаемый выводКод завершения 0
    golden text matched
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    expected.txtMatch exact line endings and an empty golden fileОткрыть песочницу →
    1. alpha
    2. β
    empty.txtMatch exact line endings and an empty golden fileОткрыть песочницу →
    Ожидаемый результат

    Ожидаемый результат

    Выполнение завершается. Код завершения 0.

    Ожидаемый стандартный вывод
    1. golden text matched

    Resolve a golden path from the working directory

    Ссылка
    nested/main.tpzResolve a golden path from the working directoryОткрыть песочницу →
    1. import std.test
    2. import std.io
    3. test.assertGolden("expected.txt", "working directory\n")
    4. io.writeLine("working directory matched")
    Ожидаемый выводКод завершения 0
    working directory matched
    
    $ topaz run nested/main.tpz · Целевой продукт 5.21.3
    expected.txtResolve a golden path from the working directoryОткрыть песочницу →
    1. working directory
    nested/expected.txtResolve a golden path from the working directoryОткрыть песочницу →
    1. source directory
    Ожидаемый результат

    Ожидаемый результат

    Выполнение завершается. Код завершения 0.

    Ожидаемый стандартный вывод
    1. working directory matched

    Fail when the final newline is missing from actual text

    Ссылка
    main.tpzFail when the final newline is missing from actual textОткрыть песочницу →
    1. import std.test
    2. import std.io
    3. io.writeLine("before")
    4. test.assertGolden("expected.txt", "line")
    5. io.writeLine("after")
    Ожидаемый выводНенулевой код завершения
    before
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    expected.txtFail when the final newline is missing from actual textОткрыть песочницу →
    1. line
    Ожидаемый результат

    Ожидаемый результат

    Этап отклонения run. Ненулевой код завершения.

    • TPZ4007 →

      stdlib-assert-golden-mismatch at main.tpz:5:1

    Ожидаемый стандартный вывод
    1. before

    Fail instead of creating a missing golden file

    Ссылка
    main.tpzFail instead of creating a missing golden fileОткрыть песочницу →
    1. import std.test
    2. import std.io
    3. io.writeLine("before")
    4. test.assertGolden("missing.txt", "new text")
    5. io.writeLine("after")
    Ожидаемый выводНенулевой код завершения
    before
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Этап отклонения run. Ненулевой код завершения.

    • TPZ4007 →

      stdlib-assert-golden-missing at main.tpz:5:1

    Ожидаемый стандартный вывод
    1. before

    Fail instead of replacing invalid UTF-8 with a character

    Ссылка
    main.tpzFail instead of replacing invalid UTF-8 with a characterОткрыть песочницу →
    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")
    Ожидаемый выводНенулевой код завершения
    before
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Этап отклонения run. Ненулевой код завершения.

    • TPZ4007 →

      stdlib-assert-golden-invalid-utf8 at main.tpz:9:1

    Ожидаемый стандартный вывод
    1. before

    Diagnostics

    Ссылка

    Assertion failed →

    Меню

    Загрузка →