aboutsummaryrefslogtreecommitdiff
path: root/tests/parse_tests.rs
blob: 1f15bd22cde8f47c0d916c0a5645c45ab047c020 (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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use aip_filter::parse;
use std::fs;
use std::path::Path;

/// Parse a txtar-style test file into named sections.
/// Sections are delimited by `-- name --` lines.
fn parse_txtar(content: &str) -> Vec<(String, String)> {
    let mut files: Vec<(String, String)> = Vec::new();
    let mut current_name: Option<String> = None;
    let mut current_data = String::new();

    for line in content.split('\n') {
        let trimmed = line.trim_end_matches('\r');
        if trimmed.starts_with("-- ") && trimmed.ends_with(" --") {
            if let Some(name) = current_name.take() {
                files.push((name, current_data.clone()));
            }
            let name = trimmed[3..trimmed.len() - 3].to_string();
            current_name = Some(name);
            current_data.clear();
        } else if current_name.is_some() {
            current_data.push_str(line);
            current_data.push('\n');
        }
    }
    if let Some(name) = current_name {
        files.push((name, current_data));
    }
    files
}

#[test]
fn test_parse() {
    let test_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("testdata/parse");
    if !test_dir.exists() {
        eprintln!("Skipping parse tests: testdata/parse not found");
        return;
    }

    let mut total = 0;
    let mut passed = 0;
    let mut failed_tests = Vec::new();

    for entry in fs::read_dir(&test_dir).unwrap() {
        let entry = entry.unwrap();
        let path = entry.path();
        if path.extension().map(|e| e == "txt").unwrap_or(false) {
            let content = fs::read_to_string(&path).unwrap();
            let files = parse_txtar(&content);

            let mut i = 0;
            while i < files.len() {
                let (name, data) = &files[i];
                if !name.ends_with(".test") {
                    i += 1;
                    continue;
                }

                let base = name.strip_suffix(".test").unwrap();
                total += 1;

                let result = parse(data);

                i += 1;
                if i >= files.len() {
                    eprintln!("  {}: missing expected result", base);
                    continue;
                }

                let (expected_name, expected_data) = &files[i];

                if expected_name.ends_with(".out") {
                    match result {
                        Err(e) => {
                            failed_tests.push(format!(
                                "{}:{}: got error '{}', expected success",
                                path.file_name().unwrap().to_str().unwrap(),
                                base,
                                e
                            ));
                        }
                        Ok(expr) => {
                            let got = match expr {
                                Some(e) => format!("{}", e),
                                None => String::new(),
                            };
                            let want = expected_data.trim_end_matches('\n');
                            let got_trimmed = got.trim_end_matches('\n');
                            if got_trimmed == want {
                                passed += 1;
                            } else {
                                failed_tests.push(format!(
                                    "{}:{}: output mismatch\n  got:  {:?}\n  want: {:?}",
                                    path.file_name().unwrap().to_str().unwrap(),
                                    base,
                                    got_trimmed,
                                    want
                                ));
                            }
                        }
                    }
                } else if expected_name.ends_with(".err") {
                    match result {
                        Ok(_) => {
                            failed_tests.push(format!(
                                "{}:{}: got success, expected error '{}'",
                                path.file_name().unwrap().to_str().unwrap(),
                                base,
                                expected_data.trim()
                            ));
                        }
                        Err(e) => {
                            let want = expected_data.trim();
                            let got = e.trim();
                            if got == want || regex_error_prefix_matches(got, want) {
                                passed += 1;
                            } else {
                                failed_tests.push(format!(
                                    "{}:{}: error mismatch\n  got:  {:?}\n  want: {:?}",
                                    path.file_name().unwrap().to_str().unwrap(),
                                    base,
                                    got,
                                    want
                                ));
                            }
                        }
                    }
                }
            }
        }
    }

    println!("\nParse tests: {}/{} passed", passed, total);
    if !failed_tests.is_empty() {
        println!("\nFailed tests:");
        for f in &failed_tests {
            println!("  {}", f);
        }
        panic!("{} of {} parse tests failed", failed_tests.len(), total);
    }
}

/// Returns true when the position prefix and "invalid regular expression:" label
/// match but the trailing error detail does not - the regex crate's error messages
/// are more verbose than the testdata expects, so we accept any error at the right
/// position with the right label.
fn regex_error_prefix_matches(got: &str, want: &str) -> bool {
    const MARKER: &str = "invalid regular expression:";
    match (got.split_once(MARKER), want.split_once(MARKER)) {
        (Some((gp, _)), Some((wp, _))) => gp == wp,
        _ => false,
    }
}