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
pub mod parser;
pub mod tokenizer;
pub mod utils;

#[cfg(test)]
mod test_tokenizer {
    use crate::tokenizer;

    #[test]
    fn tokenize() {
        let raw_string = String::from("\n{|123||\n|}<><nowiki>");
        let expect_result = Vec::from(["\n{|", "1", "2", "3", "||", "\n|}", "<", ">", "<nowiki>"]);
        let tokenizer = tokenizer::Tokenizer::build();
        let out = tokenizer.tokenize(&raw_string);
        assert_eq!(out.join(" / "), expect_result.join(" / "));
    }
}

#[cfg(test)]
mod test_parser {
    use crate::parser::{Event, WikitextTableParser};
    use std::fs::File;
    use std::io::Read;

    fn test_parse_struct_table(path: String, expect_rows: i32, expect_cols: i32) {
        /* Test a wiki text table that has expect number of rows and cols */

        // read table from text
        let mut file = match File::open(&path) {
            Ok(file) => file,
            Err(_) => {
                return;
            }
        };
        let mut content: String = String::new();
        if let Err(_) = file.read_to_string(&mut content) {
            return;
        }

        //
        let mut count_rows = 0;
        let mut count_cols = 0;
        let mut count_table_start = 0;
        let mut count_table_end = 0;

        let wikitext_table_parser = WikitextTableParser::new(&content);
        for event in wikitext_table_parser {
            match event {
                Event::RowStyle(row_style) => {
                    if count_rows > 0 {
                        // do not work just after parse the first row, which is a table headr.
                        assert_eq!(expect_cols, count_cols);
                    }
                    count_rows += 1;
                    count_cols = 0;
                    println!("----- {:?} -----", row_style);
                }
                Event::Col(text) => {
                    count_cols += 1;
                    println!("col: {:?}#", text);
                }
                Event::TableStart => count_table_start += 1,
                Event::TableEnd => count_table_end += 1,
                _ => {}
            }
        }
        assert_eq!(expect_rows, count_rows);
        assert_eq!(1, count_table_start);
        assert_eq!(1, count_table_end)
    }

    fn test_table_caption(path:String,expect_caption:String) {
        // read table from text
        let mut file = match File::open(&path) {
            Ok(file) => file,
            Err(_) => {
                return;
            }
        };
        let mut content: String = String::new();
        if let Err(_) = file.read_to_string(&mut content) {
            return;
        }

        let wikitext_table_parser = WikitextTableParser::new(&content);
        for event in wikitext_table_parser {
            match event {
                Event::TableCaption(caption)=>{
                    assert_eq!(caption,expect_caption);
                }
                _ => {}
            }
        }
    
    }

    #[test]
    fn test_parse_struct_table_1() {
        test_parse_struct_table(String::from("wikitext_tables/1.txt"), 11, 2)
    }

    #[test]
    fn test_parse_struct_table_2() {
        test_parse_struct_table(String::from("wikitext_tables/2.txt"), 5, 5)
    }

    #[test]
    fn test_parse_struct_table_3() {
        test_parse_struct_table(String::from("wikitext_tables/3.txt"), 12, 5)
    }

    #[test]
    fn test_parse_struct_table_4() {
        test_parse_struct_table(String::from("wikitext_tables/4.txt"), 8, 5)
    }

    #[test]
    fn test_parse_struct_table_5() {
        test_parse_struct_table(String::from("wikitext_tables/5.txt"), 14, 7)
    }

    #[test]
    fn test_table_caption_1() {
        let path = String::from("wikitext_tables/1.txt");
        let expect_caption = "Seawater elemental composition<br>(salinity = 3.5%) {{citation needed|date=June 2019}}";
        test_table_caption(path, expect_caption.to_string())
    }
}