-
Notifications
You must be signed in to change notification settings - Fork 7
Add support for #[should_panic] macro
#151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+397
−19
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7f83351
fix: Recursive parsing of attributes
skogseth 94e0434
feat: Add functions `assert_panic` and `assert_panic_contains`
skogseth 1dae3b5
feat: Add support for '#[should_panic]' macro
skogseth 2d7b1cc
fix: Add tests for '#[should_panic]' macro
skogseth 23e0c85
docs: Document should_panic macro deviation
skogseth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| //! This module contains functionality related to handling panics | ||
|
|
||
| use std::borrow::Cow; | ||
|
|
||
| const DID_NOT_PANIC: &str = "test did not panic as expected"; | ||
|
|
||
| /// Error returned by [`assert_panic`] and [`assert_panic_contains`] | ||
| #[derive(Debug)] | ||
| pub struct AssertPanicError(Cow<'static, str>); | ||
|
|
||
| impl std::fmt::Display for AssertPanicError { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| std::fmt::Display::fmt(&self.0, f) | ||
| } | ||
| } | ||
|
|
||
| impl std::error::Error for AssertPanicError {} | ||
|
|
||
| /// Assert that a piece of code is intended to panic | ||
| /// | ||
| /// This will wrap the provided closure and check the result for a panic. If the function fails to panic | ||
| /// an error value is returned, otherwise `Ok(())` is returned. | ||
| /// | ||
| /// ```rust | ||
| /// # use libtest2::panic::assert_panic; | ||
| /// fn panicky_test() { | ||
| /// panic!("intentionally fails"); | ||
| /// } | ||
| /// | ||
| /// let result = assert_panic(panicky_test); | ||
| /// assert!(result.is_ok()); | ||
| /// ``` | ||
| /// | ||
| /// If you also want to check that the panic contains a specific message see [`assert_panic_contains`]. | ||
| /// | ||
| /// # Notes | ||
| /// This function will wrap the provided closure with a call to [`catch_unwind`](`std::panic::catch_unwind`), | ||
| /// and will therefore inherit the caveats of this function, most notably that it will be unable to catch | ||
| /// panics if they are not implemented via unwinding. | ||
| pub fn assert_panic<T, F: FnOnce() -> T>(f: F) -> Result<(), AssertPanicError> { | ||
| match std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)) { | ||
| // The test should have panicked, but didn't. | ||
| Ok(_) => Err(AssertPanicError(Cow::Borrowed(DID_NOT_PANIC))), | ||
|
|
||
| // The test panicked, as expected. | ||
| Err(_) => Ok(()), | ||
| } | ||
| } | ||
|
|
||
| /// Assert that a piece of code is intended to panic with a specific message | ||
| /// | ||
| /// This will wrap the provided closure and check the result for a panic. If the function fails to panic with | ||
| /// a message that contains the expected string an error value is returned, otherwise `Ok(())` is returned. | ||
| /// | ||
| /// ```rust | ||
| /// # use libtest2::panic::assert_panic_contains; | ||
| /// fn panicky_test() { | ||
| /// panic!("intentionally fails"); | ||
| /// } | ||
| /// | ||
| /// let result = assert_panic_contains(panicky_test, "fail"); | ||
| /// assert!(result.is_ok()); | ||
| /// | ||
| /// let result = assert_panic_contains(panicky_test, "can't find this"); | ||
| /// assert!(result.is_err()); | ||
| /// ``` | ||
| /// | ||
| /// If you don't want to check that the panic contains a specific message see [`assert_panic`]. | ||
| /// | ||
| /// # Notes | ||
| /// This function will wrap the provided closure with a call to [`catch_unwind`](`std::panic::catch_unwind`), | ||
| /// and will therefore inherit the caveats of this function, most notably that it will be unable to catch | ||
| /// panics if they are not implemented via unwinding. | ||
| pub fn assert_panic_contains<T, F: FnOnce() -> T>( | ||
| f: F, | ||
| expected: &str, | ||
| ) -> Result<(), AssertPanicError> { | ||
| match std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)) { | ||
| // The test should have panicked, but didn't. | ||
| Ok(_) => Err(AssertPanicError(Cow::Borrowed(DID_NOT_PANIC))), | ||
|
|
||
| // The test panicked, as expected, but we need to check the panic message | ||
| Err(payload) => check_panic_message(&*payload, expected), | ||
| } | ||
| } | ||
|
|
||
| #[cold] | ||
| fn check_panic_message( | ||
| payload: &dyn std::any::Any, | ||
| expected: &str, | ||
| ) -> Result<(), AssertPanicError> { | ||
| // The `panic` information is just an `Any` object representing the | ||
| // value the panic was invoked with. For most panics (which use | ||
| // `panic!` like `println!`), this is either `&str` or `String`. | ||
| let maybe_panic_str = payload | ||
| .downcast_ref::<String>() | ||
| .map(|s| s.as_str()) | ||
| .or_else(|| payload.downcast_ref::<&str>().copied()); | ||
|
|
||
| // Check the panic message against the expected message. | ||
| match maybe_panic_str { | ||
| Some(panic_str) if panic_str.contains(expected) => Ok(()), | ||
|
|
||
| Some(panic_str) => { | ||
| let error_msg = ::std::format!( | ||
| r#"panic did not contain expected string | ||
| panic message: {panic_str:?} | ||
| expected substring: {expected:?}"# | ||
| ); | ||
|
|
||
| Err(AssertPanicError(Cow::Owned(error_msg))) | ||
| } | ||
|
|
||
| None => { | ||
| let type_id = (*payload).type_id(); | ||
| let error_msg = ::std::format!( | ||
| r#"expected panic with string value, | ||
| found non-string value: `{type_id:?}` | ||
| expected substring: {expected:?}"#, | ||
| ); | ||
|
|
||
| Err(AssertPanicError(Cow::Owned(error_msg))) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn assert_panic_with_panic() { | ||
| let result = assert_panic(|| panic!("some message")); | ||
| result.unwrap(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn assert_panic_no_panic() { | ||
| let result = assert_panic(|| { /* do absolutely nothing */ }); | ||
| let error = result.unwrap_err(); | ||
| assert_eq!(error.to_string(), DID_NOT_PANIC); | ||
| } | ||
|
|
||
| #[test] | ||
| fn assert_panic_contains_correct_panic_message() { | ||
| let result = assert_panic_contains(|| panic!("some message"), "mess"); | ||
| result.unwrap(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn assert_panic_contains_no_panic() { | ||
| let result = assert_panic_contains(|| { /* do absolutely nothing */ }, "fail"); | ||
| let error = result.unwrap_err(); | ||
| assert_eq!(error.to_string(), DID_NOT_PANIC); | ||
| } | ||
|
|
||
| #[test] | ||
| fn assert_panic_contains_wrong_panic_message() { | ||
| let result = assert_panic_contains(|| panic!("some message"), "fail"); | ||
| let error = result.unwrap_err(); | ||
| assert_eq!( | ||
| error.0, | ||
| r#"panic did not contain expected string | ||
| panic message: "some message" | ||
| expected substring: "fail""# | ||
| ); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ mod all_passing; | |
| mod argfile; | ||
| mod mixed_bag; | ||
| mod panic; | ||
| mod should_panic; | ||
| mod util; | ||
|
|
||
| pub use util::*; | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.