Format remaining files

This commit is contained in:
Pyfisch 2018-11-06 13:01:35 +01:00
parent bf47f90da6
commit cb07debcb6
252 changed files with 5944 additions and 3744 deletions

View file

@ -14,14 +14,22 @@ use style_traits::ToCss;
fn test_animation_name() {
use self::animation_name::single_value::SpecifiedValue as SingleValue;
let other_name = Atom::from("other-name");
assert_eq!(parse_longhand!(animation_name, "none"),
animation_name::SpecifiedValue(vec![SingleValue(None)]));
assert_eq!(parse_longhand!(animation_name, "other-name, none, 'other-name', \"other-name\""),
animation_name::SpecifiedValue(
vec![SingleValue(Some(KeyframesName::Ident(CustomIdent(other_name.clone())))),
SingleValue(None),
SingleValue(Some(KeyframesName::QuotedString(other_name.clone()))),
SingleValue(Some(KeyframesName::QuotedString(other_name.clone())))]));
assert_eq!(
parse_longhand!(animation_name, "none"),
animation_name::SpecifiedValue(vec![SingleValue(None)])
);
assert_eq!(
parse_longhand!(
animation_name,
"other-name, none, 'other-name', \"other-name\""
),
animation_name::SpecifiedValue(vec![
SingleValue(Some(KeyframesName::Ident(CustomIdent(other_name.clone())))),
SingleValue(None),
SingleValue(Some(KeyframesName::QuotedString(other_name.clone()))),
SingleValue(Some(KeyframesName::QuotedString(other_name.clone())))
])
);
}
#[test]

View file

