public SelectedMemberInfo Match(SelectedMemberInfo subjectMember, object expectation, string memberPath, IEquivalencyAssertionOptions config)
        {
            SelectedMemberInfo compareeSelectedMemberInfoInfo = null;

            if (config.IncludeProperties)
            {
                compareeSelectedMemberInfoInfo = SelectedMemberInfo.Create(expectation.GetType()
                    .FindProperty(subjectMember.Name, subjectMember.MemberType));
            }

            if ((compareeSelectedMemberInfoInfo == null) && config.IncludeFields)
            {
                compareeSelectedMemberInfoInfo = SelectedMemberInfo.Create(expectation.GetType()
                    .FindField(subjectMember.Name, subjectMember.MemberType));
            }

            if ((compareeSelectedMemberInfoInfo == null) && ExpectationImplementsMemberExplicitly(expectation, subjectMember))
            {
                compareeSelectedMemberInfoInfo = subjectMember;
            }

            if (compareeSelectedMemberInfoInfo == null)
            {
                string path = (memberPath.Length > 0) ? memberPath + "." : "member ";

                Execute.Assertion.FailWith(
                    "Subject has " + path + subjectMember.Name + " that the other object does not have.");
            }

            return compareeSelectedMemberInfoInfo;
        }
        private static IEquivalencyValidationContext CreateNested(
            this IEquivalencyValidationContext equivalencyValidationContext,
            SelectedMemberInfo subjectProperty,
            object subject,
            object expectation,
            string memberType,
            string memberDescription,
            string separator,
            Type compileTimeType)
        {
            string propertyPath = equivalencyValidationContext.IsRoot
                                      ? memberType
                                      : equivalencyValidationContext.SelectedMemberDescription + separator;

            return(new EquivalencyValidationContext
            {
                SelectedMemberInfo = subjectProperty,
                Subject = subject,
                Expectation = expectation,
                SelectedMemberPath =
                    equivalencyValidationContext.SelectedMemberPath.Combine(
                        memberDescription,
                        separator),
                SelectedMemberDescription = propertyPath + memberDescription,
                Reason = equivalencyValidationContext.Reason,
                ReasonArgs = equivalencyValidationContext.ReasonArgs,
                CompileTimeType = compileTimeType,
            });
        }
Example #3
0
        public SelectedMemberInfo Match(SelectedMemberInfo subjectMember, object expectation, string memberPath, IEquivalencyAssertionOptions config)
        {
            SelectedMemberInfo compareeSelectedMemberInfoInfo = null;

            if (config.IncludeProperties)
            {
                compareeSelectedMemberInfoInfo = SelectedMemberInfo.Create(expectation.GetType()
                                                                           .FindProperty(subjectMember.Name, subjectMember.MemberType));
            }

            if ((compareeSelectedMemberInfoInfo == null) && config.IncludeFields)
            {
                compareeSelectedMemberInfoInfo = SelectedMemberInfo.Create(expectation.GetType()
                                                                           .FindField(subjectMember.Name, subjectMember.MemberType));
            }

            if ((compareeSelectedMemberInfoInfo == null) && ExpectationImplementsMemberExplicitly(expectation, subjectMember))
            {
                compareeSelectedMemberInfoInfo = subjectMember;
            }

            if (compareeSelectedMemberInfoInfo == null)
            {
                string path = (memberPath.Length > 0) ? memberPath + "." : "member ";

                Execute.Assertion.FailWith(
                    "Subject has " + path + subjectMember.Name + " that the other object does not have.");
            }

            return(compareeSelectedMemberInfoInfo);
        }
 public NestedSelectionContext(ISubjectInfo context, SelectedMemberInfo selectedMemberInfo)
 {
     SelectedMemberPath = context.SelectedMemberPath.Combine(selectedMemberInfo.Name);
     SelectedMemberDescription = context.SelectedMemberDescription.Combine(selectedMemberInfo.Name);
     CompileTimeType = selectedMemberInfo.MemberType;
     RuntimeType = selectedMemberInfo.MemberType;
     SelectedMemberInfo = selectedMemberInfo;
 }
Example #5
0
 public NestedSelectionContext(ISubjectInfo context, SelectedMemberInfo selectedMemberInfo)
 {
     SelectedMemberPath        = context.SelectedMemberPath.Combine(selectedMemberInfo.Name);
     SelectedMemberDescription = context.SelectedMemberDescription.Combine(selectedMemberInfo.Name);
     CompileTimeType           = selectedMemberInfo.MemberType;
     RuntimeType        = selectedMemberInfo.MemberType;
     SelectedMemberInfo = selectedMemberInfo;
 }
 public AssertionContext(SelectedMemberInfo subjectProperty, TSubject subject, TSubject expectation, string because,
                         object[] reasonArgs)
 {
     SubjectProperty = subjectProperty;
     Subject         = subject;
     Expectation     = expectation;
     Reason          = because;
     ReasonArgs      = reasonArgs;
 }
        private static SelectedMemberInfo FindMatchFor(SelectedMemberInfo selectedMemberInfo, IEquivalencyValidationContext context, IEquivalencyAssertionOptions config)
        {
            var query =
                from rule in config.MatchingRules
                let match = rule.Match(selectedMemberInfo, context.Expectation, context.SelectedMemberDescription, config)
                where match != null
                select match;

            return query.FirstOrDefault();
        }
        private static SelectedMemberInfo FindMatchFor(SelectedMemberInfo selectedMemberInfo, IEquivalencyValidationContext context, IEquivalencyAssertionOptions config)
        {
            var query =
                from rule in config.MatchingRules
                let match = rule.Match(selectedMemberInfo, context.Expectation, context.SelectedMemberDescription, config)
                            where match != null
                            select match;

            return(query.FirstOrDefault());
        }
        /// <summary>
        /// Includes the specified member in the equality check.
        /// </summary>
        /// <remarks>
        /// This overrides the default behavior of including all declared members.
        /// </remarks>
        public EquivalencyAssertionOptions <TSubject> Including(Expression <Func <TSubject, object> > expression)
        {
            RemoveStandardSelectionRules();

            SelectedMemberInfo selectedMemberInfo = expression.GetSelectedMemberInfo();

            AddSelectionRule(
                new IncludeMemberByPathSelectionRule(selectedMemberInfo));
            return(this);
        }
 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);
         }
     }
 }
        public SelectedMemberInfo Match(SelectedMemberInfo subjectMember, object expectation, string memberPath,
            IEquivalencyAssertionOptions config)
        {
            var propertySelectedMemberInfo = subjectMember as PropertySelectedMemberInfo;
            if (propertySelectedMemberInfo != null)
            {
                return
                    SelectedMemberInfo.Create(obsoleteMatchingRule.Match(propertySelectedMemberInfo.PropertyInfo,
                        expectation, memberPath));
            }

            return null;
        }
Example #12
0
        public SelectedMemberInfo Match(SelectedMemberInfo subjectMember, object expectation, string memberPath,
                                        IEquivalencyAssertionOptions config)
        {
            var propertySelectedMemberInfo = subjectMember as PropertySelectedMemberInfo;

            if (propertySelectedMemberInfo != null)
            {
                return
                    (SelectedMemberInfo.Create(obsoleteMatchingRule.Match(propertySelectedMemberInfo.PropertyInfo,
                                                                          expectation, memberPath)));
            }

            return(null);
        }
Example #13
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);
                }
            }
        }
        internal static IEquivalencyValidationContext CreateForNestedMember(
            this IEquivalencyValidationContext equivalencyValidationContext,
            SelectedMemberInfo nestedMember,
            SelectedMemberInfo matchingProperty)
        {
            object subject     = nestedMember.GetValue(equivalencyValidationContext.Subject, null);
            object expectation = matchingProperty.GetValue(equivalencyValidationContext.Expectation, null);

            return(CreateNested(
                       equivalencyValidationContext,
                       nestedMember,
                       subject,
                       expectation,
                       "member ",
                       nestedMember.Name,
                       ".",
                       nestedMember.MemberType));
        }
Example #15
0
        internal static IEquivalencyValidationContext CreateForNestedMember(this IEquivalencyValidationContext context,
                                                                            SelectedMemberInfo nestedMember, SelectedMemberInfo matchingProperty)
        {
            string memberDescription = nestedMember.Name;
            string propertyPath      = (context.SelectedMemberDescription.Length == 0) ? "member " : context.SelectedMemberDescription + ".";

            return(new EquivalencyValidationContext
            {
                SelectedMemberInfo = nestedMember,
                Subject = nestedMember.GetValue(context.Subject, null),
                Expectation = matchingProperty.GetValue(context.Expectation, null),
                SelectedMemberPath = context.SelectedMemberPath.Combine(memberDescription, "."),
                SelectedMemberDescription = propertyPath + memberDescription,
                Because = context.Because,
                BecauseArgs = context.BecauseArgs,
                CompileTimeType = nestedMember.MemberType,
                RootIsCollection = context.RootIsCollection
            });
        }
 public IncludeMemberByPathSelectionRule(SelectedMemberInfo selectedMemberInfo)
 {
     this.selectedMemberInfo = selectedMemberInfo;
 }
 private static bool ExpectationImplementsMemberExplicitly(object expectation, SelectedMemberInfo subjectMember)
 {
     return subjectMember.DeclaringType.IsInstanceOfType(expectation);
 }
 public IncludeMemberByPathSelectionRule(SelectedMemberInfo selectedMemberInfo)
 {
     this.selectedMemberInfo = selectedMemberInfo;
 }
 public SelectedMemberInfo Match(SelectedMemberInfo subjectMember, object expectation, string memberPath, IEquivalencyAssertionOptions config)
 {
     return(matchingRule.Match(subjectMember, expectation, memberPath, config));
 }
 public SelectedMemberInfo Match(SelectedMemberInfo subjectMember, object expectation, string memberPath, IEquivalencyAssertionOptions config)
 {
     return expectation.GetType().FindMember(subjectMember.Name, subjectMember.MemberType);
 }
Example #21
0
 private static bool ExpectationImplementsMemberExplicitly(object expectation, SelectedMemberInfo subjectMember)
 {
     return(subjectMember.DeclaringType.IsInstanceOfType(expectation));
 }
        private static string GetPropertyValueTextFor(object value, SelectedMemberInfo selectedMemberInfo, int nextMemberNestingLevel,
            IList<object> processedObjects)
        {
            object propertyValue;

            try
            {
                propertyValue = selectedMemberInfo.GetValue(value, null);
            }
            catch (Exception ex)
            {
                propertyValue = string.Format("[Member '{0}' threw an exception: '{1}']", selectedMemberInfo.Name, ex.Message);
            }

            return string.Format("{0}{1} = {2}",
                CreateWhitespaceForLevel(nextMemberNestingLevel),
                selectedMemberInfo.Name,
                Formatter.ToString(propertyValue, false, processedObjects, nextMemberNestingLevel));
        }
 public SelectedMemberInfo Match(SelectedMemberInfo subjectMember, object expectation, string memberPath, IEquivalencyAssertionOptions config)
 {
     return(expectation.GetType().FindMember(subjectMember.Name, subjectMember.MemberType));
 }