aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIszak Bryan <iszak.bryan@gmail.com>2016-02-07 18:46:41 +0000
committerIszak Bryan <iszak.bryan@gmail.com>2016-02-07 18:46:41 +0000
commit68d512c1eaa581654780f2c17ab9398660b3907c (patch)
treed2c56ece727684fb441361903de78e9b80197304
parent28ecb0bba3fa395ed56bb2448a21e02c3ff88c8b (diff)
downloadservo-68d512c1eaa581654780f2c17ab9398660b3907c.tar.gz
servo-68d512c1eaa581654780f2c17ab9398660b3907c.zip
Added test to verify parse error reporter is functioning
-rw-r--r--tests/unit/style/stylesheets.rs78
1 files changed, 77 insertions, 1 deletions
diff --git a/tests/unit/style/stylesheets.rs b/tests/unit/style/stylesheets.rs
index a929f487bc4..997a0c8af6f 100644
--- a/tests/unit/style/stylesheets.rs
+++ b/tests/unit/style/stylesheets.rs
@@ -2,14 +2,16 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-use cssparser;
+use cssparser::{self, Parser, SourcePosition};
use media_queries::CSSErrorReporterTest;
use selectors::parser::*;
use std::borrow::ToOwned;
use std::sync::Arc;
+use std::sync::Mutex;
use string_cache::Atom;
use style::properties::{PropertyDeclaration, PropertyDeclarationBlock, DeclaredValue, longhands};
use style::stylesheets::{CSSRule, StyleRule, Origin, Stylesheet};
+use style::error_reporting::ParseErrorReporter;
#[test]
fn test_parse_stylesheet() {
@@ -139,3 +141,77 @@ fn test_parse_stylesheet() {
],
});
}
+
+
+struct CSSError {
+ pub line: usize,
+ pub column: usize,
+ pub message: String
+}
+
+struct CSSInvalidErrorReporterTest {
+ pub errors: Arc<Mutex<Vec<CSSError>>>
+}
+
+impl CSSInvalidErrorReporterTest {
+ pub fn new() -> CSSInvalidErrorReporterTest {
+ return CSSInvalidErrorReporterTest{
+ errors: Arc::new(Mutex::new(Vec::new()))
+ }
+ }
+}
+
+impl ParseErrorReporter for CSSInvalidErrorReporterTest {
+ fn report_error(&self, input: &mut Parser, position: SourcePosition, message: &str) {
+ let location = input.source_location(position);
+
+ let errors = self.errors.clone();
+ let mut errors = errors.lock().unwrap();
+
+ errors.push(
+ CSSError{
+ line: location.line,
+ column: location.column,
+ message: message.to_owned()
+ }
+ );
+ }
+
+ fn clone(&self) -> Box<ParseErrorReporter + Send + Sync> {
+ return Box::new(
+ CSSInvalidErrorReporterTest{
+ errors: self.errors.clone()
+ }
+ );
+ }
+}
+
+
+#[test]
+fn test_report_error_stylesheet() {
+ let css = r"
+ div {
+ background-color: red;
+ display: invalid;
+ invalid: true;
+ }
+ ";
+ let url = url!("about::test");
+ let error_reporter = Box::new(CSSInvalidErrorReporterTest::new());
+
+ let errors = error_reporter.errors.clone();
+
+ Stylesheet::from_str(css, url, Origin::UserAgent, error_reporter);
+
+ let mut errors = errors.lock().unwrap();
+
+ let error = errors.pop().unwrap();
+ assert_eq!("Unsupported property declaration: 'invalid: true;'", error.message);
+ assert_eq!(5, error.line);
+ assert_eq!(9, error.column);
+
+ let error = errors.pop().unwrap();
+ assert_eq!("Unsupported property declaration: 'display: invalid;'", error.message);
+ assert_eq!(4, error.line);
+ assert_eq!(9, error.column);
+}