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

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

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

            if ((compareeSelectedMemberInfoInfo is null || !config.UseRuntimeTyping) && ExpectationImplementsMemberExplicitly(subject, expectedMember))
            {
                compareeSelectedMemberInfoInfo = expectedMember;
            }

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

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

            return(compareeSelectedMemberInfoInfo);
        }
        public static SelectedMemberInfo GetSelectedMemberInfo <T, TValue>(this Expression <Func <T, TValue> > expression)
        {
            if (ReferenceEquals(expression, null))
            {
                throw new ArgumentNullException(nameof(expression), "Expected an expression, but found <null>.");
            }

            MemberInfo memberInfo = AttemptToGetMemberInfoFromCastExpression(expression) ??
                                    AttemptToGetMemberInfoFromMemberExpression(expression);

            if (memberInfo != null)
            {
                var propertyInfo = memberInfo as PropertyInfo;
                if (propertyInfo != null)
                {
                    return(SelectedMemberInfo.Create(propertyInfo));
                }

                var fieldInfo = memberInfo as FieldInfo;
                if (fieldInfo != null)
                {
                    return(SelectedMemberInfo.Create(fieldInfo));
                }
            }

            throw new ArgumentException(
                      string.Format("Expression <{0}> cannot be used to select a member.", expression.Body),
                      nameof(expression));
        }
        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;
 }
Exemple #5
0
 public NestedSelectionContext(IMemberInfo context, SelectedMemberInfo selectedMemberInfo)
 {
     SelectedMemberPath        = context.SelectedMemberPath.Combine(selectedMemberInfo.Name);
     SelectedMemberDescription = context.SelectedMemberDescription.Combine(selectedMemberInfo.Name);
     CompileTimeType           = selectedMemberInfo.MemberType;
     RuntimeType        = selectedMemberInfo.MemberType;
     SelectedMemberInfo = selectedMemberInfo;
 }
Exemple #6
0
 public AssertionContext(SelectedMemberInfo subjectProperty, TSubject subject, TSubject expectation, string because,
                         object[] becauseArgs)
 {
     SubjectProperty = subjectProperty;
     Subject         = subject;
     Expectation     = expectation;
     Because         = because;
     BecauseArgs     = becauseArgs;
 }
            public SelectedMemberInfo Match(SelectedMemberInfo subjectMember, object expectation, string memberPath, IEquivalencyAssertionOptions config)
            {
                string name = subjectMember.Name;
                if (name.EndsWith("Id"))
                {
                    name = name.Replace("Id", "");
                }

                return SelectedMemberInfo.Create(expectation.GetType().GetRuntimeProperty(name));
            }
        private static void EvaluateEquality(SelectedMemberInfo memberInfo, IEquivalencyValidationContext context, IEquivalencyAssertionOptions config)
        {
            var memberMatch = FindMatchFor(memberInfo, context, config);

            memberMatch.Should().NotBeNull($"{context.Expectation.GetType().Name} should have a matching member for {memberInfo.Name}");

            var subjectProperty = memberInfo.GetValue(context.Subject, new object[0]);

            var expectedProperty = memberMatch.GetValue(context.Expectation, new object[0]);

            subjectProperty.ShouldBeEquivalentTo(expectedProperty, options => options.Using(new ExcludeOneOfAsMembersSelectionRule()), context.Because, context.BecauseArgs);
        }
        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;
        }
Exemple #10
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);
        }
        private static string GetPropertyValueTextFor(object value, SelectedMemberInfo selectedMemberInfo, FormattingContext context, FormatChild formatChild)
        {
            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(context.Depth + 1),
                                 selectedMemberInfo.Name,
                                 formatChild(selectedMemberInfo.Name, propertyValue)));
        }
