Example #1
0
        private bool CompareProcessingInstructions(XmlCompareContext context, XProcessingInstruction expectedInstruction, XProcessingInstruction actualInstruction)
        {
            if (!this.IgnoreProcessingInstructions)
            {
                if (expectedInstruction.Target != actualInstruction.Target)
                {
                    var comparison = new XmlComparison(
                        XmlComparisonType.ProcessingInstructionTarget,
                        new XmlComparisonDetails(expectedInstruction, expectedInstruction.Target),
                        new XmlComparisonDetails(actualInstruction, actualInstruction.Target));

                    if (!this.HandleDifference(context, comparison))
                    {
                        return(false);
                    }
                }

                if (expectedInstruction.Data != actualInstruction.Data)
                {
                    var comparison = new XmlComparison(
                        XmlComparisonType.ProcessingInstructionData,
                        new XmlComparisonDetails(expectedInstruction, expectedInstruction.Data),
                        new XmlComparisonDetails(actualInstruction, actualInstruction.Data));

                    if (!this.HandleDifference(context, comparison))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Example #2
0
        private bool CompareSchemaLocationAttribute(
            XmlCompareContext context,
            XAttribute expectedAttribute,
            XAttribute actualAttribute,
            XElement expectedElement,
            XElement actualElement)
        {
            XmlComparison comparison = null;

            if (expectedAttribute != null && actualAttribute != null)
            {
                if (expectedAttribute.Value != actualAttribute.Value)
                {
                    comparison = new XmlComparison(
                        XmlComparisonType.SchemaLocation,
                        new XmlComparisonDetails(expectedElement, expectedAttribute),
                        new XmlComparisonDetails(actualElement, expectedAttribute));
                }
            }
            else if (!object.ReferenceEquals(expectedAttribute, actualAttribute))
            {
                comparison = new XmlComparison(
                    XmlComparisonType.SchemaLocation,
                    new XmlComparisonDetails(expectedElement, expectedAttribute != null),
                    new XmlComparisonDetails(actualElement, expectedAttribute != null));
            }

            return(comparison == null || this.HandleDifference(context, comparison));
        }
Example #3
0
        public virtual XmlComparisonState Analyze(XmlComparison comparison)
        {
            var state = XmlComparisonState.Different;

            switch (comparison.ComparisonType)
            {
            case XmlComparisonType.Doctype:
            case XmlComparisonType.NodeListSequence:
            case XmlComparisonType.NamespacePrefix:
            case XmlComparisonType.Encoding:
                state = XmlComparisonState.Similar;
                break;

            case XmlComparisonType.NodeType:
                var expectedNode = comparison.ExpectedDetails.Node as XText;
                var actualNode   = comparison.ActualDetails.Node as XText;

                if (expectedNode != null && actualNode != null)
                {
                    var expectedText = XmlComparer.GetNormalizedText(expectedNode.Value);
                    var actualText   = XmlComparer.GetNormalizedText(actualNode.Value);

                    if (expectedText == actualText)
                    {
                        state = XmlComparisonState.Similar;
                    }
                }

                break;
            }

            return(state);
        }
        public virtual string ToString(XmlComparison comparison)
        {
            var builder = new StringBuilder();

            this.Append(builder, comparison);
            return(builder.ToString());
        }
 protected virtual void Append(StringBuilder builder, XmlComparison comparison)
 {
     builder.Append(comparison.ComparisonType).Append(": ");
     builder.Append("expected ");
     this.Append(builder, comparison.ExpectedDetails);
     builder.Append(", but was ");
     this.Append(builder, comparison.ActualDetails);
     builder.Append(".");
 }
        public XmlDifference(XmlComparisonState state, XmlComparison difference)
        {
            if (difference == null)
            {
                throw new ArgumentNullException("difference");
            }

            this.state      = state;
            this.difference = difference;
        }
Example #7
0
        private bool CompareElements(XmlCompareContext context, XElement expectedElement, XElement actualElement)
        {
            if (expectedElement.Name != actualElement.Name)
            {
                var comparisonType = XmlComparisonType.ElementName;

                if (expectedElement.Name.LocalName == actualElement.Name.LocalName)
                {
                    comparisonType = XmlComparisonType.NamespaceUri;
                }

                var comparison = new XmlComparison(
                    comparisonType,
                    new XmlComparisonDetails(expectedElement, expectedElement.Name),
                    new XmlComparisonDetails(actualElement, actualElement.Name));

                if (!this.HandleDifference(context, comparison))
                {
                    return(false);
                }

                if (context.State == XmlComparisonState.Different)
                {
                    return(true);
                }
            }

            if (!this.CompareAttributes(context, expectedElement, actualElement))
            {
                return(false);
            }

            if (expectedElement.HasElements)
            {
                return(this.CompareContainers(context, expectedElement, actualElement));
            }

            if (!this.TextEquals(expectedElement.Value, actualElement.Value))
            {
                var comparison = new XmlComparison(
                    XmlComparisonType.TextValue,
                    new XmlComparisonDetails(expectedElement, expectedElement.Value),
                    new XmlComparisonDetails(actualElement, actualElement.Value));

                if (!this.HandleDifference(context, comparison))
                {
                    return(false);
                }
            }

            return(true);
        }
Example #8
0
        private bool CompareDocumentTypes(XmlCompareContext context, XDocument expectedDocument, XDocument actualDocument)
        {
            var expectedDocumentType = expectedDocument.DocumentType;
            var actualDocumentType   = actualDocument.DocumentType;

            if (expectedDocumentType == null || actualDocumentType == null)
            {
                if (expectedDocumentType != actualDocumentType)
                {
                    var comparison = new XmlComparison(
                        XmlComparisonType.Doctype,
                        new XmlComparisonDetails(expectedDocument, expectedDocumentType),
                        new XmlComparisonDetails(actualDocument, actualDocumentType));

                    if (!this.HandleDifference(context, comparison))
                    {
                        return(false);
                    }
                }

                return(true);
            }

            if (string.Compare(expectedDocumentType.PublicId, actualDocumentType.PublicId, StringComparison.OrdinalIgnoreCase) != 0)
            {
                var comparison = new XmlComparison(
                    XmlComparisonType.DoctypePublicId,
                    new XmlComparisonDetails(expectedDocument, expectedDocumentType.PublicId),
                    new XmlComparisonDetails(actualDocument, actualDocumentType.PublicId));

                if (!this.HandleDifference(context, comparison))
                {
                    return(false);
                }
            }

            if (string.Compare(expectedDocumentType.SystemId, actualDocumentType.SystemId, StringComparison.OrdinalIgnoreCase) != 0)
            {
                var comparison = new XmlComparison(
                    XmlComparisonType.DoctypePublicId,
                    new XmlComparisonDetails(expectedDocument, expectedDocumentType.SystemId),
                    new XmlComparisonDetails(actualDocument, actualDocumentType.SystemId));

                if (!this.HandleDifference(context, comparison))
                {
                    return(false);
                }
            }

            return(true);
        }
        public XmlComparisonState Analyze(XmlComparison comparison)
        {
            foreach (var info in this.infos)
            {
                if (info.ComparisonType == comparison.ComparisonType)
                {
                    if (info.Condition == null || info.Condition(comparison))
                    {
                        return(info.State);
                    }
                }
            }

            return(this.defaultAnalyzer.Analyze(comparison));
        }
Example #10
0
        private bool HandleDifference(XmlCompareContext context, XmlComparison comparison)
        {
            var state = this.Analyzer.Analyze(comparison);

            context.SetState(state);

            if (state != XmlComparisonState.Equal)
            {
                var item = new XmlDifference(state, comparison);
                context.Differences.Add(item);
                return(this.Handler(context));
            }

            return(true);
        }
Example #11
0
        private bool CompareText(XmlCompareContext context, XText expectedText, XText actualText)
        {
            if (!this.TextEquals(expectedText.Value, actualText.Value))
            {
                var comparison = new XmlComparison(
                    XmlComparisonType.TextValue,
                    new XmlComparisonDetails(expectedText, expectedText),
                    new XmlComparisonDetails(actualText, actualText));

                if (!this.HandleDifference(context, comparison))
                {
                    return(false);
                }
            }

            return(true);
        }
Example #12
0
        private bool CompareComments(XmlCompareContext context, XComment expectedComment, XComment actualComment)
        {
            if (!this.IgnoreComments)
            {
                if (!this.TextEquals(expectedComment.Value, actualComment.Value))
                {
                    var comparison = new XmlComparison(
                        XmlComparisonType.Comment,
                        new XmlComparisonDetails(expectedComment, expectedComment.Value),
                        new XmlComparisonDetails(actualComment, actualComment.Value));

                    if (!this.HandleDifference(context, comparison))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Example #13
0
        private bool CompareNodes(XmlCompareContext context, XNode expectedNode, XNode actualNode)
        {
            if (actualNode.NodeType != expectedNode.NodeType)
            {
                var comparison = new XmlComparison(
                    XmlComparisonType.NodeType,
                    new XmlComparisonDetails(expectedNode, expectedNode.NodeType),
                    new XmlComparisonDetails(actualNode, expectedNode.NodeType));

                if (!this.HandleDifference(context, comparison))
                {
                    return(false);
                }

                return(true);
            }

            switch (actualNode.NodeType)
            {
            case XmlNodeType.Document:
                return(this.CompareDocuments(context, (XDocument)expectedNode, (XDocument)actualNode));

            case XmlNodeType.ProcessingInstruction:
                return(this.CompareProcessingInstructions(context, (XProcessingInstruction)expectedNode, (XProcessingInstruction)actualNode));

            case XmlNodeType.CDATA:
            case XmlNodeType.Text:
                return(this.CompareText(context, (XText)expectedNode, (XText)actualNode));

            case XmlNodeType.Comment:
                return(this.CompareComments(context, (XComment)expectedNode, (XComment)actualNode));

            case XmlNodeType.Element:
                return(this.CompareElements(context, (XElement)expectedNode, (XElement)actualNode));
            }

            throw new XmlCompareException(string.Format("Unexpected node type \"{0}\".", actualNode.NodeType));
        }
Example #14
0
 public XmlComparisonState Analyze(XmlComparison comparison)
 {
     return(this.state);
 }
Example #15
0
        private bool CompareAttributes(XmlCompareContext context, XElement expectedElement, XElement actualElement)
        {
            var expectedAttributes = new Attributes(expectedElement);
            var actualAttributes   = new Attributes(actualElement);

            if (expectedAttributes.Items.Count != actualAttributes.Items.Count)
            {
                var comparison = new XmlComparison(
                    XmlComparisonType.AttributeList,
                    new XmlComparisonDetails(expectedElement, expectedAttributes.Items.Count),
                    new XmlComparisonDetails(actualElement, actualAttributes.Items.Count));

                if (!this.HandleDifference(context, comparison))
                {
                    return(false);
                }
            }

            if (!this.CompareSchemaLocationAttribute(
                    context,
                    expectedAttributes.SchemaLocation,
                    actualAttributes.SchemaLocation,
                    expectedElement,
                    actualElement))
            {
                return(false);
            }

            if (!this.CompareSchemaLocationAttribute(
                    context,
                    expectedAttributes.NoNamespaceSchemaLocation,
                    actualAttributes.NoNamespaceSchemaLocation,
                    expectedElement,
                    actualElement))
            {
                return(false);
            }

            foreach (var expectedAttribute in expectedAttributes.Declarations)
            {
                var actualAttribute = actualAttributes.Declarations.FirstOrDefault(x => x.Name == expectedAttribute.Name);

                if (actualAttribute == null)
                {
                    actualAttribute = actualAttributes.Declarations.FirstOrDefault(x => x.Value == expectedAttribute.Value);

                    if (actualAttribute != null)
                    {
                        var comparison = new XmlComparison(
                            XmlComparisonType.NamespacePrefix,
                            new XmlComparisonDetails(expectedElement, expectedAttribute),
                            new XmlComparisonDetails(actualElement, actualAttribute));

                        if (!this.HandleDifference(context, comparison))
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        var comparison = new XmlComparison(
                            XmlComparisonType.NamespaceUri,
                            new XmlComparisonDetails(expectedElement, expectedAttribute),
                            new XmlComparisonDetails(actualElement, actualAttribute));

                        if (!this.HandleDifference(context, comparison))
                        {
                            return(false);
                        }
                    }
                }
            }

            foreach (var expectedAttribute in expectedAttributes.Items)
            {
                var actualAttribute = actualAttributes.Items.FirstOrDefault(x => x.Name == expectedAttribute.Name);

                if (actualAttribute == null)
                {
                    actualAttribute = actualAttributes.Items.FirstOrDefault(x => x.Name.LocalName == expectedAttribute.Name.LocalName);

                    if (actualAttribute != null)
                    {
                        var comparison = new XmlComparison(
                            XmlComparisonType.NamespaceUri,
                            new XmlComparisonDetails(expectedElement, expectedAttribute),
                            new XmlComparisonDetails(actualElement, actualAttribute));

                        if (!this.HandleDifference(context, comparison))
                        {
                            return(false);
                        }
                    }
                }

                if (actualAttribute == null)
                {
                    var comparison = new XmlComparison(
                        XmlComparisonType.AttributeLookup,
                        new XmlComparisonDetails(expectedElement, expectedAttribute),
                        new XmlComparisonDetails(actualElement, actualAttribute));

                    if (!this.HandleDifference(context, comparison))
                    {
                        return(false);
                    }
                }
                else if (!this.TextEquals(expectedAttribute.Value, actualAttribute.Value))
                {
                    var comparison = new XmlComparison(
                        XmlComparisonType.AttributeValue,
                        new XmlComparisonDetails(expectedElement, expectedAttribute.Value),
                        new XmlComparisonDetails(actualElement, actualAttribute.Value));

                    if (!this.HandleDifference(context, comparison))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Example #16
0
        private bool CompareContainers(XmlCompareContext context, XContainer expectedContainer, XContainer actualContainer)
        {
            var expectedNodes = expectedContainer.Nodes().Where(this.IsAcceptableType).ToList();
            var actualNodes   = actualContainer.Nodes().Where(this.IsAcceptableType).ToList();

            if (expectedNodes.Count != actualNodes.Count)
            {
                var comparison = new XmlComparison(
                    XmlComparisonType.NodeList,
                    new XmlComparisonDetails(expectedContainer, expectedNodes.Count),
                    new XmlComparisonDetails(actualContainer, actualNodes.Count));

                if (!this.HandleDifference(context, comparison))
                {
                    return(false);
                }
            }

            var matches = this.NodeMatcher.MatchNodes(expectedNodes, actualNodes);

            foreach (var match in matches)
            {
                var expectedNode = match.ExpectedNode;
                var actualNode   = match.ActualNode;

                if (actualNode == null)
                {
                    var comparison = new XmlComparison(
                        XmlComparisonType.NodeListLookup,
                        new XmlComparisonDetails(expectedContainer, expectedNode),
                        new XmlComparisonDetails(actualContainer, null));

                    if (!this.HandleDifference(context, comparison))
                    {
                        return(false);
                    }
                }
                else
                {
                    if (expectedNode.NodeType == XmlNodeType.Element &&
                        actualNode.NodeType == XmlNodeType.Element)
                    {
                        var expectedIndex = ((XElement)expectedNode).GetElementIndex();
                        var actualIndex   = ((XElement)actualNode).GetElementIndex();

                        if (expectedIndex != actualIndex)
                        {
                            var comparison = new XmlComparison(
                                XmlComparisonType.NodeListSequence,
                                new XmlComparisonDetails(expectedNode, expectedIndex),
                                new XmlComparisonDetails(actualNode, actualIndex));

                            if (!this.HandleDifference(context, comparison))
                            {
                                return(false);
                            }
                        }
                    }

                    if (!this.CompareNodes(context, expectedNode, actualNode))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Example #17
0
        private bool CompareDeclarations(XmlCompareContext context, XDocument expectedDocument, XDocument actualDocument)
        {
            var expectedDeclaration = expectedDocument.Declaration;
            var actualDeclaration   = actualDocument.Declaration;

            if (expectedDeclaration == null || actualDeclaration == null)
            {
                if (!object.ReferenceEquals(expectedDeclaration, actualDeclaration))
                {
                    var comparison = new XmlComparison(
                        XmlComparisonType.Declaration,
                        new XmlComparisonDetails(expectedDocument, expectedDeclaration != null),
                        new XmlComparisonDetails(actualDocument, actualDeclaration != null));

                    if (!this.HandleDifference(context, comparison))
                    {
                        return(false);
                    }
                }

                return(true);
            }

            if (expectedDeclaration.Encoding != actualDeclaration.Encoding)
            {
                var comparison = new XmlComparison(
                    XmlComparisonType.Encoding,
                    new XmlComparisonDetails(expectedDocument, expectedDeclaration.Encoding),
                    new XmlComparisonDetails(actualDocument, actualDeclaration.Encoding));

                if (!this.HandleDifference(context, comparison))
                {
                    return(false);
                }
            }

            if (expectedDeclaration.Standalone != actualDeclaration.Standalone)
            {
                var comparison = new XmlComparison(
                    XmlComparisonType.Standalone,
                    new XmlComparisonDetails(expectedDocument, expectedDeclaration.Standalone),
                    new XmlComparisonDetails(actualDocument, actualDeclaration.Standalone));

                if (!this.HandleDifference(context, comparison))
                {
                    return(false);
                }
            }

            if (expectedDeclaration.Version != actualDeclaration.Version)
            {
                var comparison = new XmlComparison(
                    XmlComparisonType.Version,
                    new XmlComparisonDetails(expectedDocument, expectedDeclaration.Version),
                    new XmlComparisonDetails(actualDocument, actualDeclaration.Version));

                if (!this.HandleDifference(context, comparison))
                {
                    return(false);
                }
            }

            return(true);
        }