mirror of
https://github.com/swc-project/swc.git
synced 2024-12-24 06:05:02 +03:00
test(html/parser): Refactor tests (#4865)
This commit is contained in:
parent
e93102e0a3
commit
762c72d1c9
@ -447,68 +447,67 @@ fn parser_recovery_verify(input: PathBuf) {
|
||||
#[testing::fixture(
|
||||
"../swc_html_parser/tests/html5lib-tests-fixture/**/*.html",
|
||||
exclude(
|
||||
"adoption01_dat/5.html",
|
||||
"adoption01_dat/6.html",
|
||||
"adoption01_dat/7.html",
|
||||
"adoption01_dat/8.html",
|
||||
"adoption02_dat/0.html",
|
||||
"tests1_dat/30.html",
|
||||
"tests1_dat/68.html",
|
||||
"tests1_dat/69.html",
|
||||
"tests1_dat/70.html",
|
||||
"tests1_dat/71.html",
|
||||
"tests1_dat/77.html",
|
||||
"tests1_dat/90.html",
|
||||
"tests1_dat/103.html",
|
||||
"tests2_dat/12.html",
|
||||
"tests4_dat/3.fragment.style.html",
|
||||
"tests4_dat/4.fragment.plaintext.html",
|
||||
"tests15_dat/0.html",
|
||||
"tests15_dat/1.html",
|
||||
"tests16_dat/31.html",
|
||||
"tests16_dat/32.html",
|
||||
"tests16_dat/33.html",
|
||||
"tests16_dat/34.html",
|
||||
"tests16_dat/35.html",
|
||||
"tests16_dat/36.html",
|
||||
"tests16_dat/37.html",
|
||||
"tests16_dat/48.html",
|
||||
"tests16_dat/49.html",
|
||||
"tests16_dat/50.html",
|
||||
"tests16_dat/51.html",
|
||||
"tests16_dat/52.html",
|
||||
"tests16_dat/53.html",
|
||||
"tests16_dat/130.html",
|
||||
"tests16_dat/131.html",
|
||||
"tests16_dat/132.html",
|
||||
"tests16_dat/133.html",
|
||||
"tests16_dat/134.html",
|
||||
"tests16_dat/135.html",
|
||||
"tests16_dat/136.html",
|
||||
"tests16_dat/147.html",
|
||||
"tests16_dat/148.html",
|
||||
"tests16_dat/149.html",
|
||||
"tests16_dat/150.html",
|
||||
"tests16_dat/196.html",
|
||||
"tests18_dat/7.html",
|
||||
"tests18_dat/8.html",
|
||||
"tests18_dat/9.html",
|
||||
"tests18_dat/12.html",
|
||||
"tests18_dat/15.html",
|
||||
"tests18_dat/21.html",
|
||||
"tests19_dat/103.html",
|
||||
"tests20_dat/41.html",
|
||||
"tests26_dat/2.html",
|
||||
"tricky01_dat/6.html",
|
||||
"plain-text-unsafe_dat/0.html",
|
||||
"template_dat/68.html",
|
||||
"template_dat/107.html",
|
||||
"tests_innerHTML_1_dat/82.fragment.html.html",
|
||||
"adoption01_dat/5/input.html",
|
||||
"adoption01_dat/6/input.html",
|
||||
"adoption01_dat/7/input.html",
|
||||
"adoption01_dat/8/input.html",
|
||||
"adoption02_dat/0/input.html",
|
||||
"tests1_dat/30/input.html",
|
||||
"tests1_dat/68/input.html",
|
||||
"tests1_dat/69/input.html",
|
||||
"tests1_dat/70/input.html",
|
||||
"tests1_dat/71/input.html",
|
||||
"tests1_dat/77/input.html",
|
||||
"tests1_dat/90/input.html",
|
||||
"tests1_dat/103/input.html",
|
||||
"tests2_dat/12/input.html",
|
||||
"tests4_dat/3.fragment_style/input.html",
|
||||
"tests4_dat/4.fragment_plaintext/input.html",
|
||||
"tests15_dat/0/input.html",
|
||||
"tests15_dat/1/input.html",
|
||||
"tests16_dat/31/input.html",
|
||||
"tests16_dat/32/input.html",
|
||||
"tests16_dat/33/input.html",
|
||||
"tests16_dat/34/input.html",
|
||||
"tests16_dat/35/input.html",
|
||||
"tests16_dat/36/input.html",
|
||||
"tests16_dat/37/input.html",
|
||||
"tests16_dat/48/input.html",
|
||||
"tests16_dat/49/input.html",
|
||||
"tests16_dat/50/input.html",
|
||||
"tests16_dat/51/input.html",
|
||||
"tests16_dat/52/input.html",
|
||||
"tests16_dat/53/input.html",
|
||||
"tests16_dat/130/input.html",
|
||||
"tests16_dat/131/input.html",
|
||||
"tests16_dat/132/input.html",
|
||||
"tests16_dat/133/input.html",
|
||||
"tests16_dat/134/input.html",
|
||||
"tests16_dat/135/input.html",
|
||||
"tests16_dat/136/input.html",
|
||||
"tests16_dat/147/input.html",
|
||||
"tests16_dat/148/input.html",
|
||||
"tests16_dat/149/input.html",
|
||||
"tests16_dat/150/input.html",
|
||||
"tests16_dat/196/input.html",
|
||||
"tests18_dat/7/input.html",
|
||||
"tests18_dat/8/input.html",
|
||||
"tests18_dat/9/input.html",
|
||||
"tests18_dat/12/input.html",
|
||||
"tests18_dat/15/input.html",
|
||||
"tests18_dat/21/input.html",
|
||||
"tests19_dat/103/input.html",
|
||||
"tests20_dat/41/input.html",
|
||||
"tests26_dat/2/input.html",
|
||||
"tricky01_dat/6/input.html",
|
||||
"plain-text-unsafe_dat/0/input.html",
|
||||
"template_dat/68/input.html",
|
||||
"template_dat/107/input.html",
|
||||
)
|
||||
)]
|
||||
fn html5lib_tests_verify(input: PathBuf) {
|
||||
let file_stem = input.file_stem().unwrap().to_str().unwrap().to_owned();
|
||||
let scripting_enabled = file_stem.contains("script_on");
|
||||
let parent = input.parent().unwrap().to_string_lossy();
|
||||
let scripting_enabled = parent.contains("script_on");
|
||||
let parser_config = ParserConfig {
|
||||
scripting_enabled,
|
||||
iframe_srcdoc: false,
|
||||
@ -522,13 +521,14 @@ fn html5lib_tests_verify(input: PathBuf) {
|
||||
scripting_enabled,
|
||||
};
|
||||
|
||||
if file_stem.contains("fragment") {
|
||||
if parent.contains("fragment") {
|
||||
let mut context_element_namespace = Namespace::HTML;
|
||||
let mut context_element_tag_name = "";
|
||||
let context_element = file_stem
|
||||
let context_element = parent
|
||||
.split('.')
|
||||
.last()
|
||||
.expect("failed to get context element from filename");
|
||||
.expect("failed to get context element from filename")
|
||||
.replace("fragment_", "");
|
||||
|
||||
if context_element.contains('_') {
|
||||
let mut splited = context_element.split('_');
|
||||
@ -547,7 +547,7 @@ fn html5lib_tests_verify(input: PathBuf) {
|
||||
context_element_tag_name = tag_name;
|
||||
}
|
||||
} else {
|
||||
context_element_tag_name = context_element;
|
||||
context_element_tag_name = &context_element;
|
||||
}
|
||||
|
||||
let context_element = Element {
|
||||
|
@ -1107,7 +1107,7 @@ fn html5lib_test_tree_construction(input: PathBuf) {
|
||||
let mut file_stem = counter.to_string();
|
||||
|
||||
if !document_fragment.is_empty() {
|
||||
file_stem += ".fragment.";
|
||||
file_stem += ".fragment_";
|
||||
file_stem += &document_fragment.join("").replace(' ', "_");
|
||||
}
|
||||
|
||||
@ -1115,12 +1115,17 @@ fn html5lib_test_tree_construction(input: PathBuf) {
|
||||
file_stem += ".script_on";
|
||||
}
|
||||
|
||||
let html_path = dir.join(file_stem.clone() + ".html");
|
||||
let test_case_dir = dir.join(file_stem);
|
||||
|
||||
fs::create_dir_all(test_case_dir.clone())
|
||||
.expect("failed to create directory for fixtures");
|
||||
|
||||
let html_path = test_case_dir.join("input.html");
|
||||
|
||||
fs::write(html_path, data.join("\n"))
|
||||
.expect("Something went wrong when writing to the file");
|
||||
|
||||
let dom_snapshot_path = dir.join(file_stem.clone() + ".dom");
|
||||
let dom_snapshot_path = test_case_dir.join("dom.rust-debug");
|
||||
|
||||
let mut dom = document.join("\n");
|
||||
|
||||
@ -1132,7 +1137,7 @@ fn html5lib_test_tree_construction(input: PathBuf) {
|
||||
.expect("Something went wrong when writing to the file");
|
||||
|
||||
let errors = errors.join("\n");
|
||||
let errors_snapshot_path = dir.join(file_stem + ".errors");
|
||||
let errors_snapshot_path = test_case_dir.join("output.stderr");
|
||||
|
||||
fs::write(errors_snapshot_path, errors)
|
||||
.expect("Something went wrong when writing to the file");
|
||||
@ -1149,10 +1154,11 @@ fn html5lib_test_tree_construction(input: PathBuf) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let file_stem = input.file_stem().unwrap().to_str().unwrap().to_owned();
|
||||
let parent = input.parent().unwrap();
|
||||
let parent_str = parent.to_string_lossy();
|
||||
|
||||
let scripting_enabled = file_stem.contains("script_on");
|
||||
let json_path = input.parent().unwrap().join(file_stem.clone() + ".json");
|
||||
let scripting_enabled = parent_str.contains("script_on");
|
||||
let json_path = parent.join("output.json");
|
||||
let fm = cm.load_file(&input).unwrap();
|
||||
|
||||
let lexer = Lexer::new(SourceFileInput::from(&*fm));
|
||||
@ -1161,14 +1167,15 @@ fn html5lib_test_tree_construction(input: PathBuf) {
|
||||
iframe_srcdoc: false,
|
||||
};
|
||||
let mut parser = Parser::new(lexer, config);
|
||||
let document_or_document_fragment = if file_stem.contains("fragment") {
|
||||
let document_or_document_fragment = if parent_str.contains("fragment") {
|
||||
let mut context_element_namespace = Namespace::HTML;
|
||||
let mut context_element_tag_name = "unknown";
|
||||
|
||||
let context_element = file_stem
|
||||
let context_element = parent_str
|
||||
.split('.')
|
||||
.last()
|
||||
.expect("failed to get context element from filename");
|
||||
.expect("failed to get context element from filename")
|
||||
.replace("fragment_", "");
|
||||
|
||||
if context_element.contains('_') {
|
||||
let mut splited = context_element.split('_');
|
||||
@ -1187,7 +1194,7 @@ fn html5lib_test_tree_construction(input: PathBuf) {
|
||||
context_element_tag_name = tag_name;
|
||||
}
|
||||
} else {
|
||||
context_element_tag_name = context_element;
|
||||
context_element_tag_name = &context_element;
|
||||
}
|
||||
|
||||
let context_element = Element {
|
||||
@ -1206,29 +1213,30 @@ fn html5lib_test_tree_construction(input: PathBuf) {
|
||||
DocumentOrDocumentFragment::Document(parser.parse_document())
|
||||
};
|
||||
|
||||
let parent_name = input.parent().unwrap().to_string_lossy();
|
||||
let parent_parent = parent.parent().unwrap().to_string_lossy();
|
||||
// `scripted` for browser tests with JS
|
||||
// `search` proposed, but not merged in spec
|
||||
let need_skip_tests = parent_name.contains("scripted") || parent_name.contains("search");
|
||||
let need_skip_tests =
|
||||
parent_parent.contains("scripted") || parent_parent.contains("search");
|
||||
|
||||
if !need_skip_tests {
|
||||
let errors = parser.take_errors();
|
||||
let errors_path = input.parent().unwrap().join(file_stem.clone() + ".errors");
|
||||
let errors_path = input.parent().unwrap().join("output.stderr");
|
||||
let contents =
|
||||
fs::read_to_string(errors_path).expect("Something went wrong reading the file");
|
||||
|
||||
// TODO bug in tests - https://github.com/html5lib/html5lib-tests/issues/138
|
||||
let actual_number_of_errors =
|
||||
if parent_name.contains("tests19_dat") && file_stem.contains("84") {
|
||||
if parent_parent.contains("tests19_dat") && parent_str.contains("84") {
|
||||
errors.len() + 1
|
||||
} else if (parent_name.contains("math_dat") || parent_name.contains("svg_dat"))
|
||||
&& (file_stem.contains("5.fragment.tbody")
|
||||
|| file_stem.contains("6.fragment.tbody")
|
||||
|| file_stem.contains("7.fragment.tbody"))
|
||||
} else if (parent_parent.contains("math_dat") || parent_parent.contains("svg_dat"))
|
||||
&& (parent_str.contains("5.fragment_tbody")
|
||||
|| parent_str.contains("6.fragment_tbody")
|
||||
|| parent_str.contains("7.fragment_tbody"))
|
||||
{
|
||||
errors.len() - 1
|
||||
} else if parent_name.contains("foreign-fragment_dat")
|
||||
&& file_stem.contains("3.fragment.svg_path")
|
||||
} else if parent_parent.contains("foreign-fragment_dat")
|
||||
&& parent_str.contains("3.fragment_svg_path")
|
||||
{
|
||||
errors.len() - 1
|
||||
} else {
|
||||
@ -1247,7 +1255,7 @@ fn html5lib_test_tree_construction(input: PathBuf) {
|
||||
|
||||
actual_json.compare_to_file(&json_path).unwrap();
|
||||
|
||||
if parent_name.contains("scripted") || parent_name.contains("search") {
|
||||
if parent_parent.contains("scripted") || parent_parent.contains("search") {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@ -1258,10 +1266,8 @@ fn html5lib_test_tree_construction(input: PathBuf) {
|
||||
indent: 0,
|
||||
});
|
||||
|
||||
let dir = input.parent().unwrap().to_path_buf();
|
||||
|
||||
NormalizedOutput::from(dom_buf)
|
||||
.compare_to_file(&dir.join(file_stem + ".dom"))
|
||||
.compare_to_file(&parent.join("dom.rust-debug"))
|
||||
.unwrap();
|
||||
|
||||
Ok(())
|
||||
@ -1284,10 +1290,8 @@ fn html5lib_test_tree_construction(input: PathBuf) {
|
||||
indent: 0,
|
||||
});
|
||||
|
||||
let dir = input.parent().unwrap().to_path_buf();
|
||||
|
||||
NormalizedOutput::from(dom_buf)
|
||||
.compare_to_file(&dir.join(file_stem + ".dom"))
|
||||
.compare_to_file(&parent.join("dom.rust-debug"))
|
||||
.unwrap();
|
||||
|
||||
Ok(())
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user