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