1#[macro_export]
41macro_rules! assert_gt_as_result {
42 ($a:expr, $b:expr $(,)?) => {
43 match (&$a, &$b) {
44 (a, b) => {
45 if a > b {
46 Ok(())
47 } else {
48 Err(format!(
49 concat!(
50 "assertion failed: `assert_gt!(a, b)`\n",
51 "https://docs.rs/assertables/9.9.0/assertables/macro.assert_gt.html\n",
52 " a label: `{}`,\n",
53 " a debug: `{:?}`,\n",
54 " b label: `{}`,\n",
55 " b debug: `{:?}`",
56 ),
57 stringify!($a),
58 a,
59 stringify!($b),
60 b
61 ))
62 }
63 }
64 }
65 };
66}
67
68#[cfg(test)]
69mod test_assert_gt_as_result {
70 use std::sync::Once;
71
72 mod integer {
73 use super::*;
74
75 #[test]
76 fn gt() {
77 let a: i8 = 2;
78 let b: i8 = 1;
79 for _ in 0..1 {
80 let actual = assert_gt_as_result!(a, b);
81 assert_eq!(actual.unwrap(), ());
82 }
83 }
84
85 #[test]
86 fn gt_once() {
87 static A: Once = Once::new();
88 fn a() -> i8 {
89 if A.is_completed() {
90 panic!("A.is_completed()")
91 } else {
92 A.call_once(|| {})
93 }
94 2
95 }
96
97 static B: Once = Once::new();
98 fn b() -> i8 {
99 if B.is_completed() {
100 panic!("B.is_completed()")
101 } else {
102 B.call_once(|| {})
103 }
104 1
105 }
106
107 assert_eq!(A.is_completed(), false);
108 assert_eq!(B.is_completed(), false);
109 let result = assert_gt_as_result!(a(), b());
110 assert!(result.is_ok());
111 assert_eq!(A.is_completed(), true);
112 assert_eq!(B.is_completed(), true);
113 }
114
115 #[test]
116 fn eq() {
117 let a: i8 = 1;
118 let b: i8 = 1;
119 let actual = assert_gt_as_result!(a, b);
120 let message = concat!(
121 "assertion failed: `assert_gt!(a, b)`\n",
122 "https://docs.rs/assertables/9.9.0/assertables/macro.assert_gt.html\n",
123 " a label: `a`,\n",
124 " a debug: `1`,\n",
125 " b label: `b`,\n",
126 " b debug: `1`",
127 );
128 assert_eq!(actual.unwrap_err(), message);
129 }
130
131 #[test]
132 fn lt() {
133 let a: i8 = 1;
134 let b: i8 = 2;
135 let actual = assert_gt_as_result!(a, b);
136 let message = concat!(
137 "assertion failed: `assert_gt!(a, b)`\n",
138 "https://docs.rs/assertables/9.9.0/assertables/macro.assert_gt.html\n",
139 " a label: `a`,\n",
140 " a debug: `1`,\n",
141 " b label: `b`,\n",
142 " b debug: `2`",
143 );
144 assert_eq!(actual.unwrap_err(), message);
145 }
146 }
147 mod string {
148 use super::*;
149
150 #[test]
151 fn gt() {
152 let a: String = String::from("2");
153 let b: String = String::from("1");
154 for _ in 0..1 {
155 let actual = assert_gt_as_result!(a, b);
156 assert_eq!(actual.unwrap(), ());
157 }
158 }
159
160 #[test]
161 fn gt_once() {
162 static A: Once = Once::new();
163 fn a() -> String {
164 if A.is_completed() {
165 panic!("A.is_completed()")
166 } else {
167 A.call_once(|| {})
168 }
169 String::from("2")
170 }
171
172 static B: Once = Once::new();
173 fn b() -> String {
174 if B.is_completed() {
175 panic!("B.is_completed()")
176 } else {
177 B.call_once(|| {})
178 }
179 String::from("1")
180 }
181
182 assert_eq!(A.is_completed(), false);
183 assert_eq!(B.is_completed(), false);
184 let result = assert_gt_as_result!(a(), b());
185 assert!(result.is_ok());
186 assert_eq!(A.is_completed(), true);
187 assert_eq!(B.is_completed(), true);
188 }
189
190 #[test]
191 fn eq() {
192 let a: String = String::from("1");
193 let b: String = String::from("1");
194 let actual = assert_gt_as_result!(a, b);
195 let message = concat!(
196 "assertion failed: `assert_gt!(a, b)`\n",
197 "https://docs.rs/assertables/9.9.0/assertables/macro.assert_gt.html\n",
198 " a label: `a`,\n",
199 " a debug: `\"1\"`,\n",
200 " b label: `b`,\n",
201 " b debug: `\"1\"`",
202 );
203 assert_eq!(actual.unwrap_err(), message);
204 }
205
206 #[test]
207 fn lt() {
208 let a: String = String::from("1");
209 let b: String = String::from("2");
210 let actual = assert_gt_as_result!(a, b);
211 let message = concat!(
212 "assertion failed: `assert_gt!(a, b)`\n",
213 "https://docs.rs/assertables/9.9.0/assertables/macro.assert_gt.html\n",
214 " a label: `a`,\n",
215 " a debug: `\"1\"`,\n",
216 " b label: `b`,\n",
217 " b debug: `\"2\"`",
218 );
219 assert_eq!(actual.unwrap_err(), message);
220 }
221 }
222}
223
224#[macro_export]
277macro_rules! assert_gt {
278 ($a:expr, $b:expr $(,)?) => {
279 match $crate::assert_gt_as_result!($a, $b) {
280 Ok(()) => (),
281 Err(err) => panic!("{}", err),
282 }
283 };
284 ($a:expr, $b:expr, $($message:tt)+) => {
285 match $crate::assert_gt_as_result!($a, $b) {
286 Ok(()) => (),
287 Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
288 }
289 };
290}
291
292#[cfg(test)]
293mod test_assert_gt {
294 use std::panic;
295
296 mod integer {
297 use super::*;
298
299 #[test]
300 fn gt() {
301 let a: i8 = 2;
302 let b: i8 = 1;
303 for _ in 0..1 {
304 let actual = assert_gt!(a, b);
305 let expect = ();
306 assert_eq!(actual, expect);
307 }
308 }
309
310 #[test]
311 fn eq() {
312 let a: i8 = 1;
313 let b: i8 = 1;
314 let result = panic::catch_unwind(|| {
315 let _actual = assert_gt!(a, b);
316 });
317 let message = concat!(
318 "assertion failed: `assert_gt!(a, b)`\n",
319 "https://docs.rs/assertables/9.9.0/assertables/macro.assert_gt.html\n",
320 " a label: `a`,\n",
321 " a debug: `1`,\n",
322 " b label: `b`,\n",
323 " b debug: `1`",
324 );
325 assert_eq!(
326 result
327 .unwrap_err()
328 .downcast::<String>()
329 .unwrap()
330 .to_string(),
331 message
332 );
333 }
334
335 #[test]
336 fn lt() {
337 let a: i8 = 1;
338 let b: i8 = 2;
339 let result = panic::catch_unwind(|| {
340 let _actual = assert_gt!(a, b);
341 });
342 let message = concat!(
343 "assertion failed: `assert_gt!(a, b)`\n",
344 "https://docs.rs/assertables/9.9.0/assertables/macro.assert_gt.html\n",
345 " a label: `a`,\n",
346 " a debug: `1`,\n",
347 " b label: `b`,\n",
348 " b debug: `2`",
349 );
350 assert_eq!(
351 result
352 .unwrap_err()
353 .downcast::<String>()
354 .unwrap()
355 .to_string(),
356 message
357 );
358 }
359 }
360
361 mod string {
362 use super::*;
363
364 #[test]
365 fn gt() {
366 let a: String = String::from("2");
367 let b: String = String::from("1");
368 for _ in 0..1 {
369 let actual = assert_gt!(a, b);
370 let expect = ();
371 assert_eq!(actual, expect);
372 }
373 }
374
375 #[test]
376 fn eq() {
377 let a: String = String::from("1");
378 let b: String = String::from("1");
379 let result = panic::catch_unwind(|| {
380 let _actual = assert_gt!(a, b);
381 });
382 let message = concat!(
383 "assertion failed: `assert_gt!(a, b)`\n",
384 "https://docs.rs/assertables/9.9.0/assertables/macro.assert_gt.html\n",
385 " a label: `a`,\n",
386 " a debug: `\"1\"`,\n",
387 " b label: `b`,\n",
388 " b debug: `\"1\"`",
389 );
390 assert_eq!(
391 result
392 .unwrap_err()
393 .downcast::<String>()
394 .unwrap()
395 .to_string(),
396 message
397 );
398 }
399
400 #[test]
401 fn lt() {
402 let a: String = String::from("1");
403 let b: String = String::from("2");
404 let result = panic::catch_unwind(|| {
405 let _actual = assert_gt!(a, b);
406 });
407 let message = concat!(
408 "assertion failed: `assert_gt!(a, b)`\n",
409 "https://docs.rs/assertables/9.9.0/assertables/macro.assert_gt.html\n",
410 " a label: `a`,\n",
411 " a debug: `\"1\"`,\n",
412 " b label: `b`,\n",
413 " b debug: `\"2\"`",
414 );
415 assert_eq!(
416 result
417 .unwrap_err()
418 .downcast::<String>()
419 .unwrap()
420 .to_string(),
421 message
422 );
423 }
424 }
425}
426
427#[macro_export]
459macro_rules! debug_assert_gt {
460 ($($arg:tt)*) => {
461 if cfg!(debug_assertions) {
462 $crate::assert_gt!($($arg)*);
463 }
464 };
465}
466
467#[cfg(test)]
468mod test_debug_assert_gt {
469 use std::panic;
470
471 mod integer {
472 use super::*;
473
474 #[test]
475 fn gt() {
476 let a: i8 = 2;
477 let b: i8 = 1;
478 for _ in 0..1 {
479 let _actual = debug_assert_gt!(a, b);
480 let _expect = ();
481 }
483 }
484
485 #[test]
486 fn eq() {
487 let a: i8 = 1;
488 let b: i8 = 1;
489 let result = panic::catch_unwind(|| {
490 let _actual = debug_assert_gt!(a, b);
491 });
492 let message = concat!(
493 "assertion failed: `assert_gt!(a, b)`\n",
494 "https://docs.rs/assertables/9.9.0/assertables/macro.assert_gt.html\n",
495 " a label: `a`,\n",
496 " a debug: `1`,\n",
497 " b label: `b`,\n",
498 " b debug: `1`",
499 );
500 assert_eq!(
501 result
502 .unwrap_err()
503 .downcast::<String>()
504 .unwrap()
505 .to_string(),
506 message
507 );
508 }
509
510 #[test]
511 fn lt() {
512 let a: i8 = 1;
513 let b: i8 = 2;
514 let result = panic::catch_unwind(|| {
515 let _actual = debug_assert_gt!(a, b);
516 });
517 let message = concat!(
518 "assertion failed: `assert_gt!(a, b)`\n",
519 "https://docs.rs/assertables/9.9.0/assertables/macro.assert_gt.html\n",
520 " a label: `a`,\n",
521 " a debug: `1`,\n",
522 " b label: `b`,\n",
523 " b debug: `2`",
524 );
525 assert_eq!(
526 result
527 .unwrap_err()
528 .downcast::<String>()
529 .unwrap()
530 .to_string(),
531 message
532 );
533 }
534 }
535
536 mod string {
537 use super::*;
538
539 #[test]
540 fn gt() {
541 let a: String = String::from("2");
542 let b: String = String::from("1");
543 for _ in 0..1 {
544 let _actual = debug_assert_gt!(a, b);
545 let _expect = ();
546 }
548 }
549
550 #[test]
551 fn eq() {
552 let a: String = String::from("1");
553 let b: String = String::from("1");
554 let result = panic::catch_unwind(|| {
555 let _actual = debug_assert_gt!(a, b);
556 });
557 let message = concat!(
558 "assertion failed: `assert_gt!(a, b)`\n",
559 "https://docs.rs/assertables/9.9.0/assertables/macro.assert_gt.html\n",
560 " a label: `a`,\n",
561 " a debug: `\"1\"`,\n",
562 " b label: `b`,\n",
563 " b debug: `\"1\"`",
564 );
565 assert_eq!(
566 result
567 .unwrap_err()
568 .downcast::<String>()
569 .unwrap()
570 .to_string(),
571 message
572 );
573 }
574
575 #[test]
576 fn lt() {
577 let a: String = String::from("1");
578 let b: String = String::from("2");
579 let result = panic::catch_unwind(|| {
580 let _actual = debug_assert_gt!(a, b);
581 });
582 let message = concat!(
583 "assertion failed: `assert_gt!(a, b)`\n",
584 "https://docs.rs/assertables/9.9.0/assertables/macro.assert_gt.html\n",
585 " a label: `a`,\n",
586 " a debug: `\"1\"`,\n",
587 " b label: `b`,\n",
588 " b debug: `\"2\"`",
589 );
590 assert_eq!(
591 result
592 .unwrap_err()
593 .downcast::<String>()
594 .unwrap()
595 .to_string(),
596 message
597 );
598 }
599 }
600}