]> git.lizzy.rs Git - rust.git/commitdiff
Use enum for message kind in compiletest harness.
authorCorey Farwell <coreyf@rwell.org>
Mon, 14 Mar 2016 14:28:38 +0000 (10:28 -0400)
committerCorey Farwell <coreyf@rwell.org>
Fri, 18 Mar 2016 23:04:29 +0000 (19:04 -0400)
src/compiletest/errors.rs
src/compiletest/runtest.rs

index c1fbfcda475597f804bd59eca1909e9236ddefb0..0a7784ccc007f7005ea091db605d5ee3826df614 100644 (file)
@@ -9,14 +9,54 @@
 // except according to those terms.
 use self::WhichLine::*;
 
+use std::fmt;
 use std::fs::File;
 use std::io::BufReader;
 use std::io::prelude::*;
 use std::path::Path;
+use std::str::FromStr;
+
+#[derive(Clone, Debug, PartialEq)]
+pub enum ErrorKind {
+    Help,
+    Error,
+    Note,
+    Suggestion,
+    Warning,
+}
+
+impl FromStr for ErrorKind {
+    type Err = ();
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
+        match &s.trim_right_matches(':') as &str {
+            "HELP" => Ok(ErrorKind::Help),
+            "ERROR" => Ok(ErrorKind::Error),
+            "NOTE" => Ok(ErrorKind::Note),
+            "SUGGESTION" => Ok(ErrorKind::Suggestion),
+            "WARN" => Ok(ErrorKind::Warning),
+            "WARNING" => Ok(ErrorKind::Warning),
+            _ => Err(()),
+        }
+    }
+}
+
+impl fmt::Display for ErrorKind {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        match *self {
+            ErrorKind::Help => write!(f, "help"),
+            ErrorKind::Error => write!(f, "error"),
+            ErrorKind::Note => write!(f, "note"),
+            ErrorKind::Suggestion => write!(f, "suggestion"),
+            ErrorKind::Warning => write!(f, "warning"),
+        }
+    }
+}
 
 pub struct ExpectedError {
     pub line_num: usize,
-    pub kind: String,
+    /// What kind of message we expect (e.g. warning, error, suggestion).
+    /// `None` if not specified or unknown message kind.
+    pub kind: Option<ErrorKind>,
     pub msg: String,
 }
 
@@ -84,8 +124,9 @@ fn parse_expected(last_nonfollow_error: Option<usize>,
     let letters = line[kind_start..].chars();
     let kind = letters.skip_while(|c| c.is_whitespace())
                       .take_while(|c| !c.is_whitespace())
-                      .flat_map(|c| c.to_lowercase())
-                      .collect::<String>();
+                      .collect::<String>()
+                      .parse::<ErrorKind>()
+                      .ok();
     let letters = line[kind_start..].chars();
     let msg = letters.skip_while(|c| c.is_whitespace())
                      .skip_while(|c| !c.is_whitespace())
index be011107c50301b6c9c0324b80ff7146035e2a04..82e624e055a9cad051572cdbed4be0fd1b3e1353 100644 (file)
@@ -11,7 +11,7 @@
 use common::Config;
 use common::{CompileFail, ParseFail, Pretty, RunFail, RunPass, RunPassValgrind};
 use common::{Codegen, DebugInfoLldb, DebugInfoGdb, Rustdoc, CodegenUnits};
-use errors;
+use errors::{self, ErrorKind};
 use header::TestProps;
 use header;
 use procsrv;
@@ -1013,8 +1013,8 @@ fn check_expected_errors(revision: Option<&str>,
         expected_errors.iter()
                         .fold((false, false),
                               |(acc_help, acc_note), ee|
-                                  (acc_help || ee.kind == "help:" || ee.kind == "help",
-                                   acc_note || ee.kind == "note:" || ee.kind == "note"));
+                                  (acc_help || ee.kind == Some(ErrorKind::Help),
+                                   acc_note || ee.kind == Some(ErrorKind::Note)));
 
     // Scan and extract our error/warning messages,
     // which look like:
@@ -1032,15 +1032,15 @@ fn check_expected_errors(revision: Option<&str>,
         let mut prev = 0;
         for (i, ee) in expected_errors.iter().enumerate() {
             if !found_flags[i] {
-                debug!("prefix={} ee.kind={} ee.msg={} line={}",
+                debug!("prefix={} ee.kind={:?} ee.msg={} line={}",
                        prefixes[i],
                        ee.kind,
                        ee.msg,
                        line);
                 // Suggestions have no line number in their output, so take on the line number of
                 // the previous expected error
-                if ee.kind == "suggestion" {
-                    assert!(expected_errors[prev].kind == "help",
+                if ee.kind == Some(ErrorKind::Suggestion) {
+                    assert!(expected_errors[prev].kind == Some(ErrorKind::Help),
                             "SUGGESTIONs must be preceded by a HELP");
                     if line.contains(&ee.msg) {
                         found_flags[i] = true;
@@ -1050,7 +1050,7 @@ fn check_expected_errors(revision: Option<&str>,
                 }
                 if
                     (prefix_matches(line, &prefixes[i]) || continuation(line)) &&
-                    line.contains(&ee.kind) &&
+                    (ee.kind.is_none() || line.contains(&ee.kind.as_ref().unwrap().to_string())) &&
                     line.contains(&ee.msg)
                 {
                     found_flags[i] = true;
@@ -1076,7 +1076,10 @@ fn check_expected_errors(revision: Option<&str>,
         if !flag {
             let ee = &expected_errors[i];
             error(revision, &format!("expected {} on line {} not found: {}",
-                                     ee.kind, ee.line_num, ee.msg));
+                                     ee.kind.as_ref()
+                                            .map_or("message".into(),
+                                                    |k| k.to_string()),
+                                     ee.line_num, ee.msg));
             not_found += 1;
         }
     }