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