Compare() public static method

public static Compare ( string reference, string output ) : void
reference string
output string
return void
Example #1
0
        public void IdentityXmlTest()
        {
            XmlComparer target = new XmlComparer();

            target.AddIdentify(new AttributeIdentify("A", "name"));
            XmlCompareResults result = target.Compare(@"<Persons>
                                <A name=""1"" value=""a1"" />
                                <A name=""2"" value=""a2"" />
                                <A name=""3"" value=""a3"" />
                             </Persons>",
                                                      @"<Persons >
                                <A name=""1"" value=""a1"" />
                                <A name=""2"" value=""a2"" />
                                <A name=""3"" value=""a3"" />
                             </Persons>");

            Assert.IsTrue(result.IsValid);
            result = target.Compare(@"<Persons>
                                <A name=""1"" value=""a1"" />
                                <A name=""2"" value=""a1"" />
                                <A name=""3"" value=""a3"" />
                             </Persons>",
                                    @"<Persons >
                                <A name=""1"" value=""a1"" />
                                <A name=""2"" value=""a2"" />
                                <A name=""3"" value=""a3"" />
                             </Persons>");
            Assert.IsTrue(!result.IsValid);
            target = new XmlComparer();
            target.AddIdentify(new AttributeIdentify("A", "name", "urn:schemas-microsoft-com:windows:storage:mapping:CS"));
            result = target.Compare(@"<Persons xmlns=""urn:schemas-microsoft-com:windows:storage:mapping:CS"" xmlns:store=""china"">
                                <A name=""1"" value=""a1"" />
                                <A name=""2"" value=""a2"" />
                                <A name=""3"" value=""a3"" />
                             </Persons>",
                                    @"<Persons  xmlns=""urn:schemas-microsoft-com:windows:storage:mapping:CS"" xmlns:store=""china"">
                                <A name=""1"" value=""a1"" />
                                <A name=""2"" value=""a2"" />
                                <A name=""3"" value=""a3"" />
                             </Persons>");
            Assert.IsTrue(result.IsValid);
            result = target.Compare(@"<Persons xmlns=""urn:schemas-microsoft-com:windows:storage:mapping:CS"" xmlns:store=""china"">
                                <A name=""1"" value=""a1"" />
                                <A name=""2"" value=""a1"" />
                                <A name=""3"" value=""a3"" />
                             </Persons>",
                                    @"<Persons  xmlns=""urn:schemas-microsoft-com:windows:storage:mapping:CS"" xmlns:store=""china"">
                                <A name=""1"" value=""a1"" />
                                <A name=""2"" value=""a2"" />
                                <A name=""3"" value=""a3"" />
                             </Persons>");
            Assert.IsTrue(!result.IsValid);
        }
Example #2
0
        public void DiffAttrXmlTest()
        {
            XmlComparer target = new XmlComparer();
            //target.AddIdentify
            XmlCompareResults result = target.Compare(@"<Persons attr1=""2"">
                                <A attra=""a2"" >
                                    <X />
                                </A>
                                <C attra=""a1"" />
                                <B attra=""a1"" />
                             </Persons>", @"<Persons attr1=""2"">
                                <A attra=""a1"" >
                                    <X />
                                </A>
                                <C attra=""a1"" />
                                <B attra=""a1"" />
                             </Persons>");

            Assert.IsTrue(!result.IsValid);

            result = target.Compare(@"<Persons attr1=""2"">
                                <A attra=""a1"" >
                                    <X attr=""xx"" />
                                </A>
                                <C attra=""a1"" />
                                <B attra=""a1"" />
                             </Persons>", @"<Persons attr1=""2"">
                                <A attra=""a1"" >
                                    <X />
                                </A>
                                <C attra=""a1"" />
                                <B attra=""a1"" />
                             </Persons>");

            Assert.IsTrue(!result.IsValid);

            result = target.Compare(@"<Persons attr1=""2"">
                                <A attra=""a1"" >
                                    <X/>
                                </A>
                                <C attra=""a1"" />
                                <B attra=""a1"" />
                             </Persons>", @"<Persons attr1=""2"">
                                <A attra=""a1"" >
                                    <X  attr=""xx"" />
                                </A>
                                <C attra=""a1"" />
                                <B attra=""a1"" />
                             </Persons>");

            Assert.IsTrue(!result.IsValid);
        }
Example #3
0
        public void Compare_DifferentRootElements()
        {
            var sourceAttr = new XAttribute("value", 1);
            var source     = new XElement("root", sourceAttr);
            var resultAttr = new XAttribute("value", 2);
            var result     = new XElement("root", resultAttr);

            DiffNode output = _xmlDiff.Compare(source, result);

            Assert.AreEqual(result, output.Raw);
            verifyAttribute(DiffAction.Removed, output.Attributes, sourceAttr);
            verifyAttribute(DiffAction.Added, output.Attributes, resultAttr);
        }
Example #4
0
        private static XmlComparisonResult CompareItems(XmlComparer comparer, string expectedItem, string actualItem)
        {
            var expectedXml = XmlSamples.GetContent(expectedItem);
            var actualXml   = XmlSamples.GetContent(actualItem);

            return(comparer.Compare(expectedXml, actualXml));
        }
Example #5
0
        public void Xdt_SameDocument_ProducesBlankXdt()
        {
            DiffNode output = _xmlDiff.Compare(_simpleDoc.Root, new XDocument(_simpleDoc).Root);

            var visitor = new XdtVisitor();

            visitor.Visit(output);
            var result = XDocument.Parse(visitor.Result);

            // document should have a root element with the same name as in the original document
            Assert.AreEqual(result.Root.Name, _simpleDoc.Root.Name);
            // that root element should have the xdt prefix namespace
            Assert.AreEqual(result.Root.GetPrefixOfNamespace(XdtVisitor.XdtNamespaceUri), "xdt");
            // that root element should be empty, as there are no differences
            Assert.IsFalse(result.Root.HasElements);
        }
Example #6
0
        public void XmlCompareResultsTest()
        {
            XmlComparer target = new XmlComparer();

            target.AddIdentify(new AttributeIdentify("A", "name"));
            XmlCompareResults result = target.Compare(@"<Persons>
                                <A name=""1"" value=""a1"" />
                                <A name=""2"" value=""a1"" />
                                <A name=""3"" value=""a3"" />
                             </Persons>",
                                                      @"<Persons >
                                <A name=""1"" value=""a1"" />
                                <A name=""2"" value=""a2"" />
                                <A name=""3"" value=""a3"" />
                             </Persons>");

            Assert.IsTrue(!result.IsValid);
            Assert.AreEqual(1, result.Errors.Count());
        }
Example #7
0
        public static void AssertAreEqual(string expected, string actual)
        {
            XmlComparer compare = new XmlComparer(expected);

            compare.Compare(actual);
        }
Example #8
0
        static void Main(string[] args)
        {
            var opt       = CliParser.StrictParse <Options>(args);
            var stopwatch = new Stopwatch();

            XDocument leftDoc  = null;
            XDocument rightDoc = null;

            if (opt.Verbose)
            {
                Console.WriteLine("Loading \"{0}\"...", opt.LeftFile);
            }
            leftDoc = XDocument.Load(opt.LeftFile);
            if (opt.Verbose)
            {
                Console.WriteLine("Loading \"{0}\"...", opt.RightFile);
            }
            rightDoc = XDocument.Load(opt.RightFile);
            if (opt.Verbose)
            {
                Console.WriteLine("Comparing differences...");
            }
            stopwatch.Start();

            var comparer = new XmlComparer();
            var diff     = comparer.Compare(leftDoc.Root, rightDoc.Root);

            if (!diff.IsChanged && opt.Verbose)
            {
                Console.WriteLine("No changes detected!");
            }

            if (opt.Verbose)
            {
                Console.WriteLine("Compared in {0} ms.", stopwatch.ElapsedMilliseconds);
            }

            if (!string.IsNullOrEmpty(opt.OutputHtmlFile))
            {
                if (opt.Verbose)
                {
                    Console.WriteLine("Creating HTML output...");
                    stopwatch.Restart();
                }

                var visitor = new HtmlVisitor();
                visitor.Visit(diff);

                if (opt.Verbose)
                {
                    Console.WriteLine("Writing HTML output to \"{0}\"...", opt.OutputHtmlFile);
                }
                File.WriteAllText(opt.OutputHtmlFile, visitor.Result);

                if (opt.Verbose)
                {
                    Console.WriteLine("HTML output file created in {0} ms.", stopwatch.ElapsedMilliseconds);
                }
            }

            if (!string.IsNullOrEmpty(opt.OutputXdtFile))
            {
                if (opt.Verbose)
                {
                    Console.WriteLine("Creating XDT output...");
                    stopwatch.Restart();
                }

                var visitor = new XdtVisitor();
                visitor.Visit(diff);

                if (opt.Verbose)
                {
                    Console.WriteLine("Writing XDT output to \"{0}\"...", opt.OutputXdtFile);
                }
                File.WriteAllText(opt.OutputXdtFile, visitor.Result);

                if (opt.Verbose)
                {
                    Console.WriteLine("XDT output file created in {0} ms.", stopwatch.ElapsedMilliseconds);
                }
            }
            stopwatch.Stop();

            if (opt.Verbose)
            {
                Console.WriteLine("\nShowing text diff:");
            }
            if (opt.Verbose || (string.IsNullOrEmpty(opt.OutputHtmlFile) && string.IsNullOrEmpty(opt.OutputXdtFile)))
            {
                var vistor = new ToStringVisitor();
                vistor.Visit(diff);
                Console.WriteLine(vistor.Result);
            }
        }
Example #9
0
        public void NamespaceXmlTest()
        {
            XmlComparer target = new XmlComparer();
            //target.AddIdentify
            XmlCompareResults result = target.Compare(@"<Persons attr1=""2""  xmlns=""urn:schemas-microsoft-com:windows:storage:mapping:CS"">
                                <A attra=""a1"" >
                                    <X />
                                </A>
                                <C attra=""a1"" />
                                <B attra=""a1"" />
                             </Persons>",
                                                      @"<Persons attr1=""2""  xmlns=""urn:schemas-microsoft-com:windows:storage:mapping:CS"">
                                <A attra=""a1"" >
                                    <X />
                                </A>
                                <C attra=""a1"" />
                                <B attra=""a1"" />
                             </Persons>");

            Assert.IsTrue(result.IsValid);

            result = target.Compare(@"<Persons attr1=""2""  xmlns=""urn:schemas-microsoft-com:windows:storage:mapping:CS"">
                                <A attra=""a1"" >
                                    <X />
                                </A>
                                <D attra=""a1"" />
                                <B attra=""a1"" />
                             </Persons>",
                                    @"<Persons attr1=""2""  xmlns=""urn:schemas-microsoft-com:windows:storage:mapping:CS"">
                                <A attra=""a1"" >
                                    <X />
                                </A>
                                <C attra=""a1"" />
                                <B attra=""a1"" />
                             </Persons>");

            Assert.IsTrue(!result.IsValid);

            result = target.Compare(@"<Persons attr1=""2""  xmlns=""urn:schemas-microsoft-com:windows:storage:mapping:CS"" xmlns:store=""china"">
                                <A attra=""a1"" >
                                    <X />
                                </A>
                                <store:D attra=""a1"" />
                             </Persons>",
                                    @"<Persons attr1=""2""  xmlns=""urn:schemas-microsoft-com:windows:storage:mapping:CS"" xmlns:store=""china"">
                                <A attra=""a1"" >
                                    <X />
                                </A>
                                <store:D attra=""a1"" />
                             </Persons>");

            Assert.IsTrue(result.IsValid);

            result = target.Compare(@"<Persons attr1=""2""  xmlns=""urn:schemas-microsoft-com:windows:storage:mapping:CS"" xmlns:store=""china"">
                                <A attra=""a1"" >
                                    <X />
                                </A>
                                <store:D attra=""a1"" />
                             </Persons>",
                                    @"<Persons attr1=""2""  xmlns=""urn:schemas-microsoft-com:windows:storage:mapping:CS"" xmlns:store=""china"">
                                <A attra=""a1"" >
                                    <X />
                                </A>
                                <D attra=""a1"" />
                             </Persons>");

            Assert.IsTrue(!result.IsValid);
        }