예제 #1
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)));
            }

            var diff = new Distinctions();

            if (ReferenceEquals(expected, actual))
            {
                return(diff);
            }

            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);
        }
예제 #2
0
        public Distinctions CompareDictionary <TKey, TValue>(IDictionary <TKey, TValue> expected,
                                                             IDictionary <TKey, TValue> actual, string propertyName)
        {
            var diff = new Distinctions();

            if (expected.Count != actual.Count)
            {
                return(Distinctions.Create("Dictionary has different length", expected.Count, actual.Count));
            }
            foreach (var kvp in expected)
            {
                if (!actual.TryGetValue(kvp.Key, out var secondValue))
                {
                    diff.Add(new Distinction(kvp.Key.ToString(), "Should be", "Does not exist"));
                }

                var diffRes = Comparator.Compare(kvp.Value, secondValue, $"{propertyName}[{kvp.Key}]");
                diff.AddRange(diffRes);
            }

            return(diff);
        }