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.
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::*;
13 use std::io::BufReader;
14 use std::io::prelude::*;
17 pub struct ExpectedError {
23 #[derive(PartialEq, Debug)]
24 enum WhichLine { ThisLine, FollowPrevious(usize), AdjustBackward(usize) }
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.
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());
37 // `last_nonfollow_error` tracks the most recently seen
38 // line with an error template that did not use the
39 // follow-syntax, "//~| ...".
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;
47 rdr.lines().enumerate().filter_map(|(line_no, ln)| {
48 parse_expected(last_nonfollow_error,
51 .map(|(which, error)| {
53 FollowPrevious(_) => {}
54 _ => last_nonfollow_error = Some(error.line),
61 fn parse_expected(last_nonfollow_error: Option<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) == '|' {
68 (false, line[start + 3..].chars().take_while(|c| *c == '^').count())
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())
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();
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.")
86 (FollowPrevious(line), line)
89 if adjusts > 0 { AdjustBackward(adjusts) } else { ThisLine };
90 let line = line_num - adjusts;
94 debug!("line={} which={:?} kind={:?} msg={:?}", line_num, which, kind, msg);
95 Some((which, ExpectedError { line: line,