예제 #1
0
        private void AssertGenericListMatches(IEnumerable actualEnumerable, IEnumerable <TListElement> expectedEnumerable)
        {
            if (actualEnumerable == null)
            {
                throw new ArgumentNullException("actualEnumerable",
                                                "Actual and expected are not equal (actual was null).");
            }
            if (expectedEnumerable == null)
            {
                throw new ArgumentNullException("expectedEnumerable",
                                                "Actual and expected are not equal (expected was null).");
            }

            var actualList = actualEnumerable.Cast <object>().ToList();

            var expectedList = expectedEnumerable.ToList();

            if (actualList.Count != expectedList.Count)
            {
                throw new ApplicationException(String.Format("Actual count ({0}) does not equal expected count ({1})", actualList.Count, expectedList.Count));
            }

            var equalsFunc = (EntityEqualityComparer != null)
                ? new Func <object, object, bool>((a, b) => EntityEqualityComparer.Equals(a, b))
                : new Func <object, object, bool>(Equals);


            var result = actualList.FirstOrDefault(item => actualList.Count(x => equalsFunc(item, x)) != expectedList.Count(x => equalsFunc(item, x)));

            if (result != null)
            {
                throw new ApplicationException(String.Format("Actual count of item {0} ({1}) does not equal expected item count ({2})", result, actualList.Count(x => x == result), expectedList.Count(x => (object)x == result)));
            }
        }
예제 #2
0
        static void Main(string[] args)
        {
            // More specific = less specific type
            IEqualityComparer <User> entityComparer = new EntityEqualityComparer();
            var areEqual = entityComparer.Equals(user1, user2);

            Console.WriteLine($"user1 {(areEqual ? "==" : "!=")} user2");
            var user1ref = user1;

            areEqual = entityComparer.Equals(user1ref, user1);
            Console.WriteLine($"user1ref {(areEqual ? "==" : "!=")} user1");
            Entity entity1ref = user1;

            // Downcast
            areEqual = (entityComparer as IEqualityComparer <Entity>).Equals(entity1ref, user1);
            Console.WriteLine($"entity1ref {(areEqual ? "==" : "!=")} user1");
        }
 public void UserCanBeComparedWithEntityComparer()
 {
     IEqualityComparer<User> entityComparer = new EntityEqualityComparer();
     var user1 = new User();
     var user2 = new User();
     entityComparer.Equals(user1, user2)
         .Should().BeFalse();
 }
        public void UserCanBeComparedWithEntityComparer()
        {
            IEqualityComparer <User> entityComparer = new EntityEqualityComparer();
            var user1 = new User();
            var user2 = new User();

            entityComparer.Equals(user1, user2)
            .Should().BeFalse();
        }
예제 #5
0
        private void AssertGenericListMatches(IEnumerable actualEnumerable, IEnumerable <TListElement> expectedEnumerable)
        {
            if (actualEnumerable == null)
            {
                throw new ArgumentNullException("actualEnumerable",
                                                "Actual and expected are not equal (actual was null).");
            }
            if (expectedEnumerable == null)
            {
                throw new ArgumentNullException("expectedEnumerable",
                                                "Actual and expected are not equal (expected was null).");
            }

            List <object> actualList = new List <object>();

            foreach (var item in actualEnumerable)
            {
                actualList.Add(item);
            }

            var expectedList = expectedEnumerable.ToList();

            if (actualList.Count != expectedList.Count)
            {
                throw new ApplicationException("Actual count does not equal expected count");
            }

            var equalsFunc = (EntityEqualityComparer != null)
                ? new Func <object, object, bool>((a, b) => EntityEqualityComparer.Equals(a, b))
                : new Func <object, object, bool>(Equals);

            for (var i = 0; i < actualList.Count; i++)
            {
                if (equalsFunc(actualList[i], expectedList[i]))
                {
                    continue;
                }

                var message = String.Format("Expected '{0}' but got '{1}' at position {2}",
                                            expectedList[i],
                                            actualList[i],
                                            i);

                throw new ApplicationException(message);
            }
        }
        static void Main(string[] args)
        {
            IEntityRepository <Entity> entityRepository = new EntityRepository();
            IEntityRepository <Entity> userRepository   = new UserRepository();


            IEqualityComparer <User> entityComparer = new EntityEqualityComparer();
            var user1  = new User();
            var user2  = new User();
            var result = entityComparer.Equals(user1, user2);

            if (result)
            {
                Console.WriteLine("ERROR");
            }

            Console.ReadLine();
        }