Example #1
0
        protected override EquivalencyResult OnHandle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
        {
            var subject     = comparands.Subject as DataRow;
            var expectation = comparands.Expectation as DataRow;

            if (expectation is null)
            {
                if (subject is not null)
                {
                    AssertionScope.Current.FailWith("Expected {context:DataRow} value to be null, but found {0}", subject);
                }
            }
            else
            {
                if (subject is null)
                {
                    if (comparands.Subject is null)
                    {
                        AssertionScope.Current.FailWith("Expected {context:DataRow} to be non-null, but found null");
                    }
                    else
                    {
                        AssertionScope.Current.FailWith("Expected {context:DataRow} to be of type {0}, but found {1} instead",
                                                        expectation.GetType(), comparands.Subject.GetType());
                    }
                }
                else
                {
                    var dataSetConfig   = context.Options as DataEquivalencyAssertionOptions <DataSet>;
                    var dataTableConfig = context.Options as DataEquivalencyAssertionOptions <DataTable>;
                    var dataRowConfig   = context.Options as DataEquivalencyAssertionOptions <DataRow>;

                    if (dataSetConfig?.AllowMismatchedTypes != true &&
                        dataTableConfig?.AllowMismatchedTypes != true &&
                        dataRowConfig?.AllowMismatchedTypes != true)
                    {
                        AssertionScope.Current
                        .ForCondition(subject.GetType() == expectation.GetType())
                        .FailWith("Expected {context:DataRow} to be of type '{0}'{reason}, but found '{1}'",
                                  expectation.GetType(), subject.GetType());
                    }

                    SelectedDataRowMembers selectedMembers = GetMembersFromExpectation(comparands, context.CurrentNode, context.Options);

                    CompareScalarProperties(subject, expectation, selectedMembers);

                    CompareFieldValues(context, nestedValidator, subject, expectation, dataSetConfig, dataTableConfig,
                                       dataRowConfig);
                }
            }

            return(EquivalencyResult.AssertionCompleted);
        }
Example #2
0
        private static void CompareScalarProperties(DataRow subject, DataRow expectation, SelectedDataRowMembers selectedMembers)
        {
            // Note: The members here are listed in the XML documentation for the DataRow.BeEquivalentTo extension
            // method in DataRowAssertions.cs. If this ever needs to change, keep them in sync.
            if (selectedMembers.HasErrors)
            {
                AssertionScope.Current
                .ForCondition(subject.HasErrors == expectation.HasErrors)
                .FailWith("Expected {context:DataRow} to have HasErrors value of '{0}'{reason}, but found '{1}' instead",
                          expectation.HasErrors, subject.HasErrors);
            }

            if (selectedMembers.RowState)
            {
                AssertionScope.Current
                .ForCondition(subject.RowState == expectation.RowState)
                .FailWith("Expected {context:DataRow} to have RowState value of '{0}'{reason}, but found '{1}' instead",
                          expectation.RowState, subject.RowState);
            }
        }