Skip to content
Browse files

Add a truckload of tests for <track-size> and <track-list>

  • Loading branch information
wafflespeanut committed May 18, 2017
1 parent 4899bb4 commit 10badb3efd775fc213915406a8236fc8bc2468b7
Showing with 98 additions and 1 deletion.
  1. +32 −0 tests/unit/style/parsing/
  2. +66 −1 tests/unit/style/parsing/
@@ -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<T, F: Fn(&ParserContext, &mut Parser) -> Result<T, ()>>(f: F, s: &str) -> Result<T, ()> {
let url = ::servo_url::ServoUrl::parse("http://localhost").unwrap();
@@ -24,6 +30,32 @@ fn parse_entirely<T, F: Fn(&ParserContext, &mut Parser) -> Result<T, ()>>(f: F,
parse(|context, parser| parser.parse_entirely(|p| f(context, p)), s)

fn assert_computed_serialization<C, F, T>(f: F, input: &str, output: &str)
where F: Fn(&ParserContext, &mut Parser) -> Result<T, ()>,
T: ToComputedValue<ComputedValue=C>, 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 {
@@ -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 */

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());

fn test_grid_auto_rows_columns() {
use style::properties::longhands::grid_auto_rows;

// the grammar is <track-size>+ 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 <inflexible-breadth> is allowed in first arg of minmax
assert!(parse(grid_auto_rows::parse, "minmax(1fr, max-content)").is_err());

fn test_grid_template_rows_columns() {
use style::properties::longhands::grid_template_rows;

assert_roundtrip_with_context!(grid_template_rows::parse, "none"); // none keyword
// <track-size>{2} with `<track-breadth> minmax(<inflexible-breadth>, <track-breadth>)`
assert_roundtrip_with_context!(grid_template_rows::parse, "1fr minmax(min-content, 1fr)");
// <track-size> with <track-breadth> as <length-percentage>
assert_roundtrip_with_context!(grid_template_rows::parse, "calc(4em + 5px)");
// <track-size> with <length> followed by <track-repeat> with `<track-size>{3}` (<flex>, auto, minmax)
assert_roundtrip_with_context!(grid_template_rows::parse, "10px repeat(2, 1fr auto minmax(200px, 1fr))");
// <track-repeat> with `<track-size> <line-names>` followed by <track-size>
assert_roundtrip_with_context!(grid_template_rows::parse, "repeat(4, 10px [col-start] 250px [col-end]) 10px");
// mixture of <track-size>, <track-repeat> and <line-names>
"[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 <line-names>
assert!(parse(grid_template_rows::parse, "[a span] 10px").is_err());
// <track-list> needs at least one <track-size> | <track-repeat>
assert!(parse(grid_template_rows::parse, "[a b c]").is_err());
// at least one argument of <fixed-size> should be a <fixed-breadth> (i.e., <length-percentage>)
assert!(parse(grid_template_rows::parse, "[a b] repeat(auto-fill, 50px) minmax(auto, 1fr)").is_err());
// fit-content is not a <fixed-size>
assert!(parse(grid_template_rows::parse, "[a b] repeat(auto-fill, fit-content(20%))").is_err());
// <auto-track-list> only allows <fixed-size> | <fixed-repeat>
assert!(parse(grid_template_rows::parse, "[a] repeat(2, auto) repeat(auto-fill, 10px)").is_err());
// only <inflexible-breadth> allowed in <auto-track-repeat>
assert!(parse(grid_template_rows::parse, "[a] repeat(auto-fill, 1fr)").is_err());
// <auto-track-repeat> is allowed only once
assert!(parse(grid_template_rows::parse, "[a] repeat(auto-fit, [b] 8px) [c] repeat(auto-fill, [c] 8px)").is_err());

fn test_computed_grid_template_rows_colums() {
use style::properties::longhands::grid_template_rows;

"[a] repeat(calc(1 + 1), [b] auto)", "[a b] auto [b] auto");

"[a] repeat(2, [b c] auto [e] auto [d])",
"[a b c] auto [e] auto [d b c] auto [e] auto [d]");

"[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]");

"10px repeat(2, 1fr auto minmax(200px, 1fr))",
"10px minmax(auto, 1fr) auto minmax(200px, 1fr) minmax(auto, 1fr) auto minmax(200px, 1fr)");

0 comments on commit 10badb3

Please sign in to comment.
You can’t perform that action at this time.