コード例 #1
0
        public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config)
        {
            bool expectationIsNotNull = AssertionScope.Current
                .ForCondition(!ReferenceEquals(context.Expectation, null))
                .FailWith(
                    "Expected {context:subject} to be <null>, but found {0}.",
                    context.Subject);

            bool subjectIsNotNull =
                AssertionScope.Current.ForCondition(
                    !ReferenceEquals(context.Subject, null))
                    .FailWith(
                        "Expected {context:object} to be {0}{reason}, but found {1}.",
                        context.Expectation,
                        context.Subject);

            IEnumerable<SelectedMemberInfo> selectedMembers = GetSelectedMembers(context, config).ToArray();
            if (context.IsRoot && !selectedMembers.Any())
            {
                throw new InvalidOperationException(
                    "No members were found for comparison. " +
                    "Please specify some members to include in the comparison or choose a more meaningful assertion.");
            }

            if (expectationIsNotNull && subjectIsNotNull)
            {
                foreach (var selectedMemberInfo in selectedMembers)
                {
                    AssertMemberEquality(context, parent, selectedMemberInfo, config);
                }
            }
            return true;
        }
コード例 #2
0
 public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent,
     IEquivalencyAssertionOptions config)
 {
     return config.UserEquivalencySteps
         .Where(s => s.CanHandle(context, config))
         .Any(step => step.Handle(context, parent, config));
 }
コード例 #3
0
        /// <summary>
        /// Applies a step as part of the task to compare two objects for structural equality.
        /// </summary>
        /// <value>
        /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
        /// have to be executed. Should return <c>false</c> otherwise.
        /// </value>
        /// <remarks>
        /// May throw when preconditions are not met or if it detects mismatching data.
        /// </remarks>
        public bool Handle(EquivalencyValidationContext context, IEquivalencyValidator parent,
            IEquivalencyAssertionOptions config)
        {
            context.Subject.Should().Be(context.Expectation, context.Reason, context.ReasonArgs);

            return true;
        }
        /// <summary>
        /// Applies a step as part of the task to compare two objects for structural equality.
        /// </summary>
        /// <value>
        /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
        /// have to be executed. Should return <c>false</c> otherwise.
        /// </value>
        /// <remarks>
        /// May throw when preconditions are not met or if it detects mismatching data.
        /// </remarks>
        public bool Handle(EquivalencyValidationContext context, IEquivalencyValidator parent)
        {
            if (context.PropertyInfo != null)
            {
                return context.Config.AssertionRules.Any(rule => rule.AssertEquality(context));
            }

            return false;
        }
 private void AssertPropertyEquality(EquivalencyValidationContext context, IEquivalencyValidator parent,
     PropertyInfo propertyInfo)
 {
     var nestedContext = context.CreateForNestedProperty(propertyInfo);
     if (nestedContext != null)
     {
         parent.AssertEqualityUsing(nestedContext);
     }
 }
        public bool Handle(
            IEquivalencyValidationContext context,
            IEquivalencyValidator parent,
            IEquivalencyAssertionOptions config)
        {
            var equivalencyValidationContext = CreateAdjustedCopy(context);

            return eqivalencyStep.Handle(equivalencyValidationContext, parent, config);
        }
 private void EnumerateElements(EquivalencyValidationContext context, object[] subject, object[] expectation,
     IEquivalencyValidator parent)
 {
     if (!subject.SequenceEqual(expectation))
     {
         for (int i = 0; i < subject.Length; i++)
         {
             parent.AssertEqualityUsing(context.CreateForCollectionItem(i, subject[i], expectation[i]));
         }
     }
 }
コード例 #8
0
 private static void AssertMemberEquality(IEquivalencyValidationContext context, IEquivalencyValidator parent, SelectedMemberInfo selectedMemberInfo, IEquivalencyAssertionOptions config)
 {
     var matchingMember = FindMatchFor(selectedMemberInfo, context, config);
     if (matchingMember != null)
     {
         var nestedContext = context.CreateForNestedMember(selectedMemberInfo, matchingMember);
         if (nestedContext != null)
         {
             parent.AssertEqualityUsing(nestedContext);
         }
     }
 }
コード例 #9
0
 private void AssertPropertyEquality(EquivalencyValidationContext context, IEquivalencyValidator parent, PropertyInfo propertyInfo, IEquivalencyAssertionOptions config)
 {
     var matchingProperty = FindMatchFor(propertyInfo, context, config.MatchingRules);
     if (matchingProperty != null)
     {
         EquivalencyValidationContext nestedContext = context.CreateForNestedProperty(propertyInfo, matchingProperty);
         if (nestedContext != null)
         {
             parent.AssertEqualityUsing(nestedContext);
         }
     }
 }
