Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the current parser location for CSS error #18585

Closed
wants to merge 9 commits into from

Use arrays for error reporting test expected values

  • Loading branch information
SimonSapin committed Oct 10, 2017
commit 30e5b6fcd9edf32711ee43666e4de78f401ddf4c
@@ -12,7 +12,7 @@ use servo_arc::Arc;
use servo_atoms::Atom;
use servo_url::ServoUrl;
use std::borrow::ToOwned;
use std::sync::Mutex;
use std::cell::RefCell;
use std::sync::atomic::AtomicBool;
use style::context::QuirksMode;
use style::error_reporting::{ParseErrorReporter, ContextualParseError};
@@ -259,39 +259,55 @@ fn test_parse_stylesheet() {
assert_eq!(format!("{:#?}", stylesheet), format!("{:#?}", expected));
}

#[derive(Debug)]
struct CSSError {
pub url : ServoUrl,
pub line: u32,
pub column: u32,
pub message: String
}

struct CSSInvalidErrorReporterTest {
pub errors: Arc<Mutex<Vec<CSSError>>>
struct TestingErrorReporter {
errors: RefCell<Vec<CSSError>>,
}

impl CSSInvalidErrorReporterTest {
pub fn new() -> CSSInvalidErrorReporterTest {
return CSSInvalidErrorReporterTest{
errors: Arc::new(Mutex::new(Vec::new()))
impl TestingErrorReporter {
pub fn new() -> Self {
TestingErrorReporter {
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,
url: &ServoUrl,
location: SourceLocation,
error: ContextualParseError) {
let mut errors = self.errors.lock().unwrap();
errors.push(
self.errors.borrow_mut().push(
CSSError{
url: url.clone(),
line: location.line,
column: location.column,
message: error.to_string(),
}
);
)
}
}

@@ -306,31 +322,19 @@ fn test_report_error_stylesheet() {
}
";
let url = ServoUrl::parse("about::test").unwrap();
let error_reporter = CSSInvalidErrorReporterTest::new();

let errors = error_reporter.errors.clone();
let error_reporter = TestingErrorReporter::new();

let lock = SharedRwLock::new();
let media = Arc::new(lock.wrap(MediaList::empty()));
Stylesheet::from_str(css, url.clone(), Origin::UserAgent, media, lock,
None, &error_reporter, QuirksMode::NoQuirks, 5);

let mut errors = errors.lock().unwrap();

let error = errors.pop().unwrap();
assert_eq!("Unsupported property declaration: 'invalid: true;', \
Custom(PropertyDeclaration(UnknownProperty(\"invalid\")))", error.message);
assert_eq!(9, error.line);
assert_eq!(9, error.column);
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!("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);
assert_eq!(error_reporter.errors.borrow()[0].url, url);
}

#[test]
@@ -343,21 +347,16 @@ fn test_no_report_unrecognized_vendor_properties() {
}
";
let url = ServoUrl::parse("about::test").unwrap();
let error_reporter = CSSInvalidErrorReporterTest::new();

let errors = error_reporter.errors.clone();
let error_reporter = TestingErrorReporter::new();

let lock = SharedRwLock::new();
let media = Arc::new(lock.wrap(MediaList::empty()));
Stylesheet::from_str(css, url, Origin::UserAgent, media, lock,
None, &error_reporter, QuirksMode::NoQuirks, 0);

let mut errors = errors.lock().unwrap();
let error = errors.pop().unwrap();
assert_eq!("Unsupported property declaration: '-moz-background-color: red;', \
Custom(PropertyDeclaration(UnknownProperty(\"-moz-background-color\")))",
error.message);
assert!(errors.is_empty());
error_reporter.assert_messages_contain(&[
(4, 9, "Unsupported property declaration: '-moz-background-color: red;'"),
]);
}

#[test]
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.