/// <summary> /// 1 Serializes <paramref name="actual"/> to an xml string using <see cref="DataContractSerializer"/> /// 2 Compares the xml with <paramref name="expectedXml"/> /// 3 Creates a ContainerClass{T} this is to catch errors in ReadEndElement() when implementing <see cref="System.Xml.Serialization.IXmlSerializable"/> /// 4 Serializes it to xml. /// 5 Compares the xml /// 6 Deserializes it to container class /// 7 Does 2 and 3 again, we repeat this to catch any errors from deserializing /// 8 Returns roundtripped instance /// </summary> /// <typeparam name="T">The type</typeparam> /// <param name="expectedXml">The expected xml</param> /// <param name="actual">The actual item</param> /// <param name="options">How to compare the xml</param> /// <returns>The roundtripped instance</returns> public static T Equal <T>(string expectedXml, T actual, XmlAssertOptions options = XmlAssertOptions.Verbatim) { var actualXml = ToXml(actual, nameof(actual)); XmlAssert.Equal(expectedXml, actualXml, options); return(Roundtrip(actual)); }
private static void CheckOrder <T>( IReadOnlyList <T> expecteds, IReadOnlyList <T> actuals, Func <T, XName> nameGetter, string errorMessage, XmlAssertOptions options) where T : IXAndSource { if (expecteds == null || actuals == null) { return; } var nameComparer = XNameComparer.GetFor(options); var actualIndex = 0; foreach (var expected in expecteds) { var indexOf = actuals.IndexOf(expected, nameGetter, actualIndex, nameComparer); if (indexOf < 0) { continue; } if (actualIndex > indexOf) { var actual = actuals[indexOf]; var message = CreateMessage(expected, actual, errorMessage); throw new AssertException(message); } actualIndex = indexOf; } }
public XAttributeAndSource(string sourceXml, XAttribute attribute, XmlAssertOptions options) { Ensure.NotNull(attribute, nameof(attribute)); Ensure.NotNullOrEmpty(sourceXml, nameof(sourceXml)); this.SourceXml = sourceXml; this.Attribute = attribute; this.Options = options; }
/// <summary> /// Parses the xml and compares expected to actual. /// </summary> /// <param name="expected">The expected xml</param> /// <param name="actual">The actual xml</param> /// <param name="attributeComparer">Additional comparer used for comparing attributes</param> /// <param name="options">How to compare the xml</param> public static void Equal( string expected, string actual, IEqualityComparer <XAttribute> attributeComparer, XmlAssertOptions options = XmlAssertOptions.Verbatim) { Equal(expected, actual, null, attributeComparer, options); }
/// <summary> /// Parses the xml and compares expected to actual. /// </summary> /// <param name="expected">The expected xml</param> /// <param name="actual">The actual xml</param> /// <param name="elementComparer">Additional comparer used for comparing leaf elements</param> /// <param name="options">How to compare the xml</param> public static void Equal( string expected, string actual, IEqualityComparer <XElement> elementComparer, XmlAssertOptions options = XmlAssertOptions.Verbatim) { Equal(expected, actual, elementComparer, null, options); }
public XElementAndSource(string sourceXml, XElement element, XmlAssertOptions options) { Ensure.NotNull(element, nameof(element)); Ensure.NotNullOrEmpty(sourceXml, nameof(sourceXml)); this.SourceXml = sourceXml; this.Element = element; this.Options = options; }
/// <summary> /// 1 Serializes <paramref name="actual"/> to an xml string using <see cref="XmlSerializer"/> /// 2 Compares the xml with <paramref name="expectedXml"/> /// 3 Creates a ContainerClass<T> this is to catch errors in ReadEndElement() when implementing <see cref="IXmlSerializable"/> /// 4 Serializes it to xml. /// 5 Compares the xml /// 6 Deserializes it to container class /// 7 Does 2 & 3 again, we repeat this to catch any errors from deserializing /// 8 Returns roundtripped instance /// </summary> /// <typeparam name="T">The type</typeparam> /// <param name="expectedXml">The expected xml</param> /// <param name="actual">The actual item</param> /// <param name="elementComparer"> /// Custom element comparer /// This comparer is used first, then the default comparer is used as fallback. /// </param> /// <param name="options">How to compare the xml</param> /// <returns>The roundtripped instance</returns> public static T Equal <T>( string expectedXml, T actual, IEqualityComparer <XElement> elementComparer, XmlAssertOptions options = XmlAssertOptions.Verbatim) { return(Equal(expectedXml, actual, elementComparer, null, options)); }
public XDocumentAndSource(string sourceXml, XDocument document, XmlAssertOptions options) { Ensure.NotNull(document, nameof(document)); Ensure.NotNullOrEmpty(sourceXml, nameof(sourceXml)); this.SourceXml = sourceXml; this.Document = document; this.Options = options; this.Element = new XElementAndSource(sourceXml, document.Root, options); }
/// <summary>Returns a cached <see cref="XNameComparer"/></summary> public static XNameComparer GetFor(XmlAssertOptions options) { if (options.HasFlag(XmlAssertOptions.IgnoreNamespaces)) { return(IgnoringNamespaces); } return(Default); }
private static void Equal( XElementAndSource expected, XElementAndSource actual, IEqualityComparer <XElement> customElementComparer, IEqualityComparer <XAttribute> customAttributeComparer, XmlAssertOptions options) { CheckAttributes(expected, actual, customAttributeComparer, options); if ((expected?.AllElements.Count ?? 0) == 0 && (actual?.AllElements.Count ?? 0) == 0) { if (XElementComparer.GetFor(options).Equals(expected?.Element, actual?.Element)) { return; } if (customElementComparer?.Equals(expected?.Element, actual?.Element) == true) { return; } var message = CreateMessage(expected, actual); throw new AssertException(message); } var nameComparer = XNameComparer.GetFor(options); if (!nameComparer.Equals(expected?.Element.Name, actual?.Element.Name)) { var message = CreateMessage(expected, actual); throw new AssertException(message); } if (!options.IsSet(XmlAssertOptions.IgnoreElementOrder)) { var expectedElements = expected?.AllElements; var actualElements = actual?.AllElements; CheckOrder(expectedElements, actualElements, x => x.Element.Name, " The order of elements is incorrect.", options); } var expectedElementsToCheck = expected?.ElementsToCheck; var actualElementsToCheck = actual?.ElementsToCheck; for (int i = 0; i < Math.Max(expectedElementsToCheck?.Count ?? 0, actualElementsToCheck?.Count ?? 0); i++) { var expectedChild = expectedElementsToCheck.ElementAtOrDefault(i); var actualChild = actualElementsToCheck.ElementAtOrDefault(i); Equal(expectedChild, actualChild, customElementComparer, customAttributeComparer, options); } }
/// <summary> /// 1 Serializes <paramref name="actual"/> to an xml string using <see cref="XmlSerializer"/> /// 2 Compares the xml with <paramref name="expectedXml"/> /// 3 Creates a ContainerClass<T> this is to catch errors in ReadEndElement() when implementing <see cref="IXmlSerializable"/> /// 4 Serializes it to xml. /// 5 Compares the xml /// 6 Deserializes it to container class /// 7 Does 2 & 3 again, we repeat this to catch any errors from deserializing /// 8 Returns roundtripped instance /// </summary> /// <typeparam name="T">The type</typeparam> /// <param name="expectedXml">The expected xml</param> /// <param name="actual">The actual item</param> /// <param name="attributeComparer"> /// Custom attribute comparer /// This comparer is used first, then the default comparer is used as fallback. /// </param> /// <param name="options">How to compare the xml</param> /// <returns>The roundtripped instance</returns> public static T Equal <T>( string expectedXml, T actual, IEqualityComparer <XAttribute> attributeComparer, XmlAssertOptions options = XmlAssertOptions.Verbatim) { var actualXml = ToXml(actual, nameof(actual)); XmlAssert.Equal(expectedXml, actualXml, null, attributeComparer, options); return(Roundtrip(actual)); }
private static void Equal( XDocumentAndSource expected, XDocumentAndSource actual, IEqualityComparer <XElement> elementComparer, IEqualityComparer <XAttribute> attributeComparer, XmlAssertOptions options) { if (!options.IsSet(XmlAssertOptions.IgnoreDeclaration)) { if (!XDeclarationComparer.Default.Equals(expected.Document.Declaration, actual.Document.Declaration)) { var message = CreateMessage(expected, actual); throw new AssertException(message); } } Equal(expected.Element, actual.Element, elementComparer, attributeComparer, options); }
private static void CheckAttributes( XElementAndSource expectedElement, XElementAndSource actualElement, IEqualityComparer <XAttribute> customAttributeComparer, XmlAssertOptions options) { if (!options.IsSet(XmlAssertOptions.IgnoreAttributeOrder)) { var expectedAttributes = expectedElement?.AllAttributes; var actualAttributes = actualElement?.AllAttributes; CheckOrder(expectedAttributes, actualAttributes, x => x.Attribute.Name, " The order of attributes is incorrect.", options); } var expectedAttributesToCheck = expectedElement?.AttributesToCheck; var actualAttributesToCheck = actualElement?.AttributesToCheck; var defaultAttributeComparer = XAttributeComparer.GetFor(options); for (int i = 0; i < Math.Max(expectedAttributesToCheck?.Count ?? 0, actualAttributesToCheck?.Count ?? 0); i++) { var expectedAttribute = expectedAttributesToCheck.ElementAtOrDefault(i); var actualAttribute = actualAttributesToCheck.ElementAtOrDefault(i); if (defaultAttributeComparer.Equals(expectedAttribute?.Attribute, actualAttribute?.Attribute)) { continue; } if (customAttributeComparer?.Equals(expectedAttribute?.Attribute, actualAttribute?.Attribute) == true) { continue; } var message = expectedAttribute == null || actualAttribute == null ? CreateMessage(expectedElement, actualElement) : CreateMessage(expectedAttribute, actualAttribute); throw new AssertException(message); } }
/// <summary> /// Parses the xml and compares expected to actual. /// </summary> /// <param name="expected">The expected xml</param> /// <param name="actual">The actual xml</param> /// <param name="elementComparer">Additional comparer used for comparing leaf elements</param> /// <param name="attributeComparer">Additional comparer used for comparing attributes</param> /// <param name="options">How to compare the xml</param> public static void Equal( string expected, string actual, IEqualityComparer <XElement> elementComparer, IEqualityComparer <XAttribute> attributeComparer, XmlAssertOptions options = XmlAssertOptions.Verbatim) { var expectedXml = ParseDocument(expected, nameof(expected), options); // we want to parse first to assert that it is valid xml if (expected == actual) { return; } var actualXml = ParseDocument(actual, nameof(actual), options); Equal(expectedXml, actualXml, elementComparer, attributeComparer, options); }
internal static bool IsSet(this XmlAssertOptions options, XmlAssertOptions flag) { return (options & flag) != 0; }
/// <summary>Gets an <see cref="XAttributeComparer"/> from cache.</summary> public static XAttributeComparer GetFor(XmlAssertOptions options) { return(Cache.GetOrAdd(options, x => new XAttributeComparer(x))); }
public XAttributeComparer(XmlAssertOptions options) { this.options = options; this.nameComparer = XNameComparer.GetFor(this.options); }
/// <summary>Gets a cached instance.</summary> public static XElementComparer GetFor(XmlAssertOptions options) { return(Cache.GetOrAdd(options, x => new XElementComparer(x))); }
public XElementComparer(XmlAssertOptions options) { this.options = options; this.nameComparer = XNameComparer.GetFor(this.options); }
/// <summary> /// Parses the xml and compares expected to actual. /// </summary> /// <param name="expected">The expected xml</param> /// <param name="actual">The actual xml</param> /// <param name="options">How to compare the xml</param> public static void Equal(string expected, string actual, XmlAssertOptions options = XmlAssertOptions.Verbatim) { Equal(expected, actual, null, null, options); }
private XNameComparer(XmlAssertOptions options) { this.options = options; }
private static XDocumentAndSource ParseDocument(string xml, string parameterName, XmlAssertOptions options) { try { return(new XDocumentAndSource(xml, XDocument.Parse(xml, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo), options)); } catch (Exception e) { throw AssertException.CreateFromException($"{parameterName} is not valid xml.", e); } }
internal static bool IsAnySet(this XmlAssertOptions options, XmlAssertOptions flag1, XmlAssertOptions flag2) { return(IsSet(options, flag1) || IsSet(options, flag2)); }
internal static bool IsSet(this XmlAssertOptions options, XmlAssertOptions flag) { return((options & flag) != 0); }
internal static bool IsAnySet(this XmlAssertOptions options, XmlAssertOptions flag1, XmlAssertOptions flag2) { return IsSet(options, flag1) || IsSet(options, flag2); }
/// <summary> /// 1 Serializes <paramref name="actual"/> to an xml string using <see cref="XmlSerializer"/> /// 2 Compares the xml with <paramref name="expectedXml"/> /// 3 Creates a ContainerClass<T> this is to catch errors in ReadEndElement() when implementing <see cref="IXmlSerializable"/> /// 4 Serializes it to xml. /// 5 Compares the xml /// 6 Deserializes it to container class /// 7 Does 2 & 3 again, we repeat this to catch any errors from deserializing /// 8 Returns roundtripped instance /// </summary> /// <typeparam name="T">The type</typeparam> /// <param name="expectedXml">The expected xml</param> /// <param name="actual">The actual item</param> /// <param name="options">How to compare the xml</param> /// <returns>The roundtripped instance</returns> public static T Equal <T>(string expectedXml, T actual, XmlAssertOptions options = XmlAssertOptions.Verbatim) { return(Equal(expectedXml, actual, null, null, options)); }