mod.rs (4637B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ 4 5 //! Tests for parsing and serialization of values/properties 6 7 use cssparser::{Parser, ParserInput}; 8 use style::context::QuirksMode; 9 use style::parser::ParserContext; 10 use style::stylesheets::{CssRuleType, Origin}; 11 use style_traits::{ParseError, ParsingMode}; 12 13 fn parse<T, F>(f: F, s: &'static str) -> Result<T, ParseError<'static>> 14 where 15 F: for<'t> Fn(&ParserContext, &mut Parser<'static, 't>) -> Result<T, ParseError<'static>>, 16 { 17 let mut input = ParserInput::new(s); 18 parse_input(f, &mut input) 19 } 20 21 fn parse_input<'i: 't, 't, T, F>(f: F, input: &'t mut ParserInput<'i>) -> Result<T, ParseError<'i>> 22 where 23 F: Fn(&ParserContext, &mut Parser<'i, 't>) -> Result<T, ParseError<'i>>, 24 { 25 let url = ::servo_url::ServoUrl::parse("http://localhost").unwrap(); 26 let context = ParserContext::new( 27 Origin::Author, 28 &url, 29 Some(CssRuleType::Style), 30 ParsingMode::DEFAULT, 31 QuirksMode::NoQuirks, 32 None, 33 None, 34 ); 35 let mut parser = Parser::new(input); 36 f(&context, &mut parser) 37 } 38 39 fn parse_entirely<T, F>(f: F, s: &'static str) -> Result<T, ParseError<'static>> 40 where 41 F: for<'t> Fn(&ParserContext, &mut Parser<'static, 't>) -> Result<T, ParseError<'static>>, 42 { 43 let mut input = ParserInput::new(s); 44 parse_entirely_input(f, &mut input) 45 } 46 47 fn parse_entirely_input<'i: 't, 't, T, F>( 48 f: F, 49 input: &'t mut ParserInput<'i>, 50 ) -> Result<T, ParseError<'i>> 51 where 52 F: Fn(&ParserContext, &mut Parser<'i, 't>) -> Result<T, ParseError<'i>>, 53 { 54 parse_input( 55 |context, parser| parser.parse_entirely(|p| f(context, p)), 56 input, 57 ) 58 } 59 60 // This is a macro so that the file/line information 61 // is preserved in the panic 62 macro_rules! assert_roundtrip_with_context { 63 ($fun:expr, $string:expr) => { 64 assert_roundtrip_with_context!($fun, $string, $string); 65 }; 66 ($fun:expr, $input:expr, $output:expr) => {{ 67 let mut input = ::cssparser::ParserInput::new($input); 68 let serialized = super::parse_input( 69 |context, i| { 70 let parsed = $fun(context, i).expect(&format!("Failed to parse {}", $input)); 71 let serialized = ToCss::to_css_string(&parsed); 72 assert_eq!(serialized, $output); 73 Ok(serialized) 74 }, 75 &mut input, 76 ) 77 .unwrap(); 78 79 let mut input = ::cssparser::ParserInput::new(&serialized); 80 let unwrapped = super::parse_input( 81 |context, i| { 82 let re_parsed = 83 $fun(context, i).expect(&format!("Failed to parse serialization {}", $input)); 84 let re_serialized = ToCss::to_css_string(&re_parsed); 85 assert_eq!(serialized, re_serialized); 86 Ok(()) 87 }, 88 &mut input, 89 ) 90 .unwrap(); 91 unwrapped 92 }}; 93 } 94 95 macro_rules! assert_roundtrip { 96 ($fun:expr, $string:expr) => { 97 assert_roundtrip!($fun, $string, $string); 98 }; 99 ($fun:expr, $input:expr, $output:expr) => { 100 let mut input = ParserInput::new($input); 101 let mut parser = Parser::new(&mut input); 102 let parsed = $fun(&mut parser).expect(&format!("Failed to parse {}", $input)); 103 let serialized = ToCss::to_css_string(&parsed); 104 assert_eq!(serialized, $output); 105 106 let mut input = ParserInput::new(&serialized); 107 let mut parser = Parser::new(&mut input); 108 let re_parsed = 109 $fun(&mut parser).expect(&format!("Failed to parse serialization {}", $input)); 110 let re_serialized = ToCss::to_css_string(&re_parsed); 111 assert_eq!(serialized, re_serialized) 112 }; 113 } 114 115 macro_rules! assert_parser_exhausted { 116 ($fun:expr, $string:expr, $should_exhausted:expr) => {{ 117 parse( 118 |context, input| { 119 let parsed = $fun(context, input); 120 assert_eq!(parsed.is_ok(), true); 121 assert_eq!(input.is_exhausted(), $should_exhausted); 122 Ok(()) 123 }, 124 $string, 125 ) 126 .unwrap() 127 }}; 128 } 129 130 macro_rules! parse_longhand { 131 ($name:ident, $s:expr) => { 132 parse($name::parse, $s).unwrap() 133 }; 134 } 135 136 mod animation; 137 mod background; 138 mod border; 139 mod box_; 140 mod column; 141 mod effects; 142 mod image; 143 mod inherited_text; 144 mod outline; 145 mod position; 146 mod selectors; 147 mod supports; 148 mod text_overflow; 149 mod transition_duration; 150 mod transition_timing_function;