swc/crates/swc_html_parser/tests/fixture.rs

220 lines
5.5 KiB
Rust
Raw Normal View History

2022-04-04 07:12:45 +03:00
#![allow(clippy::needless_update)]
use std::path::PathBuf;
2022-04-28 20:27:39 +03:00
use swc_common::{errors::Handler, input::SourceFileInput, Spanned};
2022-04-04 07:12:45 +03:00
use swc_html_ast::*;
use swc_html_parser::{
lexer::Lexer,
parser::{PResult, Parser, ParserConfig},
};
use swc_html_visit::{Visit, VisitWith};
use testing::NormalizedOutput;
fn test_pass(input: PathBuf, config: ParserConfig) {
testing::run_test2(false, |cm, handler| {
2022-04-28 20:27:39 +03:00
let json_path = input.parent().unwrap().join("output.json");
2022-04-04 07:12:45 +03:00
let fm = cm.load_file(&input).unwrap();
let lexer = Lexer::new(SourceFileInput::from(&*fm), config);
let mut parser = Parser::new(lexer, config);
let document: PResult<Document> = parser.parse_document();
2022-04-28 20:27:39 +03:00
let errors = parser.take_errors();
for err in &errors {
err.to_diagnostics(&handler).emit();
}
if !errors.is_empty() {
return Err(());
}
2022-04-04 07:12:45 +03:00
match document {
Ok(document) => {
let actual_json = serde_json::to_string_pretty(&document)
.map(NormalizedOutput::from)
.expect("failed to serialize document");
2022-04-28 20:27:39 +03:00
actual_json.compare_to_file(&json_path).unwrap();
2022-04-04 07:12:45 +03:00
Ok(())
}
Err(err) => {
let mut d = err.to_diagnostics(&handler);
2022-04-28 20:27:39 +03:00
d.note(&format!("current token = {}", parser.dump_cur()));
2022-04-04 07:12:45 +03:00
d.emit();
Err(())
}
}
})
.unwrap();
}
2022-04-28 20:27:39 +03:00
fn test_recovery(input: PathBuf, config: ParserConfig) {
let stderr_path = input.parent().unwrap().join("output.stderr");
let mut recovered = false;
let stderr = testing::run_test2(false, |cm, handler| {
// Type annotation
if false {
return Ok(());
}
let json_path = input.parent().unwrap().join("output.json");
let fm = cm.load_file(&input).unwrap();
let lexer = Lexer::new(SourceFileInput::from(&*fm), config);
let mut parser = Parser::new(lexer, config);
let document: PResult<Document> = parser.parse_document();
let errors = parser.take_errors();
for err in &errors {
err.to_diagnostics(&handler).emit();
}
if !errors.is_empty() {
recovered = true;
}
match document {
Ok(document) => {
let actual_json = serde_json::to_string_pretty(&document)
.map(NormalizedOutput::from)
.expect("failed to serialize document");
actual_json.compare_to_file(&json_path).unwrap();
Err(())
}
Err(err) => {
let mut d = err.to_diagnostics(&handler);
d.note(&format!("current token = {}", parser.dump_cur()));
d.emit();
Err(())
}
}
})
.unwrap_err();
if !recovered {
panic!(
"Parser should emit errors (recover mode), but parser parsed everything successfully \
{}",
stderr
);
}
stderr.compare_to_file(&stderr_path).unwrap();
2022-04-04 07:12:45 +03:00
}
struct SpanVisualizer<'a> {
handler: &'a Handler,
}
macro_rules! mtd {
($T:ty,$name:ident) => {
fn $name(&mut self, n: &$T) {
self.handler
.struct_span_err(n.span(), stringify!($T))
.emit();
n.visit_children_with(self);
}
};
}
impl Visit for SpanVisualizer<'_> {
mtd!(Document, visit_document);
mtd!(DocumentFragment, visit_document_fragment);
mtd!(Child, visit_child);
mtd!(DocumentType, visit_document_type);
mtd!(Element, visit_element);
mtd!(Attribute, visit_attribute);
mtd!(Text, visit_text);
mtd!(Comment, visit_comment);
2022-04-04 07:12:45 +03:00
fn visit_token_and_span(&mut self, n: &TokenAndSpan) {
self.handler
.struct_span_err(n.span, &format!("{:?}", n.token))
.emit();
}
}
2022-04-28 20:27:39 +03:00
fn test_span_visualizer(input: PathBuf, config: ParserConfig) {
2022-04-04 07:12:45 +03:00
let dir = input.parent().unwrap().to_path_buf();
let output = testing::run_test2(false, |cm, handler| {
// Type annotation
if false {
return Ok(());
}
let fm = cm.load_file(&input).unwrap();
let lexer = Lexer::new(SourceFileInput::from(&*fm), config);
let mut parser = Parser::new(lexer, config);
let document: PResult<Document> = parser.parse_document();
2022-04-04 07:12:45 +03:00
match document {
Ok(document) => {
document.visit_with(&mut SpanVisualizer { handler: &handler });
Err(())
}
Err(err) => {
let mut d = err.to_diagnostics(&handler);
2022-04-28 20:27:39 +03:00
d.note(&format!("current token = {}", parser.dump_cur()));
2022-04-04 07:12:45 +03:00
d.emit();
panic!();
}
}
})
.unwrap_err();
output
.compare_to_file(&dir.join("span.rust-debug"))
.unwrap();
}
2022-04-28 20:27:39 +03:00
#[testing::fixture("tests/fixture/**/input.html")]
fn pass(input: PathBuf) {
test_pass(
input,
ParserConfig {
..Default::default()
},
)
}
#[testing::fixture("tests/recovery/**/input.html")]
fn recovery(input: PathBuf) {
test_recovery(
input,
ParserConfig {
..Default::default()
},
)
}
#[testing::fixture("tests/fixture/**/input.html")]
#[testing::fixture("tests/recovery/**/input.html")]
fn span_visualizer(input: PathBuf) {
test_span_visualizer(
input,
ParserConfig {
..Default::default()
},
)
}