assertables/assert_ok/assert_ok_eq_x.rs
1//! Assert an expression is Ok and its value is equal to an expression.
2//!
3//! Pseudocode:<br>
4//! (a ⇒ Ok(a1) ⇒ a1) = b
5//!
6//! # Example
7//!
8//! ```rust
9//! use assertables::*;
10//!
11//! let a: Result<i8, i8> = Ok(1);
12//! let b: i8 = 1;
13//! assert_ok_eq_x!(a, b);
14//! ```
15//!
16//! # Module macros
17//!
18//! * [`assert_ok_eq_x`](macro@crate::assert_ok_eq_x)
19//! * [`assert_ok_eq_x_as_result`](macro@crate::assert_ok_eq_x_as_result)
20//! * [`debug_assert_ok_eq_x`](macro@crate::debug_assert_ok_eq_x)
21
22/// Assert an expression is Ok and its value is equal to an expression.
23///
24/// Pseudocode:<br>
25/// (a ⇒ Ok(a1) ⇒ a1) = b
26///
27/// * If true, return Result `Ok(a1)`.
28///
29/// * Otherwise, return Result `Err(message)`.
30///
31/// This macro is useful for runtime checks, such as checking parameters,
32/// or sanitizing inputs, or handling different results in different ways.
33///
34/// # Module macros
35///
36/// * [`assert_ok_eq_x`](macro@crate::assert_ok_eq_x)
37/// * [`assert_ok_eq_x_as_result`](macro@crate::assert_ok_eq_x_as_result)
38/// * [`debug_assert_ok_eq_x`](macro@crate::debug_assert_ok_eq_x)
39///
40#[macro_export]
41macro_rules! assert_ok_eq_x_as_result {
42 ($a:expr, $b:expr $(,)?) => {
43 match ($a, $b) {
44 (a, b) => match (a) {
45 Ok(a1) => {
46 if a1 == b {
47 Ok(a1)
48 } else {
49 Err(format!(
50 concat!(
51 "assertion failed: `assert_ok_eq_x!(a, b)`\n",
52 "https://docs.rs/assertables/9.8.3/assertables/macro.assert_ok_eq_x.html\n",
53 " a label: `{}`,\n",
54 " a debug: `{:?}`,\n",
55 " a inner: `{:?}`,\n",
56 " b label: `{}`,\n",
57 " b debug: `{:?}`",
58 ),
59 stringify!($a),
60 a,
61 a1,
62 stringify!($b),
63 b
64 ))
65 }
66 }
67 _ => Err(format!(
68 concat!(
69 "assertion failed: `assert_ok_eq_x!(a, b)`\n",
70 "https://docs.rs/assertables/9.8.3/assertables/macro.assert_ok_eq_x.html\n",
71 " a label: `{}`,\n",
72 " a debug: `{:?}`,\n",
73 " b label: `{}`,\n",
74 " b debug: `{:?}`",
75 ),
76 stringify!($a),
77 a,
78 stringify!($b),
79 b
80 )),
81 },
82 }
83 };
84}
85
86#[cfg(test)]
87mod test_assert_ok_eq_x_as_result {
88 use std::sync::Once;
89
90 #[test]
91 fn eq() {
92 let a: Result<i8, i8> = Ok(1);
93 let b: i8 = 1;
94 for _ in 0..1 {
95 let actual = assert_ok_eq_x_as_result!(a, b);
96 assert_eq!(actual.unwrap(), 1);
97 }
98 }
99
100 #[test]
101 fn eq_once() {
102 static A: Once = Once::new();
103 fn a() -> Result<i8, i8> {
104 if A.is_completed() {
105 panic!("A.is_completed()")
106 } else {
107 A.call_once(|| {})
108 }
109 Ok(1)
110 }
111
112 static B: Once = Once::new();
113 fn b() -> i8 {
114 if B.is_completed() {
115 panic!("B.is_completed()")
116 } else {
117 B.call_once(|| {})
118 }
119 1
120 }
121
122 assert_eq!(A.is_completed(), false);
123 assert_eq!(B.is_completed(), false);
124 let result = assert_ok_eq_x_as_result!(a(), b());
125 assert!(result.is_ok());
126 assert_eq!(A.is_completed(), true);
127 assert_eq!(B.is_completed(), true);
128 }
129
130 #[test]
131 fn ne() {
132 let a: Result<i8, i8> = Ok(1);
133 let b: i8 = 2;
134 let actual = assert_ok_eq_x_as_result!(a, b);
135 let message = concat!(
136 "assertion failed: `assert_ok_eq_x!(a, b)`\n",
137 "https://docs.rs/assertables/9.8.3/assertables/macro.assert_ok_eq_x.html\n",
138 " a label: `a`,\n",
139 " a debug: `Ok(1)`,\n",
140 " a inner: `1`,\n",
141 " b label: `b`,\n",
142 " b debug: `2`"
143 );
144 assert_eq!(actual.unwrap_err(), message);
145 }
146
147 #[test]
148 fn not_ok() {
149 let a: Result<i8, i8> = Err(1);
150 let b: i8 = 1;
151 let actual = assert_ok_eq_x_as_result!(a, b);
152 let message = concat!(
153 "assertion failed: `assert_ok_eq_x!(a, b)`\n",
154 "https://docs.rs/assertables/9.8.3/assertables/macro.assert_ok_eq_x.html\n",
155 " a label: `a`,\n",
156 " a debug: `Err(1)`,\n",
157 " b label: `b`,\n",
158 " b debug: `1`",
159 );
160 assert_eq!(actual.unwrap_err(), message);
161 }
162}
163
164/// Assert an expression is Ok and its value is equal to an expression.
165///
166/// Pseudocode:<br>
167/// (a ⇒ Ok(a1) ⇒ a1) = b
168///
169/// * If true, return `a1`.
170///
171/// * Otherwise, call [`panic!`] with a message and the values of the
172/// expressions with their debug representations.
173///
174/// # Examples
175///
176/// ```rust
177/// use assertables::*;
178/// # use std::panic;
179///
180/// # fn main() {
181/// let a: Result<i8, i8> = Ok(1);
182/// let b: i8 = 1;
183/// assert_ok_eq_x!(a, b);
184///
185/// # let result = panic::catch_unwind(|| {
186/// // This will panic
187/// let a: Result<i8, i8> = Ok(1);
188/// let b: i8 = 2;
189/// assert_ok_eq_x!(a, b);
190/// # });
191/// // assertion failed: `assert_ok_eq_x!(a, b)`
192/// // https://docs.rs/assertables/…/assertables/macro.assert_ok_eq_x.html
193/// // a label: `a`,
194/// // a debug: `Ok(1)`,
195/// // a inner: `1`,
196/// // b label: `b`,
197/// // b debug: `2`
198/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
199/// # let message = concat!(
200/// # "assertion failed: `assert_ok_eq_x!(a, b)`\n",
201/// # "https://docs.rs/assertables/9.8.3/assertables/macro.assert_ok_eq_x.html\n",
202/// # " a label: `a`,\n",
203/// # " a debug: `Ok(1)`,\n",
204/// # " a inner: `1`,\n",
205/// # " b label: `b`,\n",
206/// # " b debug: `2`",
207/// # );
208/// # assert_eq!(actual, message);
209/// # }
210/// ```
211///
212/// # Module macros
213///
214/// * [`assert_ok_eq_x`](macro@crate::assert_ok_eq_x)
215/// * [`assert_ok_eq_x_as_result`](macro@crate::assert_ok_eq_x_as_result)
216/// * [`debug_assert_ok_eq_x`](macro@crate::debug_assert_ok_eq_x)
217///
218#[macro_export]
219macro_rules! assert_ok_eq_x {
220 ($a:expr, $b:expr $(,)?) => {
221 match $crate::assert_ok_eq_x_as_result!($a, $b) {
222 Ok(x) => x,
223 Err(err) => panic!("{}", err),
224 }
225 };
226 ($a:expr, $b:expr, $($message:tt)+) => {
227 match $crate::assert_ok_eq_x_as_result!($a, $b) {
228 Ok(x) => x,
229 Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
230 }
231 };
232}
233
234#[cfg(test)]
235mod test_assert_ok_eq_x {
236 use std::panic;
237
238 #[test]
239 fn eq() {
240 let a: Result<i8, i8> = Ok(1);
241 let b: i8 = 1;
242 for _ in 0..1 {
243 let actual = assert_ok_eq_x!(a, b);
244 assert_eq!(actual, 1);
245 }
246 }
247
248 #[test]
249 fn ne() {
250 let a: Result<i8, i8> = Ok(1);
251 let b: i8 = 2;
252 let result = panic::catch_unwind(|| {
253 let _actual = assert_ok_eq_x!(a, b);
254 });
255 let message = concat!(
256 "assertion failed: `assert_ok_eq_x!(a, b)`\n",
257 "https://docs.rs/assertables/9.8.3/assertables/macro.assert_ok_eq_x.html\n",
258 " a label: `a`,\n",
259 " a debug: `Ok(1)`,\n",
260 " a inner: `1`,\n",
261 " b label: `b`,\n",
262 " b debug: `2`"
263 );
264 assert_eq!(
265 result
266 .unwrap_err()
267 .downcast::<String>()
268 .unwrap()
269 .to_string(),
270 message
271 );
272 }
273
274 #[test]
275 fn not_ok() {
276 let a: Result<i8, i8> = Err(1);
277 let b: i8 = 1;
278 let result = panic::catch_unwind(|| {
279 let _actual = assert_ok_eq_x!(a, b);
280 });
281 let message = concat!(
282 "assertion failed: `assert_ok_eq_x!(a, b)`\n",
283 "https://docs.rs/assertables/9.8.3/assertables/macro.assert_ok_eq_x.html\n",
284 " a label: `a`,\n",
285 " a debug: `Err(1)`,\n",
286 " b label: `b`,\n",
287 " b debug: `1`",
288 );
289 assert_eq!(
290 result
291 .unwrap_err()
292 .downcast::<String>()
293 .unwrap()
294 .to_string(),
295 message
296 );
297 }
298}
299
300/// Assert an expression is Ok and its value is equal to an expression.
301///
302/// Pseudocode:<br>
303/// (a ⇒ Ok(a1) ⇒ a1) = b
304///
305/// This macro provides the same statements as [`assert_ok_eq_x`](macro.assert_ok_eq_x.html),
306/// except this macro's statements are only enabled in non-optimized
307/// builds by default. An optimized build will not execute this macro's
308/// statements unless `-C debug-assertions` is passed to the compiler.
309///
310/// This macro is useful for checks that are too expensive to be present
311/// in a release build but may be helpful during development.
312///
313/// The result of expanding this macro is always type checked.
314///
315/// An unchecked assertion allows a program in an inconsistent state to
316/// keep running, which might have unexpected consequences but does not
317/// introduce unsafety as long as this only happens in safe code. The
318/// performance cost of assertions, however, is not measurable in general.
319/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
320/// after thorough profiling, and more importantly, only in safe code!
321///
322/// This macro is intended to work in a similar way to
323/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
324///
325/// # Module macros
326///
327/// * [`assert_ok_eq_x`](macro@crate::assert_ok_eq_x)
328/// * [`assert_ok_eq_x`](macro@crate::assert_ok_eq_x)
329/// * [`debug_assert_ok_eq_x`](macro@crate::debug_assert_ok_eq_x)
330///
331#[macro_export]
332macro_rules! debug_assert_ok_eq_x {
333 ($($arg:tt)*) => {
334 if $crate::cfg!(debug_assertions) {
335 $crate::assert_ok_eq_x!($($arg)*);
336 }
337 };
338}