コード例 #10
0
        /// <summary>
        /// Applies a step as part of the task to compare two objects for structural equality.
        /// </summary>
        /// <value>
        /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
        /// have to be executed. Should return <c>false</c> otherwise.
        /// </value>
        /// <remarks>
        /// May throw when preconditions are not met or if it detects mismatching data.
        /// </remarks>
        public bool Handle(EquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config)
        {
            if (AssertExpectationIsCollection(context.Expectation))
            {
                var validator = new EnumerableEquivalencyValidator(parent, context)
                {
                    Recursive = context.IsRoot || config.IsRecursive,
                    OrderingRules = config.OrderingRules
                };

                validator.Execute(ToArray(context.Subject), ToArray(context.Expectation));
            }

            return true;
        }
        /// <summary>
        /// Applies a step as part of the task to compare two objects for structural equality.
        /// </summary>
        /// <value>
        /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
        /// have to be executed. Should return <c>false</c> otherwise.
        /// </value>
        /// <remarks>
        /// May throw when preconditions are not met or if it detects mismatching data.
        /// </remarks>
        public bool Handle(EquivalencyValidationContext context, IEquivalencyValidator parent)
        {
            IEnumerable<PropertyInfo> selectedProperties = context.SelectedProperties.ToArray();
            if (context.IsRoot && !selectedProperties.Any())
            {
                throw new InvalidOperationException("Please specify some properties to include in the comparison.");
            }

            foreach (PropertyInfo propertyInfo in selectedProperties)
            {
                AssertPropertyEquality(context, parent, propertyInfo);
            }

            return true;
        }
コード例 #12
0
        /// <summary>
        /// Applies a step as part of the task to compare two objects for structural equality.
        /// </summary>
        /// <value>
        /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
        /// have to be executed. Should return <c>false</c> otherwise.
        /// </value>
        /// <remarks>
        /// May throw when preconditions are not met or if it detects mismatching data.
        /// </remarks>
        public bool Handle(EquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config)
        {
            Type subjectType = EnumerableEquivalencyStep.GetSubjectType(context, config);

            Type[] interfaces = GetIEnumerableInterfaces(subjectType);
            bool multipleInterfaces = (interfaces.Count() > 1);

            if (multipleInterfaces)
            {
                IEnumerable<Type> enumerableTypes = interfaces.Select(
                    type => type.GetGenericArguments().Single());

                AssertionScope.Current.FailWith(
                    String.Format(
                        "{{context:Subject}} is enumerable for more than one type.  " +
                        "It is not known which type should be use for equivalence.{0}" +
                        "IEnumerable is implemented for the following types: {1}",
                        Environment.NewLine,
                        String.Join(", ", enumerableTypes)));
            }

            if (AssertExpectationIsCollection(context.Expectation))
            {
                var validator = new EnumerableEquivalencyValidator(parent, context)
                {
                    Recursive = context.IsRoot || config.IsRecursive,
                    OrderingRules = config.OrderingRules
                };

                Type typeOfEnumeration = GetTypeOfEnumeration(subjectType);

                Expression subjectToArray = ToArray(context.Subject, typeOfEnumeration);
                Expression expectationToArray =
                    Expression.Constant(EnumerableEquivalencyStep.ToArray(context.Expectation));

                MethodCallExpression executeExpression = Expression.Call(
                    Expression.Constant(validator),
                    "Execute",
                    new Type[] { typeOfEnumeration },
                    subjectToArray,
                    expectationToArray);

                Expression.Lambda(executeExpression).Compile().DynamicInvoke();
            }

            return true;
        }
コード例 #13
0
        /// <summary>
        /// Applies a step as part of the task to compare two objects for structural equality.
        /// </summary>
        /// <value>
        /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
        /// have to be executed. Should return <c>false</c> otherwise.
        /// </value>
        /// <remarks>
        /// May throw when preconditions are not met or if it detects mismatching data.
        /// </remarks>
        public virtual bool Handle(EquivalencyValidationContext context, IEquivalencyValidator structuralEqualityValidator, IEquivalencyAssertionOptions config)
        {
            if (ReferenceEquals(context.Subject, context.Expectation))
            {
                return true;
            }

            if (ReferenceEquals(context.Expectation, null))
            {
                AssertionScope.Current.FailWith(
                    "Expected {context:subject} to be {0}{reason}, but found {1}.", context.Expectation, context.Subject);

                return true;
            }

            return !ReferenceEquals(context.Subject, null) && context.Subject.Equals(context.Expectation);
        }
