Example #1
0
        public void CompareWithCustomComparer()
        {
            //Arrange.
            Mock <IComparer> customComparer = new Mock <IComparer>();

            customComparer.Setup(m => m.Compare(It.IsAny <Genders>(), It.IsAny <Genders>()))
            .Returns(0)
            .Callback <Genders, Genders>((d1, d2) =>
            {
                if (d1.GetType() != typeof(Genders) || d2.GetType() != typeof(Genders))
                {
                    throw new AssertionException("The arguments of the custom comparer are not of the expected type.");
                }
            })
            .Verifiable("Custom comparer was not invoked.");
            Person         p1       = ModelsHelper.CreatePerson();
            Person         p2       = ModelsHelper.CreatePerson();
            ObjectComparer comparer = ObjectComparer.Create <Person>();

            comparer.Comparers.Add(typeof(Genders), customComparer.Object);

            //Act.
            var result = comparer.Compare(p1, p2, out bool isDifferent);

            //Assert.
            customComparer.VerifyAll();
            isDifferent.Should().BeFalse();
        }
Example #2
0
        private static IEnumerable CompareWithNullTestData()
        {
            string rootName = nameof(CompareWithNull);
            Person p        = ModelsHelper.CreatePerson();

            yield return(new TestCaseData(null, p, "object1")
                         .SetName($"{rootName} object1 null"));

            yield return(new TestCaseData(p, null, "object2")
                         .SetName($"{rootName} object2 null"));
        }
Example #3
0
        private static IEnumerable CompareTypeMismatchTestData()
        {
            string         rootName = nameof(CompareTypeMismatch);
            Person         p        = ModelsHelper.CreatePerson();
            PersonByRefUdt p2       = new PersonByRefUdt();

            yield return(new TestCaseData(p, p)
                         .SetName($"{rootName} ({nameof(Person)}, {nameof(Person)})"));

            yield return(new TestCaseData(p2, p2)
                         .SetName($"{rootName} ({nameof(PersonByRefUdt)}, {nameof(PersonByRefUdt)})"));
        }
Example #4
0
        public void CompareSameObject()
        {
            //Arrange.
            Person         p1       = ModelsHelper.CreatePerson();
            Person         p2       = p1;
            ObjectComparer comparer = ObjectComparer.Create <Person>();

            //Act.
            Action act = () => comparer.Compare(p1, p2);

            //Assert.
            act.Should().Throw <InvalidOperationException>();
        }
Example #5
0
        public void PropertyNotFound()
        {
            //Arrange.
            PersonEx       p1       = ModelsHelper.CreatePersonEx();
            Person         p2       = ModelsHelper.CreatePerson();
            ObjectComparer comparer = ObjectComparer.Create <PersonEx, Person>();

            //Act.
            var result = comparer.Compare(p1, p2, out bool isDifferent);

            //Assert.
            isDifferent.Should().BeFalse();
            result[nameof(PersonEx.NickName)].Result.Should().Be(ComparisonResult.PropertyNotFound);
        }
Example #6
0
        public void StringCoercionThrows()
        {
            //Arrange.
            Person         p1       = ModelsHelper.CreatePerson();
            Person         p2       = ModelsHelper.CreatePerson();
            ObjectComparer comparer = ComparerConfigurator.Configure <Person>()
                                      .MapProperty(src => src.Gender, dst => dst.Gender, true, "ABC", "DEF")
                                      .CreateComparer();

            //Act.
            var result = comparer.Compare(p1, p2, out bool _);

            //Assert.
            PropertyComparisonResult r = result[nameof(Person.Gender)];

            (r.Result & ComparisonResult.StringCoercionException).Should().Be(ComparisonResult.StringCoercionException);
            r.Exception.Should().NotBeNull();
        }
