Example #1
0
        public void IndexOfWithStartIndex_ComparerWithMatchBeforeAndAfterStartIndex_IndexOfMatchedInstanceAfterStartIndexIsReturned()
        {
            List <TestDummy> collection = new List <TestDummy> {
                new TestDummy {
                    TextValue = "DummyA"
                },
                new TestDummy {
                    TextValue = "DummyB"
                },
                new TestDummy {
                    TextValue = "DummyC"
                },
                new TestDummy {
                    TextValue = "DummyB"
                },
                new TestDummy {
                    TextValue = "DummyE"
                }
            };

            TestDummy selectionDummy = new TestDummy {
                TextValue = "DummyB"
            };

            /* The extension method will be able to find the object because it uses a custom comparer */
            LambdaEqualityComparer <TestDummy, string> comparer = new LambdaEqualityComparer <TestDummy, string>(td => td.TextValue);
            int result = collection.IndexOf(selectionDummy, 2, comparer);

            Assert.AreEqual(3, 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);
        }
        public void ctor_Observation_IsSet(SimulationResultQuote sut)
        {
            var comparer = new LambdaEqualityComparer<Observation>(
                EqualsMethod: (o1,o2) => o1.Date == o2.Date && o1.CurrentQuoteCount.SequenceEqual(o2.CurrentQuoteCount));

            Assert.Equal<Observation>(Observation, sut.Observation, comparer );
        }
Example #4
0
        public void IndexOfWithStartIndexAndCount_ComparerWithNoMatch_NoIndexIsReturned()
        {
            List <TestDummy> collection = new List <TestDummy> {
                new TestDummy {
                    TextValue = "DummyA"
                },
                new TestDummy {
                    TextValue = "DummyB"
                },
                new TestDummy {
                    TextValue = "DummyC"
                },
                new TestDummy {
                    TextValue = "DummyD"
                },
                new TestDummy {
                    TextValue = "DummyE"
                },
                new TestDummy {
                    TextValue = "DummyF"
                }
            };

            TestDummy selectionDummy = new TestDummy {
                TextValue = "DummyG"
            };

            /* The extension method will be able to find the object because it uses a custom comparer */
            LambdaEqualityComparer <TestDummy, string> comparer = new LambdaEqualityComparer <TestDummy, string>(td => td.TextValue);
            int result = collection.IndexOf(selectionDummy, 0, 6, comparer);

            Assert.AreEqual(-1, result);
        }
Example #5
0
        public void IndexOfWithStartIndexAndCount_ComparerWithMultipleMatchesAfterStartIndexWithinCount_IndexOfFirstMatchedInstanceIsReturned()
        {
            List <TestDummy> collection = new List <TestDummy> {
                new TestDummy {
                    TextValue = "DummyA"
                },
                new TestDummy {
                    TextValue = "DummyB"
                },
                new TestDummy {
                    TextValue = "DummyC"
                },
                new TestDummy {
                    TextValue = "DummyD"
                },
                new TestDummy {
                    TextValue = "DummyC"
                },
                new TestDummy {
                    TextValue = "DummyF"
                }
            };

            TestDummy selectionDummy = new TestDummy {
                TextValue = "DummyC"
            };

            /* The extension method will be able to find the object because it uses a custom comparer */
            LambdaEqualityComparer <TestDummy, string> comparer = new LambdaEqualityComparer <TestDummy, string>(td => td.TextValue);
            int result = collection.IndexOf(selectionDummy, 1, 5, comparer);

            Assert.AreEqual(2, 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 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);
        }
Example #8
0
        private void AssertResultMatchesExpectedTokens(string code, List <TokenBase> expectedTokens)
        {
            Lexer            lexer  = new Lexer();
            List <TokenBase> tokens = lexer.Lex(code);
            bool             equal  = tokens.SequenceEqual(expectedTokens, LambdaEqualityComparer <TokenBase> .Create(
                                                               (a, b) => a.GetType() == b.GetType() && a.Contents == b.Contents
                                                               ));

            Assert.True(equal, "Token sequence did not match.");
        }
        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 EqualityComparer_can_be_provided_in_ToBe()
 {
     using (var s = new MarbleScheduler())
     {
         var veryCuriousEqualityComparer =
             new LambdaEqualityComparer <int>((a, b) => Math.Abs(a - b) == 1, x => 0);
         s.ExpectObservable(Observable.Return(1)).ToBe(
             "(x|)",
             Dict.Map('x', 2),
             comparer: veryCuriousEqualityComparer);
     }
 }
Example #11
0
        public void LambdaEqualityComparer_ShouldInvokeGetHashCodeParameter_IfGetHashCodeIsCalled()
        {
            var called   = false;
            var comparer = new LambdaEqualityComparer <string>((s1, s2) => s1 == s2, s =>
            {
                called = true;
                return(s.GetHashCode());
            });

            comparer.GetHashCode("s");

            called.Should().BeTrue();
        }
