claims/assert_ok_eq.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
/// Asserts that the left expression contains an [`Ok(T)`] variant and its contained value of type
/// `T` equals the right expression.
///
/// ## Uses
///
/// Assertions are always checked in both debug and release builds, and cannot be disabled.
/// See [`debug_assert_ok_eq!`] for assertions that are not enabled in release builds by default.
///
/// ## Custom messages
///
/// This macro has a second form, where a custom panic message can be provided with or without
/// arguments for formatting. See [`std::fmt`] for syntax for this form.
///
/// ## Examples
///
/// ```rust
/// # #[macro_use] extern crate claims;
/// # fn main() {
/// let res: Result<i32, ()> = Ok(1);
///
/// assert_ok_eq!(res, 1);
///
/// // With a custom message
/// assert_ok_eq!(res, 1, "Everything is good with {:?}", res);
/// # }
/// ```
///
/// The contained value will be returned from the macro call:
///
/// ```rust
/// # #[macro_use] extern crate claims;
/// # fn main() {
/// let res: Result<i32, ()> = Ok(1);
///
/// let value = assert_ok_eq!(res, 1);
/// assert_eq!(value, 1);
/// # }
/// ```
///
/// An `Err(_)` variant will panic:
///
/// ```rust,should_panic
/// # #[macro_use] extern crate claims;
/// # fn main() {
/// let res: Result<i32, ()> = Err(());
///
/// assert_ok_eq!(res, 1); // Will panic
/// # }
/// ```
///
/// [`Ok(T)`]: https://doc.rust-lang.org/core/result/enum.Result.html#variant.Ok
/// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html
/// [`debug_assert_ok_eq!`]: crate::debug_assert_ok_eq!
#[macro_export]
macro_rules! assert_ok_eq {
($cond:expr, $expected:expr $(,)?) => {
match $cond {
::core::result::Result::Ok(t) => {
::core::assert_eq!(t, $expected);
t
},
e @ ::core::result::Result::Err(_) => {
::core::panic!("assertion failed, expected Ok(_), got {:?}", e);
}
}
};
($cond:expr, $expected:expr, $($arg:tt)+) => {
match $cond {
::core::result::Result::Ok(t) => {
::core::assert_eq!(t, $expected, $($arg)+);
t
},
e @ ::core::result::Result::Err(_) => {
::core::panic!("assertion failed, expected Ok(_), got {:?}: {}", e, ::core::format_args!($($arg)+));
}
}
};
}
/// Asserts that the left expression contains an [`Ok(T)`] variant and its contained value of type
/// `T` equals the right expression on debug builds.
///
/// This macro behaves nearly the same as [`assert_ok_eq!`] on debug builds, although it does not
/// return the value contained in the `Ok` variant. On release builds it is a no-op.
///
/// [`Ok(T)`]: https://doc.rust-lang.org/core/result/enum.Result.html#variant.Ok
#[macro_export]
macro_rules! debug_assert_ok_eq {
($($arg:tt)*) => {
#[cfg(debug_assertions)]
$crate::assert_ok_eq!($($arg)*);
}
}
#[cfg(test)]
mod tests {
#[test]
fn equal() {
assert_ok_eq!(Ok::<_, ()>(42), 42);
}
#[test]
#[should_panic]
fn not_equal() {
assert_ok_eq!(Ok::<_, ()>(42), 100);
}
#[test]
#[should_panic(expected = "assertion failed, expected Ok(_), got Err(())")]
fn not_ok() {
assert_ok_eq!(Err::<usize, _>(()), 42);
}
#[test]
#[should_panic(expected = "foo")]
fn not_equal_custom_message() {
assert_ok_eq!(Ok::<_, ()>(1), 2, "foo");
}
#[test]
#[should_panic(expected = "assertion failed, expected Ok(_), got Err(()): foo")]
fn not_ok_custom_message() {
assert_ok_eq!(Err::<usize, ()>(()), 2, "foo");
}
#[test]
#[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")]
fn debug_equal() {
debug_assert_ok_eq!(Ok::<_, ()>(42), 42);
}
#[test]
#[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")]
#[should_panic]
fn debug_not_equal() {
debug_assert_ok_eq!(Ok::<_, ()>(42), 100);
}
#[test]
#[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")]
#[should_panic(expected = "assertion failed, expected Ok(_), got Err(())")]
fn debug_not_ok() {
debug_assert_ok_eq!(Err::<usize, _>(()), 42);
}
#[test]
#[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")]
#[should_panic(expected = "foo")]
fn debug_not_equal_custom_message() {
debug_assert_ok_eq!(Ok::<_, ()>(1), 2, "foo");
}
#[test]
#[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")]
#[should_panic(expected = "assertion failed, expected Ok(_), got Err(()): foo")]
fn debug_not_ok_custom_message() {
debug_assert_ok_eq!(Err::<usize, ()>(()), 2, "foo");
}
#[test]
#[cfg_attr(debug_assertions, ignore = "only run in release mode")]
fn debug_release_not_equal() {
debug_assert_ok_eq!(Ok::<_, ()>(42), 100);
}
#[test]
#[cfg_attr(debug_assertions, ignore = "only run in release mode")]
fn debug_release_not_ok() {
debug_assert_ok_eq!(Err::<usize, _>(()), 42);
}
}