use parsec::{Document, Options, SyntaxError, parse_fragment_with, parse_with};
mod inputs;
mod summary;
fn parse(data: &str, case: &inputs::TreeInput, scripting: bool) -> Document {
let options = Options {
errors: true,
scripting,
..Options::default()
};
match &case.fragment {
Some((ns, context)) => parse_fragment_with(data, *ns, context, options),
None => parse_with(data, options),
}
.unwrap()
}
fn new_error(line: &str) -> Option<(usize, usize, &str)> {
let (at, code) = line.strip_prefix('(')?.split_once(") ")?;
let (l, c) = at.split_once(':')?;
Some((l.parse().ok()?, c.parse().ok()?, code.trim()))
}
#[test]
fn tree_construction_errors() {
let (mut counted, mut miscounted) = (0, Vec::new());
let (mut found, mut missing, mut skipped) = (0, Vec::new(), 0);
for case in inputs::tree_inputs() {
if case.file.starts_with("scripted_") && case.scripting == Some(true) {
skipped += 1;
continue;
}
let doc = parse(&case.data, &case, case.scripting.unwrap_or(false));
let errors = doc.errors();
let codes = || {
errors
.iter()
.map(|e| format!("({}:{}) {}", e.line, e.column, e.code))
.collect::<Vec<_>>()
};
if errors.len() == case.errors.len() {
counted += 1;
} else {
miscounted.push(format!(
"{}: {:?}\n expected {}: {:?}\n found {}: {:?}",
case.file,
case.data,
case.errors.len(),
case.errors,
errors.len(),
codes()
));
}
for line in &case.new_errors {
let Some((l, c, code)) = new_error(line) else {
continue;
};
let here = |e: &&SyntaxError| e.line == l && e.column == c && e.code == code;
if errors.iter().any(|e| here(&e)) {
found += 1;
} else {
missing.push(format!(
"{}: {:?}: {line} not in {:?}",
case.file,
case.data,
codes()
));
}
}
}
eprintln!(
"tree construction errors: {counted} counts match, {} don't; \
#new-errors: {found} found, {} missing",
miscounted.len(),
missing.len()
);
let data = std::path::Path::new(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/tree-construction"
));
summary::write(&summary::Counts {
suite: "html5lib-tree-construction-error-counts",
passed: counted,
failed: miscounted.len(),
skipped,
skipped_because: "they run script during the parse, which needs a JavaScript engine",
data,
});
summary::write(&summary::Counts {
suite: "html5lib-tree-construction-new-errors",
passed: found,
failed: missing.len(),
skipped,
skipped_because: "they run script during the parse, which needs a JavaScript engine",
data,
});
if std::env::var("ERRORS_BRIEF").is_ok() {
for m in miscounted.iter().chain(&missing) {
eprintln!("{m}");
}
}
assert!(
counted >= 1802,
"only {counted} error counts match; set ERRORS_BRIEF to see them"
);
assert!(
missing.is_empty(),
"{} #new-errors missing, first 10:\n{}",
missing.len(),
missing[..missing.len().min(10)].join("\n")
);
}
#[test]
fn errors_have_their_names_and_places() {
let input = "<!DOCTYPE html>\r\n<p a=1 a=2>\0</q>";
let doc = parse_with(
input,
Options {
errors: true,
..Options::default()
},
)
.unwrap();
let got: Vec<_> = doc
.errors()
.iter()
.map(|e| (e.code, e.line, e.column, &input[e.offset..e.offset + 1]))
.collect();
assert_eq!(
got,
[
("duplicate-attribute", 2, 9, "="),
("unexpected-null-character", 2, 12, "\0"),
("invalid-codepoint-in-body", 2, 12, "\0"),
("unexpected-end-tag", 2, 13, "<"),
]
);
assert!(parsec::parse("<p a=1 a=2>", false).errors().is_empty());
}