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
pub use paste::paste;

#[macro_export]
macro_rules! test_suite {
    ($($name:ident: $value:expr,)*) => {
    $(
        #[test]
        fn $name() {
            let (func, input, expected) = $value;
            assert_eq!(func(input), expected);
        }
    )*
    }
}

#[macro_export]
macro_rules! call {
    ($func:ident, ($($arg:expr),*)) => {
        $func($($arg,)*)
    }
}

#[macro_export]
macro_rules! test_p {
    ($func:ident, ($($suffix:ident: $args:tt, $expected:expr)*)) => {
    $(
        $crate::paste! {
            #[test]
            fn [<test_$func$suffix>]() {
                assert_eq!($crate::call!($func, $args), $expected);
            }
        }
    )*
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::convert::identity;

    test_suite! {
        foo: (identity, 42, 42),
        bar: (identity, 0xC0FFEE, 0xC0FFEE),
    }

    test_p! {
        identity,
        (
            _0: ("D'oh!"), "D'oh!"
            _1: (4711), 4711
        )
    }
}