Example #12
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();
        }
Example #13
0
        public static IEnumerable <T> Except <T>(this IEnumerable <T> first, IEnumerable <T> second, Func <T, T, bool> comparer, Func <T, int> hasher)
        {
            if (comparer == null)
            {
                throw new ArgumentNullException("comparer");
            }

            if (hasher == null)
            {
                throw new ArgumentNullException("hasher");
            }

            return(first.Except(second, LambdaEqualityComparer.Create(comparer, hasher)));
        }
Example #14
0
        public static HashSet <T> ToHashSet <T>(this IEnumerable <T> source, Func <T, T, bool> comparer, Func <T, int> hasher)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            if (comparer == null)
            {
                throw new ArgumentNullException("comparer");
            }

            return(new HashSet <T>(source, LambdaEqualityComparer.Create(comparer, hasher)));
        }
Example #15
0
        public static IEnumerable <T> Distinct <T>(this IEnumerable <T> source, Func <T, T, bool> comparer, Func <T, int> hasher)
        {
            if (comparer == null)
            {
                throw new ArgumentNullException("comparer");
            }

            if (hasher == null)
            {
                throw new ArgumentNullException("hasher");
            }

            return(source.Distinct(LambdaEqualityComparer.Create(comparer, hasher)));
        }
Example #16
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));
        }
Example #17
0
        public void IndexOf_EmptyCollection_NoIndexIsReturned()
        {
            IList <TestDummy> collection = new List <TestDummy>();

            TestDummy selectionDummy = new TestDummy {
                TextValue = "DummyF"
            };

            /* The extension method will be able to find the object because it uses a custom comparer */
            LambdaEqualityComparer <TestDummy, string> comparer = new LambdaEqualityComparer <TestDummy, string>(td => td.TextValue);
            int result = collection.IndexOf(selectionDummy, comparer);

            Assert.AreEqual(-1, result);
        }
Example #18
0
        public void Remove_EmptyCollection_NoInstanceIsRemovedFromCollection()
        {
            ICollection <TestDummy> collection = new List <TestDummy>();

            TestDummy selectionDummy = new TestDummy {
                TextValue = "DummyF"
            };

            /* The extension method will be able to remove the object because it uses a custom comparer */
            LambdaEqualityComparer <TestDummy, string> comparer = new LambdaEqualityComparer <TestDummy, string>(td => td.TextValue);
            bool result = collection.Remove(selectionDummy, comparer);

            Assert.IsFalse(result);
            Assert.AreEqual(0, collection.Count);
        }
Example #19
0
        private static bool HasDuplicates <T>(this IEnumerable <T> source, LambdaEqualityComparer <T> lambdaEqualityComparer)
        {
            bool result = false;

            HashSet <T> hashSet = lambdaEqualityComparer != null ? new HashSet <T>(lambdaEqualityComparer) : new HashSet <T>();

            foreach (T element in source)
            {
                if (!hashSet.Add(element))
                {
                    result = true;
                    break;
                }
            }

            return(result);
        }
Example #20
0
        public void PerformsSetOperationsWithLambdaComparer()
        {
            var comparer = new LambdaEqualityComparer <TestClass>((x, y) => x.TestProperty == y.TestProperty);

            IEnumerable <TestClass> set1 = new TestClass {
                TestProperty = "Test1"
            }.ToEnumerable();
            IEnumerable <TestClass> set2 = new TestClass {
                TestProperty = "Test2"
            }.ToEnumerable();
            IEnumerable <TestClass> set3 = new TestClass {
                TestProperty = "Test3"
            }.ToEnumerable();

            // Create a Union of [Test1] and [Test2], which should result in [Test1, Test2]
            IEnumerable <TestClass> union1 = set1.Union(set2, comparer);

            Assert.AreEqual(2, union1.Count());

            // Create a Union of [Test1, Test2] and [Test2], which should still result in [Test1, Test2]
            IEnumerable <TestClass> union2 = union1.Union(set2, comparer);

            Assert.AreEqual(2, union2.Count());

            // Create a Subtraction of [Test1, Test2] and [Test1], which should result in [Test2]
            IEnumerable <TestClass> except1 = union2.Except(set1, comparer);

            Assert.AreEqual(1, except1.Count());
            Assert.AreEqual("Test2", except1.First().TestProperty);

            // Create an intersection of [Test1, Test2] and [Test2, Test3], which should result in [Test2]
            IEnumerable <TestClass> union3     = set2.Union(set3, comparer);
            IEnumerable <TestClass> intersect1 = union2.Intersect(union3, comparer);

            Assert.AreEqual(1, intersect1.Count());
            Assert.AreEqual("Test2", intersect1.First().TestProperty);

            // Do a distinct on [Test1, Test2, Test2] which should result in [Test1, Test2]
            IEnumerable <TestClass> concat1   = union2.Concat(set2);
            IEnumerable <TestClass> distinct1 = concat1.Distinct(comparer);

            Assert.AreEqual(2, distinct1.Count());
            Assert.IsTrue(distinct1.Where(x => x.TestProperty == "Test1").Any());
            Assert.IsTrue(distinct1.Where(x => x.TestProperty == "Test2").Any());
        }