Exemple #12
0
        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 static SelectedMemberInfo GetSelectedMemberInfo <T, TValue>(this Expression <Func <T, TValue> > expression)
        {
            Guard.ThrowIfArgumentIsNull(expression, nameof(expression), "Expected an expression, but found <null>.");

            MemberInfo memberInfo = AttemptToGetMemberInfoFromCastExpression(expression) ??
                                    AttemptToGetMemberInfoFromMemberExpression(expression);

            if (memberInfo != null)
            {
                if (memberInfo is PropertyInfo propertyInfo)
                {
                    return(SelectedMemberInfo.Create(propertyInfo));
                }

                if (memberInfo is FieldInfo fieldInfo)
                {
                    return(SelectedMemberInfo.Create(fieldInfo));
                }
            }

            throw new ArgumentException(
                      string.Format("Expression <{0}> cannot be used to select a member.", expression.Body),
                      nameof(expression));
        }
 private static bool ExpectationImplementsMemberExplicitly(object expectation, SelectedMemberInfo subjectMember)
 {
     return subjectMember.DeclaringType.IsInstanceOfType(expectation);
 }
 private static bool ExpectationImplementsMemberExplicitly(object expectation, SelectedMemberInfo subjectMember)
 {
     return(subjectMember.DeclaringType.IsInstanceOfType(expectation));
 }
Exemple #16
0
 private static bool IsAsTMember(SelectedMemberInfo info)
 => Regex.IsMatch(info.Name, @"^AsT\d+$");
 private static SelectedMemberInfo FindMatchFor(SelectedMemberInfo selectedMemberInfo, IEquivalencyValidationContext context, IEquivalencyAssertionOptions config)
 => config.MatchingRules
 .Select(rule => rule.Match(selectedMemberInfo, context.Expectation, context.SelectedMemberDescription, config))
 .FirstOrDefault(match => match != null);
Exemple #18
0
 public SelectedMemberInfo Match(SelectedMemberInfo subjectMember, object expectation, string memberPath, IEquivalencyAssertionOptions config)
 {
     return(expectation.GetType().FindMember(subjectMember.Name, subjectMember.MemberType));
 }
 private static int GetIsMemberNumber(SelectedMemberInfo isMember)
 => int.Parse(Regex.Match(isMember.Name, IsTPropertyNameRegex).Groups[1].Value);
 public SelectedMemberInfo Match(SelectedMemberInfo subjectMember, object expectation, string memberPath, IEquivalencyAssertionOptions config)
 {
     return expectation.GetType().FindMember(subjectMember.Name, subjectMember.MemberType);
 }
 public IncludeMemberByPathSelectionRule(SelectedMemberInfo selectedMemberInfo)
 {
     this.selectedMemberInfo = selectedMemberInfo;
 }
Exemple #22
0
 /// <summary>
 ///     Determines whether two <see cref="FluentAssertions.Equivalency.SelectedMemberInfo" /> objects refer to the same
 ///     member.
 /// </summary>
 public static bool IsEquivalentTo(this SelectedMemberInfo property, SelectedMemberInfo otherProperty)
 {
     return((property.DeclaringType.IsSameOrInherits(otherProperty.DeclaringType) ||
             otherProperty.DeclaringType.IsSameOrInherits(property.DeclaringType)) &&
            property.Name == otherProperty.Name);
 }
Exemple #23
0
 /// <summary>
 ///     Finds a member by its case-sensitive name.
 /// </summary>
 /// <returns>
 ///     Returns <c>null</c> if no such member exists.
 /// </returns>
 public static SelectedMemberInfo FindMember(this Type type, string memberName, Type preferredType)
 {
     return(SelectedMemberInfo.Create(FindProperty(type, memberName, preferredType)) ??
            SelectedMemberInfo.Create(FindField(type, memberName, preferredType)));
 }
 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);
 }
Exemple #26
0
 public SelectedMemberInfo Match(SelectedMemberInfo subjectMember, object expectation, string memberPath,
                                 IEquivalencyAssertionOptions config)
 {
     return(matchingRule.Match(subjectMember, expectation, memberPath, config));
 }