public void Set_StrategyIgnore_Equal()
        {
            var act = new Student
            {
                Name    = "StudentName",
                Age     = 1,
                Courses = new[]
                {
                    new Course
                    {
                        Name = "CourseName"
                    }
                }
            };

            var exp = new Student
            {
                Name    = "StudentName1",
                Age     = 1,
                Courses = new[]
                {
                    new Course
                    {
                        Name = "CourseName1"
                    }
                }
            };

            var actual = act.GetDistinctions(exp, propName => propName == "Name");

            var expected = Distinctions.Create(new Distinction("Courses[0].Name", "CourseName", "CourseName1"));

            CollectionAssert.AreEquivalent(expected, actual);
        }
        public void DisplayCustomErrorMsg()
        {
            var actual = new Student
            {
                Name    = "Alex",
                Age     = 20,
                Vehicle = new Vehicle
                {
                    Model = "Audi"
                },
                Courses = new[]
                {
                    new Course
                    {
                        Name     = "Math",
                        Duration = TimeSpan.FromHours(4)
                    },
                    new Course
                    {
                        Name     = "Liter",
                        Duration = TimeSpan.FromHours(4)
                    }
                }
            };

            var expected = new Student
            {
                Name    = "Bob",
                Age     = 20,
                Vehicle = new Vehicle
                {
                    Model = "Opel"
                },
                Courses = new[]
                {
                    new Course
                    {
                        Name     = "Math",
                        Duration = TimeSpan.FromHours(3)
                    },
                    new Course
                    {
                        Name     = "Literature",
                        Duration = TimeSpan.FromHours(4)
                    }
                }
            };

            var skip   = new[] { "Vehicle", "Name", "Courses[1].Name" };
            var result = expected.GetDistinctions(actual,
                                                  str => str.Set(x => x.Courses[0].Duration, (act, exp) => act > TimeSpan.FromHours(3),
                                                                 new Display {
                Expected = "Expected that Duration should be more that 3 hours"
            }), skip);
            var expectedDistinctionsCollection = Distinctions.Create(new Distinction("Courses[0].Duration",
                                                                                     "Expected that Duration should be more that 3 hours",
                                                                                     "04:00:00"));

            CollectionAssert.AreEquivalent(result, expectedDistinctionsCollection);
        }
        public void CheckWithCustomRule()
        {
            var actual = new Student
            {
                Name    = "Alex",
                Age     = 20,
                Vehicle = new Vehicle
                {
                    Model = "Audi"
                },
                Courses = new[]
                {
                    new Course
                    {
                        Name     = "Math",
                        Duration = TimeSpan.FromHours(4)
                    },
                    new Course
                    {
                        Name     = "Math",
                        Duration = TimeSpan.FromHours(5)
                    }
                }
            };

            var expected = new Student
            {
                Name    = "Alex",
                Age     = 20,
                Vehicle = new Vehicle
                {
                    Model = "Audi"
                },
                Courses = new[]
                {
                    new Course
                    {
                        Name     = "Math",
                        Duration = TimeSpan.FromHours(3)
                    },
                    new Course
                    {
                        Name     = "Fake",
                        Duration = TimeSpan.FromHours(4)
                    }
                }
            };

            var newRule = new Comparator.Comparator();

            newRule.RuleForReferenceTypes.Add(new CourseRule());
            var result = ComparatorExtension.GetDistinctions(expected, actual, newRule);
            var expectedDistinctionsCollection =
                Distinctions.Create(new Distinction("Courses[1]", "Fake", "Math"));

            CollectionAssert.AreEquivalent(result, expectedDistinctionsCollection);
        }
예제 #4
0
        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())
            }));
        }
예제 #5
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);
        }
예제 #6
0
        public override Distinctions Compare <T>(T expected, T actual, string propertyName)
        {
            var listA = ((IEnumerable)expected).Cast <dynamic>().ToList();
            var listB = ((IEnumerable)actual).Cast <dynamic>().ToList();

            if (listA.Count != listB.Count)
            {
                return(Distinctions.Create(new Distinction(
                                               $"Property \"{propertyName}\": Collection has different length",
                                               $"{listA.Count}",
                                               $"{listB.Count}")));
            }

            return(Enumerable.Range(0, listA.Count).Aggregate(Distinctions.Create(),
                                                              (dc, i) => dc.AddRange(
                                                                  Comparator.GetDistinctions($"{propertyName}[{i}]", listA[i], listB[i]))));
        }
        public void DictionaryVerifications()
        {
            var exp = new Library
            {
                Books = new Dictionary <string, Book>
                {
                    ["hobbit"] = new Book {
                        Pages = 1000, Text = "hobbit Text"
                    },
                    ["murder in orient express"] = new Book {
                        Pages = 500, Text = "murder in orient express Text"
                    },
                    ["Shantaram"] = new Book {
                        Pages = 500, Text = "Shantaram Text"
                    }
                }
            };

            var act = new Library
            {
                Books = new Dictionary <string, Book>
                {
                    ["hobbit"] = new Book {
                        Pages = 1, Text = "hobbit Text"
                    },
                    ["murder in orient express"] = new Book {
                        Pages = 500, Text = "murder in orient express Text1"
                    },
                    ["Shantaram"] = new Book {
                        Pages = 500, Text = "Shantaram Text"
                    }
                }
            };

            var result = exp.GetDistinctions(act);
            var expectedDistinctionsCollection = Distinctions.Create(new[]
            {
                new Distinction("Books[hobbit].Pages", 1000, 1),
                new Distinction(
                    "Books[murder in orient express].Text", "murder in orient express Text",
                    "murder in orient express Text1")
            });

            CollectionAssert.AreEquivalent(result, expectedDistinctionsCollection);
        }
예제 #8
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));
        }
예제 #9
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);
        }
        public void Set_Strategy_For_Collection_Ref_Type_NegativeTest()
        {
            const string data = "actual";
            var          act  = new ClassA
            {
                One        = "f",
                Two        = 5,
                ArrayThird = new[] { "sss", "ggg" },
                InnerClass = new[] { new SomeClass {
                                         Foo = data
                                     }, new SomeClass {
                                         Foo = data
                                     } }
            };

            var exp = new ClassA
            {
                One        = "f",
                Two        = 5,
                ArrayThird = new[] { "error", "error1" },
                InnerClass = new[] { new SomeClass {
                                         Foo = "some"
                                     }, new SomeClass {
                                         Foo = "someFail"
                                     } }
            };

            var res = act.GetDistinctions(exp, str => str.Set(x => x.InnerClass[0].Foo,
                                                              (s, s1) => s == data));

            var expected = Distinctions.Create(new[]
            {
                new Distinction("ArrayThird[0]", "sss", "error"), new Distinction("ArrayThird[1]", "ggg", "error1"),
                new Distinction("InnerClass[1].Foo", "actual", "someFail")
            });

            CollectionAssert.AreEquivalent(res, expected);
        }