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