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;
            }
        }
        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);
            }
        }
Example #3
0
        private IReadOnlyList <XElementAndSource> GetAllElements()
        {
            var elementAndSources = this.Element.Elements().Select(x => new XElementAndSource(this.SourceXml, x, this.Options));

            if (this.Options.IsSet(XmlAssertOptions.IgnoreElementOrder))
            {
                var nameComparer = XNameComparer.GetFor(this.Options);
                return(elementAndSources.OrderBy(x => x.Element.Name, nameComparer)
                       .ToList());
            }

            return(elementAndSources.ToList());
        }
Example #4
0
        private IReadOnlyList <XAttributeAndSource> GetAllAttributes()
        {
            var attributeAndSources = this.Element.Attributes()
                                      .Where(x => !(this.Options.HasFlag(XmlAssertOptions.IgnoreNamespaces) && x.IsNamespaceDeclaration))
                                      .Select(x => new XAttributeAndSource(this.SourceXml, x, this.Options));

            if (this.Options.IsSet(XmlAssertOptions.IgnoreAttributeOrder))
            {
                var nameComparer = XNameComparer.GetFor(this.Options);
                return(attributeAndSources.OrderBy(x => x.Attribute.Name, nameComparer)
                       .ToList());
            }

            return(attributeAndSources.ToList());
        }
 public XAttributeComparer(XmlAssertOptions options)
 {
     this.options      = options;
     this.nameComparer = XNameComparer.GetFor(this.options);
 }
 public XElementComparer(XmlAssertOptions options)
 {
     this.options      = options;
     this.nameComparer = XNameComparer.GetFor(this.options);
 }