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