public void TestCase03_MethodAccess()
        {
            LambdaEqualityComparer <TestDummy, string> comparer = new LambdaEqualityComparer <TestDummy, string>(td => td.RetrieveTestValue());

            /* Situation 1: TextValue is equal, therefore result of RetrieveTestValue is equal */
            TestDummy dummyA = new TestDummy {
                TextValue = "Hello World"
            };
            TestDummy dummyB = new TestDummy {
                TextValue = "Hello World"
            };
            bool result = comparer.Equals(dummyA, dummyB);

            Assert.IsTrue(result);

            /* Situation 2: TextValue is not equal, therefore result of RetrieveTestValue is not equal */
            dummyA = new TestDummy {
                TextValue = "Hello World"
            };
            dummyB = new TestDummy {
                TextValue = "hello world"
            };
            result = comparer.Equals(dummyA, dummyB);
            Assert.IsFalse(result);
        }
        public void TestCase04_TreeMethodAccess()
        {
            LambdaEqualityComparer <TestDummy, string> comparer = new LambdaEqualityComparer <TestDummy, string>(td => td.Leaf.RetrieveTestValue());

            /* Situation 1: leaf.TextValue is equal, therefore result of leaf.RetrieveTestValue is equal */
            TestDummy dummyA = new TestDummy {
                TextValue = "Hello World", Leaf = new SubTestDummy {
                    TextValue = "Goodmorning"
                }
            };
            TestDummy dummyB = new TestDummy {
                TextValue = "Hello World", Leaf = new SubTestDummy {
                    TextValue = "Goodmorning"
                }
            };
            bool result = comparer.Equals(dummyA, dummyB);

            Assert.IsTrue(result);

            /* Situation 2: leaf.TextValue is not equal, therefore result of leaf.RetrieveTestValue is not equal */
            dummyA = new TestDummy {
                TextValue = "Hello World", Leaf = new SubTestDummy {
                    TextValue = "Goodmorning"
                }
            };
            dummyB = new TestDummy {
                TextValue = "Hello World", Leaf = new SubTestDummy {
                    TextValue = "goodMorning"
                }
            };
            result = comparer.Equals(dummyA, dummyB);
            Assert.IsFalse(result);
        }
        public void TestCase01_DirectPropertyAccess()
        {
            LambdaEqualityComparer <TestDummy, string> comparer = new LambdaEqualityComparer <TestDummy, string>(td => td.TextValue);

            /* Situation 1: TextValue is equal, numeric value is not equal */
            TestDummy dummyA = new TestDummy {
                TextValue = "Hello World", NumericValue = 5
            };
            TestDummy dummyB = new TestDummy {
                TextValue = "Hello World", NumericValue = 6
            };
            bool result = comparer.Equals(dummyA, dummyB);

            Assert.IsTrue(result);

            /* Situation 2: TextValue is not equal, numeric value is not equal */
            dummyA = new TestDummy {
                TextValue = "Hello World", NumericValue = 5
            };
            dummyB = new TestDummy {
                TextValue = "hello world", NumericValue = 6
            };
            result = comparer.Equals(dummyA, dummyB);
            Assert.IsFalse(result);

            /* Situation 3: First TextValue is null, numeric value is not equal */
            dummyA = new TestDummy {
                TextValue = null, NumericValue = 5
            };
            dummyB = new TestDummy {
                TextValue = "hello world", NumericValue = 6
            };
            result = comparer.Equals(dummyA, dummyB);
            Assert.IsFalse(result);

            /* Situation 4: Second TextValue is null, numeric value is not equal */
            dummyA = new TestDummy {
                TextValue = "Hello World", NumericValue = 5
            };
            dummyB = new TestDummy {
                TextValue = null, NumericValue = 6
            };
            result = comparer.Equals(dummyA, dummyB);
            Assert.IsFalse(result);

            /* Situation 5: Both TextValues are null, numeric value is not equal */
            dummyA = new TestDummy {
                TextValue = null, NumericValue = 5
            };
            dummyB = new TestDummy {
                TextValue = null, NumericValue = 6
            };
            result = comparer.Equals(dummyA, dummyB);
            Assert.IsTrue(result);
        }
        public void ConstructorFuncOfTOfObject_should_use_specified_key_equality()
        {
            // Arrange
            var getHashCodeCalled = new List <bool>();
            var equalsCalled      = new List <bool>();
            var mock1             = new MockParent();

            mock1.Key.GetHashCodeProvider = () => { getHashCodeCalled.Add(true); return(0); };
            mock1.Key.EqualsProvider      = obj => { equalsCalled.Add(true); return(obj.Equals(null)); };

            var mock2 = new MockParent();

            mock2.Key.GetHashCodeProvider = () => { getHashCodeCalled.Add(true); return(0); };
            mock2.Key.EqualsProvider      = obj => { equalsCalled.Add(true); return(true); };


            // Act
            var comparer = new LambdaEqualityComparer <MockParent>(_ => _.Key);
            var hashCode = comparer.GetHashCode(mock1);
            var isEqual  = comparer.Equals(mock1, mock2);


            // Assert
            CollectionAssert.AreEqual(new[] { true }, getHashCodeCalled);
            CollectionAssert.AreEqual(new[] { true, true }, equalsCalled);
            Assert.AreEqual(0, hashCode);
            Assert.IsTrue(isEqual);
        }