コード例 #14
0
        /// <summary>
        /// Applies a step as part of the task to compare two objects for structural equality.
        /// </summary>
        /// <value>
        /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
        /// have to be executed. Should return <c>false</c> otherwise.
        /// </value>
        /// <remarks>
        /// May throw when preconditions are not met or if it detects mismatching data.
        /// </remarks>
        public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator structuralEqualityValidator, IEquivalencyAssertionOptions config)
        {
            if (!ReferenceEquals(context.Expectation, null) && !ReferenceEquals(context.Subject, null)
                && !context.Subject.GetType().IsSameOrInherits(context.Expectation.GetType()))
            {
                Type expectationType = context.Expectation.GetType();

                object convertedSubject;
                if (TryChangeType(context.Subject, expectationType, out convertedSubject))
                {
                    var newContext = context.CreateWithDifferentSubject(convertedSubject, expectationType);

                    structuralEqualityValidator.AssertEqualityUsing(newContext);
                    return true;
                }
            }

            return false;
        }
        /// <summary>
        /// Applies a step as part of the task to compare two objects for structural equality.
        /// </summary>
        /// <value>
        /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
        /// have to be executed. Should return <c>false</c> otherwise.
        /// </value>
        /// <remarks>
        /// May throw when preconditions are not met or if it detects mismatching data.
        /// </remarks>
        public bool Handle(EquivalencyValidationContext context, IEquivalencyValidator structuralEqualityValidator)
        {
            if (!ReferenceEquals(context.Expectation, null) && !ReferenceEquals(context.Subject, null)
                && !context.Subject.GetType().IsSameOrInherits(context.Expectation.GetType()))
            {
                try
                {
                    context.Subject = Convert.ChangeType(context.Subject, context.Expectation.GetType(), CultureInfo.CurrentCulture);
                }
                catch (FormatException)
                {
                }
                catch (InvalidCastException)
                {
                }
            }

            return false;
        }
        /// <summary>
        /// Applies a step as part of the task to compare two objects for structural equality.
        /// </summary>
        /// <value>
        /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
        /// have to be executed. Should return <c>false</c> otherwise.
        /// </value>
        /// <remarks>
        /// May throw when preconditions are not met or if it detects mismatching data.
        /// </remarks>
        public bool Handle(EquivalencyValidationContext context, IEquivalencyValidator parent)
        {
            AssertExpectationIsCollection(context);

            var subject = ((IEnumerable)context.Subject).Cast<object>().ToArray();
            var expectation = ((IEnumerable)context.Expectation).Cast<object>().ToArray();

            AssertCollectionsHaveEqualLength(context, subject, expectation);

            if (context.IsRoot || context.Config.IsRecursive)
            {
                EnumerateElements(context, subject, expectation, parent);
            }
            else
            {
                subject.Should().Equal(expectation, context.Reason, context.ReasonArgs);
            }

            return true;
        }
コード例 #17
0
        /// <summary>
        /// Applies a step as part of the task to compare two objects for structural equality.
        /// </summary>
        /// <value>
        /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
        /// have to be executed. Should return <c>false</c> otherwise.
        /// </value>
        /// <remarks>
        /// May throw when preconditions are not met or if it detects mismatching data.
        /// </remarks>
        public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent,
            IEquivalencyAssertionOptions config)
        {
            switch (config.EnumEquivalencyHandling)
            {
                case EnumEquivalencyHandling.ByValue:
                    CompareByValue(context);
                    break;
                case EnumEquivalencyHandling.ByName:
                    context.Subject.ToString()
                        .Should()
                        .Be(context.Expectation.ToString(), context.Reason, context.ReasonArgs);
                    break;
                default:
                    throw new InvalidOperationException(string.Format("Don't know how to handle {0}",
                        config.EnumEquivalencyHandling));
            }

            return true;
        }
コード例 #18
0
        /// <summary>
        /// Applies a step as part of the task to compare two objects for structural equality.
        /// </summary>
        /// <value>
        /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
        /// have to be executed. Should return <c>false</c> otherwise.
        /// </value>
        /// <remarks>
        /// May throw when preconditions are not met or if it detects mismatching data.
        /// </remarks>
        public virtual bool Handle(EquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config)
        {
            IEnumerable<PropertyInfo> selectedProperties = GetSelectedProperties(context, config).ToArray();
            if (context.IsRoot && !selectedProperties.Any())
            {
                throw new InvalidOperationException("Please specify some properties to include in the comparison.");
            }

            bool expectationIsNotNull = AssertionScope.Current
                .ForCondition(!ReferenceEquals(context.Expectation, null))
                .FailWith("Expected {context:subject} to be <null>, but found {0}.", context.Subject);

            if (expectationIsNotNull)
            {
                foreach (PropertyInfo propertyInfo in selectedProperties)
                {
                    AssertPropertyEquality(context, parent, propertyInfo, config);
                }
            }
            return true;
        }
