assertables/assert_matches/assert_matches.rs
1//! Assert expression matches a case.
2//!
3//! # Example
4//!
5//! ```rust
6//! use assertables::*;
7//!
8//! let a = 'a';
9//! assert_matches!(a, 'a'..='z');
10//! ```
11//!
12//! Note: this implementation of `assert_matches` is relatively basic.
13//!
14//! * If you want more capabilities, consider the crate `assert_matches`.
15//!
16//! * If you're using Rust nightly, use the std lib macro `assert_matches`.
17//!
18//! # Module macros
19//!
20//! * [`assert_matches`](macro@crate::assert_matches)
21//! * [`assert_matches_as_result`](macro@crate::assert_matches_as_result)
22//! * [`debug_assert_matches`](macro@crate::debug_assert_matches)
23
24/// Assert expression matches a case.
25///
26/// * If true, return Result `Ok(())`.
27///
28/// * Otherwise, return Result `Err(message)`.
29///
30/// This macro is useful for runtime checks, such as checking parameters,
31/// or sanitizing inputs, or handling different results in different ways.
32///
33/// # Module macros
34///
35/// * [`assert_matches`](macro@crate::assert_matches)
36/// * [`assert_matches_as_result`](macro@crate::assert_matches_as_result)
37/// * [`debug_assert_matches`](macro@crate::debug_assert_matches)
38///
39#[macro_export]
40macro_rules! assert_matches_as_result {
41 ($($arg:tt)*) => {{
42 if matches!($($arg)*) {
43 Ok(())
44 } else {
45 Err(
46 format!(
47 concat!(
48 "assertion failed: `assert_matches!(a)`\n",
49 "https://docs.rs/assertables/9.5.0/assertables/macro.assert_matches.html\n",
50 " args: `{}`",
51 ),
52 stringify!($($arg)*)
53 )
54 )
55 }
56 }};
57}
58
59#[cfg(test)]
60mod test_assert_matches_as_result {
61
62 //// Use char as per https://doc.rust-lang.org/std/macro.matches.html
63 mod use_char {
64
65 #[test]
66 fn success() {
67 let a = 'a';
68 let actual = assert_matches_as_result!(a, 'a'..='z');
69 assert_eq!(actual.unwrap(), ());
70 }
71
72 #[test]
73 fn failure() {
74 let a = 'a';
75 let actual = assert_matches_as_result!(a, 'b'..='z');
76 let message = concat!(
77 "assertion failed: `assert_matches!(a)`\n",
78 "https://docs.rs/assertables/9.5.0/assertables/macro.assert_matches.html\n",
79 " args: `a, 'b'..='z'`",
80 );
81 assert_eq!(actual.unwrap_err(), message);
82 }
83 }
84
85 //// Use Some as per https://doc.rust-lang.org/std/macro.matches.html
86 mod use_some {
87
88 #[test]
89 fn success() {
90 let a = Some(1);
91 let actual = assert_matches_as_result!(a, Some(x) if x < 2);
92 assert_eq!(actual.unwrap(), ());
93 }
94
95 #[test]
96 fn failure() {
97 let a = Some(2);
98 let actual = assert_matches_as_result!(a, Some(x) if x < 2);
99 let message = concat!(
100 "assertion failed: `assert_matches!(a)`\n",
101 "https://docs.rs/assertables/9.5.0/assertables/macro.assert_matches.html\n",
102 " args: `a, Some(x) if x < 2`",
103 );
104 assert_eq!(actual.unwrap_err(), message);
105 }
106 }
107}
108
109/// Assert expression is Some.
110///
111/// * If true, return `()`.
112///
113/// * Otherwise, call [`panic!`] with a message and the values of the
114/// expressions with their debug representations.
115///
116/// # Examples
117///
118/// ```rust
119/// use assertables::*;
120/// # use std::panic;
121///
122/// # fn main() {
123/// let a = 'a';
124/// assert_matches!(a, 'a'..='z');
125///
126/// # let result = panic::catch_unwind(|| {
127/// // This will panic
128/// let a = 'a';
129/// assert_matches!(a, 'b'..='z');
130/// # });
131/// // assertion failed: `assert_matches!(a)`
132/// // https://docs.rs/assertables/9.5.0/assertables/macro.assert_matches.html
133/// // args: `a, 'b'..='z'`
134/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
135/// # let message = concat!(
136/// # "assertion failed: `assert_matches!(a)`\n",
137/// # "https://docs.rs/assertables/9.5.0/assertables/macro.assert_matches.html\n",
138/// # " args: `a, 'b'..='z'`",
139/// # );
140/// # assert_eq!(actual, message);
141/// # }
142/// ```
143///
144/// # Module macros
145///
146/// * [`assert_matches`](macro@crate::assert_matches)
147/// * [`assert_matches_as_result`](macro@crate::assert_matches_as_result)
148/// * [`debug_assert_matches`](macro@crate::debug_assert_matches)
149///
150#[macro_export]
151macro_rules! assert_matches {
152 ($expression:expr, $pattern:pat if $guard:expr $(,)?) => {{
153 match $crate::assert_matches_as_result!($expression, $pattern if $guard) {
154 Ok(()) => (),
155 Err(err) => panic!("{}", err),
156 }
157 }};
158 ($expression:expr, $pattern:pat) => {{
159 match $crate::assert_matches_as_result!($expression, $pattern) {
160 Ok(()) => (),
161 Err(err) => panic!("{}", err),
162 }
163 }};
164 ($expression:expr, $pattern:pat if $guard:expr, $($message:tt)+) => {{
165 match $crate::assert_matches_as_result!($expression, $pattern if $guard) {
166 Ok(()) => (),
167 Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
168 }
169 }};
170 ($expression:expr, $pattern:pat, $($message:tt)+) => {{
171 match $crate::assert_matches_as_result!($expression, $pattern if $guard) {
172 Ok(()) => (),
173 Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
174 }
175 }};
176}
177
178#[cfg(test)]
179mod test_assert_matches {
180
181 //// Use char as per https://doc.rust-lang.org/std/macro.matches.html
182 mod use_char {
183 use std::panic;
184
185 #[test]
186 fn success() {
187 let a = 'a';
188 let actual = assert_matches!(a, 'a'..='z');
189 assert_eq!(actual, ());
190 }
191
192 #[test]
193 fn failure() {
194 let a = 'a';
195 let result = panic::catch_unwind(|| {
196 let _actual = assert_matches!(a, 'b'..='z');
197 });
198 let message = concat!(
199 "assertion failed: `assert_matches!(a)`\n",
200 "https://docs.rs/assertables/9.5.0/assertables/macro.assert_matches.html\n",
201 " args: `a, 'b'..='z'`",
202 );
203 assert_eq!(
204 result
205 .unwrap_err()
206 .downcast::<String>()
207 .unwrap()
208 .to_string(),
209 message
210 );
211 }
212 }
213
214 //// Use Some as per https://doc.rust-lang.org/std/macro.matches.html
215 mod use_some {
216 use std::panic;
217
218 #[test]
219 fn success() {
220 let a = Some(1);
221 let actual = assert_matches!(a, Some(x) if x < 2);
222 assert_eq!(actual, ());
223 }
224
225 #[test]
226 fn failure() {
227 let a = Some(2);
228 let result = panic::catch_unwind(|| {
229 let _actual = assert_matches!(a, Some(x) if x < 2);
230 });
231 let message = concat!(
232 "assertion failed: `assert_matches!(a)`\n",
233 "https://docs.rs/assertables/9.5.0/assertables/macro.assert_matches.html\n",
234 " args: `a, Some(x) if x < 2`",
235 );
236 assert_eq!(
237 result
238 .unwrap_err()
239 .downcast::<String>()
240 .unwrap()
241 .to_string(),
242 message
243 );
244 }
245 }
246}
247
248/// Assert expression is Some.
249///
250/// This macro provides the same statements as [`assert_matches`](macro.assert_matches.html),
251/// except this macro's statements are only enabled in non-optimized
252/// builds by default. An optimized build will not execute this macro's
253/// statements unless `-C debug-assertions` is passed to the compiler.
254///
255/// This macro is useful for checks that are too expensive to be present
256/// in a release build but may be helpful during development.
257///
258/// The result of expanding this macro is always type checked.
259///
260/// An unchecked assertion allows a program in an inconsistent state to
261/// keep running, which might have unexpected consequences but does not
262/// introduce unsafety as long as this only happens in safe code. The
263/// performance cost of assertions, however, is not measurable in general.
264/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
265/// after thorough profiling, and more importantly, only in safe code!
266///
267/// This macro is intended to work in a similar way to
268/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
269///
270/// # Module macros
271///
272/// * [`assert_matches`](macro@crate::assert_matches)
273/// * [`assert_matches`](macro@crate::assert_matches)
274/// * [`debug_assert_matches`](macro@crate::debug_assert_matches)
275///
276#[macro_export]
277macro_rules! debug_assert_matches {
278 ($($arg:tt)*) => {
279 if $crate::cfg!(debug_assertions) {
280 $crate::assert_matches!($($arg)*);
281 }
282 };
283}