Fixed XmlAssert and added tests
This commit is contained in:
parent
6e845f0171
commit
3dc3d280d5
|
|
@ -2,8 +2,12 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Xml;
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
|
using Xunit;
|
||||||
using Xunit.Sdk;
|
using Xunit.Sdk;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc.Xml
|
namespace Microsoft.AspNet.Mvc.Xml
|
||||||
|
|
@ -20,30 +24,59 @@ namespace Microsoft.AspNet.Mvc.Xml
|
||||||
/// <param name="actualXml">Actual xml string.</param>
|
/// <param name="actualXml">Actual xml string.</param>
|
||||||
public static void Equal(string expectedXml, string actualXml)
|
public static void Equal(string expectedXml, string actualXml)
|
||||||
{
|
{
|
||||||
var sortedExpectedXDoc = SortAttributes(XDocument.Parse(expectedXml));
|
var sortedExpectedXDocument = SortAttributes(XDocument.Parse(expectedXml));
|
||||||
var sortedActualXDoc = SortAttributes(XDocument.Parse(actualXml));
|
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)
|
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(
|
if (expected == null && actual == null)
|
||||||
doc.Declaration,
|
{
|
||||||
SortAttributes(doc.Root));
|
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(
|
return new XElement(
|
||||||
element.Name,
|
element.Name,
|
||||||
element.Attributes().OrderBy(a => a.Name.ToString()),
|
element.Attributes().OrderBy(a => a.Name.ToString()),
|
||||||
element.Elements().Select(child => SortAttributes(child)));
|
element.Nodes().Select(child => SortAttributes(child)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -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("<A>hello</A>", "<A>hey</A>")]
|
||||||
|
[InlineData("<A><B>hello world</B></A>", "<A><B>hello world!!</B></A>")]
|
||||||
|
[InlineData("<a><b>hello</b><b>world</b></a>", "<a><b>hello</b><c>world</c></a>")]
|
||||||
|
[InlineData("<a><b>hello</b><b>world</b></a>", "<a><b>hello</b><b attribute=\"value\">world</b></a>")]
|
||||||
|
[InlineData("<a>hello<b>world</b>hello</a>", "<a>hello<b>world</b>goodbye</a>")]
|
||||||
|
[InlineData("<a><b>hello</b><b>world</b></a>", "<a><b>hello</b><b>goodbye</b></a>")]
|
||||||
|
public void Throws_WithMismatchedTextNodes(string input1, string input2)
|
||||||
|
{
|
||||||
|
var equalException = Assert.Throws<EqualException>(() => XmlAssert.Equal(input1, input2));
|
||||||
|
Assert.Equal(input1, equalException.Expected);
|
||||||
|
Assert.Equal(input2, equalException.Actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("<a>hello<b>world</b>hello</a>", "<a>hello<b>world</b>hello</a>")]
|
||||||
|
[InlineData("<a>hello<b/>hello</a>", "<a>hello<b/>hello</a>")]
|
||||||
|
[InlineData(
|
||||||
|
"<a>hello<b color=\"red\" siz=\"medium\">world</b>hello</a>",
|
||||||
|
"<a>hello<b siz=\"medium\" color=\"red\">world</b>hello</a>")]
|
||||||
|
public void ReturnsSuccessfully_WithMatchingTextNodes(string input1, string input2)
|
||||||
|
{
|
||||||
|
XmlAssert.Equal(input1, input2);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("<A></A>", "<A></A>")]
|
||||||
|
[InlineData("<A><!-- comment1 --><B></B></A>", "<A><!-- comment1 --><B></B></A>")]
|
||||||
|
[InlineData("<A/>", "<A/>")]
|
||||||
|
[InlineData("<A><![CDATA[<greeting></greeting>]]></A>", "<A><![CDATA[<greeting></greeting>]]></A>")]
|
||||||
|
public void ReturnsSuccessfully_WithEmptyElements(string input1, string input2)
|
||||||
|
{
|
||||||
|
XmlAssert.Equal(input1, input2);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("<?xml version=\"1.0\" encoding=\"UTF-8\"?><A></A>",
|
||||||
|
"<A></A>")]
|
||||||
|
[InlineData("<?xml version=\"1.0\" encoding=\"UTF-8\"?><A></A>",
|
||||||
|
"<?xml version=\"1.0\" encoding=\"UTF-16\"?><A></A>")]
|
||||||
|
public void Throws_WithMismatchedXmlDeclaration(string input1, string input2)
|
||||||
|
{
|
||||||
|
Assert.Throws<EqualException>(() => XmlAssert.Equal(input1, input2));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ReturnsSuccessfully_WithMatchingXmlDeclaration_IgnoringCase()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var input1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
|
||||||
|
"<A><B color=\"red\" size=\"medium\">hello world</B></A>";
|
||||||
|
var input2 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
|
||||||
|
"<A><B size=\"medium\" color=\"red\">hello world</B></A>";
|
||||||
|
|
||||||
|
// Act and Assert
|
||||||
|
XmlAssert.Equal(input1, input2);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(
|
||||||
|
"<A color=\"red\" size=\"medium\" />",
|
||||||
|
"<A size=\"medium\" color=\"red\" />")]
|
||||||
|
[InlineData(
|
||||||
|
"<A><B color=\"red\" size=\"medium\">hello world</B></A>",
|
||||||
|
"<A><B size=\"medium\" color=\"red\">hello world</B></A>")]
|
||||||
|
public void ReturnsSuccessfully_WithMatchingContent_IgnoringAttributeOrder(string input1, string input2)
|
||||||
|
{
|
||||||
|
XmlAssert.Equal(input1, input2);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Throws_WithMismatchedAttributeValues_ReorderingAttributes()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var expected = "<A>hello<B color=\"red\" size=\"medium\">hello world</B>hi</A>";
|
||||||
|
var actual = "<A>hello<B size=\"Medium\" color=\"red\">hello world</B>hi</A>";
|
||||||
|
var exceptionMessageForExpected = "<A>hello<B color=\"red\" size=\"medium\">hello world</B>hi</A>";
|
||||||
|
var exceptionMessageForActual = "<A>hello<B color=\"red\" size=\"Medium\">hello world</B>hi</A>";
|
||||||
|
|
||||||
|
// Act and Assert
|
||||||
|
var equalException = Assert.Throws<EqualException>(() => XmlAssert.Equal(expected, actual));
|
||||||
|
Assert.Equal(exceptionMessageForExpected, equalException.Expected);
|
||||||
|
Assert.Equal(exceptionMessageForActual, equalException.Actual);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue