Skip to content

Commit

Permalink
Centralize ParserContext for tests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jryans committed Apr 14, 2017
1 parent f7896fd commit 0936dd2
Show file tree
Hide file tree
Showing 27 changed files with 248 additions and 513 deletions.
1 change: 0 additions & 1 deletion tests/unit/style/lib.rs
Expand Up @@ -32,7 +32,6 @@ mod size_of;
mod str;
mod stylesheets;
mod stylist;
mod value;
mod viewport;

mod writing_modes {
Expand Down
5 changes: 1 addition & 4 deletions tests/unit/style/parsing/animation.rs
Expand Up @@ -2,14 +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 parsing::parse;
use servo_atoms::Atom;
use style::parser::{Parse, ParserContext};
use style::parser::Parse;
use style::properties::longhands::animation_iteration_count::single_value::computed_value::T as AnimationIterationCount;
use style::properties::longhands::animation_name;
use style::stylesheets::{CssRuleType, Origin};
use style_traits::ToCss;

#[test]
Expand Down
65 changes: 17 additions & 48 deletions tests/unit/style/parsing/background.rs
Expand Up @@ -2,24 +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::Parser;
use media_queries::CSSErrorReporterTest;
use servo_url::ServoUrl;
use style::parser::ParserContext;
use parsing::parse;
use style::properties::longhands::{background_attachment, background_clip, background_color, background_image};
use style::properties::longhands::{background_origin, background_position_x, background_position_y, background_repeat};
use style::properties::longhands::background_size;
use style::properties::shorthands::background;
use style::stylesheets::{CssRuleType, Origin};

#[test]
fn background_shorthand_should_parse_all_available_properties_when_specified() {
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("url(\"http://servo/test.png\") top center / 200px 200px repeat-x fixed padding-box \
content-box red");
let result = background::parse_value(&context, &mut parser).unwrap();
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"));
Expand All @@ -34,43 +26,33 @@ fn background_shorthand_should_parse_all_available_properties_when_specified() {

#[test]
fn background_shorthand_should_parse_when_some_fields_set() {
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("14px 40px repeat-y");
let result = background::parse_value(&context, &mut parser).unwrap();
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"));

let mut parser = Parser::new("url(\"http://servo/test.png\") repeat blue");
let result = background::parse_value(&context, &mut parser).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"));

let mut parser = Parser::new("padding-box");
let result = background::parse_value(&context, &mut parser).unwrap();
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"));

let mut parser = Parser::new("url(\"http://servo/test.png\")");
let result = background::parse_value(&context, &mut parser).unwrap();
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\")"));
}

#[test]
fn background_shorthand_should_parse_comma_separated_declarations() {
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("url(\"http://servo/test.png\") top left no-repeat, url(\"http://servo/test.png\") \
center / 100% 100% no-repeat, white");
let result = background::parse_value(&context, &mut parser).unwrap();
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"));
Expand All @@ -87,48 +69,35 @@ fn background_shorthand_should_parse_comma_separated_declarations() {

#[test]
fn background_shorthand_should_parse_position_and_size_correctly() {
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("7px 4px");
let result = background::parse_value(&context, &mut parser).unwrap();
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"));

let mut parser = Parser::new("7px 4px / 30px 20px");
let result = background::parse_value(&context, &mut parser).unwrap();
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"));

let mut parser = Parser::new("/ 30px 20px");
assert!(background::parse_value(&context, &mut parser).is_err());
assert!(parse(background::parse_value, "/ 30px 20px").is_err());

let mut parser = Parser::new("repeat-x / 30px 20px");
assert!(background::parse_value(&context, &mut parser).is_err());
assert!(parse(background::parse_value, "repeat-x / 30px 20px").is_err());
}

#[test]
fn background_shorthand_should_parse_origin_and_clip_correctly() {
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("padding-box content-box");
let result = background::parse_value(&context, &mut parser).unwrap();
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"));

let mut parser = Parser::new("padding-box padding-box");
let result = background::parse_value(&context, &mut parser).unwrap();
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"));

let mut parser = Parser::new("padding-box");
let result = background::parse_value(&context, &mut parser).unwrap();
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"));
Expand Down
5 changes: 1 addition & 4 deletions tests/unit/style/parsing/basic_shape.rs
Expand Up @@ -2,11 +2,8 @@
* 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 parsing::parse;
use style::parser::{Parse, ParserContext};
use style::stylesheets::{CssRuleType, Origin};
use style::parser::Parse;
use style::values::specified::basic_shape::*;
use style_traits::ToCss;

Expand Down
72 changes: 18 additions & 54 deletions tests/unit/style/parsing/border.rs
Expand Up @@ -2,24 +2,17 @@
* 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 style::parser::{ParserContext, Parse};
use parsing::parse;
use style::parser::Parse;
use style::properties::longhands::{border_image_outset, border_image_repeat, border_image_slice};
use style::properties::longhands::{border_image_source, border_image_width};
use style::properties::shorthands::border_image;
use style::stylesheets::{CssRuleType, Origin};
use style_traits::ToCss;

#[test]
fn border_image_shorthand_should_parse_when_all_properties_specified() {
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("linear-gradient(red, blue) 30 30% 45 fill / 20px 40px / 10px \
round stretch");
let result = border_image::parse_value(&context, &mut parser).unwrap();
let input = "linear-gradient(red, blue) 30 30% 45 fill / 20px 40px / 10px round stretch";
let result = parse(border_image::parse_value, input).unwrap();

assert_eq!(result.border_image_source,
parse_longhand!(border_image_source, "linear-gradient(red, blue)"));
Expand All @@ -31,11 +24,8 @@ fn border_image_shorthand_should_parse_when_all_properties_specified() {

#[test]
fn border_image_shorthand_should_parse_without_width() {
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("linear-gradient(red, blue) 30 30% 45 fill / / 10px round stretch");
let result = border_image::parse_value(&context, &mut parser).unwrap();
let input = "linear-gradient(red, blue) 30 30% 45 fill / / 10px round stretch";
let result = parse(border_image::parse_value, input).unwrap();

assert_eq!(result.border_image_source,
parse_longhand!(border_image_source, "linear-gradient(red, blue)"));
Expand All @@ -47,11 +37,8 @@ fn border_image_shorthand_should_parse_without_width() {

#[test]
fn border_image_shorthand_should_parse_without_outset() {
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("linear-gradient(red, blue) 30 30% 45 fill / 20px 40px round");
let result = border_image::parse_value(&context, &mut parser).unwrap();
let input = "linear-gradient(red, blue) 30 30% 45 fill / 20px 40px round";
let result = parse(border_image::parse_value, input).unwrap();

assert_eq!(result.border_image_source,
parse_longhand!(border_image_source, "linear-gradient(red, blue)"));
Expand All @@ -63,11 +50,8 @@ fn border_image_shorthand_should_parse_without_outset() {

#[test]
fn border_image_shorthand_should_parse_without_width_or_outset() {
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("linear-gradient(red, blue) 30 30% 45 fill round");
let result = border_image::parse_value(&context, &mut parser).unwrap();
let input = "linear-gradient(red, blue) 30 30% 45 fill round";
let result = parse(border_image::parse_value, input).unwrap();

assert_eq!(result.border_image_source,
parse_longhand!(border_image_source, "linear-gradient(red, blue)"));
Expand All @@ -79,11 +63,7 @@ fn border_image_shorthand_should_parse_without_width_or_outset() {

#[test]
fn border_image_shorthand_should_parse_with_just_source() {
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("linear-gradient(red, blue)");
let result = border_image::parse_value(&context, &mut parser).unwrap();
let result = parse(border_image::parse_value, "linear-gradient(red, blue)").unwrap();

assert_eq!(result.border_image_source,
parse_longhand!(border_image_source, "linear-gradient(red, blue)"));
Expand All @@ -95,41 +75,25 @@ fn border_image_shorthand_should_parse_with_just_source() {

#[test]
fn border_image_outset_should_error_on_negative_length() {
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("-1em");
let result = border_image_outset::parse(&context, &mut parser);
let result = parse(border_image_outset::parse, "-1em");
assert_eq!(result, Err(()));
}

#[test]
fn border_image_outset_should_error_on_negative_number() {
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("-15");
let result = border_image_outset::parse(&context, &mut parser);
let result = parse(border_image_outset::parse, "-15");
assert_eq!(result, Err(()));
}

#[test]
fn border_image_outset_should_return_number_on_plain_zero() {
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("0");
let result = border_image_outset::parse(&context, &mut parser);
let result = parse(border_image_outset::parse, "0");
assert_eq!(result.unwrap(), parse_longhand!(border_image_outset, "0"));
}

#[test]
fn border_image_outset_should_return_length_on_length_zero() {
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("0em");
let result = border_image_outset::parse(&context, &mut parser);
let result = parse(border_image_outset::parse, "0em");
assert_eq!(result.unwrap(), parse_longhand!(border_image_outset, "0em"));
}

Expand All @@ -153,7 +117,7 @@ fn test_border_style() {
fn test_border_spacing() {
use style::properties::longhands::border_spacing;

assert_parser_exhausted!(border_spacing, "1px rubbish", false);
assert_parser_exhausted!(border_spacing, "1px", true);
assert_parser_exhausted!(border_spacing, "1px 2px", true);
assert_parser_exhausted!(border_spacing::parse, "1px rubbish", false);
assert_parser_exhausted!(border_spacing::parse, "1px", true);
assert_parser_exhausted!(border_spacing::parse, "1px 2px", true);
}
4 changes: 0 additions & 4 deletions tests/unit/style/parsing/box_.rs
Expand Up @@ -2,11 +2,7 @@
* 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 parsing::parse;
use style::parser::ParserContext;
use style::stylesheets::{CssRuleType, Origin};
use style_traits::ToCss;

#[test]
Expand Down
20 changes: 3 additions & 17 deletions tests/unit/style/parsing/column.rs
Expand Up @@ -2,11 +2,7 @@
* 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 style::parser::ParserContext;
use style::stylesheets::{CssRuleType, Origin};
use parsing::parse;
use style_traits::ToCss;

#[test]
Expand All @@ -18,12 +14,7 @@ fn test_column_width() {
assert_roundtrip_with_context!(column_width::parse, "2.5em");
assert_roundtrip_with_context!(column_width::parse, "0.3vw");

let url = ServoUrl::parse("http://localhost").unwrap();
let reporter = CSSErrorReporterTest;
let context = ParserContext::new(Origin::Author, &url, &reporter, Some(CssRuleType::Style));

let mut negative = Parser::new("-6px");
assert!(column_width::parse(&context, &mut negative).is_err());
assert!(parse(column_width::parse, "-6px").is_err());
}

#[test]
Expand All @@ -35,10 +26,5 @@ fn test_column_gap() {
assert_roundtrip_with_context!(column_gap::parse, "2.5em");
assert_roundtrip_with_context!(column_gap::parse, "0.3vw");

let url = ServoUrl::parse("http://localhost").unwrap();
let reporter = CSSErrorReporterTest;
let context = ParserContext::new(Origin::Author, &url, &reporter, Some(CssRuleType::Style));

let mut negative = Parser::new("-6px");
assert!(column_gap::parse(&context, &mut negative).is_err());
assert!(parse(column_gap::parse, "-6px").is_err());
}
7 changes: 2 additions & 5 deletions tests/unit/style/parsing/containment.rs
Expand Up @@ -2,10 +2,7 @@
* 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};
use parsing::parse;

#[test]
fn contain_longhand_should_parse_correctly() {
Expand All @@ -22,5 +19,5 @@ fn contain_longhand_should_parse_correctly() {
assert_eq!(style_paint, contain::STYLE | contain::PAINT);

// Assert that the `2px` is not consumed, which would trigger parsing failure in real use
assert_parser_exhausted!(contain, "layout 2px", false);
assert_parser_exhausted!(contain::parse, "layout 2px", false);
}

0 comments on commit 0936dd2

Please sign in to comment.