diff options
author | Martin Fischer <martin@push-f.com> | 2023-09-02 09:13:07 +0200 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2023-09-03 23:00:05 +0200 |
commit | cfd065ac32678486376a1c3cb47e2262708d1aa7 (patch) | |
tree | 5f26be0402d0e0fcd376b8e95e3dce68f3a06a56 /tests/test_spans.rs | |
parent | 153acbee5650b9b006c7de215fb1421a64516fb4 (diff) |
refactor: let comment and doctype tests check multiple cases
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 │ |