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

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

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

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

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

    Библиотека

    std.test.assertContains

    Checks that text contains a requested string.

    assertContains(text: string, needle: 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.

    Получатель

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

    assertNe(actual, expected) returns unit when ordinary value equality says the operands differ. It performs no string conversion before comparison.

    The operands must have a common comparable type under the ordinary equality rules. Known noncomparable values produce TPZ5007 during checking. A comparison fault propagates with its own diagnostic.

    assertContains(text, needle) returns unit when the exact needle occurs as a contiguous substring. Matching is case sensitive and preserves Unicode spelling. An empty needle succeeds even when the text is empty.

    Arguments are evaluated once from left to right before testing the condition. Successful calls have no further side effects and print nothing.

    A false assertion raises TPZ4007 at the assertion call. It returns no value and stops the current test before the next statement. Earlier argument side effects remain visible. Test runner handling of later test cases follows the test execution contract.

    The inequality failure message uses the ordinary rendering of the shared value in this form.

    assertNe failed: both values are `{value}`

    The containment failure message includes the original text and needle in this form.

    assertContains failed: `{text}` does not contain `{needle}`

    These assertions add test failure reporting to ordinary equality and string containment. They do not define a separate comparison relation.

    Результат

    The declared result type is ().

    Эффекты

    Ошибки

    Result and effects

    Ссылка

    Return unit when text contains the exact needle. An empty needle succeeds. A missing needle raises TPZ4007 with both strings.

    Related entries

    Ссылка

    std.test →

    Assert inequality and text containment

    Ссылка
    Правилоtopaz.stdlib.contract.test-comparisons.rule

    Assert inequality and text containment

    assertNe(actual, expected) returns unit when ordinary value equality says the operands differ. It performs no string conversion before comparison.

    The operands must have a common comparable type under the ordinary equality rules. Known noncomparable values produce TPZ5007 during checking. A comparison fault propagates with its own diagnostic.

    assertContains(text, needle) returns unit when the exact needle occurs as a contiguous substring. Matching is case sensitive and preserves Unicode spelling. An empty needle succeeds even when the text is empty.

    Arguments are evaluated once from left to right before testing the condition. Successful calls have no further side effects and print nothing.

    A false assertion raises TPZ4007 at the assertion call. It returns no value and stops the current test before the next statement. Earlier argument side effects remain visible. Test runner handling of later test cases follows the test execution contract.

    The inequality failure message uses the ordinary rendering of the shared value in this form.

    assertNe failed: both values are `{value}`

    The containment failure message includes the original text and needle in this form.

    assertContains failed: `{text}` does not contain `{needle}`

    These assertions add test failure reporting to ordinary equality and string containment. They do not define a separate comparison relation.

    Причина

    Tests should disagree with a program value only when the same ordinary comparison disagrees. Silent success keeps output useful, and immediate failure prevents a false premise from affecting later assertions.

    Compare values and exact substrings

    Ссылка
    main.tpzCompare values and exact substringsОткрыть песочницу →
    1. import std.test
    2. import std.io
    3. test.assertNe([1, 2], [1, 3])
    4. let present: Option<int> = Some(1)
    5. let absent: Option<int> = None()
    6. test.assertNe(present, absent)
    7. test.assertContains("Topaz", "opa")
    8. test.assertContains("é", "é")
    9. test.assertContains("text", "")
    10. test.assertContains("", "")
    11. io.writeLine("comparisons passed")
    Ожидаемый выводКод завершения 0
    comparisons passed
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

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

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

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

    Evaluate each operand once in source order

    Ссылка
    main.tpzEvaluate each operand once in source orderОткрыть песочницу →
    1. import std.test
    2. import std.io
    3. function operand(label: string, value: int) -> int {
    4. io.writeLine(label)
    5. return value
    6. }
    7. let result: () = test.assertNe(operand("actual", 3), operand("expected", 4))
    8. io.writeLine("{result}")
    Ожидаемый выводКод завершения 0
    actual
    expected
    ()
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

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

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

    Ожидаемый стандартный вывод
    1. actual
    2. expected
    3. ()

    Stop after equal operands fail assertNe

    Ссылка
    main.tpzStop after equal operands fail assertNeОткрыть песочницу →
    1. import std.test
    2. import std.io
    3. function operand(label: string, value: int) -> int {
    4. io.writeLine(label)
    5. return value
    6. }
    7. test.assertNe(operand("actual", 7), operand("expected", 7))
    8. io.writeLine("after")
    Ожидаемый выводНенулевой код завершения
    actual
    expected
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

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

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

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

    Keep substring matching case sensitive

    Ссылка
    main.tpzKeep substring matching case sensitiveОткрыть песочницу →
    1. import std.test
    2. import std.io
    3. io.writeLine("before")
    4. test.assertContains("Topaz", "topaz")
    5. io.writeLine("after")
    Ожидаемый выводНенулевой код завершения
    before
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

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

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

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

    Keep composed and decomposed spellings distinct

    Ссылка
    main.tpzKeep composed and decomposed spellings distinctОткрыть песочницу →
    1. import std.test
    2. import std.io
    3. io.writeLine("before")
    4. test.assertContains("é", "é")
    5. io.writeLine("after")
    Ожидаемый выводНенулевой код завершения
    before
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

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

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

    • TPZ4007 →

      stdlib-assert-contains-spelling at main.tpz:5:1

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

    Reject values that ordinary equality cannot compare

    Ссылка
    main.tpzReject values that ordinary equality cannot compareОткрыть песочницу →
    1. import std.test
    2. let value = () => 1
    3. test.assertNe(value, value)
    Ожидаемый выводНенулевой код завершения
    Нет стандартного вывода.
    $ topaz check main.tpz --format json · Целевой продукт 5.21.3
    Ожидаемый результат

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

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

    • TPZ5007 →

      stdlib-assert-ne-non-comparable at main.tpz:4:1

    Accept exact substrings and an empty needle

    Ссылка
    main.tpzAccept exact substrings and an empty needleОткрыть песочницу →
    1. import std.test
    2. import std.io
    3. test.assertContains("Topaz", "opa")
    4. test.assertContains("é", "é")
    5. test.assertContains("text", "")
    6. test.assertContains("", "")
    7. io.writeLine("text contained")
    Ожидаемый выводКод завершения 0
    text contained
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

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

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

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

    Diagnostics

    Ссылка

    Assertion failed →

    Value is not comparable or keyable →

    Меню

    Загрузка →