예제 #1
0
        public void AttributeMarkedChanged()
        {
            var doc1 = new XmlDocument();
            doc1.LoadXml(@"
                <root>
                    <child1 name1='child1' type1='elem1'>child1</child1>
                    <child2 name1='child2' type1='elem2'>child2</child2>
                </root>"
            );

            // reordered xml but same values
            var doc2 = new XmlDocument();
            doc2.LoadXml(@"
                <root>
                    <child1 type1='elem1' name1='DIFFERENT'>child1</child1>
                    <child2 name1='child2' type1='elem2'>child2</child2>
                </root>"
            );

            var tree1 = new XTree(doc1);
            var tree2 = new XTree(doc2);
            XDiff.Diff(tree1, tree2);

            Assert.AreEqual(tree1.Root.Match, MatchType.Change);
            Assert.AreEqual(tree2.Root.Match, MatchType.Change);

            Assert.AreEqual(tree1.Root.Children.Length, 2);
            Assert.AreEqual(tree2.Root.Children.Length, 2);

            Assert.AreEqual(tree1.Root.Elements.Length, 2);
            Assert.AreEqual(tree2.Root.Elements.Length, 2);

            Assert.AreEqual(tree1.Root.Elements[0].Match, MatchType.Change);
            Assert.AreEqual(tree2.Root.Elements[0].Match, MatchType.Change);

            Assert.AreEqual(tree1.Root.Elements[1].Match, MatchType.Match);
            Assert.AreEqual(tree2.Root.Elements[1].Match, MatchType.Match);

            Assert.AreEqual(tree1.Root.Elements[0].Attributes.Length, 2);
            Assert.AreEqual(tree2.Root.Elements[0].Attributes.Length, 2);

            Assert.AreEqual(tree1.Root.Elements[0].Texts.Length, 1);
            Assert.AreEqual(tree2.Root.Elements[0].Texts.Length, 1);

            Assert.AreEqual(tree1.Root.Elements[0].Texts[0].Match, MatchType.Match);
            Assert.AreEqual(tree2.Root.Elements[0].Texts[0].Match, MatchType.Match);

            Assert.AreEqual(tree1.Root.Elements[0].Attributes[0].Match, MatchType.Change);
            Assert.AreEqual(tree2.Root.Elements[0].Attributes[0].Match, MatchType.Match);

            Assert.AreEqual(tree1.Root.Elements[0].Attributes[1].Match, MatchType.Match);
            Assert.AreEqual(tree2.Root.Elements[0].Attributes[1].Match, MatchType.Change);

            Assert.AreEqual(tree1.Root.Elements[0].Attributes[0].Matching, tree2.Root.Elements[0].Attributes[1]);
        }
예제 #2
0
        /// <summary>
        /// Compare and match the two trees
        /// </summary>
        public static void Diff(XTree tree1, XTree tree2)
        {
            if (tree1.Root.HashEquals(tree2.Root.Hash))
            {
                SetMatching(tree1.Root, tree2.Root, MatchType.Match);
                return;
            }

            if (tree1.Root.Name != tree2.Root.Name)
            {
                SetMatching(tree1.Root, tree1.Root, MatchType.NoMatch);
                return;
            }

            distanceLookup = new Dictionary<Tuple<XNode, XNode>, int>();
            SetMatching(tree1.Root, tree2.Root, MatchType.Change);
            DiffElements(tree1.Root, tree2.Root);
        }
예제 #3
0
파일: XDiff.cs 프로젝트: rvf148/XDTTool
        /// <summary>
        /// Compare and match the two trees
        /// </summary>
        public static void Diff(XTree tree1, XTree tree2)
        {
            if (tree1.Root.HashEquals(tree2.Root.Hash))
            {
                SetMatching(tree1.Root, tree2.Root, MatchType.Match);
                return;
            }

            if (tree1.Root.Name != tree2.Root.Name)
            {
                SetMatching(tree1.Root, tree1.Root, MatchType.NoMatch);
                return;
            }

            distanceLookup = new Dictionary <Tuple <XNode, XNode>, int>();
            SetMatching(tree1.Root, tree2.Root, MatchType.Change);
            DiffElements(tree1.Root, tree2.Root);
        }
예제 #4
0
        private void AssertCanTransform(string sourceXml, string targetXml)
        {
            var doc1 = new XmlDocument();
            doc1.LoadXml(sourceXml);

            // reordered xml but same values
            var doc2 = new XmlDocument();
            doc2.LoadXml(targetXml);

            var tree1 = new XTree(doc1);
            var tree2 = new XTree(doc2);
            XDiff.Diff(tree1, tree2);

            var writer = new XdtDiffWriter();
            var patch = writer.GetDiff(tree2);
            var transformed = Transform(doc1, patch);

            var transformedTree = new XTree(transformed);

            Assert.IsFalse(Enumerable.SequenceEqual(tree1.Root.Hash, tree2.Root.Hash));
            Assert.IsTrue(Enumerable.SequenceEqual(transformedTree.Root.Hash, tree2.Root.Hash));
        }
예제 #5
0
        public void DifferentXmlTextsAreDifferent()
        {
            var doc1 = new XmlDocument();
            doc1.LoadXml(@"
                <root>
                    <child1 name1='child1' type1='elem1'>child1</child1>
                    <child2 name2='child2' type2='elem2'>child2</child2>
                </root>"
            );

            var doc2 = new XmlDocument();
            doc2.LoadXml(@"
                <root>
                    <child1 name1='child1' type1='elem1'>child1</child1>
                    <child2 name2='child2' type2='elem2'>DIFFERENT</child2>
                </root>"
            );

            var tree1 = new XTree(doc1);
            var tree2 = new XTree(doc2);

            Assert.IsFalse(Enumerable.SequenceEqual(tree1.Root.Hash, tree2.Root.Hash));
        }
예제 #6
0
        private XmlDocument GetPatch(string sourceXml, string targetXml)
        {
            var doc1 = new XmlDocument();
            doc1.LoadXml(sourceXml);

            // reordered xml but same values
            var doc2 = new XmlDocument();
            doc2.LoadXml(targetXml);

            var tree1 = new XTree(doc1);
            var tree2 = new XTree(doc2);
            XDiff.Diff(tree1, tree2);

            var writer = new XdtDiffWriter();
            return writer.GetDiff(tree2);
        }
예제 #7
0
        public void IsomorphicXmlAreEqual()
        {
            var doc1 = new XmlDocument();
            doc1.LoadXml(@"
                <root>
                    <child1 name1='child1' type1='elem1'>child1</child1>
                    <child2 name2='child2' type2='elem2'>child2</child2>
                </root>"
            );

            // reordered xml but same values
            var doc2 = new XmlDocument();
            doc2.LoadXml(@"
                <root>
                    <child2 type2='elem2' name2='child2'>child2</child2>
                    <child1 type1='elem1' name1='child1'>child1</child1>
                </root>"
            );

            var tree1 = new XTree(doc1);
            var tree2 = new XTree(doc2);

            Assert.IsTrue(Enumerable.SequenceEqual(tree1.Root.Hash, tree2.Root.Hash));
        }
예제 #8
0
        public void SingleElement()
        {
            var doc1 = new XmlDocument();
            doc1.LoadXml("<root></root>");
            var tree1 = new XTree(doc1);

            Assert.AreEqual(tree1.Root.Children.Length, 0);
        }