Esempio n. 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();
        }
        public void CompareUsesFormatStringsFromConfiguration()
        {
            //Arrange.
            PersonEx2 p1        = ModelsHelper.CreatePersonEx2();
            PersonEx2 p2        = ModelsHelper.CreatePersonEx2();
            DateTime  birthDate = DateTime.Now.AddYears(-20);

            p1.BirthDate = p2.BirthDate = birthDate;
            string fs1    = "yyyyMMdd";
            string fs2    = "ddMMyyyy";
            var    config = ComparerConfigurator.Configure <PersonEx2>(true)
                            .MapProperty(src => src.BirthDate, dst => dst.BirthDate, true, fs1, fs2);
            ObjectComparer comparer = config.CreateComparer();

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

            //Assert.
            result.Should().NotBeNull();
            PropertyComparisonResult pcr = result[nameof(PersonEx2.BirthDate)];

            pcr.Should().NotBeNull();
            (pcr.Result & ComparisonResult.StringCoercion).Should().Be(ComparisonResult.StringCoercion);
            (pcr.Result & ComparisonResult.NotEqual).Should().Be(ComparisonResult.NotEqual);
            pcr.MapUsed.Should().NotBeNull();
            pcr.MapUsed.ForceStringValue.Should().BeTrue();
            pcr.MapUsed.FormatString.Should().Be(fs1);
            pcr.MapUsed.TargetFormatString.Should().Be(fs2);
            pcr.Value1.Should().NotBeNull();
            pcr.Value1.Should().BeOfType <string>();
            pcr.Value1.Should().Be(birthDate.ToString(fs1));
            pcr.Value2.Should().NotBeNull();
            pcr.Value2.Should().BeOfType <string>();
            pcr.Value2.Should().Be(birthDate.ToString(fs2));
        }
        public void CompareCoercesToStringOnDemandFromConfiguration()
        {
            //Arrange.
            PersonEx            p1 = ModelsHelper.CreatePersonEx();
            PersonExWithPropMap p2 = ModelsHelper.CreatePersonExWithPropMapping();
            var config             = ComparerConfigurator.Configure <PersonEx, PersonExWithPropMap>(true)
                                     .MapProperty(src => src.Gender, dst => dst.Gender, true);
            ObjectComparer comparer = config.CreateComparer();

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

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

            pcr.Should().NotBeNull();
            (pcr.Result & ComparisonResult.StringCoercion).Should().Be(ComparisonResult.StringCoercion);
            pcr.MapUsed.Should().NotBeNull();
            pcr.MapUsed.ForceStringValue.Should().BeTrue();
            pcr.Value1.Should().NotBeNull();
            pcr.Value1.Should().BeOfType <string>();
            pcr.Value2.Should().NotBeNull();
            pcr.Value2.Should().BeOfType <string>();
        }
Esempio n. 4
0
        private static IEnumerable IgnoreForComparisonAttributeIgnoreForOthersTestData()
        {
            string rootName = nameof(IgnoreForComparisonAttributeIgnoreForOthers);

            yield return(new TestCaseData(typeof(PersonExWithIgnore), ModelsHelper.CreatePersonExWithIgnore())
                         .SetName($"{rootName} {nameof(IgnorePropertyOptions.IgnoreForAll)}"));

            yield return(new TestCaseData(typeof(PersonExWithIgnoreForOthers), ModelsHelper.CreatePersonExWithIgnoreForOthers())
                         .SetName($"{rootName} {nameof(IgnorePropertyOptions.IgnoreForOthers)}"));
        }
Esempio n. 5
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"));
        }
Esempio n. 6
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)})"));
        }
Esempio n. 7
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>();
        }
        public void ConfigurationIncludesAttributedPropertyMappings()
        {
            //Arrange.
            PersonExWithPropMap p1  = ModelsHelper.CreatePersonExWithPropMapping();
            PersonEx            p2  = ModelsHelper.CreatePersonEx();
            var            config   = ComparerConfigurator.Configure <PersonExWithPropMap, PersonEx>();
            ObjectComparer comparer = config.CreateComparer();

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

            //Assert.
            result[nameof(PersonExWithPropMap.NewNickName)].MapUsed.Should().NotBeNull();
        }
Esempio n. 9
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);
        }
Esempio n. 10
0
        public void IgnoreForComparisonAttributeIgnoreForOthers(Type p1Type, Person p1)
        {
            //Arrange.
            PersonEx       p2       = ModelsHelper.CreatePersonEx();
            ObjectComparer comparer = new ObjectComparer(p1Type, typeof(PersonEx));

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

            //Assert.
            result.Should().NotBeNull();
            PropertyComparisonResult proprtyResult = result[nameof(PersonExWithIgnore.NickName)];

            proprtyResult.Should().NotBeNull();
            proprtyResult.Result.Should().Be(ComparisonResult.PropertyIgnored);
        }
Esempio n. 11
0
        public void CompareNullablePropertiesSameType(bool date1Null, bool date2Null)
        {
            //Arrange.
            PersonEx2WithPropertyMap p1 = ModelsHelper.CreatePersonEx2WithPropertyMap(date1Null);
            PersonEx2      p2           = ModelsHelper.CreatePersonEx2(date2Null);
            ObjectComparer comparer     = ObjectComparer.Create <PersonEx2WithPropertyMap, PersonEx2>();

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

            //Assert.
            result.Should().NotBeNull();
            PropertyComparisonResult propResult = result[nameof(PersonEx2WithPropertyMap.DateOfBirth)];

            propResult.Should().NotBeNull();
        }
