public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config)
        {
            bool subjectIsNull = ReferenceEquals(context.Subject, null);

            bool subjectIsValidType =
                AssertionScope.Current
                .ForCondition(context.Subject.GetType().IsSameOrInherits(typeof(TSubject)))
                .FailWith("Expected " + context.SelectedMemberDescription + " from subject to be a {0}{reason}, but found a {1}.",
                          typeof(TSubject), context.Subject?.GetType());

            bool expectationIsNull = ReferenceEquals(context.Expectation, null);

            bool expectationIsValidType =
                AssertionScope.Current
                .ForCondition(expectationIsNull || context.Expectation.GetType().IsSameOrInherits(typeof(TSubject)))
                .FailWith("Expected " + context.SelectedMemberDescription + " from expectation to be a {0}{reason}, but found a {1}.",
                          typeof(TSubject), context.SelectedMemberInfo.MemberType);

            if (subjectIsValidType && expectationIsValidType)
            {
                handle(AssertionContext <TSubject> .CreateFromEquivalencyValidationContext(context));
                return(true);
            }

            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Defines how a subject's property is compared for equality with the same property of the expectation.
        /// </summary>
        /// <returns>
        /// Returns <c>true</c> if the rule was applied correctly and the assertion didn't cause any exceptions.
        /// Returns <c>false</c> if this rule doesn't support the subject's type.
        /// Throws if the rule did support the data type but assertion fails.
        /// </returns>
        public bool AssertEquality(IEquivalencyValidationContext context)
        {
            if (predicate(context))
            {
                bool subjectIsNull = context.Subject is null;

                bool subjectIsValidType =
                    AssertionScope.Current
                    .ForCondition(subjectIsNull || context.Subject.GetType().IsSameOrInherits(typeof(TSubject)))
                    .FailWith("Expected " + context.SelectedMemberDescription + " from subject to be a {0}{reason}, but found a {1}.",
                              typeof(TSubject), context.Subject?.GetType());

                bool expectationIsNull = context.Expectation is null;

                bool expectationIsValidType =
                    AssertionScope.Current
                    .ForCondition(expectationIsNull || context.Expectation.GetType().IsSameOrInherits(typeof(TSubject)))
                    .FailWith("Expected " + context.SelectedMemberDescription + " from expectation to be a {0}{reason}, but found a {1}.",
                              typeof(TSubject), context.SelectedMemberInfo.MemberType);

                if (subjectIsValidType && expectationIsValidType)
                {
                    action(AssertionContext <TSubject> .CreateFromEquivalencyValidationContext(context));
                }

                return(true);
            }

            return(false);
        }
        internal static AssertionContext <TSubject> CreateFromEquivalencyValidationContext(IEquivalencyValidationContext context)
        {
            var expectation = (context.Expectation != null) ? (TSubject)context.Expectation : default(TSubject);

            var assertionContext = new AssertionContext <TSubject>(
                context.SelectedMemberInfo,
                (TSubject)context.Subject,
                expectation,
                context.Reason,
                context.ReasonArgs);

            return(assertionContext);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Defines how a subject's property is compared for equality with the same property of the expectation.
        /// </summary>
        /// <param name="subjectProperty">
        /// Provides details about the subject's property.
        /// </param>
        /// <param name="subject">
        /// The value of the subject's property.
        /// </param>
        /// <param name="expectation">
        /// The value of a property on expectation object that was identified
        /// </param>
        /// <returns>
        /// Returns <c>true</c> if the rule was applied correctly and the assertion didn't cause any exceptions.
        /// Returns <c>false</c> if this rule doesn't support the subject's type.
        /// Throws if the rule did support the data type but assertion fails.
        /// </returns>
        public bool AssertEquality(IEquivalencyValidationContext context)
        {
            if (predicate(context))
            {
                bool expectationisNull = ReferenceEquals(context.Expectation, null);

                bool succeeded =
                    AssertionScope.Current
                    .ForCondition(expectationisNull || context.Expectation.GetType().IsSameOrInherits(typeof(TSubject)))
                    .FailWith("Expected " + context.SelectedMemberDescription + " to be a {0}{reason}, but found a {1}",
                              !expectationisNull ? context.Expectation.GetType() : null, context.SelectedMemberInfo.MemberType);

                if (succeeded)
                {
                    action(AssertionContext <TSubject> .CreateFromEquivalencyValidationContext(context));
                }

                return(true);
            }

            return(false);
        }