Centralize ParserContext for tests

To simplify adding additional data to `ParserContext`, this moves test usages to
a few shared locations, instead of being spread across many tests.

MozReview-Commit-ID: 1OahV797eq
This commit is contained in:
J. Ryan Stinnett 2017-04-13 14:53:59 +08:00
parent f7896fd80b
commit 0936dd24d0
27 changed files with 248 additions and 513 deletions

View file

@ -2,19 +2,10 @@
* 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::Parser;
use media_queries::CSSErrorReporterTest;
use style::parser::ParserContext;
use properties::parse;
use style::properties::longhands::background_size;
use style::stylesheets::{CssRuleType, Origin};
#[test]
fn background_size_should_reject_negative_values() {
let url = ::servo_url::ServoUrl::parse("http://localhost").unwrap();
let reporter = CSSErrorReporterTest;
let context = ParserContext::new(Origin::Author, &url, &reporter, Some(CssRuleType::Style));
let parse_result = background_size::parse(&context, &mut Parser::new("-40% -40%"));
assert_eq!(parse_result.is_err(), true);
assert!(parse(background_size::parse, "-40% -40%").is_err());
}

View file

@ -2,6 +2,42 @@
* 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::Parser;
use media_queries::CSSErrorReporterTest;
use style::parser::ParserContext;
use style::stylesheets::{CssRuleType, Origin};
fn parse<T, F: Fn(&ParserContext, &mut Parser) -> Result<T, ()>>(f: F, s: &str) -> Result<T, ()> {
let url = ::servo_url::ServoUrl::parse("http://localhost").unwrap();
let reporter = CSSErrorReporterTest;
let context = ParserContext::new(Origin::Author, &url, &reporter, Some(CssRuleType::Style));
let mut parser = Parser::new(s);
f(&context, &mut parser)
}
macro_rules! assert_roundtrip_with_context {
($fun:expr, $string:expr) => {
assert_roundtrip_with_context!($fun, $string, $string);
};
($fun:expr, $input:expr, $output:expr) => {{
let serialized = parse(|context, i| {
let parsed = $fun(context, i)
.expect(&format!("Failed to parse {}", $input));
let serialized = ToCss::to_css_string(&parsed);
assert_eq!(serialized, $output);
Ok(serialized)
}, $input).unwrap();
parse(|context, i| {
let re_parsed = $fun(context, i)
.expect(&format!("Failed to parse serialization {}", $input));
let re_serialized = ToCss::to_css_string(&re_parsed);
assert_eq!(serialized, re_serialized);
Ok(())
}, &serialized).unwrap()
}}
}
mod background;
mod scaffolding;
mod serialization;

View file