Esempio n. 12
0
        public void CompareCoercesToStringOnAttributedDemand(bool date1Null, bool date2Null)
        {
            //Arrange.
            PersonExWithStringCoerce p1 = ModelsHelper.CreatePersonExWithStringCoerce(date1Null);
            PersonEx2      p2           = ModelsHelper.CreatePersonEx2(date2Null);
            ObjectComparer comparer     = ObjectComparer.Create <PersonExWithStringCoerce, PersonEx2>();

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

            //Assert.
            result.Should().NotBeNull();
            PropertyComparisonResult propResult = result[nameof(PersonExWithStringCoerce.DateOfBirth)];

            propResult.Should().NotBeNull();
            (propResult.Result & ComparisonResult.StringCoercion).Should().Be(ComparisonResult.StringCoercion);
        }
Esempio n. 13
0
        public void CompareNonNullableToNullableSameBaseType(bool nullDate)
        {
            //Arrange.
            PersonEx2NonNullable p1       = ModelsHelper.CreatePersonEx2NonNullable();
            PersonEx2            p2       = ModelsHelper.CreatePersonEx2(nullDate);
            ObjectComparer       comparer = ObjectComparer.Create <PersonEx2NonNullable, PersonEx2>();

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

            //Assert.
            result.Should().NotBeNull();
            PropertyComparisonResult propResult = result[nameof(PersonEx2.BirthDate)];

            propResult.Should().NotBeNull();
            (propResult.Result & ComparisonResult.StringCoercion).Should().Be(ComparisonResult.Undefined, "Comparison without coercion is possible.");
        }
Esempio n. 14
0
        public void CompareCoercesToStringOnPropertyTypeMismatch()
        {
            //Arrange.
            PersonEx2           p1       = ModelsHelper.CreatePersonEx2();
            PersonEx2StringDate p2       = ModelsHelper.CreatePersonEx2StringDate();
            ObjectComparer      comparer = ObjectComparer.Create <PersonEx2, PersonEx2StringDate>();

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

            //Assert.
            result.Should().NotBeNull();
            PropertyComparisonResult propResult = result[nameof(PersonEx2.BirthDate)];

            propResult.Should().NotBeNull();
            (propResult.Result & ComparisonResult.StringCoercion).Should().Be(ComparisonResult.StringCoercion);
        }
Esempio n. 15
0
        public void IgnoreForComparisonAttributeIgnoreForSelf()
        {
            //Arrange.
            PersonExWithIgnoreForSelf p1       = ModelsHelper.CreatePersonExWithIgnoreForSelf();
            PersonExWithIgnoreForSelf p2       = ModelsHelper.CreatePersonExWithIgnoreForSelf();
            ObjectComparer            comparer = ObjectComparer.Create <PersonExWithIgnoreForSelf>();

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

            //Assert.
            result.Should().NotBeNull();
            PropertyComparisonResult propertyResult = result[nameof(PersonExWithIgnoreForSelf.NickName)];

            propertyResult.Should().NotBeNull();
            propertyResult.Result.Should().Be(ComparisonResult.PropertyIgnored);
        }
Esempio n. 16
0
        public void ByValUserDefinedType(UdtStruct value1, UdtStruct value2, ComparisonResult expectedResult)
        {
            //Arrange.
            PersonByValUdt p1 = ModelsHelper.CreatePersonByValUdt();

            p1.ByValProperty = value1;
            PersonByValUdt p2 = ModelsHelper.CreatePersonByValUdt();

            p2.ByValProperty = value2;
            ObjectComparer comparer = ObjectComparer.Create <PersonByValUdt>();

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

            //Assert.
            isDifferent.Should().Be((expectedResult & ComparisonResult.NotEqual) == ComparisonResult.NotEqual);
            result[nameof(PersonByValUdt.ByValProperty)].Result.Should().Be(expectedResult);
        }
Esempio n. 17
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();
        }
Esempio n. 18
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);
        }
        public void CompareUsesMappingsFromConfiguration()
        {
            //Arrange.
            PersonEx            p1 = ModelsHelper.CreatePersonEx();
            PersonExWithPropMap p2 = ModelsHelper.CreatePersonExWithPropMapping();
            var config             = ComparerConfigurator.Configure <PersonEx, PersonExWithPropMap>(true)
                                     .MapProperty(src => src.NickName, dst => dst.NewNickName);
            ObjectComparer comparer = config.CreateComparer();

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

            //Assert.
            result.Should().NotBeNull();
            PropertyMap map = result[nameof(PersonEx.NickName)].MapUsed;

            map.Should().NotBeNull();
            map.TargetType.Should().Be(typeof(PersonExWithPropMap));
            map.TargetProperty.Should().Be(nameof(PersonExWithPropMap.NewNickName));
        }
Esempio n. 21
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);
        }
        public void CompareUsesComparersFromConfiguration()
        {
            //Arrange.
            PersonEx            p1             = ModelsHelper.CreatePersonEx();
            PersonExWithPropMap p2             = ModelsHelper.CreatePersonExWithPropMapping();
            Mock <IComparer>    customComparer = new Mock <IComparer>();

            customComparer.Setup(m => m.Compare(It.IsAny <Genders>(), It.IsAny <object>()))
            .Returns(-1)
            .Verifiable($"{nameof(IComparer.Compare)}() method was not invoked.");
            var config = ComparerConfigurator.Configure <PersonEx, PersonExWithPropMap>(true)
                         .MapProperty(src => src.NickName, dst => dst.NewNickName)
                         .AddComparer <Genders>(customComparer.Object);

            ;
            ObjectComparer comparer = config.CreateComparer();

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

            //Assert.
            result.Should().NotBeNull();
            customComparer.VerifyAll();
        }
Esempio n. 24
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();
        }