Exemple #5
0
        public void LambdaEqualityComparer_ShouldInvokeEqualsParameter_IfEqualsIsCalled()
        {
            var called   = false;
            var comparer = new LambdaEqualityComparer <string>((s1, s2) =>
            {
                called = true;
                return(s1 == s2);
            }, s => s.GetHashCode());

            comparer.Equals("s1", "s2");

            called.Should().BeTrue();
        }
Exemple #6
0
        public void CompareFunctionOfNonEqualPropertiesResultsInFalseResult()
        {
            var t1 = new CompareTest {
                Prop = "Test"
            };
            var t2 = new CompareTest {
                Prop = "ASDF"
            };
            var compareFunc = new Func <CompareTest, CompareTest, bool>((x, y) => x.Prop == y.Prop);
            var comparer    = new LambdaEqualityComparer <CompareTest>(compareFunc);

            Assert.IsFalse(comparer.Equals(t1, t2));
            Assert.IsFalse(object.Equals(t1, t2));
        }
        public void ConstructorFuncOfTOfint_should_use_default_Equals_and_specified_GetHashCode()
        {
            // Arrange
            var getHashCodeCalled = false;
            var equalsCalled = false;
            var mock = new MockObject();
            mock.GetHashCodeProvider = () => { getHashCodeCalled = true; return 0; };
            mock.EqualsProvider = obj => { equalsCalled = true; return true; };

            // Act
            var comparer = new LambdaEqualityComparer<MockObject>(_ => 0);
            var hashCode = comparer.GetHashCode(mock);
            var isEqual = comparer.Equals(mock, mock);

            // Assert
            Assert.IsFalse(getHashCodeCalled);
            Assert.IsTrue(equalsCalled);
            Assert.AreEqual(0, hashCode);
            Assert.IsTrue(isEqual);
        }
        public void ConstructorFuncOfTOfint_should_use_default_Equals_and_specified_GetHashCode()
        {
            // Arrange
            var getHashCodeCalled = false;
            var equalsCalled      = false;
            var mock = new MockObject();

            mock.GetHashCodeProvider = () => { getHashCodeCalled = true; return(0); };
            mock.EqualsProvider      = obj => { equalsCalled = true; return(true); };


            // Act
            var comparer = new LambdaEqualityComparer <MockObject>(_ => 0);
            var hashCode = comparer.GetHashCode(mock);
            var isEqual  = comparer.Equals(mock, mock);


            // Assert
            Assert.IsFalse(getHashCodeCalled);
            Assert.IsTrue(equalsCalled);
            Assert.AreEqual(0, hashCode);
            Assert.IsTrue(isEqual);
        }
        public void TestCase02_TreePropertyAccess()
        {
            LambdaEqualityComparer <TestDummy, string> comparer = new LambdaEqualityComparer <TestDummy, string>(td => td.Leaf.TextValue);

            /* Situation 1: TextValue is equal, leaf.TextValue is equal */
            TestDummy dummyA = new TestDummy {
                TextValue = "Hello World", Leaf = new SubTestDummy {
                    TextValue = "Goodmorning"
                }
            };
            TestDummy dummyB = new TestDummy {
                TextValue = "Hello World", Leaf = new SubTestDummy {
                    TextValue = "Goodmorning"
                }
            };
            bool result = comparer.Equals(dummyA, dummyB);

            Assert.IsTrue(result);

            /* Situation 2: TextValue is equal, leaf.TextValue is not equal */
            dummyA = new TestDummy {
                TextValue = "Hello World", Leaf = new SubTestDummy {
                    TextValue = "Goodmorning"
                }
            };
            dummyB = new TestDummy {
                TextValue = "Hello World", Leaf = new SubTestDummy {
                    TextValue = "goodMorning"
                }
            };
            result = comparer.Equals(dummyA, dummyB);
            Assert.IsFalse(result);

            /* Situation 3: TextValue is equal, first leaf.TextValue is null */
            dummyA = new TestDummy {
                TextValue = "Hello World", Leaf = new SubTestDummy {
                    TextValue = null
                }
            };
            dummyB = new TestDummy {
                TextValue = "Hello World", Leaf = new SubTestDummy {
                    TextValue = "Goodmorning"
                }
            };
            result = comparer.Equals(dummyA, dummyB);
            Assert.IsFalse(result);

            /* Situation 4: TextValue is equal, second leaf.TextValue is null */
            dummyA = new TestDummy {
                TextValue = "Hello World", Leaf = new SubTestDummy {
                    TextValue = "Goodmorning"
                }
            };
            dummyB = new TestDummy {
                TextValue = "Hello World", Leaf = new SubTestDummy {
                    TextValue = null
                }
            };
            result = comparer.Equals(dummyA, dummyB);
            Assert.IsFalse(result);

            /* Situation 5: TextValue is equal, both leaf.TextValues are null */
            dummyA = new TestDummy {
                TextValue = "Hello World", Leaf = new SubTestDummy {
                    TextValue = null
                }
            };
            dummyB = new TestDummy {
                TextValue = "Hello World", Leaf = new SubTestDummy {
                    TextValue = null
                }
            };
            result = comparer.Equals(dummyA, dummyB);
            Assert.IsTrue(result);
        }
        public void ConstructorFuncOfTOfObject_should_use_specified_key_equality()
        {
            // Arrange
            var getHashCodeCalled = new List<bool>();
            var equalsCalled = new List<bool>();
            var mock1 = new MockParent();
            mock1.Key.GetHashCodeProvider = () => { getHashCodeCalled.Add(true); return 0; };
            mock1.Key.EqualsProvider = obj => { equalsCalled.Add(true); return obj.Equals(null); };

            var mock2 = new MockParent();
            mock2.Key.GetHashCodeProvider = () => { getHashCodeCalled.Add(true); return 0; };
            mock2.Key.EqualsProvider = obj => { equalsCalled.Add(true); return true; };

            // Act
            var comparer = new LambdaEqualityComparer<MockParent>(_ => _.Key);
            var hashCode = comparer.GetHashCode(mock1);
            var isEqual = comparer.Equals(mock1, mock2);

            // Assert
            CollectionAssert.AreEqual(new[] { true }, getHashCodeCalled);
            CollectionAssert.AreEqual(new[] { true, true }, equalsCalled);
            Assert.AreEqual(0, hashCode);
            Assert.IsTrue(isEqual);
        }