natural-xml-diff 0.1.0

Natural diffing between XML documents
Documentation
use xot::Xot;

use crate::apply::apply_diff;
use crate::comparison::Comparison;

impl Comparison {
    /// Verify that a correct diff gets produced in one step.
    ///
    /// This is a convenience function that first constructs the diff document
    /// and then immediately applies it to document A and checks that the
    /// result is semantically equal to document B. If not, it returns the
    /// XML for the erroneous document as part of the error.
    pub(crate) fn verify(&mut self, xot: &mut Xot) -> Result<(), String> {
        let diff = self.diff(xot);
        apply_diff(xot, diff);
        if !xot.compare(self.doc_b, diff) {
            let result_xml = xot.serialize_to_string(diff);
            return Err(result_xml);
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use xot::Error;

    #[test]
    fn test_verify() -> Result<(), Error> {
        let mut xot = Xot::new();
        let xml_a = concat!(
            "<book><chapter><title>Text 1</title><para>Text 2</para></chapter>",
            "<chapter><title>Text 4</title><para>Text 5</para></chapter>",
            "<chapter><title>Text 6</title><para>Text 7<img/>Text 8</para></chapter>",
            "<chapter><title>Text 9</title><para>Text 10</para></chapter>",
            "<chapter><para>Text 11</para><para>Text 12</para></chapter></book>"
        );
        let xml_b = concat!(
            "<book><chapter><para>Text 2</para></chapter>",
            "<chapter><title>Text 4</title><para>Text 25</para><para>Text 11</para></chapter>",
            "<chapter><title>Text 6</title><para>Text 7<img/>Text 8</para></chapter>",
            "<chapter><title>Text 9</title><para>Text 10</para></chapter>",
            "<chapter><para>Text 12</para></chapter></book>"
        );
        let mut comparison = Comparison::from_xml(&mut xot, xml_a, xml_b)?;
        assert!(comparison.verify(&mut xot).is_ok());
        Ok(())
    }
}