Skip to content

Commit

Permalink
cargo fmt fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
jaytaph committed Sep 24, 2023
1 parent e9688c1 commit 361e9f4
Show file tree
Hide file tree
Showing 4 changed files with 309 additions and 129 deletions.
127 changes: 81 additions & 46 deletions src/bin/parser_test.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
use std::{env, fs, io};
use std::fs::File;
use regex::Regex;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
use gosub_engine::html5_parser::input_stream::InputStream;
use gosub_engine::html5_parser::node::NodeData;
use gosub_engine::html5_parser::parser::Html5Parser;
use gosub_engine::html5_parser::parser::document::Document;
use gosub_engine::html5_parser::parser::Html5Parser;
use regex::Regex;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
use std::{env, fs, io};

pub struct TestResults{
tests: usize, // Number of tests (as defined in the suite)
assertions: usize, // Number of assertions (different combinations of input/output per test)
succeeded: usize, // How many succeeded assertions
failed: usize, // How many failed assertions
failed_position: usize, // How many failed assertions where position is not correct
pub struct TestResults {
tests: usize, // Number of tests (as defined in the suite)
assertions: usize, // Number of assertions (different combinations of input/output per test)
succeeded: usize, // How many succeeded assertions
failed: usize, // How many failed assertions
failed_position: usize, // How many failed assertions where position is not correct
}

struct Test {
file_path: String, // Filename of the test
line: usize, // Line number of the test
data: String, // input stream
errors: Vec<Error>, // errors
document: Vec<String>, // document tree
document_fragment: Vec<String>, // fragment
file_path: String, // Filename of the test
line: usize, // Line number of the test
data: String, // input stream
errors: Vec<Error>, // errors
document: Vec<String>, // document tree
document_fragment: Vec<String>, // fragment
}

fn main () -> io::Result<()> {
fn main() -> io::Result<()> {
let default_dir = "./html5lib-tests";
let dir = env::args().nth(1).unwrap_or(default_dir.to_string());

let mut results = TestResults{
let mut results = TestResults {
tests: 0,
assertions: 0,
succeeded: 0,
Expand All @@ -41,7 +41,7 @@ fn main () -> io::Result<()> {
let entry = entry?;
let path = entry.path();

if ! path.ends_with("tests1.dat") {
if !path.ends_with("tests1.dat") {
continue;
}

Expand Down Expand Up @@ -85,10 +85,13 @@ fn read_tests(file_path: PathBuf) -> io::Result<Vec<Test>> {
let line = line?;

if line.starts_with("#data") {
if !current_test.data.is_empty() || !current_test.errors.is_empty() || !current_test.document.is_empty() {
if !current_test.data.is_empty()
|| !current_test.errors.is_empty()
|| !current_test.document.is_empty()
{
current_test.data = current_test.data.trim_end().to_string();
tests.push(current_test);
current_test = Test{
current_test = Test {
file_path: file_path.to_str().unwrap().clone().to_string(),
line: line_num,
data: "".to_string(),
Expand All @@ -114,9 +117,9 @@ fn read_tests(file_path: PathBuf) -> io::Result<Vec<Test>> {
let col = caps.name("col").unwrap().as_str().parse::<i64>().unwrap();
let code = caps.name("code").unwrap().as_str().to_string();

current_test.errors.push(Error{ code, line, col });
current_test.errors.push(Error { code, line, col });
}
},
}
"document" => current_test.document.push(line),
"document_fragment" => current_test.document_fragment.push(line),
_ => (),
Expand All @@ -125,16 +128,22 @@ fn read_tests(file_path: PathBuf) -> io::Result<Vec<Test>> {
}

// Push the last test if it has data
if !current_test.data.is_empty() || !current_test.errors.is_empty() || !current_test.document.is_empty() {
if !current_test.data.is_empty()
|| !current_test.errors.is_empty()
|| !current_test.document.is_empty()
{
current_test.data = current_test.data.trim_end().to_string();
tests.push(current_test);
}

Ok(tests)
}

fn run_tree_test(test_idx: usize,test: &Test, results: &mut TestResults) {
println!("🧪 Running test #{}: {}::{}", test_idx, test.file_path, test.line);
fn run_tree_test(test_idx: usize, test: &Test, results: &mut TestResults) {
println!(
"🧪 Running test #{}: {}::{}",
test_idx, test.file_path, test.line
);

results.tests += 1;

Expand All @@ -155,7 +164,11 @@ fn run_tree_test(test_idx: usize,test: &Test, results: &mut TestResults) {
}

if parse_errors.len() != test.errors.len() {
println!("❌ Unexpected errors found (wanted {}, got {}): ", test.errors.len(), parse_errors.len());
println!(
"❌ Unexpected errors found (wanted {}, got {}): ",
test.errors.len(),
parse_errors.len()
);
// for want_err in &test.errors {
// println!(" * Want: '{}' at {}:{}", want_err.code, want_err.line, want_err.col);
// }
Expand All @@ -165,7 +178,7 @@ fn run_tree_test(test_idx: usize,test: &Test, results: &mut TestResults) {
// results.assertions += 1;
// results.failed += 1;
} else {
println!("✅ Found {} errors", parse_errors.len());
println!("✅ Found {} errors", parse_errors.len());
}
//
// // Check each error messages
Expand Down Expand Up @@ -204,7 +217,6 @@ fn run_tree_test(test_idx: usize,test: &Test, results: &mut TestResults) {
// idx += 1;
// }


if old_failed != results.failed {
println!("----------------------------------------");
println!("📄 Input stream: ");
Expand All @@ -226,9 +238,9 @@ fn run_tree_test(test_idx: usize,test: &Test, results: &mut TestResults) {

#[derive(PartialEq)]
enum ErrorResult {
Success, // Found the correct error
Failure, // Didn't find the error (not even with incorrect position)
PositionFailure, // Found the error, but on an incorrect position
Success, // Found the correct error
Failure, // Didn't find the error (not even with incorrect position)
PositionFailure, // Found the error, but on an incorrect position
}

#[derive(PartialEq)]
Expand All @@ -242,27 +254,39 @@ fn match_document_tree(document: &Document, expected: &Vec<String>) -> bool {
match_node(0, -1, -1, document, expected).is_some()
}

fn match_node(node_idx: usize, expected_id: isize, indent: isize, document: &Document, expected: &Vec<String>) -> Option<usize> {
fn match_node(
node_idx: usize,
expected_id: isize,
indent: isize,
document: &Document,
expected: &Vec<String>,
) -> Option<usize> {
let node = document.get_node_by_id(node_idx).unwrap();

if node_idx > 0 {
match &node.data {
NodeData::Element { name, .. } => {
let value = format!("|{}<{}>", " ".repeat((indent as usize * 2) + 1), name);
if value != expected[expected_id as usize] {
println!("❌ {}, Found unexpected element node: {}", expected[expected_id as usize], name);
if value != expected[expected_id as usize] {
println!(
"❌ {}, Found unexpected element node: {}",
expected[expected_id as usize], name
);
return None;
} else {
println!("✅ {}", expected[expected_id as usize]);
println!("✅ {}", expected[expected_id as usize]);
}
}
NodeData::Text { value } => {
let value = format!("|{}\"{}\"", " ".repeat(indent as usize * 2 + 1), value);
let value = format!("|{}\"{}\"", " ".repeat(indent as usize * 2 + 1), value);
if value != expected[expected_id as usize] {
println!("❌ {}, Found unexpected text node: {}", expected[expected_id as usize], value);
println!(
"❌ {}, Found unexpected text node: {}",
expected[expected_id as usize], value
);
return None;
} else {
println!("✅ {}", expected[expected_id as usize]);
println!("✅ {}", expected[expected_id as usize]);
}
}
_ => {}
Expand All @@ -272,7 +296,9 @@ fn match_node(node_idx: usize, expected_id: isize, indent: isize, document: &Doc
let mut next_expected_idx = expected_id + 1;

for &child_idx in &node.children {
if let Some(new_idx) = match_node(child_idx, next_expected_idx, indent + 1, document, expected) {
if let Some(new_idx) =
match_node(child_idx, next_expected_idx, indent + 1, document, expected)
{
next_expected_idx = new_idx as isize;
} else {
return None;
Expand All @@ -286,17 +312,26 @@ fn match_node(node_idx: usize, expected_id: isize, indent: isize, document: &Doc
fn match_error(got_err: &Error, expected_err: &Error) -> ErrorResult {
if got_err == expected_err {
// Found an exact match
println!("✅ Found parse error '{}' at {}:{}", got_err.code, got_err.line, got_err.col);
println!(
"✅ Found parse error '{}' at {}:{}",
got_err.code, got_err.line, got_err.col
);

return ErrorResult::Success;
}

if got_err.code != expected_err.code {
println!("❌ Expected error '{}' at {}:{}", expected_err.code, expected_err.line, expected_err.col);
println!(
"❌ Expected error '{}' at {}:{}",
expected_err.code, expected_err.line, expected_err.col
);
return ErrorResult::Failure;
}

// Found an error with the same code, but different line/pos
println!("⚠️ Unexpected error position '{}' at {}:{} (got: {}:{})", expected_err.code, expected_err.line, expected_err.col, got_err.line, got_err.col);
println!(
"⚠️ Unexpected error position '{}' at {}:{} (got: {}:{})",
expected_err.code, expected_err.line, expected_err.col, got_err.line, got_err.col
);
ErrorResult::PositionFailure
}
}
4 changes: 1 addition & 3 deletions src/html5_parser/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ impl Node {
// This will only compare against the tag, namespace and attributes. Both nodes could still have
// other parents and children.
pub fn matches_tag_and_attrs(&self, other: &Self) -> bool {
self.name == other.name &&
self.namespace == other.namespace &&
self.data == other.data
self.name == other.name && self.namespace == other.namespace && self.data == other.data
}
}

Expand Down
30 changes: 23 additions & 7 deletions src/html5_parser/parser/adoption_agency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ impl<'a> Html5Parser<'a> {

// Step 2
let current_node_id = current_node!(self).id;
if current_node!(self).name == *subject && ! self.active_formatting_elements.iter().any(|elem| elem == &ActiveElement::NodeId(current_node_id)) {
if current_node!(self).name == *subject
&& !self
.active_formatting_elements
.iter()
.any(|elem| elem == &ActiveElement::NodeId(current_node_id))
{
self.open_elements.pop();
return;
}
Expand Down Expand Up @@ -77,7 +82,9 @@ impl<'a> Html5Parser<'a> {
}

// Step 4.5
if open_elements_has!(self, formatting_element_name) && ! self.is_in_scope(&formatting_element_name, Scope::Regular) {
if open_elements_has!(self, formatting_element_name)
&& !self.is_in_scope(&formatting_element_name, Scope::Regular)
{
self.parse_error("formatting element not in scope");
return;
}
Expand All @@ -95,7 +102,7 @@ impl<'a> Html5Parser<'a> {

for idx in (0..formatting_element_idx).rev() {
match self.active_formatting_elements[idx] {
ActiveElement::Marker => {},
ActiveElement::Marker => {}
ActiveElement::NodeId(node_id) => {
let node = self.document.get_node_by_id(node_id).unwrap();
if node.is_special() {
Expand Down Expand Up @@ -162,12 +169,19 @@ impl<'a> Html5Parser<'a> {
}

// Step 4.13.4
if inner_loop_counter > ADOPTION_AGENCY_INNER_LOOP_DEPTH && self.active_formatting_elements.contains(&ActiveElement::NodeId(node_id)) {
if inner_loop_counter > ADOPTION_AGENCY_INNER_LOOP_DEPTH
&& self
.active_formatting_elements
.contains(&ActiveElement::NodeId(node_id))
{
self.active_formatting_elements.remove(node_idx);
}

// Step 4.13.5
if ! self.active_formatting_elements.contains(&ActiveElement::NodeId(node_id)) {
if !self
.active_formatting_elements
.contains(&ActiveElement::NodeId(node_id))
{
// We have removed the node from the given node_idx
self.open_elements.remove(node_idx);
continue;
Expand Down Expand Up @@ -210,8 +224,10 @@ impl<'a> Html5Parser<'a> {
let new_element_id = self.document.add_node(new_element, furthest_block_id);

// Step 4.18
self.active_formatting_elements.remove(formatting_element_idx);
self.active_formatting_elements.insert(bookmark, ActiveElement::NodeId(new_element_id));
self.active_formatting_elements
.remove(formatting_element_idx);
self.active_formatting_elements
.insert(bookmark, ActiveElement::NodeId(new_element_id));

// Step 4.19
// Remove formatting element from the stack of open elements, and insert the new element into the stack of open elements immediately below the position of furthest block in that stack.
Expand Down
Loading

0 comments on commit 361e9f4

Please sign in to comment.