assertables/assert_command/assert_command_stdout_ne.rs
1//! Assert a command stdout string is not 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_ne!(a, b);
17//! ```
18//!
19//! # Module macros
20//!
21//! * [`assert_command_stdout_ne`](macro@crate::assert_command_stdout_ne)
22//! * [`assert_command_stdout_ne_as_result`](macro@crate::assert_command_stdout_ne_as_result)
23//! * [`debug_assert_command_stdout_ne`](macro@crate::debug_assert_command_stdout_ne)
24
25/// Assert a command stdout string is not 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_ne`](macro@crate::assert_command_stdout_ne)
40/// * [`assert_command_stdout_ne_as_result`](macro@crate::assert_command_stdout_ne_as_result)
41/// * [`debug_assert_command_stdout_ne`](macro@crate::debug_assert_command_stdout_ne)
42///
43#[macro_export]
44macro_rules! assert_command_stdout_ne_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.ne(&b) {
51 Ok((a, b))
52 } else {
53 Err(
54 format!(
55 concat!(
56 "assertion failed: `assert_command_stdout_ne!(a_command, b_command)`\n",
57 "https://docs.rs/assertables/9.5.4/assertables/macro.assert_command_stdout_ne.html\n",
58 " a label: `{}`,\n",
59 " a debug: `{:?}`,\n",
60 " b label: `{}`,\n",
61 " b debug: `{:?}`,\n",
62 " a value: `{:?}`,\n",
63 " b value: `{:?}`"
64 ),
65 stringify!($a_command),
66 $a_command,
67 stringify!($b_command),
68 $b_command,
69 a,
70 b
71 )
72 )
73 }
74 },
75 (a, b) => {
76 Err(
77 format!(
78 concat!(
79 "assertion failed: `assert_command_stdout_ne!(a_command, b_command)`\n",
80 "https://docs.rs/assertables/9.5.4/assertables/macro.assert_command_stdout_ne.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}
100
101#[cfg(test)]
102mod test_assert_command_stdout_ne_as_result {
103 use std::process::Command;
104
105 #[test]
106 fn lt() {
107 let mut a = Command::new("bin/printf-stdout");
108 a.args(["%s", "alfa"]);
109 let mut b = Command::new("bin/printf-stdout");
110 b.args(["%s", "zz"]);
111 let actual = assert_command_stdout_ne_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 #[test]
119 fn gt() {
120 let mut a = Command::new("bin/printf-stdout");
121 a.args(["%s", "alfa"]);
122 let mut b = Command::new("bin/printf-stdout");
123 b.args(["%s", "aa"]);
124 let actual = assert_command_stdout_ne_as_result!(a, b);
125 assert_eq!(
126 actual.unwrap(),
127 (vec![b'a', b'l', b'f', b'a'], vec![b'a', b'a'])
128 );
129 }
130
131 #[test]
132 fn eq() {
133 let mut a = Command::new("bin/printf-stdout");
134 a.args(["%s", "alfa"]);
135 let mut b = Command::new("bin/printf-stdout");
136 b.args(["%s", "alfa"]);
137 let actual = assert_command_stdout_ne_as_result!(a, b);
138 let message = concat!(
139 "assertion failed: `assert_command_stdout_ne!(a_command, b_command)`\n",
140 "https://docs.rs/assertables/9.5.4/assertables/macro.assert_command_stdout_ne.html\n",
141 " a label: `a`,\n",
142 " a debug: `\"bin/printf-stdout\" \"%s\" \"alfa\"`,\n",
143 " b label: `b`,\n",
144 " b debug: `\"bin/printf-stdout\" \"%s\" \"alfa\"`,\n",
145 " a value: `[97, 108, 102, 97]`,\n",
146 " b value: `[97, 108, 102, 97]`"
147 );
148 assert_eq!(actual.unwrap_err(), message);
149 }
150}
151
152/// Assert a command stdout string is not equal to another.
153///
154/// Pseudocode:<br>
155/// (a_command ⇒ stdout) = (b_command ⇒ stdout)
156///
157/// * If true, return `(a_stdout, b_stdout)`.
158///
159/// * Otherwise, call [`panic!`] with a message and the values of the
160/// expressions with their debug representations.
161///
162/// # Examples
163///
164/// ```rust
165/// use assertables::*;
166/// # use std::panic;
167/// use std::process::Command;
168///
169/// # fn main() {
170/// let mut a = Command::new("bin/printf-stdout");
171/// a.args(["%s", "alfa"]);
172/// let mut b = Command::new("bin/printf-stdout");
173/// b.args(["%s", "zz"]);
174/// assert_command_stdout_ne!(a, b);
175///
176/// # let result = panic::catch_unwind(|| {
177/// // This will panic
178/// let mut a = Command::new("bin/printf-stdout");
179/// a.args(["%s", "alfa"]);
180/// let mut b = Command::new("bin/printf-stdout");
181/// b.args(["%s", "alfa"]);
182/// assert_command_stdout_ne!(a, b);
183/// # });
184/// // assertion failed: `assert_command_stdout_ne!(a_command, b_command)`
185/// // https://docs.rs/assertables/9.5.4/assertables/macro.assert_command_stdout_ne.html
186/// // a label: `a`,
187/// // a debug: `\"bin/printf-stdout\" \"%s\" \"alfa\"`,
188/// // b label: `b`,
189/// // b debug: `\"bin/printf-stdout\" \"%s\" \"alfa\"`,
190/// // a value: `[97, 108, 102, 97]`,
191/// // b value: `[97, 108, 102, 97]`
192/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
193/// # let message = concat!(
194/// # "assertion failed: `assert_command_stdout_ne!(a_command, b_command)`\n",
195/// # "https://docs.rs/assertables/9.5.4/assertables/macro.assert_command_stdout_ne.html\n",
196/// # " a label: `a`,\n",
197/// # " a debug: `\"bin/printf-stdout\" \"%s\" \"alfa\"`,\n",
198/// # " b label: `b`,\n",
199/// # " b debug: `\"bin/printf-stdout\" \"%s\" \"alfa\"`,\n",
200/// # " a value: `[97, 108, 102, 97]`,\n",
201/// # " b value: `[97, 108, 102, 97]`"
202/// # );
203/// # assert_eq!(actual, message);
204/// # }
205/// ```
206///
207/// # Module macros
208///
209/// * [`assert_command_stdout_ne`](macro@crate::assert_command_stdout_ne)
210/// * [`assert_command_stdout_ne_as_result`](macro@crate::assert_command_stdout_ne_as_result)
211/// * [`debug_assert_command_stdout_ne`](macro@crate::debug_assert_command_stdout_ne)
212///
213#[macro_export]
214macro_rules! assert_command_stdout_ne {
215 ($a_command:expr, $b_command:expr $(,)?) => {
216 match $crate::assert_command_stdout_ne_as_result!($a_command, $b_command) {
217 Ok(x) => x,
218 Err(err) => panic!("{}", err),
219 }
220 };
221 ($a_command:expr, $b_command:expr, $($message:tt)+) => {
222 match $crate::assert_command_stdout_ne_as_result!($a_command, $b_command) {
223 Ok(x) => x,
224 Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
225 }
226 };
227}
228
229#[cfg(test)]
230mod test_assert_command_stdout_ne {
231 use std::panic;
232 use std::process::Command;
233
234 #[test]
235 fn lt() {
236 let mut a = Command::new("bin/printf-stdout");
237 a.args(["%s", "alfa"]);
238 let mut b = Command::new("bin/printf-stdout");
239 b.args(["%s", "zz"]);
240 let actual = assert_command_stdout_ne!(a, b);
241 assert_eq!(actual, (vec![b'a', b'l', b'f', b'a'], vec![b'z', b'z']));
242 }
243
244 #[test]
245 fn gt() {
246 let mut a = Command::new("bin/printf-stdout");
247 a.args(["%s", "alfa"]);
248 let mut b = Command::new("bin/printf-stdout");
249 b.args(["%s", "aa"]);
250 let actual = assert_command_stdout_ne!(a, b);
251 assert_eq!(actual, (vec![b'a', b'l', b'f', b'a'], vec![b'a', b'a']));
252 }
253
254 #[test]
255 fn eq() {
256 let result = panic::catch_unwind(|| {
257 let mut a = Command::new("bin/printf-stdout");
258 a.args(["%s", "alfa"]);
259 let mut b = Command::new("bin/printf-stdout");
260 b.args(["%s", "alfa"]);
261 let _actual = assert_command_stdout_ne!(a, b);
262 });
263 let message = concat!(
264 "assertion failed: `assert_command_stdout_ne!(a_command, b_command)`\n",
265 "https://docs.rs/assertables/9.5.4/assertables/macro.assert_command_stdout_ne.html\n",
266 " a label: `a`,\n",
267 " a debug: `\"bin/printf-stdout\" \"%s\" \"alfa\"`,\n",
268 " b label: `b`,\n",
269 " b debug: `\"bin/printf-stdout\" \"%s\" \"alfa\"`,\n",
270 " a value: `[97, 108, 102, 97]`,\n",
271 " b value: `[97, 108, 102, 97]`"
272 );
273 assert_eq!(
274 result
275 .unwrap_err()
276 .downcast::<String>()
277 .unwrap()
278 .to_string(),
279 message
280 );
281 }
282}
283
284/// Assert a command stdout string is not equal to another.
285///
286/// This macro provides the same statements as [`assert_command_stdout_ne {`](macro.assert_command_stdout_ne {.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 program 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_command_stdout_ne {`](macro@crate::assert_command_stdout_ne {)
309/// * [`assert_command_stdout_ne {`](macro@crate::assert_command_stdout_ne {)
310/// * [`debug_assert_command_stdout_ne {`](macro@crate::debug_assert_command_stdout_ne {)
311///
312#[macro_export]
313macro_rules! debug_assert_command_stdout_ne {
314 ($($arg:tt)*) => {
315 if $crate::cfg!(debug_assertions) {
316 $crate::assert_command_stdout_ne!($($arg)*);
317 }
318 };
319}