testing_table/
macros.rs

1/// Build a static table.
2///
3/// # Example
4///
5/// ```text
6/// static_table!(
7///     "|--|--|"
8///     "|XX|XY|"
9///     "|--|--|"
10/// )
11/// ```
12#[macro_export]
13macro_rules! static_table {
14    ($($line:expr)*) => {
15        concat!(
16            $($line, "\n",)*
17        )
18        .trim_end_matches('\n')
19    };
20}
21
22/// Create a test for a given table.
23///
24/// # Example
25///
26/// ```text
27/// test_table!(
28///     test_name,
29///     Table::new([[1, 2, 3], [4, 5, 6]]),
30///     "|--|--|"
31///     "|XX|XY|"
32///     "|--|--|"
33/// )
34/// ```
35///
36/// ```text
37/// test_table!(
38///     test_name,
39///     Table::new([[1, 2, 3], [4, 5, 6]]), // got
40///     Table::new([[1, 2, 3], [4, 5, 6]]), // expected
41/// )
42/// ```
43#[macro_export]
44macro_rules! test_table {
45    ($test:ident, $table:expr, $($line:expr)*) => {
46        #[test]
47        fn $test() {
48            $crate::assert_table!($table, $($line)*);
49        }
50    };
51    ($test:ident, $table:expr, $expected:expr,) => {
52        #[test]
53        fn $test() {
54            let table = $table.to_string();
55            let expected = $expected.to_string();
56            assert_eq!(
57                table,
58                expected,
59                "\ngot:\n{}\nexpected:\n{}",
60                table,
61                expected,
62            );
63        }
64    };
65}
66
67/// Assert a given table.
68///
69/// # Example
70///
71/// ```text
72/// assert_table!(
73///     Table::new([[1, 2, 3], [4, 5, 6]]),
74///     "|--|--|"
75///     "|XX|XY|"
76///     "|--|--|"
77/// )
78/// ```
79#[macro_export]
80macro_rules! assert_table {
81    ($table:expr, $($line:expr)*) => {
82        let table = $table.to_string();
83        let expected = $crate::static_table!($($line)*);
84        assert_eq!(
85            table,
86            expected,
87            "\ngot:\n{}\nexpected:\n{}",
88            table,
89            expected,
90        );
91    };
92}
93
94/// Assert a given table width.
95///
96/// # Example
97///
98/// ```text
99/// assert_width!(Table::new([[1, 2, 3], [4, 5, 6]]), 10);
100/// ```
101#[macro_export]
102macro_rules! assert_width {
103    ($table:expr, $expected:expr) => {
104        let expected = $expected;
105        let table = $table.to_string();
106        let width = $crate::get_text_width(&table);
107        assert_eq!(width, expected);
108    };
109}