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