Panic on failure instead of returning Result - #15
Merged
Merged
Conversation
shimforge is for writing tests, where a failed setup should just fail the test. Returning Result made every test add .unwrap(), or ? with a final Ok(()). With ?, the drop check of a pending expectation could also replace the real setup error with "expected at least 1 calls, observed 0". Session::new, new_local and new_global, mock!, replace!, mock_async, replace_raw, every expectation response (returns, returning, never and the rest), and verify, checkpoint and restore now return their values directly and panic with the error's message on failure. They and the generated mock methods use #[track_caller], so a failure points at the test line. try_new_local and try_new_global still return Result<Session, Error>, because reporting a busy session is their purpose. Internally the installation, rule and verification code still returns Result, and a hidden check helper turns an error into a panic at the public boundary. restore still removes every mock before it panics, so the drop check does not panic a second time. Tests drop their unwrap calls and ? operators. Assertions that expected an error use a panic_message helper and compare the message with the error's Display text. The README examples and tests/readme.rs no longer return Result. The compile_fail doctests no longer call unwrap, so they still fail for the reason they document. Checked locally with cargo fmt --check, cargo clippy --workspace --all-targets -- -D warnings on x86_64-pc-windows-msvc, aarch64-pc-windows-msvc, aarch64-unknown-linux-gnu, aarch64-apple-darwin and x86_64-apple-darwin, and cargo test --lib (69 passed). Integration tests and doctests run in CI.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Session::new,mock!,replace!, the expectation methods,verify,checkpointandrestorenow panic on failure instead of returningResult. Tests no longer need.unwrap(),?orOk(()).This also stops a pending expectation from hiding the real setup error when
?returned early.Session::try_new_localandSession::try_new_globalstill returnResult. Nothing was published yet, so no released version breaks.