From 10badb3efd775fc213915406a8236fc8bc2468b7 Mon Sep 17 00:00:00 2001 From: Ravi Shankar Date: Thu, 23 Mar 2017 00:12:11 +0530 Subject: [PATCH] Add a truckload of tests for and --- tests/unit/style/parsing/mod.rs | 32 +++++++++++++ tests/unit/style/parsing/position.rs | 67 +++++++++++++++++++++++++++- 2 files changed, 98 insertions(+), 1 deletion(-) diff --git a/tests/unit/style/parsing/mod.rs b/tests/unit/style/parsing/mod.rs index e15d2dca21b..5f48483affa 100644 --- a/tests/unit/style/parsing/mod.rs +++ b/tests/unit/style/parsing/mod.rs @@ -5,10 +5,16 @@ //! Tests for parsing and serialization of values/properties use cssparser::Parser; +use euclid::size::TypedSize2D; use media_queries::CSSErrorReporterTest; use style::context::QuirksMode; +use style::font_metrics::ServoMetricsProvider; +use style::media_queries::{Device, MediaType}; use style::parser::{PARSING_MODE_DEFAULT, ParserContext}; +use style::properties::{ComputedValues, StyleBuilder}; use style::stylesheets::{CssRuleType, Origin}; +use style::values::computed::{Context, ToComputedValue}; +use style_traits::ToCss; fn parse Result>(f: F, s: &str) -> Result { let url = ::servo_url::ServoUrl::parse("http://localhost").unwrap(); @@ -24,6 +30,32 @@ fn parse_entirely Result>(f: F, parse(|context, parser| parser.parse_entirely(|p| f(context, p)), s) } +fn assert_computed_serialization(f: F, input: &str, output: &str) + where F: Fn(&ParserContext, &mut Parser) -> Result, + T: ToComputedValue, C: ToCss +{ + let viewport_size = TypedSize2D::new(0., 0.); + let initial_style = ComputedValues::initial_values(); + let device = Device::new(MediaType::Screen, viewport_size); + + let context = Context { + is_root_element: true, + device: &device, + inherited_style: initial_style, + layout_parent_style: initial_style, + style: StyleBuilder::for_derived_style(&initial_style), + cached_system_font: None, + font_metrics_provider: &ServoMetricsProvider, + in_media_query: false, + quirks_mode: QuirksMode::NoQuirks, + }; + + let parsed = parse(f, input).unwrap(); + let computed = parsed.to_computed_value(&context); + let serialized = ToCss::to_css_string(&computed); + assert_eq!(serialized, output); +} + // This is a macro so that the file/line information // is preserved in the panic macro_rules! assert_roundtrip_with_context { diff --git a/tests/unit/style/parsing/position.rs b/tests/unit/style/parsing/position.rs index 7a3bfe1bae3..c1527c2f233 100644 --- a/tests/unit/style/parsing/position.rs +++ b/tests/unit/style/parsing/position.rs @@ -2,7 +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 parsing::{parse, parse_entirely}; +use parsing::{assert_computed_serialization, parse, parse_entirely}; use style::parser::Parse; use style::values::specified::position::*; use style_traits::ToCss; @@ -168,3 +168,68 @@ fn test_grid_auto_flow() { assert!(parse(grid_auto_flow::parse, "column 'dense'").is_err()); assert!(parse(grid_auto_flow::parse, "column 2px dense").is_err()); } + +#[test] +fn test_grid_auto_rows_columns() { + use style::properties::longhands::grid_auto_rows; + + // the grammar is + but gecko supports only a single value, so we've clamped ourselves + assert_roundtrip_with_context!(grid_auto_rows::parse, "55%"); + assert_roundtrip_with_context!(grid_auto_rows::parse, "0.5fr"); + assert_roundtrip_with_context!(grid_auto_rows::parse, "fit-content(11%)"); + // only is allowed in first arg of minmax + assert!(parse(grid_auto_rows::parse, "minmax(1fr, max-content)").is_err()); +} + +#[test] +fn test_grid_template_rows_columns() { + use style::properties::longhands::grid_template_rows; + + assert_roundtrip_with_context!(grid_template_rows::parse, "none"); // none keyword + // {2} with ` minmax(, )` + assert_roundtrip_with_context!(grid_template_rows::parse, "1fr minmax(min-content, 1fr)"); + // with as + assert_roundtrip_with_context!(grid_template_rows::parse, "calc(4em + 5px)"); + // with followed by with `{3}` (, auto, minmax) + assert_roundtrip_with_context!(grid_template_rows::parse, "10px repeat(2, 1fr auto minmax(200px, 1fr))"); + // with ` ` followed by + assert_roundtrip_with_context!(grid_template_rows::parse, "repeat(4, 10px [col-start] 250px [col-end]) 10px"); + // mixture of , and + assert_roundtrip_with_context!(grid_template_rows::parse, + "[a] auto [b] minmax(min-content, 1fr) [b c d] repeat(2, [e] 40px) repeat(5, [f g] auto [h]) [i]"); + + // no span allowed in + assert!(parse(grid_template_rows::parse, "[a span] 10px").is_err()); + // needs at least one | + assert!(parse(grid_template_rows::parse, "[a b c]").is_err()); + // at least one argument of should be a (i.e., ) + assert!(parse(grid_template_rows::parse, "[a b] repeat(auto-fill, 50px) minmax(auto, 1fr)").is_err()); + // fit-content is not a + assert!(parse(grid_template_rows::parse, "[a b] repeat(auto-fill, fit-content(20%))").is_err()); + // only allows | + assert!(parse(grid_template_rows::parse, "[a] repeat(2, auto) repeat(auto-fill, 10px)").is_err()); + // only allowed in + assert!(parse(grid_template_rows::parse, "[a] repeat(auto-fill, 1fr)").is_err()); + // is allowed only once + assert!(parse(grid_template_rows::parse, "[a] repeat(auto-fit, [b] 8px) [c] repeat(auto-fill, [c] 8px)").is_err()); +} + +#[test] +fn test_computed_grid_template_rows_colums() { + use style::properties::longhands::grid_template_rows; + + assert_computed_serialization(grid_template_rows::parse, + "[a] repeat(calc(1 + 1), [b] auto)", "[a b] auto [b] auto"); + + assert_computed_serialization(grid_template_rows::parse, + "[a] repeat(2, [b c] auto [e] auto [d])", + "[a b c] auto [e] auto [d b c] auto [e] auto [d]"); + + assert_computed_serialization(grid_template_rows::parse, + "[a] 50px [b] 10% [b c d] repeat(2, [e] 40px [f]) [g] repeat(auto-fill, [h i] 20px [j]) [k] 10px [l]", + "[a] 50px [b] 10% [b c d e] 40px [f e] 40px [f g] repeat(auto-fill, [h i] 20px [j]) [k] 10px [l]"); + + assert_computed_serialization(grid_template_rows::parse, + "10px repeat(2, 1fr auto minmax(200px, 1fr))", + "10px minmax(auto, 1fr) auto minmax(200px, 1fr) minmax(auto, 1fr) auto minmax(200px, 1fr)"); +}