data_test/lib.rs
1#![forbid(unsafe_code)]
2#![warn(missing_docs)]
3#![warn(clippy::pedantic)]
4#![doc = include_str!("../README.md")]
5
6
7/// Create sub module with tests for multiple input data
8///
9/// # Examples
10///
11/// ```no_run
12/// #[cfg(test)]
13/// mod tests {
14/// use data_test::data_test;
15///
16/// #[test]
17/// fn it_works() {
18/// assert_eq!(2 + 2, 4);
19/// }
20///
21/// data_test!{
22/// fn is_equal(input, expected) => {
23/// assert_eq!(input, expected);
24/// }
25/// - a (1, 1)
26/// - b (2, 2)
27/// - c (3, 3)
28/// - d (4, 4)
29/// - e (5, 5)
30/// - f ("hello world", "hello world")
31/// }
32/// }
33/// // cargo test output:
34/// // test tests::it_works ... ok
35/// // test tests::is_equal::a ... ok
36/// // test tests::is_equal::b ... ok
37/// // test tests::is_equal::c ... ok
38/// // test tests::is_equal::d ... ok
39/// // test tests::is_equal::e ... ok
40/// // test tests::is_equal::f ... ok
41/// # fn main() {}
42/// ```
43// NOTE: the doc test only can check if compile but doc test does not run tests inside of doc test, then use no_run attribute
44#[macro_export]
45macro_rules! data_test {
46 {
47 fn $prefix:ident $input:pat => $main:block
48 $(- $name:ident $($value:tt),*)*
49 } => {
50 mod $prefix {
51 #[allow(unused_imports)]
52 use super::*;
53 $(
54
55 #[test]
56 fn $name () {
57 let $input = ($($value),*);
58 $main
59 }
60 )*
61 }
62 };
63 {$(
64 fn $prefix:ident $input:pat => $main:block
65 $(- $name:ident $($value:tt),*)*
66 )*} => {$(
67 $crate::data_test!{
68 fn $prefix $input => $main
69 $(- $name ($($value),*))*
70 }
71 )*};
72}
73
74
75#[cfg(test)]
76mod tests {
77 use super::data_test;
78
79 #[test]
80 fn it_works() {
81 assert_eq!(2 + 2, 4);
82 }
83
84 data_test!{
85 fn is_equal(input, expected) => {
86 assert_eq!(input, expected);
87 }
88 - a (1, 1)
89 - b (1 + 1, 2)
90 - c ("hello", "hello")
91 - d ("\u{1f9ea}", "\u{1f9ea}")
92 }
93
94 data_test!{
95 fn is_not_equal(input, expected) => {
96 assert_ne!(input, expected);
97 }
98 - a (1, 2)
99 - b 1, 3
100 - c (1, 4)
101
102 fn is_not_zero input => {
103 assert_ne!(input, 0);
104 }
105 - a 1
106 - b (2)
107 - c 3
108
109 fn test_multiplication(first, second, expected) => {
110 assert_eq!(first * second, expected);
111 }
112 - a (2, 2, 4)
113 - b (2, 3, 6)
114 - c (3, 3, 9)
115 - d (9, 9, 9*9)
116 }
117}