assertables/assert_status/
assert_status_code_value_ne.rs

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