swc/css/parser/tests/fixture.rs

78 lines
2.2 KiB
Rust
Raw Normal View History

2021-08-19 08:16:32 +03:00
use std::path::PathBuf;
use swc_common::input::SourceFileInput;
use swc_css_parser::{
lexer::Lexer,
parser::{Parser, ParserConfig},
};
use testing::NormalizedOutput;
#[testing::fixture("tests/fixture/**/input.css")]
fn pass(input: PathBuf) {
eprintln!("Input: {}", input.display());
testing::run_test2(false, |cm, handler| {
let ref_json_path = input.parent().unwrap().join("output.json");
let fm = cm.load_file(&input).unwrap();
let lexer = Lexer::new(SourceFileInput::from(&*fm));
let mut parser = Parser::new(lexer, ParserConfig { parse_values: true });
let stylesheet = parser.parse_all();
match stylesheet {
Ok(stylesheet) => {
let actual_json = serde_json::to_string_pretty(&stylesheet)
.map(NormalizedOutput::from)
.expect("failed to serialize stylesheet");
actual_json.compare_to_file(&ref_json_path).unwrap();
Ok(())
}
Err(err) => {
let mut d = err.to_diagnostics(&handler);
d.note(&format!("current token = {}", parser.dump_cur()));
d.emit();
Err(())
}
}
})
.unwrap();
}
#[testing::fixture("tests/errors/**/input.css")]
fn fail(input: PathBuf) {
eprintln!("Input: {}", input.display());
let stderr_path = input.parent().unwrap().join("output.stderr");
let stderr = testing::run_test2(false, |cm, handler| -> Result<(), _> {
let fm = cm.load_file(&input).unwrap();
let lexer = Lexer::new(SourceFileInput::from(&*fm));
let mut parser = Parser::new(lexer, ParserConfig { parse_values: true });
let stylesheet = parser.parse_all();
match stylesheet {
Ok(..) => {}
Err(err) => {
err.to_diagnostics(&handler).emit();
}
}
for err in parser.take_errors() {
err.to_diagnostics(&handler).emit();
}
if !handler.has_errors() {
panic!("should error")
}
Err(())
})
.unwrap_err();
stderr.compare_to_file(&stderr_path).unwrap();
}