]> git.lizzy.rs Git - rust.git/blob - src/compiletest/errors.rs
libstd: implement From<&Path|PathBuf> for Cow<Path>
[rust.git] / src / compiletest / errors.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10 use self::WhichLine::*;
11
12 use std::fs::File;
13 use std::io::BufReader;
14 use std::io::prelude::*;
15 use std::path::Path;
16
17 pub struct ExpectedError {
18     pub line: usize,
19     pub kind: String,
20     pub msg: String,
21 }
22
23 #[derive(PartialEq, Debug)]
24 enum WhichLine { ThisLine, FollowPrevious(usize), AdjustBackward(usize) }
25
26 /// Looks for either "//~| KIND MESSAGE" or "//~^^... KIND MESSAGE"
27 /// The former is a "follow" that inherits its target from the preceding line;
28 /// the latter is an "adjusts" that goes that many lines up.
29 ///
30 /// Goal is to enable tests both like: //~^^^ ERROR go up three
31 /// and also //~^ ERROR message one for the preceding line, and
32 ///          //~| ERROR message two for that same line.
33 // Load any test directives embedded in the file
34 pub fn load_errors(testfile: &Path) -> Vec<ExpectedError> {
35     let rdr = BufReader::new(File::open(testfile).unwrap());
36
37     // `last_nonfollow_error` tracks the most recently seen
38     // line with an error template that did not use the
39     // follow-syntax, "//~| ...".
40     //
41     // (pnkfelix could not find an easy way to compose Iterator::scan
42     // and Iterator::filter_map to pass along this information into
43     // `parse_expected`. So instead I am storing that state here and
44     // updating it in the map callback below.)
45     let mut last_nonfollow_error = None;
46
47     rdr.lines().enumerate().filter_map(|(line_no, ln)| {
48         parse_expected(last_nonfollow_error,
49                        line_no + 1,
50                        &ln.unwrap())
51             .map(|(which, error)| {
52                 match which {
53                     FollowPrevious(_) => {}
54                     _ => last_nonfollow_error = Some(error.line),
55                 }
56                 error
57             })
58     }).collect()
59 }
60
61 fn parse_expected(last_nonfollow_error: Option<usize>,
62                   line_num: usize,
63                   line: &str) -> Option<(WhichLine, ExpectedError)> {
64     let start = match line.find("//~") { Some(i) => i, None => return None };
65     let (follow, adjusts) = if line.char_at(start + 3) == '|' {
66         (true, 0)
67     } else {
68         (false, line[start + 3..].chars().take_while(|c| *c == '^').count())
69     };
70     let kind_start = start + 3 + adjusts + (follow as usize);
71     let letters = line[kind_start..].chars();
72     let kind = letters.skip_while(|c| c.is_whitespace())
73                       .take_while(|c| !c.is_whitespace())
74                       .flat_map(|c| c.to_lowercase())
75                       .collect::<String>();
76     let letters = line[kind_start..].chars();
77     let msg = letters.skip_while(|c| c.is_whitespace())
78                      .skip_while(|c| !c.is_whitespace())
79                      .collect::<String>().trim().to_owned();
80
81     let (which, line) = if follow {
82         assert!(adjusts == 0, "use either //~| or //~^, not both.");
83         let line = last_nonfollow_error.unwrap_or_else(|| {
84             panic!("encountered //~| without preceding //~^ line.")
85         });
86         (FollowPrevious(line), line)
87     } else {
88         let which =
89             if adjusts > 0 { AdjustBackward(adjusts) } else { ThisLine };
90         let line = line_num - adjusts;
91         (which, line)
92     };
93
94     debug!("line={} which={:?} kind={:?} msg={:?}", line_num, which, kind, msg);
95     Some((which, ExpectedError { line: line,
96                                  kind: kind,
97                                  msg: msg, }))
98 }