コード例 #1
0
        /// <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));
        }
コード例 #2
0
        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;
            }
        }
コード例 #3
0
 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;
 }
コード例 #4
0
 /// <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);
 }
コード例 #5
0
 /// <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);
 }
コード例 #6
0
 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;
 }
コード例 #7
0
 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;
 }
コード例 #8
0
 /// <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&lt;T&gt; 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 &amp; 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));
 }
コード例 #9
0
 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);
 }
コード例 #10
0
        /// <summary>Returns a cached <see cref="XNameComparer"/></summary>
        public static XNameComparer GetFor(XmlAssertOptions options)
        {
            if (options.HasFlag(XmlAssertOptions.IgnoreNamespaces))
            {
                return(IgnoringNamespaces);
            }

            return(Default);
        }
コード例 #11
0
 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);
 }
コード例 #12
0
        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);
            }
        }
コード例 #13
0
        /// <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&lt;T&gt; 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 &amp; 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));
        }
コード例 #14
0
        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);
        }
コード例 #15
0
        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);
            }
        }
コード例 #16
0
        /// <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);
        }
コード例 #17
0
 internal static bool IsSet(this XmlAssertOptions options, XmlAssertOptions flag)
 {
     return (options & flag) != 0;
 }
コード例 #18
0
 /// <summary>Gets an <see cref="XAttributeComparer"/> from cache.</summary>
 public static XAttributeComparer GetFor(XmlAssertOptions options)
 {
     return(Cache.GetOrAdd(options, x => new XAttributeComparer(x)));
 }
コード例 #19
0
 public XAttributeComparer(XmlAssertOptions options)
 {
     this.options      = options;
     this.nameComparer = XNameComparer.GetFor(this.options);
 }
コード例 #20
0
 /// <summary>Gets a cached instance.</summary>
 public static XElementComparer GetFor(XmlAssertOptions options)
 {
     return(Cache.GetOrAdd(options, x => new XElementComparer(x)));
 }
コード例 #21
0
 public XElementComparer(XmlAssertOptions options)
 {
     this.options      = options;
     this.nameComparer = XNameComparer.GetFor(this.options);
 }
コード例 #22
0
 /// <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);
 }
コード例 #23
0
 private XNameComparer(XmlAssertOptions options)
 {
     this.options = options;
 }
コード例 #24
0
 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);
     }
 }
コード例 #25
0
 internal static bool IsAnySet(this XmlAssertOptions options, XmlAssertOptions flag1, XmlAssertOptions flag2)
 {
     return(IsSet(options, flag1) || IsSet(options, flag2));
 }
コード例 #26
0
 internal static bool IsSet(this XmlAssertOptions options, XmlAssertOptions flag)
 {
     return((options & flag) != 0);
 }
コード例 #27
0
 internal static bool IsAnySet(this XmlAssertOptions options, XmlAssertOptions flag1, XmlAssertOptions flag2)
 {
     return IsSet(options, flag1) || IsSet(options, flag2);
 }
コード例 #28
0
 /// <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&lt;T&gt; 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 &amp; 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));
 }