diff options
Diffstat (limited to 'tests/test_spans.rs')
-rw-r--r-- | tests/test_spans.rs | 108 |
1 files changed, 68 insertions, 40 deletions
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<usize>, impl AsRef<str>)>) -> 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::<Vec<_>>() - .join("\n") + .join("") } #[test] @@ -148,32 +148,31 @@ fn attribute_value_with_char_ref() { } #[test] -fn comment_proper_data_span() { - let html = "<!-- Why are you looking at the source code? -->"; - 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 = [ + "<!-- Why are you looking at the source code? -->", + + // bogus comments + "<! Why are you looking at the source code? -->", + ]; + + 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###" <!-- Why are you looking at the source code? --> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - "###); -} - -#[test] -fn comment_bogus_data_span() { - let html = "<! Why are you looking at the source code? -->"; - 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###" <! Why are you looking at the source code? --> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "###); @@ -181,12 +180,24 @@ fn comment_bogus_data_span() { #[test] fn doctype_span() { - let html = r#"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd" >"#; - 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#"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd" >"#, + ]; + + 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###" <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd" > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "###); @@ -194,15 +205,32 @@ fn doctype_span() { #[test] fn doctype_id_spans() { - let html = r#"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">"#; - 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#"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">"#, ]; - 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###" <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> ^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ system id │ |