Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 54 additions & 25 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,41 @@ use super::{
ToCss, Token, TokenSerializationType, UnicodeRange,
};

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, so keep
// include_str! on the miri path (git checkout only).
css_parsing_test_json_impl(file_name)
}

#[cfg(miri)]
fn css_parsing_test_json_impl(file_name: &str) -> String {
let s = match file_name {
"component_value_list.json" => include_str!("css-parsing-tests/component_value_list.json"),
"one_component_value.json" => include_str!("css-parsing-tests/one_component_value.json"),
"declaration_list.json" => include_str!("css-parsing-tests/declaration_list.json"),
"one_declaration.json" => include_str!("css-parsing-tests/one_declaration.json"),
"rule_list.json" => include_str!("css-parsing-tests/rule_list.json"),
"stylesheet.json" => include_str!("css-parsing-tests/stylesheet.json"),
"one_rule.json" => include_str!("css-parsing-tests/one_rule.json"),
"stylesheet_bytes.json" => include_str!("css-parsing-tests/stylesheet_bytes.json"),
"An+B.json" => include_str!("css-parsing-tests/An+B.json"),
"urange.json" => include_str!("css-parsing-tests/urange.json"),
other => panic!("unknown css-parsing-tests fixture: {other}"),
};
s.to_owned()
}

#[cfg(not(miri))]
fn css_parsing_test_json_impl(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() ),* )) }
Expand Down Expand Up @@ -108,15 +143,15 @@ fn run_json_tests<F: Fn(&mut Parser) -> 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)),
);
}

#[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<Value, ParseError<()>> = input.parse_entirely(|input| {
Ok(one_component_value_to_json(input.next()?.clone(), input))
Expand All @@ -128,31 +163,25 @@ fn one_component_value() {

#[test]
fn declaration_list() {
run_json_tests(
include_str!("css-parsing-tests/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(
include_str!("css-parsing-tests/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]
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"]))
Expand All @@ -163,7 +192,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"]))
Expand All @@ -174,7 +203,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"])
});
}
Expand All @@ -200,7 +229,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,
Expand Down Expand Up @@ -354,7 +383,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);
Expand Down Expand Up @@ -383,7 +412,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() {
Expand Down Expand Up @@ -420,7 +449,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,
Expand Down