From cfd065ac32678486376a1c3cb47e2262708d1aa7 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Sat, 2 Sep 2023 09:13:07 +0200 Subject: refactor: let comment and doctype tests check multiple cases --- tests/test_spans.rs | 108 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 68 insertions(+), 40 deletions(-) (limited to 'tests') diff --git a/tests/test_spans.rs b/tests/test_spans.rs index 7eb7b8a..3a195ad 100644 --- a/tests/test_spans.rs +++ b/tests/test_spans.rs @@ -34,9 +34,9 @@ fn annotate(html: &str, labels: Vec<(Range, impl AsRef)>) -> String // (apparently they cannot be disabled in codespan_reporting) msg.lines() .skip(3) - .flat_map(|l| l.split_once("│ ").map(|s| s.1.trim_end())) + .flat_map(|l| l.split_once("│ ").map(|s| format!("{}\n", s.1.trim_end()))) .collect::>() - .join("\n") + .join("") } #[test] @@ -148,32 +148,31 @@ fn attribute_value_with_char_ref() { } #[test] -fn comment_proper_data_span() { - let html = ""; - let Token::Comment(comment) = tokenizer(html).next().unwrap() else { - panic!("expected comment"); - }; - assert_eq!(comment.data, html[comment.data_span()]); - let labels = vec![(comment.data_span(), "")]; - assert_snapshot!(annotate(html, labels), @r###" +fn comment_data_span() { + #[rustfmt::skip] + let cases = [ + "", + + // bogus comments + "", + ]; + + let mut annotated = String::new(); + for case in cases { + let Token::Comment(comment) = tokenizer(case) + .filter(|t| !matches!(t, Token::Error { .. })) + .next() + .unwrap() + else { + panic!("expected comment"); + }; + assert_eq!(case[comment.data_span()], comment.data); + annotated.push_str(&annotate(case, vec![(comment.data_span(), "")])); + } + + assert_snapshot!(annotated, @r###" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - "###); -} - -#[test] -fn comment_bogus_data_span() { - let html = ""; - let Token::Comment(comment) = tokenizer(html) - .filter(|t| !matches!(t, Token::Error { .. })) - .next() - .unwrap() - else { - panic!("expected comment"); - }; - assert_eq!(comment.data, html[comment.data_span()]); - let labels = vec![(comment.data_span(), "")]; - assert_snapshot!(annotate(html, labels), @r###" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "###); @@ -181,12 +180,24 @@ fn comment_bogus_data_span() { #[test] fn doctype_span() { - let html = r#""#; - let Token::Doctype(doctype) = tokenizer(html).next().unwrap() else { - panic!("expected doctype"); - }; - let labels = vec![(doctype.span, "")]; - assert_snapshot!(annotate(html, labels), @r###" + #[rustfmt::skip] + let cases = [ + r#""#, + ]; + + let mut annotated = String::new(); + for case in cases { + let Token::Doctype(doctype) = tokenizer(case) + .filter(|t| !matches!(t, Token::Error { .. })) + .next() + .unwrap() + else { + panic!("expected doctype"); + }; + annotated.push_str(&annotate(case, vec![(doctype.span, "")])); + } + + assert_snapshot!(annotated, @r###" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "###); @@ -194,15 +205,32 @@ fn doctype_span() { #[test] fn doctype_id_spans() { - let html = r#""#; - let Token::Doctype(doctype) = tokenizer(html).next().unwrap() else { - panic!("expected doctype"); - }; - let labels = vec![ - (doctype.public_id_span().unwrap(), "public id"), - (doctype.system_id_span().unwrap(), "system id"), + #[rustfmt::skip] + let cases = [ + r#""#, ]; - assert_snapshot!(annotate(html, labels), @r###" + + let mut annotated = String::new(); + for case in cases { + let Token::Doctype(doctype) = tokenizer(case) + .filter(|t| !matches!(t, Token::Error { .. })) + .next() + .unwrap() + else { + panic!("expected doctype"); + }; + + let mut labels = Vec::new(); + if let Some(public_id_span) = doctype.public_id_span() { + labels.push((public_id_span, "public id")); + } + if let Some(system_id_span) = doctype.system_id_span() { + labels.push((system_id_span, "system id")); + } + annotated.push_str(&annotate(case, labels)); + } + + assert_snapshot!(annotated, @r###" ^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ system id │ -- cgit v1.2.3