コード例 #19
0
        /// <summary>
        /// Applies a step as part of the task to compare two objects for structural equality.
        /// </summary>
        /// <value>
        /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
        /// have to be executed. Should return <c>false</c> otherwise.
        /// </value>
        /// <remarks>
        /// May throw when preconditions are not met or if it detects mismatching data.
        /// </remarks>
        public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent,
            IEquivalencyAssertionOptions config)
        {
            Type subjectType = config.GetSubjectType(context);

            var interfaceTypes = GetIEnumerableInterfaces(subjectType)
                .Select(type => "IEnumerable<" + type.GetGenericArguments().Single() + ">")
                .ToList();

            AssertionScope.Current
                .ForCondition(interfaceTypes.Count() == 1)
                .FailWith("{context:Subject} implements {0}, so cannot determine which one " +
                          "to use for asserting the equivalency of the collection. ", interfaceTypes);

            if (AssertExpectationIsCollection(context.Expectation, context.Subject))
            {
                var validator = new EnumerableEquivalencyValidator(parent, context)
                {
                    Recursive = context.IsRoot || config.IsRecursive,
                    OrderingRules = config.OrderingRules
                };

                Type typeOfEnumeration = GetTypeOfEnumeration(subjectType);

                Expression subjectToArray = ToArray(context.Subject, typeOfEnumeration);
                Expression expectationToArray =
                    Expression.Constant(EnumerableEquivalencyStep.ToArray(context.Expectation));

                MethodCallExpression executeExpression = Expression.Call(
                    Expression.Constant(validator),
                    ExpressionExtensions.GetMethodName(() => validator.Execute<object>(null, null)),
                    new[] {typeOfEnumeration},
                    subjectToArray,
                    expectationToArray);

                Expression.Lambda(executeExpression).Compile().DynamicInvoke();
            }

            return true;
        }
コード例 #20
0
        /// <summary>
        /// Applies a step as part of the task to compare two objects for structural equality.
        /// </summary>
        /// <value>
        /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
        /// have to be executed. Should return <c>false</c> otherwise.
        /// </value>
        /// <remarks>
        /// May throw when preconditions are not met or if it detects mismatching data.
        /// </remarks>
        public virtual bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config)
        {
            var subject = (IDictionary)context.Subject;
            var expectation = context.Expectation as IDictionary;

            if (PreconditionsAreMet(context, expectation, subject))
            {
                foreach (object key in subject.Keys)
                {
                    if (config.IsRecursive)
                    {
                        parent.AssertEqualityUsing(context.CreateForDictionaryItem(key, subject[key], expectation[key]));
                    }
                    else
                    {
                        subject[key].Should().Be(expectation[key], context.Reason, context.ReasonArgs);
                    }
                }
            }

            return true;
        }
        /// <summary>
        /// Applies a step as part of the task to compare two objects for structural equality.
        /// </summary>
        /// <value>
        /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
        /// have to be executed. Should return <c>false</c> otherwise.
        /// </value>
        /// <remarks>
        /// May throw when preconditions are not met or if it detects mismatching data.
        /// </remarks>
        public bool Handle(EquivalencyValidationContext context, IEquivalencyValidator structuralEqualityValidator)
        {
            if (ReferenceEquals(context.Subject, context.Expectation))
            {
                return true;
            }

            if (ReferenceEquals(context.Expectation, null))
            {
                string propertyPath = context.PropertyDescription;
                if (propertyPath.Length == 0)
                {
                    propertyPath = "subject";
                }

                context.Verification
                    .FailWith("Expected " + propertyPath + " to be {0}{reason}, but found {1}.", context.Expectation,
                        context.Subject);
            }

            return !ReferenceEquals(context.Subject, null) && context.Subject.Equals(context.Expectation);
        }
