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