assert Ok<T, E>(value: Result<T, E>) - > T- 수신자
- 해당 없음
- 최소 인자 수
- 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
기대 결과
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