コード例 #22
0
        /// <summary>
        /// Applies a step as part of the task to compare two objects for structural equality.
        /// </summary>
        /// <value>
        /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
        /// have to be executed. Should return <c>false</c> otherwise.
        /// </value>
        /// <remarks>
        /// May throw when preconditions are not met or if it detects mismatching data.
        /// </remarks>
        public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent,
            IEquivalencyAssertionOptions config)
        {
            switch (config.EnumEquivalencyHandling)
            {
                case EnumEquivalencyHandling.ByValue:
                    long subjectsUnderlyingValue = Convert.ToInt64(context.Subject);
                    long expectationsUnderlyingValue = Convert.ToInt64(context.Expectation);

                    subjectsUnderlyingValue.Should().Be(expectationsUnderlyingValue, context.Because, context.BecauseArgs);
                    break;

                case EnumEquivalencyHandling.ByName:
                    context.Subject.ToString().Should().Be(context.Expectation.ToString(), context.Because, context.BecauseArgs);
                    break;

                default:
                    throw new InvalidOperationException(string.Format("Don't know how to handle {0}",
                        config.EnumEquivalencyHandling));
            }

            return true;
        }
        public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent,
            IEquivalencyAssertionOptions config)
        {
            Array subjectAsArray = (Array) context.Subject;

            if (AreComparable(context, subjectAsArray))
            {
                Digit digit = BuildDigitsRepresentingAllIndices(subjectAsArray);

                do
                {
                    var expectation = ((Array) context.Expectation).GetValue(digit.Indices);
                    IEquivalencyValidationContext itemContext = context.CreateForCollectionItem(
                        string.Join(",", digit.Indices),
                        subjectAsArray.GetValue(digit.Indices),
                        expectation);

                    parent.AssertEqualityUsing(itemContext);
                }
                while (digit.Increment());
            }

            return true;
        }
コード例 #24
0
        protected override EquivalencyResult OnHandle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
        {
            var subject     = comparands.Subject as DataTable;
            var expectation = comparands.Expectation as DataTable;

            if (expectation is null)
            {
                if (subject is not null)
                {
                    AssertionScope.Current.FailWith("Expected {context:DataTable} value to be null, but found {0}", subject);
                }
            }
            else
            {
                if (subject is null)
                {
                    if (comparands.Subject is null)
                    {
                        AssertionScope.Current.FailWith("Expected {context:DataTable} to be non-null, but found null");
                    }
                    else
                    {
                        AssertionScope.Current.FailWith("Expected {context:DataTable} 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>;

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

                    var selectedMembers = GetMembersFromExpectation(context.CurrentNode, comparands, context.Options)
                                          .ToDictionary(member => member.Name);

                    CompareScalarProperties(subject, expectation, selectedMembers);

                    CompareCollections(comparands, context, nestedValidator, context.Options, selectedMembers);
                }
            }

            return(EquivalencyResult.AssertionCompleted);
        }
コード例 #25
0
        private static void HandleCore <TSubject, TExpectation>(IMaybe <TSubject> subject, IMaybe <TExpectation> expectation, IEquivalencyValidationContext context, IEquivalencyValidator parent)
        {
            expectation.Match(none, some)();

            void none()
            {
                subject.Match(none: () => { },
                              some: _ => new Action(() => AssertionScope.Current.FailWith("Expected subject to be empty, but it was filled.")))();
            }

            void some(TExpectation e)
            {
                subject.Match(none: () => AssertionScope.Current.FailWith("Expected {context:subject} to be filled, but it was empty."),
                              some: (s) => new Action(() => parent.AssertEqualityUsing(context.CreateForMaybeValue(s, e))))();
            }
        }
コード例 #26
0
 public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
 {
     doAction();
     return(EquivalencyResult.AssertionCompleted);
 }
 EquivalencyResult IEquivalencyStep.Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
 {
     return(comparands.Subject?.GetType()?.GetMethod("<Clone>$") != null ? EquivalencyResult.AssertionCompleted : EquivalencyResult.ContinueWithNext);
 }
コード例 #28
0
 public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config)
 {
     return assertionRule.AssertEquality(context);
 }
コード例 #29
0
        public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
        {
            foreach (IEquivalencyStep step in context.Options.UserEquivalencySteps)
            {
                if (step.Handle(comparands, context, nestedValidator) == EquivalencyResult.AssertionCompleted)
                {
                    return(EquivalencyResult.AssertionCompleted);
                }
            }

            return(EquivalencyResult.ContinueWithNext);
        }
コード例 #30
0
        public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
        {
            if (!comparands.GetExpectedType(context.Options).IsEnum)
            {
                return(EquivalencyResult.ContinueWithNext);
            }

            bool succeeded = Execute.Assertion
                             .ForCondition(comparands.Subject?.GetType().IsEnum == true)
                             .FailWith(() =>
            {
                decimal?expectationsUnderlyingValue = ExtractDecimal(comparands.Expectation);
                string expectationName = GetDisplayNameForEnumComparison(comparands.Expectation, expectationsUnderlyingValue);

                return(new FailReason($"Expected {{context:enum}} to be equivalent to {expectationName}{{reason}}, but found {{0}}.", comparands.Subject));
            });

            if (succeeded)
            {
                switch (context.Options.EnumEquivalencyHandling)
                {
                case EnumEquivalencyHandling.ByValue:
                    HandleByValue(comparands);
                    break;

                case EnumEquivalencyHandling.ByName:
                    HandleByName(comparands);
                    break;

                default:
                    throw new InvalidOperationException($"Do not know how to handle {context.Options.EnumEquivalencyHandling}");
                }
            }

            return(EquivalencyResult.AssertionCompleted);
        }
コード例 #31
0
        /// <summary>
        /// Applies a step as part of the task to compare two objects for structural equality.
        /// </summary>
        /// <value>
        /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
        /// have to be executed. Should return <c>false</c> otherwise.
        /// </value>
        /// <remarks>
        /// May throw when preconditions are not met or if it detects mismatching data.
        /// </remarks>
        public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator structuralEqualityValidator, IEquivalencyAssertionOptions config)
        {
            context.Subject.Should().Be(context.Expectation, context.Because, context.BecauseArgs);

            return(true);
        }
