From 147ef3e434210da8ad909633937215c1c6db81ba Mon Sep 17 00:00:00 2001 From: rome-xi Date: Mon, 24 Aug 2026 04:19:26 +0000 Subject: [PATCH 1/4] tests: load JSON fixtures at runtime for crates.io archives Signed-off-by: rome-xi --- src/tests.rs | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index fd5127d9..049e12cf 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -21,6 +21,16 @@ use super::{ ToCss, Token, TokenSerializationType, UnicodeRange, }; + +fn css_parsing_test_json(file_name: &str) -> String { + let path = format!( + "{}/src/css-parsing-tests/{}", + env!("CARGO_MANIFEST_DIR"), + file_name + ); + std::fs::read_to_string(&path).unwrap() +} + macro_rules! JArray { ($($e: expr,)*) => { JArray![ $( $e ),* ] }; ($($e: expr),*) => { Value::Array(vec!( $( $e.to_json() ),* )) } @@ -108,7 +118,7 @@ fn run_json_tests Value>(json_data: &str, parse: F) { #[test] fn component_value_list() { run_json_tests( - include_str!("css-parsing-tests/component_value_list.json"), + &css_parsing_test_json("component_value_list.json"), |input| Value::Array(component_values_to_json(input)), ); } @@ -116,7 +126,7 @@ fn component_value_list() { #[test] fn one_component_value() { run_json_tests( - include_str!("css-parsing-tests/one_component_value.json"), + &css_parsing_test_json("one_component_value.json"), |input| { let result: Result> = input.parse_entirely(|input| { Ok(one_component_value_to_json(input.next()?.clone(), input)) @@ -129,7 +139,7 @@ fn one_component_value() { #[test] fn declaration_list() { run_json_tests( - include_str!("css-parsing-tests/declaration_list.json"), + &css_parsing_test_json("declaration_list.json"), |input| { Value::Array( RuleBodyParser::new(input, &mut JsonParser) @@ -143,7 +153,7 @@ fn declaration_list() { #[test] fn one_declaration() { run_json_tests( - include_str!("css-parsing-tests/one_declaration.json"), + &css_parsing_test_json("one_declaration.json"), |input| { parse_one_declaration(input, &mut JsonParser).unwrap_or(JArray!["error", "invalid"]) }, @@ -152,7 +162,7 @@ fn one_declaration() { #[test] fn rule_list() { - run_json_tests(include_str!("css-parsing-tests/rule_list.json"), |input| { + run_json_tests(&css_parsing_test_json("rule_list.json"), |input| { Value::Array( RuleBodyParser::new(input, &mut JsonParser) .map(|result| result.unwrap_or(JArray!["error", "invalid"])) @@ -163,7 +173,7 @@ fn rule_list() { #[test] fn stylesheet() { - run_json_tests(include_str!("css-parsing-tests/stylesheet.json"), |input| { + run_json_tests(&css_parsing_test_json("stylesheet.json"), |input| { Value::Array( StyleSheetParser::new(input, &mut JsonParser) .map(|result| result.unwrap_or(JArray!["error", "invalid"])) @@ -174,7 +184,7 @@ fn stylesheet() { #[test] fn one_rule() { - run_json_tests(include_str!("css-parsing-tests/one_rule.json"), |input| { + run_json_tests(&css_parsing_test_json("one_rule.json"), |input| { parse_one_rule(input, &mut JsonParser).unwrap_or(JArray!["error", "invalid"]) }); } @@ -200,7 +210,7 @@ fn stylesheet_from_bytes() { } run_raw_json_tests( - include_str!("css-parsing-tests/stylesheet_bytes.json"), + &css_parsing_test_json("stylesheet_bytes.json"), |input, expected| { let map = match input { Value::Object(map) => map, @@ -354,7 +364,7 @@ fn test_expect_url() { #[test] fn nth() { - run_json_tests(include_str!("css-parsing-tests/An+B.json"), |input| { + run_json_tests(&css_parsing_test_json("An+B.json"), |input| { input .parse_entirely(|i| { let result: Result<_, ParseError<()>> = parse_nth(i).map_err(Into::into); @@ -383,7 +393,7 @@ fn parse_comma_separated_ignoring_errors() { #[test] fn unicode_range() { - run_json_tests(include_str!("css-parsing-tests/urange.json"), |input| { + run_json_tests(&css_parsing_test_json("urange.json"), |input| { let result: Result<_, ParseError<()>> = input.parse_comma_separated(|input| { let result = UnicodeRange::parse(input).ok().map(|r| (r.start, r.end)); if input.is_exhausted() { @@ -420,7 +430,7 @@ fn serializer_preserving_comments() { fn serializer(preserve_comments: bool) { run_json_tests( - include_str!("css-parsing-tests/component_value_list.json"), + &css_parsing_test_json("component_value_list.json"), |input| { fn write_to( mut previous_token: TokenSerializationType, From 1f7943e49cdb269d2b586901d1b3e4d615493fc3 Mon Sep 17 00:00:00 2001 From: rome-xi <49893941+rome-xi@users.noreply.github.com> Date: Tue, 25 Aug 2026 01:43:31 +0000 Subject: [PATCH 2/4] style: cargo fmt --- src/tests.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/tests.rs b/src/tests.rs index 049e12cf..b2b6cbe3 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -21,7 +21,6 @@ use super::{ ToCss, Token, TokenSerializationType, UnicodeRange, }; - fn css_parsing_test_json(file_name: &str) -> String { let path = format!( "{}/src/css-parsing-tests/{}", From 07c058cf9dc0566b211c71bd1e379cf70d12d637 Mon Sep 17 00:00:00 2001 From: rome-xi <2685138823@qq.com> Date: Tue, 25 Aug 2026 01:44:23 +0000 Subject: [PATCH 3/4] style: cargo fmt Co-authored-by: rome-xi --- src/tests.rs | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index b2b6cbe3..846f38f4 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -137,26 +137,20 @@ fn one_component_value() { #[test] fn declaration_list() { - run_json_tests( - &css_parsing_test_json("declaration_list.json"), - |input| { - Value::Array( - RuleBodyParser::new(input, &mut JsonParser) - .map(|result| result.unwrap_or(JArray!["error", "invalid"])) - .collect(), - ) - }, - ); + run_json_tests(&css_parsing_test_json("declaration_list.json"), |input| { + Value::Array( + RuleBodyParser::new(input, &mut JsonParser) + .map(|result| result.unwrap_or(JArray!["error", "invalid"])) + .collect(), + ) + }); } #[test] fn one_declaration() { - run_json_tests( - &css_parsing_test_json("one_declaration.json"), - |input| { - parse_one_declaration(input, &mut JsonParser).unwrap_or(JArray!["error", "invalid"]) - }, - ); + run_json_tests(&css_parsing_test_json("one_declaration.json"), |input| { + parse_one_declaration(input, &mut JsonParser).unwrap_or(JArray!["error", "invalid"]) + }); } #[test] From 6ab3796ab68cb04c9d1579209f54c167e486b70f Mon Sep 17 00:00:00 2001 From: rome-xi <49893941+rome-xi@users.noreply.github.com> Date: Fri, 28 Aug 2026 01:39:16 +0000 Subject: [PATCH 4/4] test: skip JSON fixture tests under miri isolation Miri cannot open() css-parsing-tests files (they stay excluded from the crates.io package, so include_str! is not an option). Signed-off-by: rome-xi <2685138823@qq.com> --- src/tests.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/tests.rs b/src/tests.rs index 846f38f4..17612738 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -22,6 +22,9 @@ use super::{ }; fn css_parsing_test_json(file_name: &str) -> String { + // Fixtures are Cargo.toml `exclude`d so crates.io packages can compile tests + // without include_str!. Miri isolation cannot open() them; those tests are + // #[cfg_attr(miri, ignore)]. let path = format!( "{}/src/css-parsing-tests/{}", env!("CARGO_MANIFEST_DIR"), @@ -114,6 +117,7 @@ fn run_json_tests Value>(json_data: &str, parse: F) { }); } +#[cfg_attr(miri, ignore)] #[test] fn component_value_list() { run_json_tests( @@ -122,6 +126,7 @@ fn component_value_list() { ); } +#[cfg_attr(miri, ignore)] #[test] fn one_component_value() { run_json_tests( @@ -135,6 +140,7 @@ fn one_component_value() { ); } +#[cfg_attr(miri, ignore)] #[test] fn declaration_list() { run_json_tests(&css_parsing_test_json("declaration_list.json"), |input| { @@ -146,6 +152,7 @@ fn declaration_list() { }); } +#[cfg_attr(miri, ignore)] #[test] fn one_declaration() { run_json_tests(&css_parsing_test_json("one_declaration.json"), |input| { @@ -153,6 +160,7 @@ fn one_declaration() { }); } +#[cfg_attr(miri, ignore)] #[test] fn rule_list() { run_json_tests(&css_parsing_test_json("rule_list.json"), |input| { @@ -164,6 +172,7 @@ fn rule_list() { }); } +#[cfg_attr(miri, ignore)] #[test] fn stylesheet() { run_json_tests(&css_parsing_test_json("stylesheet.json"), |input| { @@ -175,6 +184,7 @@ fn stylesheet() { }); } +#[cfg_attr(miri, ignore)] #[test] fn one_rule() { run_json_tests(&css_parsing_test_json("one_rule.json"), |input| { @@ -182,6 +192,7 @@ fn one_rule() { }); } +#[cfg_attr(miri, ignore)] #[test] fn stylesheet_from_bytes() { pub struct EncodingRs; @@ -355,6 +366,7 @@ fn test_expect_url() { assert!(parse(&mut input).is_err()); } +#[cfg_attr(miri, ignore)] #[test] fn nth() { run_json_tests(&css_parsing_test_json("An+B.json"), |input| { @@ -384,6 +396,7 @@ fn parse_comma_separated_ignoring_errors() { assert_eq!(result[2], (0, 0, 255)); } +#[cfg_attr(miri, ignore)] #[test] fn unicode_range() { run_json_tests(&css_parsing_test_json("urange.json"), |input| { @@ -411,11 +424,13 @@ fn unicode_range() { }); } +#[cfg_attr(miri, ignore)] #[test] fn serializer_not_preserving_comments() { serializer(false) } +#[cfg_attr(miri, ignore)] #[test] fn serializer_preserving_comments() { serializer(true)