aboutsummaryrefslogtreecommitdiff
path: root/parse_wiki_text/examples/test/test.rs
blob: 9f0494264b918c2cc835a5bd05f02868605a1cc8 (plain) (blame)
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
// Copyright 2019 Fredrik Portström <https://portstrom.com>
// This is free software distributed under the terms specified in
// the file LICENSE at the top-level directory of this distribution.

use crate::test_cases::TEST_CASES;

pub fn run_test(configuration: &parse_wiki_text::Configuration) {
    let mut output = concat!(
        "<title>Parse Wiki Text test cases</title>",
        "<style>",
        "a{color:#006064;display:block;padding:8;text-decoration:none}",
        "a:hover{background:#eee}",
        "body{background:#f7f7f7;display:flex;font-family:sans-serif;height:100%;margin:0}",
        "div div{background:#fff;box-shadow: 0 1px 3px rgba(0,0,0,.12),0 1px 2px rgba(0,0,0,.24);margin:16;padding:16}",
        "h1{font-size:20;margin:24 16 16}",
        "hr{border:0;border-top:1px solid #ccc}",
        "pre{margin:0}",
        "span{color:#aaa}",
        "</style>",
        "<div style=\"background:#fff;box-shadow: 0 1px 3px rgba(0,0,0,.12),0 1px 2px rgba(0,0,0,.24);flex:0 1 220px;overflow:auto\">"
    ).to_owned();
    if let Some(window) = TEST_CASES
        .windows(2)
        .find(|window| window[0].0 >= window[1].0)
    {
        panic!("Sort: {:#?}", (window[0].0, window[1].0));
    }
    for (title, test_cases) in TEST_CASES {
        if let Some(window) = test_cases.windows(2).find(|window| window[0] >= window[1]) {
            panic!("Sort: {:#?}", window);
        }
        output += &format!("<a href=#{}>", title.replace(" ", "_"));
        output += title;
        output += &format!(" <span>{}</span></a>", test_cases.len());
    }
    output += "</div><div style=\"flex:1 1 200px;overflow:auto\">";
    for (title, test_cases) in TEST_CASES {
        output += &format!("<h1 id={}>", title.replace(" ", "_"));
        output += title;
        output += "</h1>";
        for wiki_text in *test_cases {
            output += "<div><pre>";
            output += &wiki_text
                .replace("&", "&amp;")
                .replace("<", "&lt;")
                .replace("\t", "<span>⭾</span>")
                .replace("\n", "<span>⏎</span>\n")
                .replace(" ", "<span>·</span>")
                .replace("</span><span>", "");
            match std::panic::catch_unwind(|| configuration.parse(wiki_text)) {
                Err(_) => {
                    eprintln!("Panic with wiki text {:?}", wiki_text);
                    output += "</pre><hr>panic</div>";
                }
                Ok(result) => {
                    output += "</pre><hr><pre>";
                    output += &format!("{:#?}", result)
                        .replace("&", "&amp;")
                        .replace("<", "&lt;");
                    output += "</pre></div>";
                }
            }
        }
    }
    output += "</div>";
    if let Err(error) = std::fs::write("report.html", output) {
        eprintln!("Failed to write report: {}", error);
        std::process::exit(1);
    }
}