コード例 #32
0
 public virtual bool Handle(EquivalencyValidationContext context, IEquivalencyValidator structuralEqualityValidator, IEquivalencyAssertionOptions config)
 {
     context.Subject.Should().NotBeDefault( context.Reason, context.ReasonArgs );
     return true;
 }
コード例 #33
0
 public EnumerableEquivalencyValidator(IEquivalencyValidator parent, EquivalencyValidationContext context)
 {
     this.parent = parent;
     this.context = context;
     Recursive = false;
 }
コード例 #34
0
        private static void AssertMemberEquality(IEquivalencyValidationContext context, IEquivalencyValidator parent, SelectedMemberInfo selectedMemberInfo, IEquivalencyAssertionOptions config)
        {
            SelectedMemberInfo matchingMember = FindMatchFor(selectedMemberInfo, context, config);

            if (matchingMember != null)
            {
                IEquivalencyValidationContext nestedContext =
                    context.CreateForNestedMember(selectedMemberInfo, matchingMember);

                if (nestedContext != null)
                {
                    parent.AssertEqualityUsing(nestedContext);
                }
            }
        }
コード例 #35
0
 /// <summary>
 /// Applies a step as part of the task to compare two objects for structural equality.
 /// </summary>
 /// <value>
 /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
 /// have to be executed. Should return <c>false</c> otherwise.
 /// </value>
 /// <remarks>
 /// May throw when preconditions are not met or if it detects mismatching data.
 /// </remarks>
 public virtual bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator structuralEqualityValidator, IEquivalencyAssertionOptions config)
 {
     return(ReferenceEquals(context.Subject, context.Expectation));
 }
        /// <summary>
        /// Applies a step as part of the task to compare two objects for structural equality.
        /// </summary>
        /// <value>
        /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
        /// have to be executed. Should return <c>false</c> otherwise.
        /// </value>
        /// <remarks>
        /// May throw when preconditions are not met or if it detects mismatching data.
        /// </remarks>
        public bool Handle(EquivalencyValidationContext context, IEquivalencyValidator structuralEqualityValidator)
        {
            context.Subject.Should().Be(context.Expectation, context.Reason, context.ReasonArgs);

            return true;
        }
コード例 #37
0
        public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
        {
            if (!context.Options.IsRecursive && !context.CurrentNode.IsRoot)
            {
                comparands.Subject.Should().Be(comparands.Expectation, context.Reason.FormattedMessage, context.Reason.Arguments);

                return(EquivalencyResult.AssertionCompleted);
            }

            return(EquivalencyResult.ContinueWithNext);
        }
コード例 #38
0
#pragma warning restore SA1110

        public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
        {
            Type expectedType = comparands.GetExpectedType(context.Options);

            if (comparands.Expectation is null || !IsGenericCollection(expectedType))
            {
                return(EquivalencyResult.ContinueWithNext);
            }

            Type[] interfaceTypes = GetIEnumerableInterfaces(expectedType);

            AssertionScope.Current
            .ForCondition(interfaceTypes.Length == 1)
            .FailWith(() => new FailReason("{context:Expectation} implements {0}, so cannot determine which one " +
                                           "to use for asserting the equivalency of the collection. ",
                                           interfaceTypes.Select(type => "IEnumerable<" + type.GetGenericArguments().Single() + ">")));

            if (AssertSubjectIsCollection(comparands.Subject))
            {
                var validator = new EnumerableEquivalencyValidator(nestedValidator, context)
                {
                    Recursive     = context.CurrentNode.IsRoot || context.Options.IsRecursive,
                    OrderingRules = context.Options.OrderingRules
                };

                Type typeOfEnumeration = GetTypeOfEnumeration(expectedType);

                var subjectAsArray = EnumerableEquivalencyStep.ToArray(comparands.Subject);

                try
                {
                    HandleMethod.MakeGenericMethod(typeOfEnumeration).Invoke(null, new[] { validator, subjectAsArray, comparands.Expectation });
                }
                catch (TargetInvocationException e)
                {
                    e.Unwrap().Throw();
                }
            }

            return(EquivalencyResult.AssertionCompleted);
        }
