public Distinctions Compare <T1>(T1 expected, T1 actual, string propertyName)
        {
            var a = (T)(object)expected;
            var b = (T)(object)actual;

            return(CompareFunc.Compile()(a, b)
                ? Distinctions.None()
                : Distinctions.Create(new[]
            {
                _display.GetDistinction(a, b, propertyName,
                                        BodyExpression.Get(CompareFunc).ToString())
            }));
        }
Esempio n. 2
0
        public Distinctions Compare <T>(T expected, T actual, string propertyName)
        {
            if (expected != null && actual == null || expected == null && actual != null)
            {
                return(Distinctions.Create(new Distinction(typeof(T).Name, expected, actual)));
            }

            if (ReferenceEquals(expected, actual))
            {
                return(Distinctions.None());
            }
            var diff = Distinctions.Create();
            var type = expected.GetType();

            foreach (var mi in type.GetMembers(BindingFlags.Public | BindingFlags.Instance).Where(x =>
                                                                                                  x.MemberType == MemberTypes.Property || x.MemberType == MemberTypes.Field))
            {
                var name = mi.Name;
                var actualPropertyPath = MemberPathBuilder.BuildMemberPath(propertyName, mi);

                if (Ignore(actualPropertyPath))
                {
                    continue;
                }

                object firstValue  = null;
                object secondValue = null;
                switch (mi.MemberType)
                {
                case MemberTypes.Field:
                    firstValue  = type.GetField(name).GetValue(expected);
                    secondValue = type.GetField(name).GetValue(actual);
                    break;

                case MemberTypes.Property:
                    firstValue  = type.GetProperty(name)?.GetValue(expected);
                    secondValue = type.GetProperty(name)?.GetValue(actual);
                    break;
                }

                var diffRes = GetDistinctions(actualPropertyPath, firstValue, secondValue);
                if (diffRes.IsNotEmpty())
                {
                    diff.AddRange(diffRes);
                }
            }

            return(diff);
        }
Esempio n. 3
0
        public Distinctions GetDistinctions(string propertyName, dynamic expected, dynamic actual)
        {
            if (Strategies.IsNotEmpty() && Strategies.Any(x => x.Key == propertyName))
            {
                return(Strategies[propertyName].Compare(expected, actual, propertyName));
            }

            if (expected == null && actual != null)
            {
                return(Distinctions.Create(propertyName, "null", actual));
            }

            if (expected != null && actual == null)
            {
                return(Distinctions.Create(propertyName, expected, "null"));
            }

            return(expected == null
                ? Distinctions.None()
                : (Distinctions)GetDifference(expected, actual, propertyName));
        }