public void IgnoreElement()
        {
            "加上忽略的元素列表,可以忽略此元素的不同,没有抛出不匹配异常".Test(() =>
            {
                var xmlString1 = @"<Foo>
<Id>123123</Id>
<F2>1.123</F2>
<F2>2.123</F2>
<F2>3.123</F2>
<F2>4.123</F2>
<F2>5.123</F2>
<F2>6.123</F2>
</Foo>";
                var xDocument1 = XDocument.Parse(xmlString1);
                var xmlString2 = @"<Foo>
<Id>123</Id>
<F2>1.123</F2>
<F2>2.123</F2>
<F2>3.123</F2>
<F2>4.123</F2>
<F2>5.123</F2>
<F2>6.123</F2>
</Foo>";
                var xDocument2 = XDocument.Parse(xmlString2);

                XmlComparer.VerifyXmlEquals(xDocument1, xDocument2, new XmlComparerSettings()
                {
                    IgnoreElementNameList = new[] { "Id" }
                });
            });
        }
        public void SerializeTestFramework()
        {
            const string sourceFile = "test_runner_test.cpp";

            TestFramework framework = new TestFrameworkBuilder(Source, "Test runner test", 1).
                                      TestCase("test1", 65536, new SourceFileInfo(sourceFile, 26)).
                                      TestCase("test2", 65537, new SourceFileInfo(sourceFile, 35)).
                                      TestSuite("SampleSuite", 2).
                                      TestSuite("SampleNestedSuite", 3).
                                      TestCase("test3", 65538, new SourceFileInfo(sourceFile, 48)).
                                      EndSuite().
                                      EndSuite().
                                      TestSuite("TemplateSuite", 4).
                                      TestCase("my_test<char>", 65539, new SourceFileInfo(sourceFile, 79)).
                                      TestCase("my_test<int>", 65540, new SourceFileInfo(sourceFile, 79)).
                                      TestCase("my_test<float>", 65541, new SourceFileInfo(sourceFile, 79)).
                                      TestCase("my_test<double>", 65542, new SourceFileInfo(sourceFile, 79)).
                                      EndSuite().
                                      Build();

            using (Stream stream = TestHelper.LoadEmbeddedResource("BoostTestAdapterNunit.Resources.TestLists.sample.test.list.xml"))
            {
                XmlDocument baseXml = new XmlDocument();
                baseXml.Load(stream);

                XmlComparer comparer = new XmlComparer();
                comparer.CompareXML(baseXml, Serialize(framework), XmlNodeTypeFilter.DefaultFilter);
            }
        }
Пример #3
0
        /// <summary>
        /// Compares two XML results.
        /// </summary>
        /// <param name="expected">The expected xml</param>
        /// <param name="actual">The actual xml</param>
        /// <param name="tagsToExclude">List of tags to exclude</param>
        /// <returns>Comparison result</returns>
        public static bool CompareXml(string expected, string actual, List <string> tagsToExclude)
        {
            expected = RemoveTags(expected, tagsToExclude);
            actual   = RemoveTags(actual, tagsToExclude);

            return(XmlComparer.AreIsomorphic(expected, actual));
        }
Пример #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));
        }
Пример #5
0
        public void WhenMultipleAttributesAreDifferentComparerFails()
        {
            string xmlA = @"
				<root>
					<parents>
						<parent id='1' name='John'>
							<child></child>
						</parent>
						<parent id='2' name='Mary'>
							<child></child>
							<child></child>
						</parent>
					</parents>
				</root>"                ;

            string xmlB = @"
				<root>
					<parents>
						<parent id='1' name='Jake'>
							<child></child>
						</parent>
						<parent id='2' name='Jane'>
							<child></child>
							<child></child>
						</parent>
					</parents>
				</root>"                ;

            XmlComparer comparer = new XmlComparer();

            comparer.AreEqual(xmlA, xmlB);
            Assert.IsFalse(comparer.Results.IsSuccessful);
        }
Пример #6
0
        public void CompareElementsWithIgnoreCommentsIsEqualTest()
        {
            var comparer = new XmlComparer {
                IgnoreComments = true
            };
            var comparison = CompareItems(comparer, "elements", "elements-with-comment");

            AssertComparison(comparison, XmlComparisonState.Equal);
        }
Пример #7
0
        public void CompareElementsBySequenceIsEqualTests()
        {
            var comparer = new XmlComparer {
                NodeMatcher = XmlNodeMatcher.BySequence
            };
            var comparison = CompareItems(comparer, "elements", "elements");

            AssertComparison(comparison, XmlComparisonState.Equal);
        }
Пример #8
0
        public void Compare_GasXml()
        {
            var comparer = new XmlComparer {
                NodeMatcher = XmlNodeMatcher.ByLocalName
            };

            var comparison = CompareItems(comparer, "RefDoc-GasXml", "ShadowDoc-GasXml");

            AssertComparison(comparison, XmlComparisonState.Different);
        }
Пример #9
0
        public void WhenXmlIsNotEqualComparerFails()
        {
            XmlComparer comparer = new XmlComparer();

            XmlDocument[] xmlDocs = new XmlDocument[2];
            xmlDocs[0] = new XmlDocument();
            xmlDocs[0].LoadXml(@"<root><child><grandchild>hello</grandchild></child></root>");
            xmlDocs[1] = new XmlDocument();
            xmlDocs[1].LoadXml(@"<root><child>hello</child></root>");
            Assert.IsFalse(comparer.AreEqual(xmlDocs[0], xmlDocs[1]));
        }
Пример #10
0
        public void WhenXmlHasGroupsAndAreTheSameComparerSucceeds()
        {
            XmlComparer comparer = new XmlComparer();

            XmlDocument[] xmlDocs = new XmlDocument[2];
            xmlDocs[0] = new XmlDocument();
            xmlDocs[0].LoadXml(@"<root><child><grandchild>hello</grandchild></child><child></child></root>");
            xmlDocs[1] = new XmlDocument();
            xmlDocs[1].LoadXml(@"<root><child><grandchild>hello</grandchild></child><child></child></root>");

            Assert.IsTrue(comparer.AreEqual(xmlDocs[0], xmlDocs[1]));
        }
Пример #11
0
        public void WhenAttributesAreDifferentComparerFails()
        {
            XmlComparer comparer = new XmlComparer();

            XmlDocument[] xmlDocs = new XmlDocument[2];
            xmlDocs[0] = new XmlDocument();
            xmlDocs[0].LoadXml(@"<root><child test='2'><grandchild>hello</grandchild></child></root>");
            xmlDocs[1] = new XmlDocument();
            xmlDocs[1].LoadXml(@"<root><child test='1'><grandchild>hello</grandchild></child></root>");

            Assert.IsFalse(comparer.AreEqual(xmlDocs[0], xmlDocs[1]));
        }
Пример #12
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);
        }
Пример #13
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);
        }
        /// <summary>
        /// Compares the serialized content of the settings structure against an Xml embedded resource string.
        /// </summary>
        /// <param name="settings">The settings structure whose serialization is to be compared</param>
        /// <param name="resource">The path to an embedded resource which contains the serialized Xml content to compare against</param>
        private void Compare(BoostTestAdapterSettings settings, string resource)
        {
            XmlElement element = settings.ToXml();

            using (Stream stream = TestHelper.LoadEmbeddedResource(resource))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(stream);

                XmlNode root = doc.DocumentElement.SelectSingleNode("/RunSettings/BoostTest");

                XmlComparer comparer = new XmlComparer();
                comparer.CompareXML(element, root, XmlNodeTypeFilter.DefaultFilter);
            }
        }
Пример #15
0
        public void CompareElementsWithoutIgnoreCommentsIsDifferentTest()
        {
            var comparer = new XmlComparer {
                IgnoreComments = false
            };
            var comparison = CompareItems(comparer, "elements", "elements-with-comment");

            AssertComparison(
                comparison,
                XmlComparisonState.Different,
                new XmlComparisonType[]
            {
                XmlComparisonType.NodeList,
                XmlComparisonType.NodeList,
            });
        }
Пример #16
0
        public void CompareElementsWithDifferentOrderAndStopAnalyzerIsDifferentTests()
        {
            var comparer = new XmlComparer {
                Analyzer = XmlAnalyzer.Constant(XmlComparisonState.Different)
            };
            var comparison = CompareItems(comparer, "elements", "elements-with-different-order");

            AssertComparison(
                comparison,
                XmlComparisonState.Different,
                new XmlComparisonType[]
            {
                XmlComparisonType.NodeListSequence,
                XmlComparisonType.NodeListSequence
            });
        }
Пример #17
0
        public void CheckWsdlImpl()
        {
            string goldWsdl;

            try
            {
                Assembly     _assembly = Assembly.GetExecutingAssembly();
                StreamReader _stream   = new StreamReader(_assembly.GetManifestResourceStream("MonoTests.System.ServiceModel.Test.FeatureBased.Features.Contracts." + typeof(TServer).Name + ".xml"));
                goldWsdl = _stream.ReadToEnd();
            }
            catch
            {
                Console.WriteLine("Couldn't test WSDL of server " + typeof(TServer).Name + " because gold wsdl is not embedded in test !");
                return;
            }
            string currentWsdl = "";

            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(getMexEndpoint() + "?wsdl");
            // Obtain a 'Stream' object associated with the response object.
            WebResponse response      = myReq.GetResponse();
            Stream      ReceiveStream = response.GetResponseStream();

            Encoding encode = global::System.Text.Encoding.GetEncoding("utf-8");

            // Pipe the stream to a higher level stream reader with the required encoding format.
            StreamReader readStream = new StreamReader(ReceiveStream, encode);

            Console.WriteLine("\nResponse stream received");
            int maxLen = 10 * 1024;

            Char [] read = new Char [maxLen];

            // Read 256 charcters at a time.
            int count = readStream.Read(read, 0, maxLen);

            while (count > 0)
            {
                currentWsdl = currentWsdl + new String(read, 0, count);
                count       = readStream.Read(read, 0, 256);
            }
            readStream.Close();
            response.Close();

            XmlComparer comparer = new XmlComparer(XmlComparer.Flags.IgnoreAttribOrder, true);

            Assert.IsTrue(comparer.AreEqual(goldWsdl, currentWsdl), "Service WSDL does not match gold WSDL");
        }
Пример #18
0
        public void Read_SingleChunkMessage_Succeeds()
        {
            ITokenReader      tokenReader      = new XmlTokenReader();
            ITokenReaderState tokenReaderState = new XmlTokenReaderState();

            String expectedMessage = StockLocationInfoResponseEnvelopeDataContractTests.Response.Xml;

            ReadOnlySequence <byte> buffer = new ReadOnlySequence <byte>(XmlSerializationSettings.Encoding.GetBytes(expectedMessage));

            bool readResult = tokenReader.Read(ref tokenReaderState, ref buffer, out ReadOnlySequence <byte> token, out _);

            Assert.IsTrue(readResult);

            String actualMessage = XmlSerializationSettings.Encoding.GetString(token.ToArray());

            Assert.IsTrue(XmlComparer.AreEqual(expectedMessage, actualMessage));
        }
Пример #19
0
        public XmlCompareConstraint(XmlComparisonState expectedState, object expected)
        {
            if (expected == null)
            {
                throw new ArgumentNullException("expected");
            }

            this.expectedNode  = GetXNode(expected);
            this.expectedState = expectedState;

            this.comparer = new XmlComparer();

            this.UseAnalizer(XmlGlobalsSettings.Analyzer)
            .UseHandler(XmlGlobalsSettings.CompareHandler)
            .UseMatcher(XmlGlobalsSettings.NodeMatcher)
            .UseFormatter(XmlGlobalsSettings.Formatter);
        }
Пример #20
0
        public void TestSubscriptionOfSingleObserver()
        {
            using (ITokenizer tokenizer = new XmlTokenizer(this.BaseStream))
            {
                Mock <IObserver <ReadOnlySequence <byte> > > observerMock = new Mock <IObserver <ReadOnlySequence <byte> > >();

                List <String> actualMessages = new List <String>();

                using (ManualResetEventSlim sync = new ManualResetEventSlim())
                {
                    observerMock.Setup(x => x.OnNext(It.IsAny <ReadOnlySequence <byte> >())).Callback((ReadOnlySequence <byte> token) =>
                    {
                        String message = XmlSerializationSettings.Encoding.GetString(token.ToArray());

                        actualMessages.Add(message);
                    });

                    observerMock.Setup(x => x.OnCompleted()).Callback(() =>
                    {
                        sync.Set();
                    });

                    using (IDisposable subscription = tokenizer.Subscribe(observerMock.Object))
                    {
                        sync.Wait();

                        // Number of calls depends on buffer size.
                        observerMock.Verify(x => x.OnNext(It.IsAny <ReadOnlySequence <byte> >()), Times.Exactly(this.Messages.Count));

                        Assert.AreEqual(actualMessages.Count, this.Messages.Count);

                        for (int i = 0; i < actualMessages.Count; i++)
                        {
                            String expected = this.Messages[i];
                            String actual   = actualMessages[i];

                            XmlComparer.AreEqual(expected, actual);
                        }

                        Assert.IsNotNull(subscription);
                    }
                }
            }
        }
Пример #21
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());
        }
Пример #22
0
        protected void Test <T> () where T : new ()
        {
            T      o        = new T();
            Type   t        = o.GetType();
            string fileName = TestResourceHelper.GetFullPathOfResource("Test/Resources/FrameworkTypes/" + t.FullName + ".xml");

            DataContractSerializer serializer    = new DataContractSerializer(t);
            StringBuilder          stringBuilder = new StringBuilder();

            using (XmlWriter xmlWriter = XmlWriter.Create(new StringWriter(stringBuilder)))
                serializer.WriteObject(xmlWriter, o);
            string actualXml   = stringBuilder.ToString();
            string expectedXml = File.ReadAllText(fileName);

            XmlComparer.AssertAreEqual(expectedXml, actualXml, "Serialization of " + t.FullName + " failed.");

            using (FileStream fs = File.OpenRead(fileName)) {
                o = (T)serializer.ReadObject(fs);
            }
        }
Пример #23
0
        // Сравнение документов
        private void CompareDocuments()
        {
            if (LeftDocument != null && RightDocument != null)
            {
                if (LeftDocument.XmlDocument != null && RightDocument.XmlDocument != null)
                {
                    try
                    {
                        this.ComparingRepresent = XmlComparer
                                                  .CompareXmlDocuments(this.LeftDocument.XmlDocument,
                                                                       this.RightDocument.XmlDocument);
                    }
                    catch (System.Exception)
                    {
                        this.ComparingRepresent = new StringBuilder("<>");
                    }

                    OnPropertyChanged("ComparingRepresent");
                }
            }
        }
Пример #24
0
        public void ComparisonResultsAreCorrect()
        {
            string xmlA = @"
				<root>
					<parents>
						<parent id='1' name='John'>
							<child></child>
						</parent>
						<parent id='2' name='Mary'>
							<child></child>
							<child></child>
						</parent>
					</parents>
				</root>"                ;

            string xmlB = @"
				<root>
					<parents>
						<parent id='1' name='Jake'>
							<child></child>
						</parent>
						<parent id='2' name='Jane'>
							<child></child>
							<child></child>
						</parent>
					</parents>
				</root>"                ;

            XmlComparer comparer = new XmlComparer();

            comparer.AreEqual(xmlA, xmlB);
            AssertResults(
                "child:Truechild:Truechild:Trueparent:Falseparent:Falseparents:Trueroot:True",
                comparer.Results
                );
        }
Пример #25
0
        public void Read_MultipleChunkMessage_Succeeds()
        {
            ITokenReader      tokenReader      = new XmlTokenReader();
            ITokenReaderState tokenReaderState = new XmlTokenReaderState();

            String expectedMessage = StockLocationInfoResponseEnvelopeDataContractTests.Response.Xml;

            (String Left, String Right)messageBlocks = expectedMessage.Divide();

            ReadOnlySequence <byte> firstChunk  = new ReadOnlySequence <byte>(XmlSerializationSettings.Encoding.GetBytes(messageBlocks.Left));
            ReadOnlySequence <byte> secondChunk = new ReadOnlySequence <byte>(XmlSerializationSettings.Encoding.GetBytes(messageBlocks.Left + messageBlocks.Right));

            bool firstReadResult = tokenReader.Read(ref tokenReaderState, ref firstChunk, out _, out _);

            Assert.IsFalse(firstReadResult);

            bool secondReadResult = tokenReader.Read(ref tokenReaderState, ref secondChunk, out ReadOnlySequence <byte> token, out _);

            Assert.IsTrue(secondReadResult);

            String actualMessage = XmlSerializationSettings.Encoding.GetString(token.ToArray());

            Assert.IsTrue(XmlComparer.AreEqual(expectedMessage, actualMessage));
        }