コード例 #39
0
        private static void CompareCollections(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config, Dictionary <string, IMember> selectedMembers)
        {
            // Note: The collections here are listed in the XML documentation for the DataTable.BeEquivalentTo extension
            // method in DataTableAssertions.cs. If this ever needs to change, keep them in sync.
            var collectionNames = new[]
            {
                nameof(DataTable.ChildRelations),
                nameof(DataTable.Columns),
                nameof(DataTable.Constraints),
                nameof(DataTable.ExtendedProperties),
                nameof(DataTable.ParentRelations),
                nameof(DataTable.PrimaryKey),
                nameof(DataTable.Rows),
            };

            foreach (var collectionName in collectionNames)
            {
                if (selectedMembers.TryGetValue(collectionName, out IMember expectationMember))
                {
                    IMember matchingMember = FindMatchFor(expectationMember, comparands.Subject, context.CurrentNode, config);
                    if (matchingMember is not null)
                    {
                        IEquivalencyValidationContext nestedContext = context.AsNestedMember(expectationMember);

                        var nestedComparands = new Comparands
                        {
                            Subject         = matchingMember.GetValue(comparands.Subject),
                            Expectation     = expectationMember.GetValue(comparands.Expectation),
                            CompileTimeType = expectationMember.Type
                        };

                        parent.RecursivelyAssertEquality(nestedComparands, nestedContext);
                    }
                }
            }
        }
コード例 #40
0
        public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config)
        {
            var expected = _dictionary[(string)context.Expectation];

            return(((TEnum)context.Subject).Equals(expected));
        }
コード例 #41
0
        protected override EquivalencyResult OnHandle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
        {
            var subject     = (XElement)comparands.Subject;
            var expectation = (XElement)comparands.Expectation;

            subject.Should().BeEquivalentTo(expectation, context.Reason.FormattedMessage, context.Reason.Arguments);

            return(EquivalencyResult.AssertionCompleted);
        }
コード例 #42
0
 /// <summary>
 /// Applies a step as part of the task to compare two objects for structural equality.
 /// </summary>
 /// <value>
 /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
 /// have to be executed. Should return <c>false</c> otherwise.
 /// </value>
 /// <remarks>
 /// May throw when preconditions are not met or if it detects mismatching data.
 /// </remarks>
 public virtual bool Handle(EquivalencyValidationContext context, IEquivalencyValidator structuralEqualityValidator,
     IEquivalencyAssertionOptions config)
 {
     return ReferenceEquals(context.Subject, context.Expectation);
 }
コード例 #43
0
        /// <summary>
        /// Applies a step as part of the task to compare two objects for structural equality.
        /// </summary>
        /// <value>
        /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
        /// have to be executed. Should return <c>false</c> otherwise.
        /// </value>
        /// <remarks>
        /// May throw when preconditions are not met or if it detects mismatching data.
        /// </remarks>
        public virtual bool Handle(EquivalencyValidationContext context, IEquivalencyValidator structuralEqualityValidator, IEquivalencyAssertionOptions config)
        {
            context.Subject.Should().Be(context.Expectation, context.Reason, context.ReasonArgs);

            return true;
        }
コード例 #44
0
 public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
 {
     return(EquivalencyResult.ContinueWithNext);
 }
コード例 #45
0
 public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config)
 {
     return(assertionRule.AssertEquality(context));
 }
コード例 #46
0
        public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
        {
            Type expectationType = comparands.GetExpectedType(context.Options);

            if (expectationType is null || expectationType != typeof(string))
            {
                return(EquivalencyResult.ContinueWithNext);
            }

            if (!ValidateAgainstNulls(comparands, context.CurrentNode))
            {
                return(EquivalencyResult.AssertionCompleted);
            }

            bool subjectIsString = ValidateSubjectIsString(comparands, context.CurrentNode);

            if (subjectIsString)
            {
                string subject     = (string)comparands.Subject;
                string expectation = (string)comparands.Expectation;

                subject.Should()
                .Be(expectation, context.Reason.FormattedMessage, context.Reason.Arguments);
            }

            return(EquivalencyResult.AssertionCompleted);
        }
