aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/span-tests/demo.html1
-rw-r--r--tests/span-tests/demo.out7
-rw-r--r--tests/test_spans.rs63
3 files changed, 71 insertions, 0 deletions
diff --git a/tests/span-tests/demo.html b/tests/span-tests/demo.html
new file mode 100644
index 0000000..900f74c
--- /dev/null
+++ b/tests/span-tests/demo.html
@@ -0,0 +1 @@
+this is a tag: <h1>test</h1>
diff --git a/tests/span-tests/demo.out b/tests/span-tests/demo.out
new file mode 100644
index 0000000..98d6766
--- /dev/null
+++ b/tests/span-tests/demo.out
@@ -0,0 +1,7 @@
+note:
+ ┌─ test.html:1:17
+ │
+1 │ this is a tag: <h1>test</h1>
+ │ ^^ ^^ end tag
+ │ │
+ │ start tag
diff --git a/tests/test_spans.rs b/tests/test_spans.rs
new file mode 100644
index 0000000..a3d1c96
--- /dev/null
+++ b/tests/test_spans.rs
@@ -0,0 +1,63 @@
+use std::include_str;
+
+use codespan_reporting::{
+ self,
+ diagnostic::{Diagnostic, Label},
+ files::SimpleFiles,
+ term::{self, termcolor::Buffer},
+};
+use html5gum::{
+ spans::{PosTracker, SpanEmitter},
+ Readable, Token, Tokenizer,
+};
+
+#[test]
+fn test() {
+ let html = include_str!("span-tests/demo.html");
+
+ let mut files = SimpleFiles::new();
+ let file_id = files.add("test.html", html);
+ let mut labels = Vec::new();
+
+ for token in Tokenizer::new_with_emitter(
+ PosTracker {
+ reader: html.to_reader(),
+ position: 0,
+ },
+ SpanEmitter::default(),
+ )
+ .infallible()
+ {
+ if let Token::StartTag(tag) = token {
+ labels.push(Label::primary(file_id, tag.name_span).with_message("start tag"));
+ } else if let Token::EndTag(tag) = token {
+ labels.push(Label::primary(file_id, tag.name_span).with_message("end tag"));
+ }
+ }
+
+ let diagnostic = Diagnostic::note().with_labels(labels);
+
+ let mut writer = Buffer::no_color();
+ let config = codespan_reporting::term::Config::default();
+ term::emit(&mut writer, &config, &files, &diagnostic).unwrap();
+
+ let actual = remove_trailing_spaces(std::str::from_utf8(writer.as_slice()).unwrap());
+ let expected = include_str!("span-tests/demo.out");
+
+ if actual != expected {
+ println!(
+ "EXPECTED:\n{banner}\n{expected}{banner}\n\nACTUAL OUTPUT:\n{banner}\n{actual}{banner}",
+ banner = "-".repeat(30),
+ expected = expected,
+ actual = actual
+ );
+ panic!("failed");
+ }
+}
+
+fn remove_trailing_spaces(text: &str) -> String {
+ text.lines()
+ .map(|l| l.trim_end())
+ .collect::<Vec<_>>()
+ .join("\n")
+}