test_macro/test_macro.rs
1/// Test that the given argument is expected to be true.
2///
3/// The first argument should be the test case name,
4/// and the second argument should be the arguments which is expected to be 'true'.
5///
6/// # Example
7///
8/// ```
9/// fn is_err() -> bool {
10/// true
11/// }
12/// ```
13///
14/// If you want to test this is_err function, you can write it as follows
15///
16/// ```
17/// test_macro::test_assert!(test_case_name, is_err());
18/// ```
19#[macro_export]
20macro_rules! test_assert {
21 ($func_name:ident, $arg:expr) => {
22 #[test]
23 fn $func_name() {
24 assert!($arg);
25 }
26 };
27}
28
29/// Generate a test code that internally uses assert_eq!.
30///
31/// The first argument should be the test case name,
32/// and the second argument should be the arguments and the expected return value.
33///
34/// Put the arguments to the left of "=>" and the return value to the right of "=>".
35/// And panic if the left is not equal to the right.
36///
37/// # Example
38///
39/// ```
40/// fn add(x: i32, y: i32) -> i32 {
41/// x + y
42/// }
43/// ```
44///
45/// If you want to test this add function, you can write it as follows
46///
47/// ```
48/// test_macro::test_assert_eq!(test_case_name, add(1, 2) => 3);
49/// ```
50#[macro_export]
51macro_rules! test_assert_eq {
52 ($func_name:ident, $arg:expr => $ans:expr) => {
53 #[test]
54 fn $func_name() {
55 assert_eq!($arg, $ans);
56 }
57 };
58}
59
60/// Generate a test code that internally uses assert_ne!.
61///
62/// The first argument should be the test case name,
63/// and the second argument should be the arguments and the unexpected return value.
64///
65/// Put the arguments to the left of "=>" and the unexpected return value to the right of "=>".
66/// And panic if the left is equal to the right.
67///
68/// # Example
69///
70/// ```
71/// fn add(x: i32, y: i32) -> i32 {
72/// x + y
73/// }
74/// ```
75///
76/// If you want to test this add function, you can write it as follows
77///
78/// ```
79/// test_macro::test_assert_ne!(test_case_name, add(1, 2) => 0);
80/// ```
81#[macro_export]
82macro_rules! test_assert_ne {
83 ($func_name:ident, $arg:expr => $ans:expr) => {
84 #[test]
85 fn $func_name() {
86 assert_ne!($arg, $ans);
87 }
88 };
89}
90
91/// Test to see if it panics as expected.
92///
93/// The first argument should be the test case name,
94/// and the second argument should be the function or macro which is expected to panic internally.
95///
96/// # Example
97///
98/// ```
99/// test_macro::test_should_panic!(test_case_name, panic!());
100/// ```
101///
102/// ```
103/// fn panic() {
104/// panic!();
105/// }
106///
107/// test_macro::test_should_panic!(test_case_name, panic());
108/// ```
109///
110/// Expected error message can be tested.
111/// ```
112/// test_macro::test_should_panic!(test_case_name, "expected error message", panic("{}", String::from("expected error message")));
113/// ```
114#[macro_export]
115macro_rules! test_should_panic {
116 ($func_name:ident, $func:expr) => {
117 #[test]
118 #[should_panic]
119 fn $func_name() {
120 $func;
121 }
122 };
123 ($func_name:ident, $message:literal, $func:expr) => {
124 #[test]
125 #[should_panic(expected = $message)]
126 fn $func_name() {
127 $func;
128 }
129 };
130}