public void OverrideMemberHigherPriorityThanByName() { var a1 = new A { ClassB = new B { Property1 = "S1" } }; var a2 = new A { ClassB = new B { Property1 = "S2" } }; var comparer = new Comparer <A>(); var valueComparer1 = Substitute.For <IValueComparer>(); valueComparer1.Compare(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <ComparisonSettings>()).Returns(true); var valueComparer2 = Substitute.For <IValueComparer>(); valueComparer2.Compare(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <ComparisonSettings>()).Returns(true); comparer.AddComparerOverride <string>(valueComparer1); comparer.AddComparerOverride(() => a1.ClassB.Property1, valueComparer1); comparer.AddComparerOverride("Property1", valueComparer2); var differences = comparer.CalculateDifferences(a1, a2).ToList(); CollectionAssert.IsEmpty(differences); valueComparer1.Received().Compare("S1", "S2", Arg.Any <ComparisonSettings>()); valueComparer2.DidNotReceive().Compare("S1", "S2", Arg.Any <ComparisonSettings>()); }
public void OverrideTypeWithFilterAndByName() { var a1 = new A { ClassB = new B { Property1 = "S1" } }; var a2 = new A { ClassB = new B { Property1 = "S2" } }; var comparer = new Comparer <A>(); var valueComparer1 = Substitute.For <IValueComparer>(); valueComparer1.Compare(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <ComparisonSettings>()).Returns(true); var valueComparer2 = Substitute.For <IValueComparer>(); valueComparer2.Compare(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <ComparisonSettings>()).Returns(true); comparer.AddComparerOverride <string>(valueComparer1, memberInfo => memberInfo.Name != "Property1"); comparer.AddComparerOverride("Property1", valueComparer2); var differences = comparer.CalculateDifferences(a1, a2).ToList(); CollectionAssert.IsEmpty(differences); valueComparer1.DidNotReceive().Compare("S1", "S2", Arg.Any <ComparisonSettings>()); valueComparer2.Received().Compare("S1", "S2", Arg.Any <ComparisonSettings>()); }
public void CompareObjects(Ticket ticket1, Ticket ticket2, string UserId) { var comparer = new Comparer(); IEnumerable <Difference> differences; comparer.AddComparerOverride <ICollection <TicketComment> >(DoNotCompareValueComparer.Instance); comparer.AddComparerOverride <ICollection <TicketAttachement> >(DoNotCompareValueComparer.Instance); comparer.AddComparerOverride <ICollection <TicketHistorie> >(DoNotCompareValueComparer.Instance); var isEqual = comparer.Compare(ticket1, ticket2, out differences); foreach (var diff in differences) { TicketHistorie historie = new TicketHistorie() { UserId = UserId, TicketId = ticket1.Id, Property = diff.MemberPath.ToString(), OldValue = diff.Value1.ToString(), NewValue = diff.Value2.ToString(), }; ticket2.TicketHistories.Add(historie); db.TicketHistories.Add(historie); } }
public void ComparerOverrideWhenNullAndValueType() { dynamic a1 = new { Field1 = (object)null }; dynamic a2 = new { Field1 = 5 }; var comparer = new Comparer(); var intComparer = Substitute.For <IValueComparer>(); intComparer.Compare(Arg.Any <object>(), Arg.Any <object>(), Arg.Any <ComparisonSettings>()).Returns(false); intComparer.ToString(5).Returns("5"); comparer.AddComparerOverride <int>(intComparer); var isEqual = comparer.Compare((object)a1, (object)a2, out var differencesEnum); var differences = differencesEnum.ToList(); Assert.IsFalse(isEqual); Assert.AreEqual(1, differences.Count); Assert.IsTrue(differences.Any( d => d.MemberPath == "Field1" && d.Value1 == string.Empty && d.Value2 == "5" && d.DifferenceType == DifferenceTypes.TypeMismatch)); }
public void OverrideBClassProperty1ComparerWithFunctionAndDefaultToString() { var a1 = new A { ClassB = new B { Property1 = "123-456-7898" } }; var a2 = new A { ClassB = new B { Property1 = "(123)-456-7899" } }; var comparer = new Comparer <A>(); comparer.AddComparerOverride( () => new B().Property1, (phone1, phone2, parentSettings) => ExtractDigits(phone1) == ExtractDigits(phone2)); var differences = comparer.CalculateDifferences(a1, a2).ToList(); CollectionAssert.IsNotEmpty(differences); Assert.AreEqual("ClassB.Property1", differences[0].MemberPath); Assert.AreEqual("123-456-7898", differences[0].Value1); Assert.AreEqual("(123)-456-7899", differences[0].Value2); }
public void OverrideBClassProperty1ByMemberInfo() { var a1 = new A { ClassB = new B { Property1 = "S1" } }; var a2 = new A { ClassB = new B { Property1 = "S2" } }; var comparer = new Comparer <A>(); var valueComparer = Substitute.For <IValueComparer>(); valueComparer.Compare(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <ComparisonSettings>()).Returns(true); comparer.AddComparerOverride( typeof(B).GetTypeInfo().GetMember("Property1").First(), valueComparer); var differences = comparer.CalculateDifferences(a1, a2).ToList(); CollectionAssert.IsEmpty(differences); valueComparer.Received().Compare("S1", "S2", Arg.Any <ComparisonSettings>()); }
public void ComparerOverrideWhenNotEqual() { dynamic a1 = new DynamicDictionary(); a1.Field1 = "A"; dynamic a2 = new DynamicDictionary(); a2.Field1 = "B"; var stringComparer = Substitute.For <IValueComparer>(); stringComparer.Compare(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <ComparisonSettings>()).Returns(false); stringComparer.ToString(Arg.Any <string>()).Returns(callInfo => callInfo.Arg <string>()); var comparer = new Comparer(); comparer.AddComparerOverride("Field1", stringComparer); var isEqual = comparer.Compare(a1, a2, out IEnumerable <Difference> differencesEnum); var differences = differencesEnum.ToList(); Assert.IsFalse(isEqual); stringComparer.Received().Compare("A", "B", Arg.Any <ComparisonSettings>()); Assert.AreEqual(1, differences.Count); Assert.IsTrue(differences.Any( d => d.MemberPath == "Field1" && d.Value1 == "A" && d.Value2 == "B" && d.DifferenceType == DifferenceTypes.ValueMismatch)); }
public void OverrideStringComparisonWhenNotEqual() { var a1 = new A { TestProperty1 = "ABC", TestProperty2 = "ABC", ClassB = new B { Property1 = "Str1" } }; var a2 = new A { TestProperty1 = "BCD", TestProperty2 = "ABC", ClassB = new B { Property1 = "Str1" } }; var comparer = new Comparer <A>(); var stringComparer = Substitute.For <IValueComparer>(); stringComparer.Compare(Arg.Any <object>(), Arg.Any <object>(), Arg.Any <ComparisonSettings>()).Returns(false); stringComparer.ToString(Arg.Any <object>()).Returns(info => (info.Arg <object>() ?? string.Empty).ToString()); comparer.AddComparerOverride(typeof(string), stringComparer); var differences = comparer.CalculateDifferences(a1, a2).ToList(); CollectionAssert.IsNotEmpty(differences); Assert.IsTrue(differences.Any(d => d.MemberPath == "TestProperty1" && d.Value1 == "ABC" && d.Value2 == "BCD")); Assert.IsTrue(differences.Any(d => d.MemberPath == "TestProperty2" && d.Value1 == "ABC" && d.Value2 == "ABC")); Assert.IsTrue(differences.Any(d => d.MemberPath == "ClassB.Property1" && d.Value1 == "Str1" && d.Value2 == "Str1")); stringComparer.Received().Compare(Arg.Any <object>(), Arg.Any <object>(), Arg.Any <ComparisonSettings>()); stringComparer.Received().ToString(Arg.Any <object>()); }
public override IComparer <T> GetObjectsComparer <T>(ComparisonSettings settings = null, BaseComparer parentComparer = null) { if (typeof(T) == typeof(Person)) { var comparer = new Comparer <Person>(settings, parentComparer, this); //Do not compare PersonId comparer.AddComparerOverride <Guid>(DoNotCompareValueComparer.Instance); //Sometimes MiddleName can be skipped. Compare only if property has value. comparer.AddComparerOverride( () => new Person().MiddleName, (s1, s2, parentSettings) => string.IsNullOrWhiteSpace(s1) || string.IsNullOrWhiteSpace(s2) || s1 == s2); comparer.AddComparerOverride( () => new Person().PhoneNumber, new PhoneNumberComparer()); return((IComparer <T>)comparer); } return(base.GetObjectsComparer <T>(settings, parentComparer)); }
public void NulableType() { DateTime?dt1 = null; DateTime?dt2 = null; var dateComparer = new DynamicValueComparer <DateTime?> ((date1, date2, settings) => date1 == date2 || (date1 != null && date2 != null && date1.Value.Date == date2.Value.Date)); var comparer = new Comparer <DateTime?>(); comparer.AddComparerOverride <DateTime?>(dateComparer); var result = comparer.Compare(dt1, dt2); Assert.IsTrue(result); }
public void OverrideByMemberFilter() { var a1 = new A { TestProperty1 = "ABC" }; var a2 = new A { TestProperty1 = "ABC" }; var comparer = new Comparer <A>(); var valueComparer = Substitute.For <IValueComparer>(); valueComparer.Compare(Arg.Any <object>(), Arg.Any <object>(), Arg.Any <ComparisonSettings>()).Returns(true); comparer.AddComparerOverride(valueComparer, member => member.Name == "TestProperty1"); var isEqual = comparer.Compare(a1, a2); Assert.IsTrue(isEqual); valueComparer.Received(1).Compare(Arg.Is("ABC"), Arg.Is("ABC"), Arg.Any <ComparisonSettings>()); }
public void OverridePropertyComparisonWhenEqual() { var a1 = new A { TestProperty1 = "ABC" }; var a2 = new A { TestProperty1 = "BCD" }; var comparer = new Comparer <A>(); var valueComparer = Substitute.For <IValueComparer>(); valueComparer.Compare(Arg.Any <object>(), Arg.Any <object>(), Arg.Any <ComparisonSettings>()).Returns(true); comparer.AddComparerOverride(() => a1.TestProperty1, valueComparer); var isEqual = comparer.Compare(a1, a2); Assert.IsTrue(isEqual); valueComparer.Received(1).Compare(Arg.Any <object>(), Arg.Any <object>(), Arg.Any <ComparisonSettings>()); }
public void ComparerOverrideWhenEqual() { dynamic a1 = new DynamicDictionary(); a1.Field1 = "A"; dynamic a2 = new DynamicDictionary(); a2.Field1 = " A "; var stringComparer = Substitute.For <IValueComparer>(); stringComparer.Compare(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <ComparisonSettings>()).Returns(true); var comparer = new Comparer(); comparer.AddComparerOverride("Field1", stringComparer); var isEqual = comparer.Compare(a1, a2); Assert.IsTrue(isEqual); stringComparer.Received().Compare("A", " A ", Arg.Any <ComparisonSettings>()); }
public void OverrideIntComparisonWhenEqual() { var a1 = new A { IntArray = new[] { 1, 2 } }; var a2 = new A { IntArray = new[] { 1, 3 } }; var comparer = new Comparer <A>(); var intComparer = Substitute.For <IValueComparer>(); intComparer.Compare(Arg.Any <object>(), Arg.Any <object>(), Arg.Any <ComparisonSettings>()).Returns(true); intComparer.ToString(Arg.Any <object>()).Returns(info => (info.Arg <object>() ?? string.Empty).ToString()); comparer.AddComparerOverride(typeof(int), intComparer); var isEqual = comparer.Compare(a1, a2); Assert.IsTrue(isEqual); intComparer.Received().Compare(Arg.Any <object>(), Arg.Any <object>(), Arg.Any <ComparisonSettings>()); }
public void OverrideBClassProperty1ComparerWhenEqual() { var a1 = new A { ClassB = new B { Property1 = "123-456-7898" } }; var a2 = new A { ClassB = new B { Property1 = "(123)-456-7898" } }; var valueComparer = CreatePhoneComparer(); var comparer = new Comparer <A>(); comparer.AddComparerOverride(() => new B().Property1, valueComparer); var isEqual = comparer.Compare(a1, a2); Assert.IsTrue(isEqual); }
public void ComparerOverrideWhenNullAndValueType() { dynamic a1 = new DynamicDictionary(); a1.Field1 = null; dynamic a2 = new DynamicDictionary(); a2.Field1 = 5.0; var comparer = new Comparer(); var doubleComparer = Substitute.For <IValueComparer>(); doubleComparer.Compare(Arg.Any <object>(), Arg.Any <object>(), Arg.Any <ComparisonSettings>()).Returns(false); doubleComparer.ToString(5.0).Returns("5.0"); comparer.AddComparerOverride <double>(doubleComparer); var isEqual = comparer.Compare(a1, a2, out IEnumerable <Difference> differencesEnum); var differences = differencesEnum.ToList(); Assert.IsFalse(isEqual); Assert.AreEqual(1, differences.Count); Assert.IsTrue(differences.Any( d => d.MemberPath == "Field1" && d.Value1 == string.Empty && d.Value2 == "5.0" && d.DifferenceType == DifferenceTypes.TypeMismatch)); }
public void OverrideStringComparisonWhenEqual() { var a1 = new A { TestProperty1 = "ABC", TestProperty2 = "ABC", ClassB = new B { Property1 = "Str1" } }; var a2 = new A { TestProperty1 = "BCD", TestProperty2 = "ABC", ClassB = new B { Property1 = "Str2" } }; var comparer = new Comparer <A>(); var stringComparer = Substitute.For <IValueComparer>(); stringComparer.Compare(Arg.Any <object>(), Arg.Any <object>(), Arg.Any <ComparisonSettings>()).Returns(true); comparer.AddComparerOverride <string>(stringComparer); var isEqual = comparer.Compare(a1, a2); Assert.IsTrue(isEqual); stringComparer.Received().Compare(Arg.Any <object>(), Arg.Any <object>(), Arg.Any <ComparisonSettings>()); }
public void OverrideIntComparisonWhenNotEqual() { var a1 = new A { IntArray = new[] { 1, 2 } }; var a2 = new A { IntArray = new[] { 1, 3 } }; var comparer = new Comparer <A>(); var stringComparer = Substitute.For <IValueComparer>(); stringComparer.Compare(Arg.Any <object>(), Arg.Any <object>(), Arg.Any <ComparisonSettings>()).Returns(false); stringComparer.ToString(Arg.Any <object>()).Returns(info => (info.Arg <object>() ?? string.Empty).ToString()); comparer.AddComparerOverride <int>(stringComparer); var differences = comparer.CalculateDifferences(a1, a2).ToList(); CollectionAssert.IsNotEmpty(differences); Assert.IsTrue(differences.Any(d => d.MemberPath == "IntArray[0]" && d.Value1 == "1" && d.Value2 == "1")); Assert.IsTrue(differences.Any(d => d.MemberPath == "IntArray[1]" && d.Value1 == "2" && d.Value2 == "3")); stringComparer.Received().Compare(Arg.Any <object>(), Arg.Any <object>(), Arg.Any <ComparisonSettings>()); stringComparer.Received().ToString(Arg.Any <object>()); }
public void OverrideBClassProperty1ComparerWithFunction() { var a1 = new A { ClassB = new B { Property1 = "123-456-7898" } }; var a2 = new A { ClassB = new B { Property1 = "(123)-456-7898" } }; var comparer = new Comparer <A>(); comparer.AddComparerOverride( () => new B().Property1, (phone1, phone2, parentSettings) => ExtractDigits(phone1) == ExtractDigits(phone2), s => s); var isEqual = comparer.Compare(a1, a2); Assert.IsTrue(isEqual); }
public void OverrideByName() { var a1 = new A { ClassB = new B { Property1 = "123-456-7899" } }; var a2 = new A { ClassB = new B { Property1 = "(123)-456-7899" } }; var comparer = new Comparer <A>(); var phoneComparer = Substitute.For <IValueComparer>(); phoneComparer.Compare("123-456-7899", "(123)-456-7899", Arg.Any <ComparisonSettings>()).Returns(true); comparer.AddComparerOverride("Property1", phoneComparer); var differences = comparer.CalculateDifferences(a1, a2).ToList(); CollectionAssert.IsEmpty(differences); phoneComparer.Received().Compare("123-456-7899", "(123)-456-7899", Arg.Any <ComparisonSettings>()); }
public void OverrideBClassProperty1ComparerWhenNotEqual() { var a1 = new A { ClassB = new B { Property1 = "123-456-7898" } }; var a2 = new A { ClassB = new B { Property1 = "(123)-456-7899" } }; var valueComparer = CreatePhoneComparer(); var comparer = new Comparer <A>(); comparer.AddComparerOverride(() => new B().Property1, valueComparer); var differences = comparer.CalculateDifferences(a1, a2).ToList(); CollectionAssert.IsNotEmpty(differences); Assert.AreEqual("ClassB.Property1", differences[0].MemberPath); Assert.AreEqual("123-456-7898", differences[0].Value1); Assert.AreEqual("(123)-456-7899", differences[0].Value2); }
public void Overrides() { var comparer = new Comparer <ClassA>(); // Type //Use MyComparer to compare all members of type string comparer.AddComparerOverride <string>(new MyValueComparer()); comparer.AddComparerOverride(typeof(string), new MyValueComparer()); //Use MyComparer to compare all members of type string except members which name starts with "Xyz" comparer.AddComparerOverride <string>(new MyValueComparer(), member => !member.Name.StartsWith("Xyz")); // Member Info //Use MyValueComparer to compare StringProperty of ClassA //comparer.AddComparerOverride(() => new ClassA().StringProperty, new MyValueComparer()); //comparer.AddComparerOverride( // typeof(ClassA).GetTypeInfo().GetMember("StringProperty").First(), // new MyValueComparer()); //Compare StringProperty of ClassA by length. If length equal consider that values are equal //comparer.AddComparerOverride( // () => new ClassA().StringProperty, // (s1, s2, parentSettings) => s1?.Length == s2?.Length, // s => s?.ToString()); //comparer.AddComparerOverride( // () => new ClassA().StringProperty, // (s1, s2, parentSettings) => s1?.Length == s2?.Length); // Member filter //comparer.AddComparerOverride(new MyValueComparer(), m => m.Name == "StringProperty"); // Member Name comparer.AddComparerOverride("StringProperty", new MyValueComparer()); //Exception comparer.AddComparerOverride <string>(new MyValueComparer(), member => member.Name.StartsWith("String")); comparer.AddComparerOverride <string>(DoNotCompareValueComparer.Instance, member => member.Name.EndsWith("Property")); //var result = comparer.Compare(a1, a2);//Exception here }
public void SetUp() { _comparer = new Comparer(new ComparisonSettings { UseDefaultIfMemberNotExist = true }); //Some fields should be ignored _comparer.AddComparerOverride("ConnectionString", DoNotCompareValueComparer.Instance); _comparer.AddComparerOverride("Email", DoNotCompareValueComparer.Instance); _comparer.AddComparerOverride("Notifications", DoNotCompareValueComparer.Instance); //Smart Modes are disabled by default. These fields are not case sensitive var disabledByDefaultComparer = new DefaultValueValueComparer <string>("Disabled", IgnoreCaseStringsValueComparer.Instance); _comparer.AddComparerOverride("SmartMode1", disabledByDefaultComparer); _comparer.AddComparerOverride("SmartMode2", disabledByDefaultComparer); _comparer.AddComparerOverride("SmartMode3", disabledByDefaultComparer); //http prefix in URLs should be ignored var urlComparer = new DynamicValueComparer <string>( (url1, url2, settings) => url1.Trim('/').Replace(@"http://", string.Empty) == url2.Trim('/').Replace(@"http://", string.Empty)); _comparer.AddComparerOverride("SomeUrl", urlComparer); _comparer.AddComparerOverride("SomeOtherUrl", urlComparer); //DataCompression is Off by default. _comparer.AddComparerOverride("DataCompression", new DefaultValueValueComparer <string>("Off", NulableStringsValueComparer.Instance)); //ProcessTaskTimeout and TotalProcessTimeout fields have default values. _comparer.AddComparerOverride("ProcessTaskTimeout", new DefaultValueValueComparer <long>(100, DefaultValueComparer.Instance)); _comparer.AddComparerOverride("TotalProcessTimeout", new DefaultValueValueComparer <long>(500, DefaultValueComparer.Instance)); }