@ -13,72 +13,179 @@ fn background_shorthand_should_parse_all_available_properties_when_specified() {
let input = "url(\"http://servo/test.png\") top center / 200px 200px repeat-x fixed padding-box content-box red";
let result = parse(background::parse_value, input).unwrap();
assert_eq!(result.background_image, parse_longhand!(background_image, "url(\"http://servo/test.png\")"));
assert_eq!(result.background_position_x, parse_longhand!(background_position_x, "center"));
assert_eq!(result.background_position_y, parse_longhand!(background_position_y, "top"));
assert_eq!(result.background_size, parse_longhand!(background_size, "200px 200px"));
assert_eq!(result.background_repeat, parse_longhand!(background_repeat, "repeat-x"));
assert_eq!(result.background_attachment, parse_longhand!(background_attachment, "fixed"));
assert_eq!(result.background_origin, parse_longhand!(background_origin, "padding-box"));
assert_eq!(result.background_clip, parse_longhand!(background_clip, "content-box"));
assert_eq!(result.background_color, parse_longhand!(background_color, "red"));
assert_eq!(
result.background_image,
parse_longhand!(background_image, "url(\"http://servo/test.png\")")
);
assert_eq!(
result.background_position_x,
parse_longhand!(background_position_x, "center")
);
assert_eq!(
result.background_position_y,
parse_longhand!(background_position_y, "top")
);
assert_eq!(
result.background_size,
parse_longhand!(background_size, "200px 200px")
);
assert_eq!(
result.background_repeat,
parse_longhand!(background_repeat, "repeat-x")
);
assert_eq!(
result.background_attachment,
parse_longhand!(background_attachment, "fixed")
);
assert_eq!(
result.background_origin,
parse_longhand!(background_origin, "padding-box")
);
assert_eq!(
result.background_clip,
parse_longhand!(background_clip, "content-box")
);
assert_eq!(
result.background_color,
parse_longhand!(background_color, "red")
);
}
#[test]
fn background_shorthand_should_parse_when_some_fields_set() {
let result = parse(background::parse_value, "14px 40px repeat-y").unwrap();
assert_eq!(result.background_position_x, parse_longhand!(background_position_x, "14px"));
assert_eq!(result.background_position_y, parse_longhand!(background_position_y, "40px"));
assert_eq!(result.background_repeat, parse_longhand!(background_repeat, "repeat-y"));
assert_eq!(
result.background_position_x,
parse_longhand!(background_position_x, "14px")
);
assert_eq!(
result.background_position_y,
parse_longhand!(background_position_y, "40px")
);
assert_eq!(
result.background_repeat,
parse_longhand!(background_repeat, "repeat-y")
);
let result = parse(background::parse_value, "url(\"http://servo/test.png\") repeat blue").unwrap();
let result = parse(
background::parse_value,
"url(\"http://servo/test.png\") repeat blue",
)
.unwrap();
assert_eq!(result.background_image, parse_longhand!(background_image, "url(\"http://servo/test.png\")"));
assert_eq!(result.background_repeat, parse_longhand!(background_repeat, "repeat"));
assert_eq!(result.background_color, parse_longhand!(background_color, "blue"));
assert_eq!(
result.background_image,
parse_longhand!(background_image, "url(\"http://servo/test.png\")")
);
assert_eq!(
result.background_repeat,
parse_longhand!(background_repeat, "repeat")
);
assert_eq!(
result.background_color,
parse_longhand!(background_color, "blue")
);
let result = parse(background::parse_value, "padding-box").unwrap();
assert_eq!(result.background_origin, parse_longhand!(background_origin, "padding-box"));
assert_eq!(result.background_clip, parse_longhand!(background_clip, "padding-box"));
assert_eq!(
result.background_origin,
parse_longhand!(background_origin, "padding-box")
);
assert_eq!(
result.background_clip,
parse_longhand!(background_clip, "padding-box")
);
let result = parse(background::parse_value, "url(\"http://servo/test.png\")").unwrap();
assert_eq!(result.background_image, parse_longhand!(background_image, "url(\"http://servo/test.png\")"));
assert_eq!(
result.background_image,
parse_longhand!(background_image, "url(\"http://servo/test.png\")")
);
}
#[test]
fn background_shorthand_should_parse_comma_separated_declarations() {
let input = "url(\"http://servo/test.png\") top left no-repeat, url(\"http://servo/test.png\") \
center / 100% 100% no-repeat, white";
let input =
"url(\"http://servo/test.png\") top left no-repeat, url(\"http://servo/test.png\") \
center / 100% 100% no-repeat, white";
let result = parse(background::parse_value, input).unwrap();
assert_eq!(result.background_image, parse_longhand!(background_image, "url(\"http://servo/test.png\"), \
url(\"http://servo/test.png\"), none"));
assert_eq!(result.background_position_x, parse_longhand!(background_position_x, "left, center, 0%"));
assert_eq!(result.background_position_y, parse_longhand!(background_position_y, "top, center, 0%"));
assert_eq!(result.background_repeat, parse_longhand!(background_repeat, "no-repeat, no-repeat, repeat"));
assert_eq!(result.background_clip, parse_longhand!(background_clip, "border-box, border-box, border-box"));
assert_eq!(result.background_origin, parse_longhand!(background_origin, "padding-box, padding-box, \
padding-box"));
assert_eq!(result.background_size, parse_longhand!(background_size, "auto auto, 100% 100%, auto auto"));
assert_eq!(result.background_attachment, parse_longhand!(background_attachment, "scroll, scroll, scroll"));
assert_eq!(result.background_color, parse_longhand!(background_color, "white"));
assert_eq!(
result.background_image,
parse_longhand!(
background_image,
"url(\"http://servo/test.png\"), \
url(\"http://servo/test.png\"), none"
)
);
assert_eq!(
result.background_position_x,
parse_longhand!(background_position_x, "left, center, 0%")
);
assert_eq!(
result.background_position_y,
parse_longhand!(background_position_y, "top, center, 0%")
);
assert_eq!(
result.background_repeat,
parse_longhand!(background_repeat, "no-repeat, no-repeat, repeat")
);
assert_eq!(
result.background_clip,
parse_longhand!(background_clip, "border-box, border-box, border-box")
);
assert_eq!(
result.background_origin,
parse_longhand!(
background_origin,
"padding-box, padding-box, \
padding-box"
)
);
assert_eq!(
result.background_size,
parse_longhand!(background_size, "auto auto, 100% 100%, auto auto")
);
assert_eq!(
result.background_attachment,
parse_longhand!(background_attachment, "scroll, scroll, scroll")
);
assert_eq!(
result.background_color,
parse_longhand!(background_color, "white")
);
}
#[test]
fn background_shorthand_should_parse_position_and_size_correctly() {
let result = parse(background::parse_value, "7px 4px").unwrap();
assert_eq!(result.background_position_x, parse_longhand!(background_position_x, "7px"));
assert_eq!(result.background_position_y, parse_longhand!(background_position_y, "4px"));
assert_eq!(
result.background_position_x,
parse_longhand!(background_position_x, "7px")
);
assert_eq!(
result.background_position_y,
parse_longhand!(background_position_y, "4px")
);
let result = parse(background::parse_value, "7px 4px / 30px 20px").unwrap();
assert_eq!(result.background_position_x, parse_longhand!(background_position_x, "7px"));
assert_eq!(result.background_position_y, parse_longhand!(background_position_y, "4px"));
assert_eq!(result.background_size, parse_longhand!(background_size, "30px 20px"));
assert_eq!(
result.background_position_x,
parse_longhand!(background_position_x, "7px")
);
assert_eq!(
result.background_position_y,
parse_longhand!(background_position_y, "4px")
);
assert_eq!(
result.background_size,
parse_longhand!(background_size, "30px 20px")
);
assert!(parse(background::parse_value, "/ 30px 20px").is_err());
@ -89,16 +196,34 @@ fn background_shorthand_should_parse_position_and_size_correctly() {
fn background_shorthand_should_parse_origin_and_clip_correctly() {
let result = parse(background::parse_value, "padding-box content-box").unwrap();
assert_eq!(result.background_origin, parse_longhand!(background_origin, "padding-box"));
assert_eq!(result.background_clip, parse_longhand!(background_clip, "content-box"));
assert_eq!(
result.background_origin,
parse_longhand!(background_origin, "padding-box")
);
assert_eq!(
result.background_clip,
parse_longhand!(background_clip, "content-box")
);
let result = parse(background::parse_value, "padding-box padding-box").unwrap();
assert_eq!(result.background_origin, parse_longhand!(background_origin, "padding-box"));
assert_eq!(result.background_clip, parse_longhand!(background_clip, "padding-box"));
assert_eq!(
result.background_origin,
parse_longhand!(background_origin, "padding-box")
);
assert_eq!(
result.background_clip,
parse_longhand!(background_clip, "padding-box")
);
let result = parse(background::parse_value, "padding-box").unwrap();
assert_eq!(result.background_origin, parse_longhand!(background_origin, "padding-box"));
assert_eq!(result.background_clip, parse_longhand!(background_clip, "padding-box"));
assert_eq!(
result.background_origin,
parse_longhand!(background_origin, "padding-box")
);
assert_eq!(
result.background_clip,
parse_longhand!(background_clip, "padding-box")
);
}

View file

@ -13,31 +13,60 @@ use style_traits::ToCss;
macro_rules! assert_longhand {
($parsed_shorthand: expr, $prop: ident, $value_string: expr) => {
assert_eq!($parsed_shorthand.$prop, parse_longhand!($prop, $value_string).maybe_boxed())
}
assert_eq!(
$parsed_shorthand.$prop,
parse_longhand!($prop, $value_string).maybe_boxed()
)
};
}
macro_rules! assert_initial {
($parsed_shorthand: expr, $prop: ident) => {
assert_eq!($parsed_shorthand.$prop, $prop::get_initial_specified_value().maybe_boxed())
}
assert_eq!(
$parsed_shorthand.$prop,
$prop::get_initial_specified_value().maybe_boxed()
)
};
}
macro_rules! assert_border_radius_values {
($input:expr; $tlw:expr, $trw:expr, $brw:expr, $blw:expr ;
$tlh:expr, $trh:expr, $brh:expr, $blh:expr) => {
let input = parse(BorderRadius::parse, $input)
.expect(&format!("Failed parsing {} as border radius",
$input));
assert_eq!(::style_traits::ToCss::to_css_string(&input.top_left.0.width()), $tlw);
assert_eq!(::style_traits::ToCss::to_css_string(&input.top_right.0.width()), $trw);
assert_eq!(::style_traits::ToCss::to_css_string(&input.bottom_right.0.width()), $brw);
assert_eq!(::style_traits::ToCss::to_css_string(&input.bottom_left.0.width()), $blw);
assert_eq!(::style_traits::ToCss::to_css_string(&input.top_left.0.height()), $tlh);
assert_eq!(::style_traits::ToCss::to_css_string(&input.top_right.0.height()), $trh);
assert_eq!(::style_traits::ToCss::to_css_string(&input.bottom_right.0.height()), $brh);
assert_eq!(::style_traits::ToCss::to_css_string(&input.bottom_left.0.height()), $blh);
}
.expect(&format!("Failed parsing {} as border radius", $input));
assert_eq!(
::style_traits::ToCss::to_css_string(&input.top_left.0.width()),
$tlw
);
assert_eq!(
::style_traits::ToCss::to_css_string(&input.top_right.0.width()),
$trw
);
assert_eq!(
::style_traits::ToCss::to_css_string(&input.bottom_right.0.width()),
$brw
);
assert_eq!(
::style_traits::ToCss::to_css_string(&input.bottom_left.0.width()),
$blw
);
assert_eq!(
::style_traits::ToCss::to_css_string(&input.top_left.0.height()),
$tlh
);
assert_eq!(
::style_traits::ToCss::to_css_string(&input.top_right.0.height()),
$trh
);
assert_eq!(
::style_traits::ToCss::to_css_string(&input.bottom_right.0.height()),
$brh
);
assert_eq!(
::style_traits::ToCss::to_css_string(&input.bottom_left.0.height()),
$blh
);
};
}
#[test]

