assertables/assert_none/assert_none.rs
1//! Assert expression is None.
2//!
3//! Pseudocode:<br>
4//! a is None
5//!
6//! # Example
7//!
8//! ```rust
9//! use assertables::*;
10//!
11//! let a: Option<i8> = Option::None;
12//! assert_none!(a);
13//! ```
14//!
15//! # Module macros
16//!
17//! * [`assert_none`](macro@crate::assert_none)
18//! * [`assert_none_as_result`](macro@crate::assert_none_as_result)
19//! * [`debug_assert_none`](macro@crate::debug_assert_none)
20
21/// Assert an expression is None.
22///
23/// Pseudocode:<br>
24/// a is None
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_none`](macro@crate::assert_none)
36/// * [`assert_none_as_result`](macro@crate::assert_none_as_result)
37/// * [`debug_assert_none`](macro@crate::debug_assert_none)
38///
39#[macro_export]
40macro_rules! assert_none_as_result {
41 ($a:expr $(,)?) => {{
42 match (&$a) {
43 a => {
44 match (a) {
45 None => {
46 Ok(())
47 },
48 _ => {
49 Err(
50 format!(
51 concat!(
52 "assertion failed: `assert_none!(a)`\n",
53 "https://docs.rs/assertables/9.5.4/assertables/macro.assert_none.html\n",
54 " a label: `{}`,\n",
55 " a debug: `{:?}`",
56 ),
57 stringify!($a),
58 a
59 )
60 )
61 }
62 }
63 }
64 }
65 }};
66}
67
68#[cfg(test)]
69mod test_assert_none_as_result {
70
71 #[test]
72 fn success() {
73 let a: Option<i8> = Option::None;
74 let actual = assert_none_as_result!(a);
75 assert_eq!(actual.unwrap(), ());
76 }
77
78 #[test]
79 fn failure() {
80 let a: Option<i8> = Option::Some(1);
81 let actual = assert_none_as_result!(a);
82 let message = concat!(
83 "assertion failed: `assert_none!(a)`\n",
84 "https://docs.rs/assertables/9.5.4/assertables/macro.assert_none.html\n",
85 " a label: `a`,\n",
86 " a debug: `Some(1)`",
87 );
88 assert_eq!(actual.unwrap_err(), message);
89 }
90}
91
92/// Assert expression is None.
93///
94/// Pseudocode:<br>
95/// a is None
96///
97/// * If true, return `()`.
98///
99/// * Otherwise, call [`panic!`] with a message and the values of the
100/// expressions with their debug representations.
101///
102/// # Examples
103///
104/// ```rust
105/// use assertables::*;
106/// # use std::panic;
107///
108/// # fn main() {
109/// let a: Option<i8> = Option::None;
110/// assert_none!(a);
111///
112/// # let result = panic::catch_unwind(|| {
113/// // This will panic
114/// let a: Option<i8> = Option::Some(1);
115/// assert_none!(a);
116/// # });
117/// // assertion failed: `assert_none!(a)`
118/// // https://docs.rs/assertables/9.5.4/assertables/macro.assert_none.html
119/// // a label: `a`,
120/// // a debug: `Some(1)`
121/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
122/// # let message = concat!(
123/// # "assertion failed: `assert_none!(a)`\n",
124/// # "https://docs.rs/assertables/9.5.4/assertables/macro.assert_none.html\n",
125/// # " a label: `a`,\n",
126/// # " a debug: `Some(1)`",
127/// # );
128/// # assert_eq!(actual, message);
129/// # }
130/// ```
131///
132/// # Module macros
133///
134/// * [`assert_none`](macro@crate::assert_none)
135/// * [`assert_none_as_result`](macro@crate::assert_none_as_result)
136/// * [`debug_assert_none`](macro@crate::debug_assert_none)
137///
138#[macro_export]
139macro_rules! assert_none {
140 ($a:expr $(,)?) => {{
141 match $crate::assert_none_as_result!($a) {
142 Ok(x) => x,
143 Err(err) => panic!("{}", err),
144 }
145 }};
146 ($a:expr, $($message:tt)+) => {{
147 match $crate::assert_none_as_result!($a) {
148 Ok(x) => x,
149 Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
150 }
151 }};
152}
153
154#[cfg(test)]
155mod test_assert_none {
156 use std::panic;
157
158 #[test]
159 fn success() {
160 let a: Option<i8> = Option::None;
161 let actual = assert_none!(a);
162 assert_eq!(actual, ());
163 }
164
165 #[test]
166 fn failure() {
167 let a: Option<i8> = Option::Some(1);
168 let result = panic::catch_unwind(|| {
169 let _actual = assert_none!(a);
170 });
171 let message = concat!(
172 "assertion failed: `assert_none!(a)`\n",
173 "https://docs.rs/assertables/9.5.4/assertables/macro.assert_none.html\n",
174 " a label: `a`,\n",
175 " a debug: `Some(1)`",
176 );
177 assert_eq!(
178 result
179 .unwrap_err()
180 .downcast::<String>()
181 .unwrap()
182 .to_string(),
183 message
184 );
185 }
186}
187
188/// Assert expression is None.
189///
190/// Pseudocode:<br>
191/// a is None
192///
193/// This macro provides the same statements as [`assert_none`](macro.assert_none.html),
194/// except this macro's statements are only enabled in non-optimized
195/// builds by default. An optimized build will not execute this macro's
196/// statements unless `-C debug-assertions` is passed to the compiler.
197///
198/// This macro is useful for checks that are too expensive to be present
199/// in a release build but may be helpful during development.
200///
201/// The result of expanding this macro is always type checked.
202///
203/// An unchecked assertion allows a program in an inconsistent state to
204/// keep running, which might have unexpected consequences but does not
205/// introduce unsafety as long as this only happens in safe code. The
206/// performance cost of assertions, however, is not measurable in general.
207/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
208/// after thorough profiling, and more importantly, only in safe code!
209///
210/// This macro is intended to work in a similar way to
211/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
212///
213/// # Module macros
214///
215/// * [`assert_none`](macro@crate::assert_none)
216/// * [`assert_none`](macro@crate::assert_none)
217/// * [`debug_assert_none`](macro@crate::debug_assert_none)
218///
219#[macro_export]
220macro_rules! debug_assert_none {
221 ($($arg:tt)*) => {
222 if $crate::cfg!(debug_assertions) {
223 $crate::assert_none!($($arg)*);
224 }
225 };
226}