Example #21
0
        /// <summary>
        /// Updates a collection, adding/removing values.
        /// All newValues must already exist in the database.
        /// Does not update the fields of each element.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <param name="newValues"></param>
        /// <param name="context"></param>
        public static void UpdateCollection <T>(this ICollection <T> collection, IEnumerable <T> newValues, IMyDbContext context) where T : class, IEntity
        {
            var comparer = new LambdaEqualityComparer <T>((x, y) => x.ID == y.ID, x => x.ID);
            var toAdd    = newValues.Except(collection, comparer).ToList();
            var toRemove = collection.Except(newValues, comparer).ToList();
            //grab everything so we don't need an individual query for every item
            var allItems = context.Set <T>().ToList().ToDictionary(x => x.ID, x => x);

            foreach (var element in toAdd)
            {
                var entry = allItems[element.ID];
                collection.Add(entry);
            }
            foreach (var element in toRemove)
            {
                var entry = allItems[element.ID];
                collection.Remove(entry);
            }
        }
Example #22
0
        public static bool HasDuplicates <T>(this IEnumerable <T> source, Func <T, T, bool> comparer, Func <T, int> hasher)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            if (comparer == null)
            {
                throw new ArgumentNullException("comparer");
            }

            if (hasher == null)
            {
                throw new ArgumentNullException("hasher");
            }

            return(source.HasDuplicates(LambdaEqualityComparer.Create(comparer, hasher)));
        }
        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);
        }
Example #24
0
        public void Remove_ComparerWithNoMatch_NoInstanceIsRemovedFromCollection()
        {
            TestDummy dummyA = new TestDummy {
                TextValue = "DummyA"
            };
            TestDummy dummyB = new TestDummy {
                TextValue = "DummyB"
            };
            TestDummy dummyC = new TestDummy {
                TextValue = "DummyC"
            };
            TestDummy dummyD = new TestDummy {
                TextValue = "DummyD"
            };
            TestDummy dummyE = new TestDummy {
                TextValue = "DummyE"
            };

            List <TestDummy> collection = new List <TestDummy> {
                dummyA, dummyB, dummyC, dummyD, dummyE
            };

            TestDummy selectionDummy = new TestDummy {
                TextValue = "DummyF"
            };

            /* The extension method will be able to remove the object because it uses a custom comparer */
            LambdaEqualityComparer <TestDummy, string> comparer = new LambdaEqualityComparer <TestDummy, string>(td => td.TextValue);
            bool result = collection.Remove(selectionDummy, comparer);

            Assert.IsFalse(result);
            Assert.AreEqual(5, collection.Count);
            CollectionAssert.Contains(collection, dummyA);
            CollectionAssert.Contains(collection, dummyB);
            CollectionAssert.Contains(collection, dummyC);
            CollectionAssert.Contains(collection, dummyD);
            CollectionAssert.Contains(collection, dummyE);
        }
Example #25
0
        public void Remove_ComparerWithMultipleMatches_FirstMatchedInstanceIsRemovedFromCollection()
        {
            TestDummy dummyA = new TestDummy {
                TextValue = "DummyA"
            };
            TestDummy dummyB1 = new TestDummy {
                TextValue = "DummyB"
            };
            TestDummy dummyC = new TestDummy {
                TextValue = "DummyC"
            };
            TestDummy dummyD = new TestDummy {
                TextValue = "DummyD"
            };
            TestDummy dummyB2 = new TestDummy {
                TextValue = "DummyB"
            };

            List <TestDummy> collection = new List <TestDummy> {
                dummyA, dummyB1, dummyC, dummyD, dummyB2
            };

            TestDummy selectionDummy = new TestDummy {
                TextValue = "DummyB"
            };

            /* The extension method will be able to remove the object because it uses a custom comparer */
            LambdaEqualityComparer <TestDummy, string> comparer = new LambdaEqualityComparer <TestDummy, string>(td => td.TextValue);
            bool result = collection.Remove(selectionDummy, comparer);

            Assert.IsTrue(result);
            Assert.AreEqual(4, collection.Count);
            CollectionAssert.Contains(collection, dummyA);
            CollectionAssert.DoesNotContain(collection, dummyB1);
            CollectionAssert.Contains(collection, dummyC);
            CollectionAssert.Contains(collection, dummyD);
            CollectionAssert.Contains(collection, dummyB2);
        }
        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);
        }
