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
mod content;
mod content_iterator;
mod data_item;
mod data_source;
mod table_data_source;
pub mod table;

pub use content::ContentStyle;

#[cfg(test)]
mod tests {
    use super::*;
    use std::env;
    use table::{Border, Table};
    use table::row::Row;
    use table::cell::Cell;
    use colored::Color;

    #[test]
    fn test_simple_table() {
        let table =
            table!(
                "{^:10:}" => "Food", "{^:10:}" => "Count";
                "Fish", "15", "Pizza", "10", "Steak", "6"
            );

        let output = table.format();
        println!("{}", output);

        let expected = "+----------+----------+\n|   Food   |  Count   |\n+----------+----------+\n|Fish      |15        |\n+----------+----------+\n|Pizza     |10        |\n+----------+----------+\n|Steak     |6         |\n+----------+----------+\n";

        assert_eq!(output, expected);
    }

    #[test]
    fn test_simple_vector_table() {
        let table = table!(
            "{c^:15:}" => "Food", "{c^:10:}" => "Count";
            "Fish", "3", "Pears", "5", "Pizza", "13"
        );

        let output = table.format();
        println!("{}", output);

        let expected =
            match env::var("NO_COLOR") {
                Ok(_) => "+---------------+----------+\n|     Food      |  Count   |\n+---------------+----------+\n|Fish           |3         |\n+---------------+----------+\n|Pears          |5         |\n+---------------+----------+\n|Pizza          |13        |\n+---------------+----------+\n",
                Err(_) => "+---------------+----------+\n|\u{1b}[36m     Food      \u{1b}[0m|\u{1b}[36m  Count   \u{1b}[0m|\n+---------------+----------+\n|Fish           |3         |\n+---------------+----------+\n|Pears          |5         |\n+---------------+----------+\n|Pizza          |13        |\n+---------------+----------+\n",
            };

        assert_eq!(output, expected);
    }

    #[test]
    fn test_custom_border_table() {
        let mut table = table!(
            "{R^:10:}" => "Custom", "{C^:20:}" => "Borders";
            "are", "super fun", "and", "super awesome"
        );

        table.border = Border {
            top_left: '┌',
            top: '─',
            top_right: '┐',
            top_split: '┬',
            bottom_left: '└',
            bottom: '─',
            bottom_right: '┘',
            bottom_split: '┴',
            left: '│',
            left_split:'├',
            right: '│',
            right_split: '┤',
            vertical_split: '│',
            vertical_split_intersect_left: '┤',
            vertical_split_intersect_right: '├',
            vertical_split_intersect_both: '┼',
            horizontal_split: '─',
            horizontal_split_intersect_top: '┴',
            horizontal_split_intersect_bottom: '┬',
            horizontal_split_intersect_both: '┼',
            color: Color::Red
        };

        let output = table.format();
        println!("{}", output);

        let expected =
            match env::var("NO_COLOR") {
                Ok(_) => "┌──────────┬────────────────────┐\n│  Custom  │      Borders       │\n├──────────┼────────────────────┤\n│are       │super fun           │\n├──────────┼────────────────────┤\n│and       │super awesome       │\n└──────────┴────────────────────┘\n",
                Err(_) => "┌──────────┬────────────────────┐\n│\u{1b}[91m  Custom  \u{1b}[0m│\u{1b}[96m      Borders       \u{1b}[0m│\n├──────────┼────────────────────┤\n│are       │super fun           │\n├──────────┼────────────────────┤\n│and       │super awesome       │\n└──────────┴────────────────────┘\n",
            };

        assert_eq!(output, expected);
    }
}