@ -2,15 +2,11 @@
* 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::Parser;
use media_queries::CSSErrorReporterTest;
use servo_url::ServoUrl;
use properties::parse;
use style::computed_values::display::T::inline_block;
use style::parser::ParserContext;
use style::properties::{PropertyDeclaration, PropertyDeclarationBlock, Importance, PropertyId};
use style::properties::{PropertyDeclaration, Importance, PropertyId};
use style::properties::longhands::outline_color::computed_value::T as ComputedColor;
use style::properties::parse_property_declaration_list;
use style::stylesheets::{CssRuleType, Origin};
use style::values::{RGBA, Auto};
use style::values::specified::{BorderStyle, BorderWidth, CSSColor, Length, NoCalcLength};
use style::values::specified::{LengthOrPercentage, LengthOrPercentageOrAuto, LengthOrPercentageOrAutoOrContent};
@ -18,14 +14,6 @@ use style::values::specified::url::SpecifiedUrl;
use style_traits::ToCss;
use stylesheets::block_from;
fn parse_declaration_block(css_properties: &str) -> PropertyDeclarationBlock {
let url = ServoUrl::parse("http://localhost").unwrap();
let reporter = CSSErrorReporterTest;
let context = ParserContext::new(Origin::Author, &url, &reporter, Some(CssRuleType::Style));
let mut parser = Parser::new(css_properties);
parse_property_declaration_list(&context, &mut parser)
}
#[test]
fn property_declaration_block_should_serialize_correctly() {
use style::properties::longhands::overflow_x::SpecifiedValue as OverflowXValue;
@ -626,7 +614,7 @@ mod shorthand_serialization {
font-language-override: normal; \
font-kerning: none";
let block = parse_declaration_block(block_text);
let block = parse(|c, i| Ok(parse_property_declaration_list(c, i)), block_text).unwrap();
let mut s = String::new();
let id = PropertyId::parse("font".into()).unwrap();
@ -651,7 +639,7 @@ mod shorthand_serialization {
font-variant-position: normal; \
font-language-override: normal;";
let block = parse_declaration_block(block_text);
let block = parse(|c, i| Ok(parse_property_declaration_list(c, i)), block_text).unwrap();
let serialization = block.to_css_string();
@ -675,7 +663,8 @@ mod shorthand_serialization {
background-position-y: 4px; \
background-origin: border-box; \
background-clip: padding-box;";
let block = parse_declaration_block(block_text);
let block = parse(|c, i| Ok(parse_property_declaration_list(c, i)), block_text).unwrap();
let serialization = block.to_css_string();
@ -698,7 +687,8 @@ mod shorthand_serialization {
background-position-y: 4px; \
background-origin: padding-box; \
background-clip: padding-box;";
let block = parse_declaration_block(block_text);
let block = parse(|c, i| Ok(parse_property_declaration_list(c, i)), block_text).unwrap();
let serialization = block.to_css_string();
@ -721,7 +711,8 @@ mod shorthand_serialization {
background-position-y: 4px, 40px; \
background-origin: border-box, padding-box; \
background-clip: padding-box, padding-box;";
let block = parse_declaration_block(block_text);
let block = parse(|c, i| Ok(parse_property_declaration_list(c, i)), block_text).unwrap();
let serialization = block.to_css_string();
@ -751,7 +742,8 @@ mod shorthand_serialization {
background-position: 7px 4px, 5px 6px; \
background-origin: border-box; \
background-clip: padding-box, padding-box;";
let block = parse_declaration_block(block_text);
let block = parse(|c, i| Ok(parse_property_declaration_list(c, i)), block_text).unwrap();
let serialization = block.to_css_string();
@ -909,7 +901,7 @@ mod shorthand_serialization {
#[test]
fn serialize_mask_position_with_multiple_values() {
let block_text = "mask-position: 1px 2px, 4px 3px;";
let block = parse_declaration_block(block_text);
let block = parse(|c, i| Ok(parse_property_declaration_list(c, i)), block_text).unwrap();
let serialization = block.to_css_string();
assert_eq!(serialization, block_text);
}
@ -967,22 +959,9 @@ mod shorthand_serialization {
#[test]
fn should_serialize_none_correctly() {
use cssparser::Parser;
use media_queries::CSSErrorReporterTest;
use style::parser::ParserContext;
use style::properties::longhands::transform;
use style::stylesheets::Origin;
let mut s = String::new();
let url = ::servo_url::ServoUrl::parse("http://localhost").unwrap();
let reporter = CSSErrorReporterTest;
let context = ParserContext::new(Origin::Author, &url, &reporter, Some(CssRuleType::Style));
let parsed = transform::parse(&context, &mut Parser::new("none")).unwrap();
let try_serialize = parsed.to_css(&mut s);
assert_eq!(try_serialize.is_ok(), true);
assert_eq!(s, "none");
assert_roundtrip_with_context!(transform::parse, "none");
}
#[inline(always)]
@ -1031,22 +1010,9 @@ mod shorthand_serialization {
#[test]
fn should_serialize_none_correctly() {
use cssparser::Parser;
use media_queries::CSSErrorReporterTest;
use style::parser::ParserContext;
use style::properties::longhands::quotes;
use style::stylesheets::Origin;
let mut s = String::new();
let url = ::servo_url::ServoUrl::parse("http://localhost").unwrap();
let reporter = CSSErrorReporterTest;
let context = ParserContext::new(Origin::Author, &url, &reporter, Some(CssRuleType::Style));
let parsed = quotes::parse(&context, &mut Parser::new("none")).unwrap();
let try_serialize = parsed.to_css(&mut s);
assert_eq!(try_serialize.is_ok(), true);
assert_eq!(s, "none");
assert_roundtrip_with_context!(quotes::parse, "none");
}
}
@ -1055,7 +1021,7 @@ mod shorthand_serialization {
#[test]
fn serialize_single_animation() {
let block = parse_declaration_block("\
let block_text = "\
animation-name: bounce;\
animation-duration: 1s;\
animation-timing-function: ease-in;\
@ -1063,7 +1029,9 @@ mod shorthand_serialization {
animation-direction: normal;\
animation-fill-mode: forwards;\
animation-iteration-count: infinite;\
animation-play-state: paused;");
animation-play-state: paused;";
let block = parse(|c, i| Ok(parse_property_declaration_list(c, i)), block_text).unwrap();
let serialization = block.to_css_string();
@ -1072,7 +1040,7 @@ mod shorthand_serialization {
#[test]
fn serialize_multiple_animations() {
let block = parse_declaration_block("\
let block_text = "\
animation-name: bounce, roll;\
animation-duration: 1s, 0.2s;\
animation-timing-function: ease-in, linear;\
@ -1080,7 +1048,9 @@ mod shorthand_serialization {
animation-direction: normal, reverse;\
animation-fill-mode: forwards, backwards;\
animation-iteration-count: infinite, 2;\
animation-play-state: paused, running;");
animation-play-state: paused, running;";
let block = parse(|c, i| Ok(parse_property_declaration_list(c, i)), block_text).unwrap();
let serialization = block.to_css_string();
@ -1105,7 +1075,8 @@ mod shorthand_serialization {
animation-fill-mode: forwards, backwards; \
animation-iteration-count: infinite, 2; \
animation-play-state: paused, running;";
let block = parse_declaration_block(block_text);
let block = parse(|c, i| Ok(parse_property_declaration_list(c, i)), block_text).unwrap();
let serialization = block.to_css_string();
@ -1121,7 +1092,8 @@ mod shorthand_serialization {
animation-fill-mode: forwards, backwards; \
animation-iteration-count: infinite, 2; \
animation-play-state: paused, running;";
let block = parse_declaration_block(block_text);
let block = parse(|c, i| Ok(parse_property_declaration_list(c, i)), block_text).unwrap();
let serialization = block.to_css_string();
@ -1138,7 +1110,8 @@ mod shorthand_serialization {
transition-duration: 3s; \
transition-delay: 4s; \
transition-timing-function: cubic-bezier(0.2, 5, 0.5, 2);";
let block = parse_declaration_block(block_text);
let block = parse(|c, i| Ok(parse_property_declaration_list(c, i)), block_text).unwrap();
let serialization = block.to_css_string();
@ -1151,7 +1124,8 @@ mod shorthand_serialization {
transition-duration: 3s, 2s; \
transition-delay: 4s, 5s; \
transition-timing-function: cubic-bezier(0.2, 5, 0.5, 2), ease;";
let block = parse_declaration_block(block_text);
let block = parse(|c, i| Ok(parse_property_declaration_list(c, i)), block_text).unwrap();
let serialization = block.to_css_string();
@ -1172,7 +1146,8 @@ mod shorthand_serialization {
transition-duration: 3s, 2s, 4s; \
transition-delay: 4s, 5s; \
transition-timing-function: cubic-bezier(0.2, 5, 0.5, 2), ease;";
let block = parse_declaration_block(block_text);
let block = parse(|c, i| Ok(parse_property_declaration_list(c, i)), block_text).unwrap();
let serialization = block.to_css_string();
@ -1185,7 +1160,7 @@ mod shorthand_serialization {
#[test]
fn css_wide_keywords_should_be_parsed() {
let block_text = "--a:inherit;";
let block = parse_declaration_block(block_text);
let block = parse(|c, i| Ok(parse_property_declaration_list(c, i)), block_text).unwrap();
let serialization = block.to_css_string();
assert_eq!(serialization, "--a: inherit;");
@ -1194,7 +1169,7 @@ mod shorthand_serialization {
#[test]
fn non_keyword_custom_property_should_be_unparsed() {
let block_text = "--main-color: #06c;";
let block = parse_declaration_block(block_text);
let block = parse(|c, i| Ok(parse_property_declaration_list(c, i)), block_text).unwrap();
let serialization = block.to_css_string();
assert_eq!(serialization, block_text);
@ -1214,7 +1189,7 @@ mod shorthand_serialization {
let shadow_decl = BoxShadow(vec![shadow_val]);
properties.push(PropertyDeclaration:: BoxShadow(shadow_decl));
let shadow_css = "box-shadow: 1px 2px 3px 4px;";
let shadow = parse_declaration_block(shadow_css);
let shadow = parse(|c, i| Ok(parse_property_declaration_list(c, i)), shadow_css).unwrap();
assert_eq!(shadow.to_css_string(), shadow_css);
}