From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- servo/tests/unit/style/parsing/mod.rs | 126 ++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 servo/tests/unit/style/parsing/mod.rs (limited to 'servo/tests/unit/style/parsing/mod.rs') diff --git a/servo/tests/unit/style/parsing/mod.rs b/servo/tests/unit/style/parsing/mod.rs new file mode 100644 index 0000000000..dc931633e7 --- /dev/null +++ b/servo/tests/unit/style/parsing/mod.rs @@ -0,0 +1,126 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +//! Tests for parsing and serialization of values/properties + +use cssparser::{Parser, ParserInput}; +use style::context::QuirksMode; +use style::parser::ParserContext; +use style::stylesheets::{CssRuleType, Origin}; +use style_traits::{ParsingMode, ParseError}; + +fn parse(f: F, s: &'static str) -> Result> +where F: for<'t> Fn(&ParserContext, &mut Parser<'static, 't>) -> Result> { + 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> +where F: Fn(&ParserContext, &mut Parser<'i, 't>) -> Result> { + let url = ::servo_url::ServoUrl::parse("http://localhost").unwrap(); + let context = ParserContext::new( + Origin::Author, + &url, + Some(CssRuleType::Style), + ParsingMode::DEFAULT, + QuirksMode::NoQuirks, + None, + None, + ); + let mut parser = Parser::new(input); + f(&context, &mut parser) +} + +fn parse_entirely(f: F, s: &'static str) -> Result> +where F: for<'t> Fn(&ParserContext, &mut Parser<'static, 't>) -> Result> { + 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> +where F: Fn(&ParserContext, &mut Parser<'i, 't>) -> Result> { + parse_input(|context, parser| parser.parse_entirely(|p| f(context, p)), input) +} + +// This is a macro so that the file/line information +// is preserved in the panic +macro_rules! assert_roundtrip_with_context { + ($fun:expr, $string:expr) => { + assert_roundtrip_with_context!($fun, $string, $string); + }; + ($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 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(); + unwrapped + }} +} + +macro_rules! assert_roundtrip { + ($fun:expr, $string:expr) => { + assert_roundtrip!($fun, $string, $string); + }; + ($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 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_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() + }} +} + +macro_rules! parse_longhand { + ($name:ident, $s:expr) => { + parse($name::parse, $s).unwrap() + }; +} + +mod animation; +mod background; +mod border; +mod box_; +mod column; +mod effects; +mod image; +mod inherited_text; +mod outline; +mod position; +mod selectors; +mod supports; +mod text_overflow; +mod transition_duration; +mod transition_timing_function; -- cgit v1.2.3