public void CompareTestClassBySecondProp() { var o1 = new ComparersTestClass { A = "test1", B = 1 }; var oe1 = new ComparersTestClass { A = "test1", B = 1 }; var o2 = new ComparersTestClass { A = "test1", B = 2 }; var comparer = EqualityComparerEx.Key((ComparersTestClass a) => a.B); Assert.True(comparer.Equals(o1, o1)); Assert.True(comparer.Equals(o1, oe1)); Assert.False(comparer.Equals(o1, o2)); Assert.Equal(comparer.GetHashCode(o1), comparer.GetHashCode(o1)); Assert.Equal(comparer.GetHashCode(o1), comparer.GetHashCode(oe1)); Assert.NotEqual(comparer.GetHashCode(o1), comparer.GetHashCode(o2)); }
public void CompareTestClassByOneProp() { var o1 = new ComparersTestClass { A = "test1", B = 1 }; var oe1 = new ComparersTestClass { A = "test1", B = 2 }; var o2 = new ComparersTestClass { A = "test2", B = 2 }; var comparer = EqualityComparerEx.Func <ComparersTestClass>((a, b) => a.A == b.A, a => a.A.GetHashCode()); Assert.True(comparer.Equals(o1, o1)); Assert.True(comparer.Equals(o1, oe1)); Assert.False(comparer.Equals(o1, o2)); // ReSharper disable once EqualExpressionComparison Assert.True(comparer.GetHashCode(o1) == comparer.GetHashCode(o1)); Assert.True(comparer.GetHashCode(o1) == comparer.GetHashCode(oe1)); Assert.False(comparer.GetHashCode(o1) == comparer.GetHashCode(o2)); }