View file

@ -16,18 +16,26 @@ fn test_clip() {
assert_roundtrip_with_context!(clip::parse, "rect(auto, auto, auto, auto)");
// Non-standard syntax
assert_roundtrip_with_context!(clip::parse,
"rect(1px 2px 3px 4px)",
"rect(1px, 2px, 3px, 4px)");
assert_roundtrip_with_context!(clip::parse,
"rect(auto 2px 3px auto)",
"rect(auto, 2px, 3px, auto)");
assert_roundtrip_with_context!(clip::parse,
"rect(1px auto auto 4px)",
"rect(1px, auto, auto, 4px)");
assert_roundtrip_with_context!(clip::parse,
"rect(auto auto auto auto)",
"rect(auto, auto, auto, auto)");
assert_roundtrip_with_context!(
clip::parse,
"rect(1px 2px 3px 4px)",
"rect(1px, 2px, 3px, 4px)"
);
assert_roundtrip_with_context!(
clip::parse,
"rect(auto 2px 3px auto)",
"rect(auto, 2px, 3px, auto)"
);
assert_roundtrip_with_context!(
clip::parse,
"rect(1px auto auto 4px)",
"rect(1px, auto, auto, 4px)"
);
assert_roundtrip_with_context!(
clip::parse,
"rect(auto auto auto auto)",
"rect(auto, auto, auto, auto)"
);
}
#[test]
@ -85,7 +93,7 @@ fn test_parse_factor() {
#[test]
fn blur_radius_should_not_accept_negavite_values() {
use style::properties::longhands::box_shadow;
assert!(parse(box_shadow::parse, "1px 1px -1px").is_err());// for -ve values
assert!(parse(box_shadow::parse, "1px 1px 0").is_ok());// for zero
assert!(parse(box_shadow::parse, "1px 1px 1px").is_ok());// for +ve value
assert!(parse(box_shadow::parse, "1px 1px -1px").is_err()); // for -ve values
assert!(parse(box_shadow::parse, "1px 1px 0").is_ok()); // for zero
assert!(parse(box_shadow::parse, "1px 1px 1px").is_ok()); // for +ve value
}

View file

@ -33,72 +33,122 @@ fn test_linear_gradient() {
#[test]
fn test_radial_gradient() {
// Parsing with all values
assert_roundtrip_with_context!(Image::parse, "radial-gradient(circle closest-side at 20px 30px, red, green)");
assert_roundtrip_with_context!(Image::parse, "radial-gradient(ellipse closest-side at 20px 30px, red, green)",
"radial-gradient(closest-side at 20px 30px, red, green)");
assert_roundtrip_with_context!(Image::parse, "radial-gradient(closest-side circle at 20px 30px, red, green)",
"radial-gradient(circle closest-side at 20px 30px, red, green)");
assert_roundtrip_with_context!(Image::parse, "radial-gradient(closest-side ellipse at 20px 30px, red, green)",
"radial-gradient(closest-side at 20px 30px, red, green)");
assert_roundtrip_with_context!(
Image::parse,
"radial-gradient(circle closest-side at 20px 30px, red, green)"
);
assert_roundtrip_with_context!(
Image::parse,
"radial-gradient(ellipse closest-side at 20px 30px, red, green)",
"radial-gradient(closest-side at 20px 30px, red, green)"
);
assert_roundtrip_with_context!(
Image::parse,
"radial-gradient(closest-side circle at 20px 30px, red, green)",
"radial-gradient(circle closest-side at 20px 30px, red, green)"
);
assert_roundtrip_with_context!(
Image::parse,
"radial-gradient(closest-side ellipse at 20px 30px, red, green)",
"radial-gradient(closest-side at 20px 30px, red, green)"
);
// Parsing with <shape-keyword> and <size> reversed
assert_roundtrip_with_context!(Image::parse, "radial-gradient(closest-side circle at 20px 30px, red, green)",
"radial-gradient(circle closest-side at 20px 30px, red, green)");
assert_roundtrip_with_context!(Image::parse, "radial-gradient(closest-corner ellipse at 20px 30px, red, green)",
"radial-gradient(closest-corner at 20px 30px, red, green)");
assert_roundtrip_with_context!(Image::parse, "radial-gradient(30px circle, red, green)",
"radial-gradient(30px at center center, red, green)");
assert_roundtrip_with_context!(Image::parse, "radial-gradient(30px 40px ellipse, red, green)",
"radial-gradient(30px 40px at center center, red, green)");
assert_roundtrip_with_context!(
Image::parse,
"radial-gradient(closest-side circle at 20px 30px, red, green)",
"radial-gradient(circle closest-side at 20px 30px, red, green)"
);
assert_roundtrip_with_context!(
Image::parse,
"radial-gradient(closest-corner ellipse at 20px 30px, red, green)",
"radial-gradient(closest-corner at 20px 30px, red, green)"
);
assert_roundtrip_with_context!(
Image::parse,
"radial-gradient(30px circle, red, green)",
"radial-gradient(30px at center center, red, green)"
);
assert_roundtrip_with_context!(
Image::parse,
"radial-gradient(30px 40px ellipse, red, green)",
"radial-gradient(30px 40px at center center, red, green)"
);
// Parsing without <size>
assert_roundtrip_with_context!(Image::parse,
"radial-gradient(circle, red, green)",
"radial-gradient(circle at center center, red, green)");
assert_roundtrip_with_context!(Image::parse,
"radial-gradient(ellipse, red, green)",
"radial-gradient(at center center, red, green)");
assert_roundtrip_with_context!(Image::parse,
"radial-gradient(circle at 20px 30px, red, green)");
assert_roundtrip_with_context!(Image::parse,
"radial-gradient(ellipse at 20px 30px, red, green)",
"radial-gradient(at 20px 30px, red, green)");
assert_roundtrip_with_context!(
Image::parse,
"radial-gradient(circle, red, green)",
"radial-gradient(circle at center center, red, green)"
);
assert_roundtrip_with_context!(
Image::parse,
"radial-gradient(ellipse, red, green)",
"radial-gradient(at center center, red, green)"
);
assert_roundtrip_with_context!(
Image::parse,
"radial-gradient(circle at 20px 30px, red, green)"
);
assert_roundtrip_with_context!(
Image::parse,
"radial-gradient(ellipse at 20px 30px, red, green)",
"radial-gradient(at 20px 30px, red, green)"
);
// Parsing without <shape-keyword>
assert_roundtrip_with_context!(Image::parse,
"radial-gradient(20px at 20px 30px, red, green)");
assert_roundtrip_with_context!(Image::parse,
"radial-gradient(20px 30px at left center, red, green)");
assert_roundtrip_with_context!(Image::parse,
"radial-gradient(closest-side at center, red, green)",
"radial-gradient(closest-side at center center, red, green)");
assert_roundtrip_with_context!(Image::parse,
"radial-gradient(20px, red, green)",
"radial-gradient(20px at center center, red, green)");
assert_roundtrip_with_context!(Image::parse,
"radial-gradient(20px 30px, red, green)",
"radial-gradient(20px 30px at center center, red, green)");
assert_roundtrip_with_context!(Image::parse,
"radial-gradient(closest-side, red, green)",
"radial-gradient(closest-side at center center, red, green)");
assert_roundtrip_with_context!(
Image::parse,
"radial-gradient(20px at 20px 30px, red, green)"
);
assert_roundtrip_with_context!(
Image::parse,
"radial-gradient(20px 30px at left center, red, green)"
);
assert_roundtrip_with_context!(
Image::parse,
"radial-gradient(closest-side at center, red, green)",
"radial-gradient(closest-side at center center, red, green)"
);
assert_roundtrip_with_context!(
Image::parse,
"radial-gradient(20px, red, green)",
"radial-gradient(20px at center center, red, green)"
);
assert_roundtrip_with_context!(
Image::parse,
"radial-gradient(20px 30px, red, green)",
"radial-gradient(20px 30px at center center, red, green)"
);
assert_roundtrip_with_context!(
Image::parse,
"radial-gradient(closest-side, red, green)",
"radial-gradient(closest-side at center center, red, green)"
);
// Parsing without <shape-keyword> and <size>
assert_roundtrip_with_context!(Image::parse,
"radial-gradient(at center, red, green)",
"radial-gradient(at center center, red, green)");
assert_roundtrip_with_context!(Image::parse,
"radial-gradient(at center bottom, red, green)");
assert_roundtrip_with_context!(Image::parse,
"radial-gradient(at 40px 50px, red, green)");
assert_roundtrip_with_context!(
Image::parse,
"radial-gradient(at center, red, green)",
"radial-gradient(at center center, red, green)"
);
assert_roundtrip_with_context!(
Image::parse,
"radial-gradient(at center bottom, red, green)"
);
assert_roundtrip_with_context!(Image::parse, "radial-gradient(at 40px 50px, red, green)");
// Parsing with just color stops
assert_roundtrip_with_context!(Image::parse,
"radial-gradient(red, green)",
"radial-gradient(at center center, red, green)");
assert_roundtrip_with_context!(
Image::parse,
"radial-gradient(red, green)",
"radial-gradient(at center center, red, green)"
);
// Parsing repeating radial gradient
assert_roundtrip_with_context!(Image::parse,
"repeating-radial-gradient(red, green)",
"repeating-radial-gradient(at center center, red, green)");
assert_roundtrip_with_context!(
Image::parse,
"repeating-radial-gradient(red, green)",
"repeating-radial-gradient(at center center, red, green)"
);
}

View file

@ -11,7 +11,9 @@ fn negative_letter_spacing_should_parse_properly() {
use style::values::specified::length::{Length, NoCalcLength, FontRelativeLength};
let negative_value = parse_longhand!(letter_spacing, "-0.5em");
let expected = Spacing::Value(Length::NoCalc(NoCalcLength::FontRelative(FontRelativeLength::Em(-0.5))));
let expected = Spacing::Value(Length::NoCalc(NoCalcLength::FontRelative(
FontRelativeLength::Em(-0.5),
)));
assert_eq!(negative_value, expected);
}
@ -21,9 +23,9 @@ fn negative_word_spacing_should_parse_properly() {
use style::values::specified::length::{NoCalcLength, LengthOrPercentage, FontRelativeLength};
let negative_value = parse_longhand!(word_spacing, "-0.5em");
let expected = Spacing::Value(LengthOrPercentage::Length(
NoCalcLength::FontRelative(FontRelativeLength::Em(-0.5))
));
let expected = Spacing::Value(LengthOrPercentage::Length(NoCalcLength::FontRelative(
FontRelativeLength::Em(-0.5),
)));
assert_eq!(negative_value, expected);
}

View file

@ -11,13 +11,17 @@ use style::stylesheets::{CssRuleType, Origin};
use style_traits::{ParsingMode, ParseError};
fn parse<T, F>(f: F, s: &'static str) -> Result<T, ParseError<'static>>
where F: for<'t> Fn(&ParserContext, &mut Parser<'static, 't>) -> Result<T, ParseError<'static>> {
where
F: for<'t> Fn(&ParserContext, &mut Parser<'static, 't>) -> Result<T, ParseError<'static>>,
{
let mut input = ParserInput::new(s);
parse_input(f, &mut input)
}
fn parse_input<'i: 't, 't, T, F>(f: F, input: &'t mut ParserInput<'i>) -> Result<T, ParseError<'i>>
where F: Fn(&ParserContext, &mut Parser<'i, 't>) -> Result<T, ParseError<'i>> {
where
F: Fn(&ParserContext, &mut Parser<'i, 't>) -> Result<T, ParseError<'i>>,
{
let url = ::servo_url::ServoUrl::parse("http://localhost").unwrap();
let context = ParserContext::new(
Origin::Author,
@ -33,14 +37,24 @@ where F: Fn(&ParserContext, &mut Parser<'i, 't>) -> Result<T, ParseError<'i>> {
}
fn parse_entirely<T, F>(f: F, s: &'static str) -> Result<T, ParseError<'static>>
where F: for<'t> Fn(&ParserContext, &mut Parser<'static, 't>) -> Result<T, ParseError<'static>> {
where
F: for<'t> Fn(&ParserContext, &mut Parser<'static, 't>) -> Result<T, ParseError<'static>>,
{
let mut input = ParserInput::new(s);
parse_entirely_input(f, &mut input)
}
fn parse_entirely_input<'i: 't, 't, T, F>(f: F, input: &'t mut ParserInput<'i>) -> Result<T, ParseError<'i>>
where F: Fn(&ParserContext, &mut Parser<'i, 't>) -> Result<T, ParseError<'i>> {
parse_input(|context, parser| parser.parse_entirely(|p| f(context, p)), input)
fn parse_entirely_input<'i: 't, 't, T, F>(
f: F,
input: &'t mut ParserInput<'i>,
) -> Result<T, ParseError<'i>>
where
F: Fn(&ParserContext, &mut Parser<'i, 't>) -> Result<T, ParseError<'i>>,
{
parse_input(
|context, parser| parser.parse_entirely(|p| f(context, p)),
input,
)
}
// This is a macro so that the file/line information
@ -51,24 +65,31 @@ macro_rules! assert_roundtrip_with_context {
};
($fun:expr, $input:expr, $output:expr) => {{
let mut input = ::cssparser::ParserInput::new($input);
let serialized = super::parse_input(|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)
}, &mut input).unwrap();
let serialized = super::parse_input(
|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)
},
&mut input,
)
.unwrap();
let mut input = ::cssparser::ParserInput::new(&serialized);
let unwrapped = super::parse_input(|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(())
}, &mut input).unwrap();
let unwrapped = super::parse_input(
|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(())
},
&mut input,
)
.unwrap();
unwrapped
}}
}};
}
macro_rules! assert_roundtrip {
@ -78,34 +99,37 @@ macro_rules! assert_roundtrip {
($fun:expr, $input:expr, $output:expr) => {
let mut input = ParserInput::new($input);
let mut parser = Parser::new(&mut input);
let parsed = $fun(&mut parser)
.expect(&format!("Failed to parse {}", $input));
let parsed = $fun(&mut parser).expect(&format!("Failed to parse {}", $input));
let serialized = ToCss::to_css_string(&parsed);
assert_eq!(serialized, $output);
let mut input = ParserInput::new(&serialized);
let mut parser = Parser::new(&mut input);
let re_parsed = $fun(&mut parser)
.expect(&format!("Failed to parse serialization {}", $input));
let re_parsed =
$fun(&mut parser).expect(&format!("Failed to parse serialization {}", $input));
let re_serialized = ToCss::to_css_string(&re_parsed);
assert_eq!(serialized, re_serialized)
}
};
}
macro_rules! assert_parser_exhausted {
($fun:expr, $string:expr, $should_exhausted:expr) => {{
parse(|context, input| {
let parsed = $fun(context, input);
assert_eq!(parsed.is_ok(), true);
assert_eq!(input.is_exhausted(), $should_exhausted);
Ok(())
}, $string).unwrap()
}}
parse(
|context, input| {
let parsed = $fun(context, input);
assert_eq!(parsed.is_ok(), true);
assert_eq!(input.is_exhausted(), $should_exhausted);
Ok(())
},
$string,
)
.unwrap()
}};
}
macro_rules! parse_longhand {
($name:ident, $s:expr) => {
parse($name::parse, $s).unwrap()
parse($name::parse, $s).unwrap()
};
}

