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