diff options
author | Martin Fischer <martin@push-f.com> | 2021-11-30 18:10:53 +0100 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2021-12-05 02:52:36 +0100 |
commit | c29558e8d165df1d3fc1cb32079da9e40f957dc6 (patch) | |
tree | 57708a17596e021aadef0ccd3e5d9de1e7008aa8 /tests/test_spans.rs | |
parent | cb8c8b260dc95317b16efddd921e7de2cae57245 (diff) |
spans: add span tests
Diffstat (limited to 'tests/test_spans.rs')
-rw-r--r-- | tests/test_spans.rs | 63 |
1 files changed, 63 insertions, 0 deletions
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") +} |