Skip to content
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

Add IgnoreErr variant to Unwrap enum #57

Open
apollo-sturdy opened this issue May 1, 2024 · 1 comment
Open

Add IgnoreErr variant to Unwrap enum #57

apollo-sturdy opened this issue May 1, 2024 · 1 comment

Comments

@apollo-sturdy
Copy link
Contributor

No description provided.

@pacmanifold
Copy link
Contributor

/// An enum to choose which type of unwrap to use. When using `Unwrap::Err`, the
/// result must be an `Err` or the test will panic. If the result contains an
/// `Err`, the test will pass only if the error message contains the provided
/// string.
pub enum Unwrap {
    /// Unwrap the result, expecting it to be `Ok`.
    Ok,
    /// Unwrap the result, expecting it to be `Err`. The error message must
    /// contain the provided string.
    Err(&'static str),
    /// Call `ok()` on the result, returning `None` if it is an `Err`.
    IgnoreErr,
}

impl Unwrap {
    pub fn unwrap<T: Debug, E: Debug>(self, result: Result<T, E>) -> Option<T> {
        match self {
            Unwrap::Ok => {
                let res = result.unwrap();
                Some(res)
            }
            Unwrap::Err(s) => {
                let err = result.unwrap_err();
                assert!(
                    format!("{:?}", err).contains(s),
                    "Expected error message to contain {:?}, got {:?}",
                    s,
                    err
                );
                None
            }
            Unwrap::IgnoreErr => result.ok(),
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants