diff --git a/test/Microsoft.AspNet.Mvc.Xml.Test/XmlAssert.cs b/test/Microsoft.AspNet.Mvc.Xml.Test/XmlAssert.cs
index d22e336a42..0fe52db4fe 100644
--- a/test/Microsoft.AspNet.Mvc.Xml.Test/XmlAssert.cs
+++ b/test/Microsoft.AspNet.Mvc.Xml.Test/XmlAssert.cs
@@ -2,8 +2,12 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
+using System.IO;
using System.Linq;
+using System.Text;
+using System.Xml;
using System.Xml.Linq;
+using Xunit;
using Xunit.Sdk;
namespace Microsoft.AspNet.Mvc.Xml
@@ -20,30 +24,59 @@ namespace Microsoft.AspNet.Mvc.Xml
/// Actual xml string.
public static void Equal(string expectedXml, string actualXml)
{
- var sortedExpectedXDoc = SortAttributes(XDocument.Parse(expectedXml));
- var sortedActualXDoc = SortAttributes(XDocument.Parse(actualXml));
+ var sortedExpectedXDocument = SortAttributes(XDocument.Parse(expectedXml));
+ var sortedActualXDocument = SortAttributes(XDocument.Parse(actualXml));
- bool areEqual = XNode.DeepEquals(sortedExpectedXDoc, sortedActualXDoc);
+ // Since XNode's DeepEquals does not check for presence of xml declaration,
+ // check it explicitly
+ bool areEqual = EqualDeclarations(sortedExpectedXDocument.Declaration, sortedActualXDocument.Declaration);
+
+ areEqual = areEqual && XNode.DeepEquals(sortedExpectedXDocument, sortedActualXDocument);
if (!areEqual)
{
- throw new EqualException(sortedExpectedXDoc, sortedActualXDoc);
+ throw new EqualException(
+ sortedExpectedXDocument.ToString(SaveOptions.DisableFormatting),
+ sortedActualXDocument.ToString(SaveOptions.DisableFormatting));
}
}
- private static XDocument SortAttributes(XDocument doc)
+ private static bool EqualDeclarations(XDeclaration expected, XDeclaration actual)
{
- return new XDocument(
- doc.Declaration,
- SortAttributes(doc.Root));
+ if (expected == null && actual == null)
+ {
+ return true;
+ }
+
+ if (expected == null || actual == null)
+ {
+ return false;
+ }
+
+ // Note that this ignores 'Standalone' property comparison.
+ return string.Equals(expected.Version, actual.Version, StringComparison.OrdinalIgnoreCase)
+ && string.Equals(expected.Encoding, actual.Encoding, StringComparison.OrdinalIgnoreCase);
}
- private static XElement SortAttributes(XElement element)
+ private static XDocument SortAttributes(XDocument document)
{
+ return new XDocument(
+ document.Declaration,
+ SortAttributes(document.Root));
+ }
+
+ private static XNode SortAttributes(XNode node)
+ {
+ XElement element = node as XElement;
+ if (element == null)
+ {
+ return node;
+ }
+
return new XElement(
- element.Name,
- element.Attributes().OrderBy(a => a.Name.ToString()),
- element.Elements().Select(child => SortAttributes(child)));
+ element.Name,
+ element.Attributes().OrderBy(a => a.Name.ToString()),
+ element.Nodes().Select(child => SortAttributes(child)));
}
}
}
\ No newline at end of file
diff --git a/test/Microsoft.AspNet.Mvc.Xml.Test/XmlAssertTest.cs b/test/Microsoft.AspNet.Mvc.Xml.Test/XmlAssertTest.cs
new file mode 100644
index 0000000000..6996ca49e0
--- /dev/null
+++ b/test/Microsoft.AspNet.Mvc.Xml.Test/XmlAssertTest.cs
@@ -0,0 +1,97 @@
+// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System;
+using Xunit;
+using Xunit.Sdk;
+
+namespace Microsoft.AspNet.Mvc.Xml
+{
+ public class XmlAssertTest
+ {
+ [Theory]
+ [InlineData("hello", "hey")]
+ [InlineData("hello world", "hello world!!")]
+ [InlineData("helloworld", "helloworld")]
+ [InlineData("helloworld", "helloworld")]
+ [InlineData("helloworldhello", "helloworldgoodbye")]
+ [InlineData("helloworld", "hellogoodbye")]
+ public void Throws_WithMismatchedTextNodes(string input1, string input2)
+ {
+ var equalException = Assert.Throws(() => XmlAssert.Equal(input1, input2));
+ Assert.Equal(input1, equalException.Expected);
+ Assert.Equal(input2, equalException.Actual);
+ }
+
+ [Theory]
+ [InlineData("helloworldhello", "helloworldhello")]
+ [InlineData("hellohello", "hellohello")]
+ [InlineData(
+ "helloworldhello",
+ "helloworldhello")]
+ public void ReturnsSuccessfully_WithMatchingTextNodes(string input1, string input2)
+ {
+ XmlAssert.Equal(input1, input2);
+ }
+
+ [Theory]
+ [InlineData("", "")]
+ [InlineData("", "")]
+ [InlineData("", "")]
+ [InlineData("]]>", "]]>")]
+ public void ReturnsSuccessfully_WithEmptyElements(string input1, string input2)
+ {
+ XmlAssert.Equal(input1, input2);
+ }
+
+ [Theory]
+ [InlineData("",
+ "")]
+ [InlineData("",
+ "")]
+ public void Throws_WithMismatchedXmlDeclaration(string input1, string input2)
+ {
+ Assert.Throws(() => XmlAssert.Equal(input1, input2));
+ }
+
+ [Fact]
+ public void ReturnsSuccessfully_WithMatchingXmlDeclaration_IgnoringCase()
+ {
+ // Arrange
+ var input1 = "" +
+ "hello world";
+ var input2 = "" +
+ "hello world";
+
+ // Act and Assert
+ XmlAssert.Equal(input1, input2);
+ }
+
+ [Theory]
+ [InlineData(
+ "",
+ "")]
+ [InlineData(
+ "hello world",
+ "hello world")]
+ public void ReturnsSuccessfully_WithMatchingContent_IgnoringAttributeOrder(string input1, string input2)
+ {
+ XmlAssert.Equal(input1, input2);
+ }
+
+ [Fact]
+ public void Throws_WithMismatchedAttributeValues_ReorderingAttributes()
+ {
+ // Arrange
+ var expected = "hellohello worldhi";
+ var actual = "hellohello worldhi";
+ var exceptionMessageForExpected = "hellohello worldhi";
+ var exceptionMessageForActual = "hellohello worldhi";
+
+ // Act and Assert
+ var equalException = Assert.Throws(() => XmlAssert.Equal(expected, actual));
+ Assert.Equal(exceptionMessageForExpected, equalException.Expected);
+ Assert.Equal(exceptionMessageForActual, equalException.Actual);
+ }
+ }
+}
\ No newline at end of file