Пример #26
0
        public static void AssertAreEqual(string expected, string actual)
        {
            XmlComparer compare = new XmlComparer(expected);

            compare.Compare(actual);
        }
        public void VerifyXmlEquals()
        {
            "传入列表元素数量不同的 XML 内容,抛出异常".Test(() =>
            {
                var xmlString1 = @"<Foo>
<F2>1.123</F2>
<F2>2.123</F2>
<F2>3.123</F2>
<F2>4.123</F2>
<F2>5.123</F2>
<F2>6.123</F2>
</Foo>";
                var xDocument1 = XDocument.Parse(xmlString1);
                var xmlString2 = @"<Foo>
<F2>1.123</F2>
<F2>2.123</F2>
<F2>3.123</F2>
<F2>4.123</F2>
<F2>5.123</F2>
</Foo>";
                var xDocument2 = XDocument.Parse(xmlString2);

                Assert.ThrowsException <ElementNotMatchException>(() =>
                {
                    XmlComparer.VerifyXmlEquals(xDocument1, xDocument2);
                });
            });

            "传入有相同列表元素的 XML 内容,没有抛出不匹配异常".Test(() =>
            {
                var xmlString = @"<Foo>
<F2>1.123</F2>
<F2>2.123</F2>
<F2>3.123</F2>
<F2>4.123</F2>
<F2>5.123</F2>
<F2>6.123</F2>
</Foo>";

                var xmlString1 = xmlString;
                var xDocument1 = XDocument.Parse(xmlString1);
                var xmlString2 = xmlString;
                var xDocument2 = XDocument.Parse(xmlString2);

                XmlComparer.VerifyXmlEquals(xDocument1, xDocument2);
            });

            "传入一个嵌套两层和一个嵌套一层的 XML 内容,抛出异常".Test(() =>
            {
                var xmlString1 = @"<Foo><F2>1.123</F2></Foo>";
                var xDocument1 = XDocument.Parse(xmlString1);
                var xmlString2 = @"<Foo>1.12301</Foo>";
                var xDocument2 = XDocument.Parse(xmlString2);

                Assert.ThrowsException <ElementNotMatchException>(() =>
                {
                    XmlComparer.VerifyXmlEquals(xDocument1, xDocument2);
                });
            });

            "传入嵌套多层的两个相同的 XML 内容,没有抛出不匹配异常".Test(() =>
            {
                var xmlString  = @"<Foo><F2>1.123</F2></Foo>";
                var xmlString1 = xmlString;
                var xDocument1 = XDocument.Parse(xmlString1);
                var xmlString2 = xmlString;
                var xDocument2 = XDocument.Parse(xmlString2);

                XmlComparer.VerifyXmlEquals(xDocument1, xDocument2);
            });

            "传入浮点值,存在精度误差,没有抛出不匹配异常".Test(() =>
            {
                var xmlString1 = @"<Foo>1.123</Foo>";
                var xDocument1 = XDocument.Parse(xmlString1);
                var xmlString2 = @"<Foo>1.12301</Foo>";
                var xDocument2 = XDocument.Parse(xmlString2);

                XmlComparer.VerifyXmlEquals(xDocument1, xDocument2);
            });

            "传入值不相同的XML内容,抛出异常".Test(() =>
            {
                var xmlString1 = @"<Foo>Foo</Foo>";
                var xDocument1 = XDocument.Parse(xmlString1);
                var xmlString2 = @"<Foo>F1</Foo>";
                var xDocument2 = XDocument.Parse(xmlString2);

                Assert.ThrowsException <ElementNotMatchException>(() =>
                {
                    XmlComparer.VerifyXmlEquals(xDocument1, xDocument2);
                });
            });

            "传入两个完全相同的XML内容,没有抛出不匹配异常".Test(() =>
            {
                var xmlString  = @"<Foo>Foo</Foo>";
                var xDocument1 = XDocument.Parse(xmlString);
                var xDocument2 = XDocument.Parse(xmlString);

                XmlComparer.VerifyXmlEquals(xDocument1, xDocument2);
            });
        }
Пример #28
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);
        }
Пример #29
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);
            }
        }