コード例 #47
0
        public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
        {
            if (!IsCollection(comparands.GetExpectedType(context.Options)))
            {
                return(EquivalencyResult.ContinueWithNext);
            }

            if (AssertSubjectIsCollection(comparands.Subject))
            {
                var validator = new EnumerableEquivalencyValidator(nestedValidator, context)
                {
                    Recursive     = context.CurrentNode.IsRoot || context.Options.IsRecursive,
                    OrderingRules = context.Options.OrderingRules
                };

                validator.Execute(ToArray(comparands.Subject), ToArray(comparands.Expectation));
            }

            return(EquivalencyResult.AssertionCompleted);
        }
コード例 #48
0
        protected override EquivalencyResult OnHandle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
        {
            if (comparands.Subject is not Constraint)
            {
                AssertionScope.Current
                .FailWith("Expected {context:constraint} to be a value of type Constraint, but found {0}",
                          comparands.Subject.GetType());
            }
            else
            {
                var subject     = (Constraint)comparands.Subject;
                var expectation = (Constraint)comparands.Expectation;

                var selectedMembers = GetMembersFromExpectation(comparands, context.CurrentNode, context.Options)
                                      .ToDictionary(member => member.Name);

                CompareCommonProperties(context, nestedValidator, context.Options, subject, expectation, selectedMembers);

                bool matchingType = subject.GetType() == expectation.GetType();

                AssertionScope.Current
                .ForCondition(matchingType)
                .FailWith("Expected {context:constraint} to be of type {0}, but found {1}", expectation.GetType(),
                          subject.GetType());

                if (matchingType)
                {
                    if ((subject is UniqueConstraint subjectUniqueConstraint) &&
                        (expectation is UniqueConstraint expectationUniqueConstraint))
                    {
                        CompareConstraints(nestedValidator, context, subjectUniqueConstraint, expectationUniqueConstraint,
                                           selectedMembers);
                    }
                    else if ((subject is ForeignKeyConstraint subjectForeignKeyConstraint) &&
                             (expectation is ForeignKeyConstraint expectationForeignKeyConstraint))
                    {
                        CompareConstraints(nestedValidator, context, subjectForeignKeyConstraint, expectationForeignKeyConstraint,
                                           selectedMembers);
                    }
コード例 #49
0
 public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
 {
     throw new TException();
 }
コード例 #50
0
            public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent,
                IEquivalencyAssertionOptions config)
            {
                Execute.Assertion.FailWith(GetType().FullName);

                return true;
            }
コード例 #51
0
 public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
 {
     comparands.Subject.Should().Be(comparands.Expectation, context.Reason.FormattedMessage, context.Reason.Arguments);
     return(EquivalencyResult.AssertionCompleted);
 }
 public override bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config)
 {
     doAction();
     return(true);
 }
 public abstract bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config);
コード例 #54
0
 public EnumerableEquivalencyValidator(IEquivalencyValidator parent, IEquivalencyValidationContext context)
 {
     this.parent  = parent;
     this.context = context;
     Recursive    = false;
 }
 public override bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config)
 {
     throw new TException();
 }
コード例 #56
0
 /// <summary>
 /// Applies a step as part of the task to compare two objects for structural equality.
 /// </summary>
 /// <value>
 /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
 /// have to be executed. Should return <c>false</c> otherwise.
 /// </value>
 /// <remarks>
 /// May throw when preconditions are not met or if it detects mismatching data.
 /// </remarks>
 public virtual bool Handle(EquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config)
 {
     return(config.AssertionRules.Any(rule => rule.AssertEquality(context)));
 }
コード例 #57
0
 /// <summary>
 /// Applies a step as part of the task to compare two objects for structural equality.
 /// </summary>
 /// <value>
 /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
 /// have to be executed. Should return <c>false</c> otherwise.
 /// </value>
 /// <remarks>
 /// May throw when preconditions are not met or if it detects mismatching data.
 /// </remarks>
 public virtual bool Handle(EquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config)
 {
     return config.AssertionRules.Any(rule => rule.AssertEquality(context));
 }
 public override bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config)
 {
     return(false);
 }
            public override bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config)
            {
                context.Subject.Should().Be(context.Expectation, context.Because, context.BecauseArgs);

                return(true);
            }
コード例 #60
0
            public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
            {
                if (comparands.Expectation is DateTime time)
                {
                    ((DateTime)comparands.Subject).Should().BeCloseTo(time, 1.Minutes());

                    return(EquivalencyResult.AssertionCompleted);
                }

                return(EquivalencyResult.ContinueWithNext);
            }