Example #7
0
        public void CompareEqualObjects()
        {
            //Arrange.
            Person         p1       = ModelsHelper.CreatePerson();
            Person         p2       = ModelsHelper.CreatePerson();
            ObjectComparer comparer = ObjectComparer.Create <Person>();

            //Act.
            var result = comparer.Compare(p1, p2, out bool isDifferent);

            //Assert.
            isDifferent.Should().BeFalse();
            result.Should().NotBeNull();
            result.Count.Should().Be(5);
            foreach (PropertyComparisonResult pcr in result)
            {
                pcr.Result.Should().Be(ComparisonResult.Equal);
            }
        }
        public void IgnoredPropertyDoesNotResultInDifferent()
        {
            //Arrange.
            Person p1 = ModelsHelper.CreatePerson();
            Person p2 = ModelsHelper.CreatePerson();

            p2.Id = p1.Id + 1;
            var config = ComparerConfigurator.Configure <Person>()
                         .IgnoreProperty(src => src.Id);
            ObjectComparer comparer = config.CreateComparer();

            //Act.
            var result = comparer.Compare(p1, p2, out bool isDifferent);

            //Assert.
            result.Should().NotBeNull();
            isDifferent.Should().BeFalse();
            result[nameof(Person.Id)].Result.Should().Be(ComparisonResult.PropertyIgnored);
        }
Example #9
0
        public void IComparerResult(int comparerReturnValue, ComparisonResult expectedResult)
        {
            //Arrange.
            Mock <IComparer> customComparer = new Mock <IComparer>();

            customComparer.Setup(m => m.Compare(It.IsAny <Genders>(), It.IsAny <Genders>()))
            .Returns(comparerReturnValue)
            .Verifiable("Custom comparer was not invoked.");
            Person         p1       = ModelsHelper.CreatePerson();
            Person         p2       = ModelsHelper.CreatePerson();
            ObjectComparer comparer = ObjectComparer.Create <Person>();

            comparer.Comparers.Add(typeof(Genders), customComparer.Object);

            //Act.
            var result = comparer.Compare(p1, p2, out bool isDifferent);

            //Assert.
            customComparer.VerifyAll();
            isDifferent.Should().Be(expectedResult != ComparisonResult.Equal);
            result[nameof(Person.Gender)].Result.Should().Be(expectedResult);
        }
        public void PropertyIsIgnoredForSpecificType()
        {
            //Arrange.
            Person   p1     = ModelsHelper.CreatePerson();
            PersonEx p2     = ModelsHelper.CreatePersonEx();
            var      config = ComparerConfigurator.Configure <Person, PersonEx>()
                              .IgnoreProperty(src => src.Email);
            ObjectComparer comparer = config.CreateComparer();

            //Act.
            var result = comparer.Compare(p1, p2, out bool _);

            //Assert.
            result.Should().NotBeNull();
            PropertyComparisonResult pcr = result[nameof(Person.Email)];

            pcr.Should().NotBeNull();
            pcr.Result.Should().Be(ComparisonResult.PropertyIgnored);
            pcr.MapUsed.Should().NotBeNull();
            pcr.MapUsed.Operation.Should().Be(PropertyMapOperation.IgnoreProperty);
            pcr.Property1.IgnoreProperty.Should().Be(IgnorePropertyOptions.DoNotIgnore);
        }
Example #11
0
        public void IComparerThrows()
        {
            //Arrange.
            Person           p1 = ModelsHelper.CreatePerson();
            Person           p2 = ModelsHelper.CreatePerson();
            Mock <IComparer> throwingComparer = new Mock <IComparer>();

            throwingComparer.Setup(m => m.Compare(It.IsAny <object>(), It.IsAny <object>()))
            .Throws(new InvalidOperationException("Compare throws."))
            .Verifiable($"{nameof(IComparer.Compare)}() method not invoked.");
            ObjectComparer comparer = ObjectComparer.Create <Person>();

            comparer.Comparers.Add(typeof(Genders), throwingComparer.Object);

            //Act.
            var result = comparer.Compare(p1, p2, out bool isDifferent);

            //Assert.
            throwingComparer.VerifyAll();
            PropertyComparisonResult r = result[nameof(Person.Gender)];

            r.Result.Should().Be(ComparisonResult.ComparisonException);
            r.Exception.Should().NotBeNull();
        }