Use arrays for error reporting test expected values

This commit is contained in:
Simon Sapin 2017-09-25 17:08:34 +02:00
parent fa3ce34afe
commit 30e5b6fcd9

View file

@ -12,7 +12,7 @@ use servo_arc::Arc;
use servo_atoms::Atom; use servo_atoms::Atom;
use servo_url::ServoUrl; use servo_url::ServoUrl;
use std::borrow::ToOwned; use std::borrow::ToOwned;
use std::sync::Mutex; use std::cell::RefCell;
use std::sync::atomic::AtomicBool; use std::sync::atomic::AtomicBool;
use style::context::QuirksMode; use style::context::QuirksMode;
use style::error_reporting::{ParseErrorReporter, ContextualParseError}; use style::error_reporting::{ParseErrorReporter, ContextualParseError};
@ -259,6 +259,7 @@ fn test_parse_stylesheet() {
assert_eq!(format!("{:#?}", stylesheet), format!("{:#?}", expected)); assert_eq!(format!("{:#?}", stylesheet), format!("{:#?}", expected));
} }
#[derive(Debug)]
struct CSSError { struct CSSError {
pub url : ServoUrl, pub url : ServoUrl,
pub line: u32, pub line: u32,
@ -266,32 +267,47 @@ struct CSSError {
pub message: String pub message: String
} }
struct CSSInvalidErrorReporterTest { struct TestingErrorReporter {
pub errors: Arc<Mutex<Vec<CSSError>>> errors: RefCell<Vec<CSSError>>,
} }
impl CSSInvalidErrorReporterTest { impl TestingErrorReporter {
pub fn new() -> CSSInvalidErrorReporterTest { pub fn new() -> Self {
return CSSInvalidErrorReporterTest{ TestingErrorReporter {
errors: Arc::new(Mutex::new(Vec::new())) errors: RefCell::new(Vec::new()),
}
}
fn assert_messages_contain(&self, expected_errors: &[(u32, u32, &str)]) {
let errors = self.errors.borrow();
for (i, (error, &(line, column, message))) in errors.iter().zip(expected_errors).enumerate() {
assert_eq!((error.line, error.column), (line, column),
"line/column numbers of the {}th error", i + 1);
assert!(error.message.contains(message),
"{:?} does not contain {:?}", error.message, message);
}
if errors.len() < expected_errors.len() {
panic!("Missing errors: {:?}", &expected_errors[errors.len()..]);
}
if errors.len() > expected_errors.len() {
panic!("Extra errors: {:?}", &errors[expected_errors.len()..]);
} }
} }
} }
impl ParseErrorReporter for CSSInvalidErrorReporterTest { impl ParseErrorReporter for TestingErrorReporter {
fn report_error(&self, fn report_error(&self,
url: &ServoUrl, url: &ServoUrl,
location: SourceLocation, location: SourceLocation,
error: ContextualParseError) { error: ContextualParseError) {
let mut errors = self.errors.lock().unwrap(); self.errors.borrow_mut().push(
errors.push(
CSSError{ CSSError{
url: url.clone(), url: url.clone(),
line: location.line, line: location.line,
column: location.column, column: location.column,
message: error.to_string(), message: error.to_string(),
} }
); )
} }
} }
@ -306,31 +322,19 @@ fn test_report_error_stylesheet() {
} }
"; ";
let url = ServoUrl::parse("about::test").unwrap(); let url = ServoUrl::parse("about::test").unwrap();
let error_reporter = CSSInvalidErrorReporterTest::new(); let error_reporter = TestingErrorReporter::new();
let errors = error_reporter.errors.clone();
let lock = SharedRwLock::new(); let lock = SharedRwLock::new();
let media = Arc::new(lock.wrap(MediaList::empty())); let media = Arc::new(lock.wrap(MediaList::empty()));
Stylesheet::from_str(css, url.clone(), Origin::UserAgent, media, lock, Stylesheet::from_str(css, url.clone(), Origin::UserAgent, media, lock,
None, &error_reporter, QuirksMode::NoQuirks, 5); None, &error_reporter, QuirksMode::NoQuirks, 5);
let mut errors = errors.lock().unwrap(); error_reporter.assert_messages_contain(&[
(8, 9, "Unsupported property declaration: 'display: invalid;'"),
(9, 9, "Unsupported property declaration: 'invalid: true;'"),
]);
let error = errors.pop().unwrap(); assert_eq!(error_reporter.errors.borrow()[0].url, url);
assert_eq!("Unsupported property declaration: 'invalid: true;', \
Custom(PropertyDeclaration(UnknownProperty(\"invalid\")))", error.message);
assert_eq!(9, error.line);
assert_eq!(9, error.column);
let error = errors.pop().unwrap();
assert_eq!("Unsupported property declaration: 'display: invalid;', \
Custom(PropertyDeclaration(InvalidValue(\"display\", None)))", error.message);
assert_eq!(8, error.line);
assert_eq!(9, error.column);
// testing for the url
assert_eq!(url, error.url);
} }
#[test] #[test]
@ -343,21 +347,16 @@ fn test_no_report_unrecognized_vendor_properties() {
} }
"; ";
let url = ServoUrl::parse("about::test").unwrap(); let url = ServoUrl::parse("about::test").unwrap();
let error_reporter = CSSInvalidErrorReporterTest::new(); let error_reporter = TestingErrorReporter::new();
let errors = error_reporter.errors.clone();
let lock = SharedRwLock::new(); let lock = SharedRwLock::new();
let media = Arc::new(lock.wrap(MediaList::empty())); let media = Arc::new(lock.wrap(MediaList::empty()));
Stylesheet::from_str(css, url, Origin::UserAgent, media, lock, Stylesheet::from_str(css, url, Origin::UserAgent, media, lock,
None, &error_reporter, QuirksMode::NoQuirks, 0); None, &error_reporter, QuirksMode::NoQuirks, 0);
let mut errors = errors.lock().unwrap(); error_reporter.assert_messages_contain(&[
let error = errors.pop().unwrap(); (4, 9, "Unsupported property declaration: '-moz-background-color: red;'"),
assert_eq!("Unsupported property declaration: '-moz-background-color: red;', \ ]);
Custom(PropertyDeclaration(UnknownProperty(\"-moz-background-color\")))",
error.message);
assert!(errors.is_empty());
} }
#[test] #[test]