Example #27
0
        public static bool ContainsAny <T>(this IEnumerable <T> source, Func <T, T, bool> comparer, Func <T, int> hasher, params T[] values)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            if (comparer == null)
            {
                throw new ArgumentNullException("comparer");
            }

            if (hasher == null)
            {
                throw new ArgumentNullException("hasher");
            }

            if (values == null)
            {
                throw new ArgumentNullException("values");
            }

            bool result = false;

            LambdaEqualityComparer <T> lambdaEqualityComparer = LambdaEqualityComparer.Create(comparer, hasher);

            foreach (T value in values)
            {
                if (source.Contains(value, lambdaEqualityComparer))
                {
                    result = true;
                    break;
                }
            }

            return(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);
        }
Example #29
0
        /// <summary>
        /// Проверка полей базовых контентов для UNION на соответствие
        /// </summary>
        public IEnumerable <RuleViolation> UnionFieldsMatchCheck(List <int> unionSourceContentIds)
        {
            var contents     = ContentRepository.GetList(unionSourceContentIds.Distinct()).ToList();
            var contentNames = new Dictionary <int, string>(contents.Count);

            foreach (var c in contents)
            {
                contentNames.Add(c.Id, c.Name);
            }

            var unionBaseContentFields = VirtualContentRepository.GetFieldsOfContents(unionSourceContentIds);

            // Группируем по имени поля
            var _ = unionBaseContentFields

                    //.GroupBy(f => f.Name, new LambdaComparer<string>((s1, s2) => s1.Equals(s2, StringComparison.InvariantCultureIgnoreCase)));
                    .GroupBy(f => f.Name.ToLowerInvariant());

            var result = new List <RuleViolation>();

            // Проверяет поля на соответствие
            var __ = new LambdaEqualityComparer <Field>(
                (f1, f2) =>
            {
                var violation = СheckUnionMatchedFields(contentNames[f1.ContentId], contentNames[f2.ContentId], f1, f2);
                if (violation != null)
                {
                    result.Add(violation);
                }
                return(true);
            },
                f => f.Name.ToLowerInvariant().GetHashCode()
                );

            return(result);
        }
Example #30
0
        public void EqualityComparer()
        {
            var cmp = new LambdaEqualityComparer <Person>((x, y) =>
                                                          x.Name == y.Name &&
                                                          x.Age == y.Age &&
                                                          x.Sex == y.Sex
                                                          );

            var dic = new Dictionary <Person, object>(cmp);
            var x0  = new Person {
                Name = "Mike", Age = 30, Sex = Sex.Male
            };
            var x1 = new Person {
                Name = "Jack", Age = 30, Sex = Sex.Male
            };
            var x2 = new Person {
                Name = "Mike", Age = 30, Sex = Sex.Male
            };

            dic.Add(x0, new object());
            dic.Add(x1, new object());
            Assert.That(dic.Count, Is.EqualTo(2));
            Assert.That(() => dic.Add(x2, new object()), Throws.TypeOf <System.ArgumentException>());
        }
        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);
        }
Example #32
0
        public static IEnumerable <TResult> Join <TOuter, TInner, TKey, TResult>(this IEnumerable <TOuter> outer, IEnumerable <TInner> inner, Func <TOuter, TKey> outerKeySelector, Func <TInner, TKey> innerKeySelector, Func <TOuter, TInner, TResult> resultSelector, Func <TKey, TKey, bool> comparer, Func <TKey, int> hasher)
        {
            if (comparer == null)
            {
                throw new ArgumentNullException("comparer");
            }

            if (hasher == null)
            {
                throw new ArgumentNullException("hasher");
            }

            return(outer.Join(inner, outerKeySelector, innerKeySelector, resultSelector, LambdaEqualityComparer.Create(comparer, hasher)));
        }
Example #33
0
        public static IEnumerable <TResult> LeftJoin <TLeft, TRight, TKey, TResult>(this IEnumerable <TLeft> left, IEnumerable <TRight> right, Func <TLeft, TKey> leftKeySelector, Func <TRight, TKey> rightKeySelector, Func <TLeft, TRight, TResult> resultSelector, Func <TKey, TKey, bool> comparer, Func <TKey, int> hasher)
        {
            if (comparer == null)
            {
                throw new ArgumentNullException("comparer");
            }

            if (hasher == null)
            {
                throw new ArgumentNullException("hasher");
            }

            var groupJoin = left.GroupJoin(right, leftKeySelector, rightKeySelector, (leftElement, rightElements) => new { LeftElement = leftElement, RightElements = rightElements }, LambdaEqualityComparer.Create(comparer, hasher));

            return(groupJoin.SelectMany(joinedElement => joinedElement.RightElements.DefaultIfEmpty(), (joinedElement, rightElement) => resultSelector(joinedElement.LeftElement, rightElement)));
        }