assert Ok<T, E>(value: Result<T, E>) - > T- Модуль
- std.
test → - Получатель
- Неприменимо
- Минимальное число аргументов
- 1
- Максимальное число аргументов
- 1
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 a Result<T, E>. It returns the T payload of Ok(value). An Err(error) raises TPZ4007.
assert accepts a Result<T, E>. It returns the E payload of Err(error). An Ok(value) raises TPZ4007.
The returned payload is unchanged. No conversion, recursive unwrapping, or equality comparison occurs. A nested Err inside an outer Ok still satisfies assertOk.
The argument is evaluated once before its outer constructor is inspected. Success prints nothing. Failure reports the assertion call and stops the current test without running its next statement. Earlier effects remain visible.
The failure messages identify the unexpected constructor and its ordinarily rendered payload.
assertOk failed: got Err({error})
assertErr failed: got Ok({value})These helpers express a test expectation. Match the Result in ordinary code when either constructor is part of normal control flow.
Связанное правило · Assert a Result constructor and return its payload →Результат
The declared result type is T.
Эффекты
Ошибки
Result and effects
СсылкаReturn the payload of an outer Ok unchanged. An outer Err raises TPZ4007 with its error.
Related entries
СсылкаAssert a Result constructor and return its payload
СсылкаAssert a Result constructor and return its payload
assert accepts a Result<T, E>. It returns the T payload of Ok(value). An Err(error) raises TPZ4007.
assert accepts a Result<T, E>. It returns the E payload of Err(error). An Ok(value) raises TPZ4007.
The returned payload is unchanged. No conversion, recursive unwrapping, or equality comparison occurs. A nested Err inside an outer Ok still satisfies assertOk.
The argument is evaluated once before its outer constructor is inspected. Success prints nothing. Failure reports the assertion call and stops the current test without running its next statement. Earlier effects remain visible.
The failure messages identify the unexpected constructor and its ordinarily rendered payload.
assertOk failed: got Err({error})
assertErr failed: got Ok({value})These helpers express a test expectation. Match the Result in ordinary code when either constructor is part of normal control flow.
Причина
Inspecting the outer constructor preserves the distinction between an expected failure value and a failed test. Returning the payload lets later assertions examine its contents without repeating the match.
Extract both Result payloads and preserve a nested Err
Ссылкаimport std.testimport std.iolet okValue: Result<int, string> = Ok(7)let errValue: Result<int, string> = Err("bad")let nested: Result<Result<int, string>, string> = Ok(Err("inner"))let value = test.assertOk(okValue)let error = test.assertErr(errValue)let inner: Result<int, string> = test.assertOk(nested)let nestedError = test.assertErr(inner)io.writeLine("{value}/{error}/{nestedError}")
7/bad/inner
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
7/bad/inner
Fail assertOk with the rendered error
Ссылкаimport std.testimport std.iolet value: Result<int, string> = Err("bad")io.writeLine("before")test.assertOk(value)io.writeLine("after")
before
Ожидаемый результат
Ожидаемый результат
Этап отклонения run. Ненулевой код завершения.
stdlib-assert-ok-err at
main.tpz:6:1
before
Fail assertErr with the rendered value
Ссылкаimport std.testimport std.iolet value: Result<int, string> = Ok(7)io.writeLine("before")test.assertErr(value)io.writeLine("after")
before
Ожидаемый результат
Ожидаемый результат
Этап отклонения run. Ненулевой код завершения.
stdlib-assert-err-ok at
main.tpz:6:1
before