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