Repository: jaytaph/gosub-browser
File: src/testing/tree_construction.rs (Rust)

Code before:
    }

    /// Returns true if the whole document tree matches the expected result
    pub fn match_document_tree(&self, document: &Document) -> SubtreeResult {
        if self.spec.document_fragment.is_some() {
            self.match_node(NodeId::from(1), 0, 0, document)
        } else {
            self.match_node(NodeId::root(), 0, -1, document)
        }
    }

    /// Match a single node and its children
    fn match_node(
        &self,
        node_idx: NodeId,
        document_offset_id: isize,
        indent: isize,
        document: &Document,
    ) -> SubtreeResult {
        let mut next_expected_idx = document_offset_id;

        let node = document.get_node_by_id(node_idx).unwrap();

        let node_result = match &node.data {
            NodeData::DocType(doctype) => {
                let doctype_text =
                    if doctype.pub_identifier.is_empty() && doctype.sys_identifier.is_empty() {
                        // <!DOCTYPE html>
                        doctype.name.to_string()
                    } else {
                        // <!DOCTYPE html "pubid" "sysid">
                        format!(
                            "{} \"{}\" \"{}\"",
                            doctype.name, doctype.pub_identifier, doctype.sys_identifier
                        )
                    };

                let actual = format!(
                    "|{}<!DOCTYPE {}>",
                    " ".repeat(indent as usize * 2 + 1),
                    doctype_text.trim(),
                );

                let expected = self.document[next_expected_idx as usize].to_owned();
                next_expected_idx += 1;

                if actual != expected {
                    let node = Some(NodeResult::DocTypeMatchFailure {
                        actual,
                        expected: "".to_string(),
                    });

Review comment (suggestion):
this is where we generate the string for the doctype node.. It depends a bit on the pub_identifier and sys_identifier how it should look.

Code after:
        Ok((document, parse_errors))
    }

    /// Returns true if the whole document tree matches the expected result
    pub fn match_document_tree(&self, document: &Document) -> SubtreeResult {
        if self.spec.document_fragment.is_some() {
            self.match_node(NodeId::from(1), 0, 0, document)
        } else {
            self.match_node(NodeId::root(), 0, -1, document)
        }
    }

    /// Match a single node and its children
    fn match_node(
        &self,
        node_idx: NodeId,
        document_offset_id: isize,
        indent: isize,
        document: &Document,
    ) -> SubtreeResult {
        let mut next_expected_idx = document_offset_id;

        let node = document.get_node_by_id(node_idx).unwrap();

        let node_result = match &node.data {
            NodeData::DocType(DocTypeData {
                name,
                pub_identifier,
                sys_identifier,
            }) => {
                let doctype_text = if pub_identifier.is_empty() && sys_identifier.is_empty() {
                    // <!DOCTYPE html>
                    name.to_string()
                } else {
                    // <!DOCTYPE html "pubid" "sysid">
                    format!(r#"{name} "{pub_identifier}" "{sys_identifier}""#,)
                };

                let actual = format!(
                    "|{}<!DOCTYPE {}>",
                    " ".repeat(indent as usize * 2 + 1),
                    doctype_text.trim(),
                );

                let expected = self.document[next_expected_idx as usize].to_owned();
                next_expected_idx += 1;

                if actual != expected {
                    let node = Some(NodeResult::DocTypeMatchFailure {
                        actual,
                        expected: "".to_string(),