View file

@ -143,4 +143,3 @@ fn test_vertical_position() {
assert!(parse(VerticalPosition::parse, "y-start").is_err());
assert!(parse(VerticalPosition::parse, "y-end").is_err());
}

View file

@ -8,7 +8,9 @@ use style::selector_parser::{SelectorImpl, SelectorParser};
use style::stylesheets::{Origin, Namespaces};
use style_traits::ParseError;
fn parse_selector<'i, 't>(input: &mut Parser<'i, 't>) -> Result<SelectorList<SelectorImpl>, ParseError<'i>> {
fn parse_selector<'i, 't>(
input: &mut Parser<'i, 't>,
) -> Result<SelectorList<SelectorImpl>, ParseError<'i>> {
let mut ns = Namespaces::default();
ns.prefixes.insert("svg".into(), ns!(svg));
let parser = SelectorParser {
@ -24,7 +26,10 @@ fn test_selectors() {
assert_roundtrip!(parse_selector, "div");
assert_roundtrip!(parse_selector, "svg|circle");
assert_roundtrip!(parse_selector, "p:before", "p::before");
assert_roundtrip!(parse_selector, "[border=\"0\"]:-servo-nonzero-border ~ ::-servo-details-summary");
assert_roundtrip!(
parse_selector,
"[border=\"0\"]:-servo-nonzero-border ~ ::-servo-details-summary"
);
assert_roundtrip!(parse_selector, "* > *");
assert_roundtrip!(parse_selector, "*|* + *", "* + *");
}

View file

@ -10,7 +10,10 @@ use style_traits::ToCss;
fn test_supports_condition() {
assert_roundtrip!(SupportsCondition::parse, "(margin: 1px)");
assert_roundtrip!(SupportsCondition::parse, "not (--be: to be)");
assert_roundtrip!(SupportsCondition::parse, "(color: blue) and future-extension(4)");
assert_roundtrip!(
SupportsCondition::parse,
"(color: blue) and future-extension(4)"
);
assert_roundtrip!(SupportsCondition::parse, "future-\\1 extension(4)");
assert_roundtrip!(SupportsCondition::parse, "((test))");
}

View file

@ -8,14 +8,41 @@ use style_traits::ToCss;
#[test]
fn test_cubic_bezier() {
assert_roundtrip_with_context!(transition_timing_function::parse, "cubic-bezier(0, 0, 0, 0)");
assert_roundtrip_with_context!(transition_timing_function::parse, "cubic-bezier(0.25, 0, 0.5, 0)");
assert_roundtrip_with_context!(transition_timing_function::parse, "cubic-bezier(1, 1, 1, 1)");
assert_roundtrip_with_context!(
transition_timing_function::parse,
"cubic-bezier(0, 0, 0, 0)"
);
assert_roundtrip_with_context!(
transition_timing_function::parse,
"cubic-bezier(0.25, 0, 0.5, 0)"
);
assert_roundtrip_with_context!(
transition_timing_function::parse,
"cubic-bezier(1, 1, 1, 1)"
);
// p1x and p2x values must be in range [0, 1]
assert!(parse(transition_timing_function::parse, "cubic-bezier(-1, 0, 0, 0").is_err());
assert!(parse(transition_timing_function::parse, "cubic-bezier(0, 0, -1, 0").is_err());
assert!(parse(transition_timing_function::parse, "cubic-bezier(-1, 0, -1, 0").is_err());
assert!(
parse(
transition_timing_function::parse,
"cubic-bezier(-1, 0, 0, 0"
)
.is_err()
);
assert!(
parse(
transition_timing_function::parse,
"cubic-bezier(0, 0, -1, 0"
)
.is_err()
);
assert!(
parse(
transition_timing_function::parse,
"cubic-bezier(-1, 0, -1, 0"
)
.is_err()
);
assert!(parse(transition_timing_function::parse, "cubic-bezier(2, 0, 0, 0").is_err());
assert!(parse(transition_timing_function::parse, "cubic-bezier(0, 0, 2, 0").is_err());
@ -27,7 +54,11 @@ fn test_steps() {
assert_roundtrip_with_context!(transition_timing_function::parse, "steps(1)");
assert_roundtrip_with_context!(transition_timing_function::parse, "steps( 1)", "steps(1)");
assert_roundtrip_with_context!(transition_timing_function::parse, "steps(1, start)");
assert_roundtrip_with_context!(transition_timing_function::parse, "steps(2, end) ", "steps(2)");
assert_roundtrip_with_context!(
transition_timing_function::parse,
"steps(2, end) ",
"steps(2)"
);
// Step interval value must be an integer greater than 0
assert!(parse(transition_timing_function::parse, "steps(0)").is_err());