about summary refs log tree commit diff
path: root/src/compiletest/errors.rs
blob: c96e688c2900cae3d864b04715d2ef9df5cd5b7d (plain)
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
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::io::{BufferedReader, File};
use regex::Regex;

pub struct ExpectedError {
    pub line: uint,
    pub kind: String,
    pub msg: String,
}

pub static EXPECTED_PATTERN : &'static str = r"//~(?P<adjusts>\^*)\s*(?P<kind>\S*)\s*(?P<msg>.*)";

// Load any test directives embedded in the file
pub fn load_errors(re: &Regex, testfile: &Path) -> Vec<ExpectedError> {
    let mut rdr = BufferedReader::new(File::open(testfile).unwrap());

    rdr.lines().enumerate().filter_map(|(line_no, ln)| {
        parse_expected(line_no + 1, ln.unwrap().as_slice(), re)
    }).collect()
}

fn parse_expected(line_num: uint, line: &str, re: &Regex) -> Option<ExpectedError> {
    re.captures(line).and_then(|caps| {
        let adjusts = caps.name("adjusts").len();
        let kind = caps.name("kind").to_ascii().to_lower().into_str().to_string();
        let msg = caps.name("msg").trim().to_string();

        debug!("line={} kind={} msg={}", line_num, kind, msg);
        Some(ExpectedError {
            line: line_num - adjusts,
            kind: kind,
            msg: msg,
        })
    })
}