예제 #1
0
        public void covariant_Contains_accepts_any_types()
        {
            TestCollection <Animal> c = new TestCollection <Animal>();
            Animal oneElement         = new Animal(null);

            c.Content.Add(oneElement);

            bool containsCalled = false;

            c.ContainsCalled += (o, e) => { containsCalled = true; };
            Assert.That(c.Contains(oneElement));
            Assert.That(containsCalled, "It is our Contains method that is called."); containsCalled = false;
            Assert.That(!c.Contains(56), "Contains should accept ANY object without any error.");
            Assert.That(containsCalled, "It is our Contains method that is called."); containsCalled = false;
            Assert.That(!c.Contains(null), "Contains should accept ANY object without any error.");
            Assert.That(containsCalled); containsCalled = false;
        }
예제 #2
0
        public void linq_with_mere_IReadOnlyCollection_implementation_is_not_optimal_for_Count()
        {
            TestCollection <int> c = new TestCollection <int>();

            c.Content.Add(2);

            bool containsCalled = false, countCalled = false;

            c.ContainsCalled += (o, e) => { containsCalled = true; };
            c.CountCalled    += (o, e) => { countCalled = true; };

            Assert.That(c.Count == 1);
            Assert.That(countCalled, "Count property on the concrete type logs the calls."); countCalled = false;

            Assert.That(c.Count() == 1, "Use Linq extension methods (on the concrete type).");
            Assert.That(!countCalled, "The Linq extension method did NOT call our Count.");

            IEnumerable <int> cLinq = c;

            Assert.That(cLinq.Count() == 1, "Linq can not use our implementation...");
            Assert.That(!countCalled, "...it did not call our Count property.");

            // Addressing the concrete type: it is our method that is called.
            Assert.That(c.Contains(2));
            Assert.That(containsCalled, "It is our Contains method that is called (not the Linq one)."); containsCalled = false;
            Assert.That(!c.Contains(56));
            Assert.That(containsCalled, "It is our Contains method that is called."); containsCalled = false;
            Assert.That(!c.Contains(null), "Contains should accept ANY object without any error.");
            Assert.That(containsCalled, "It is our Contains method that is called."); containsCalled = false;

            // Unfortunately, addressing the IEnumerable base type, Linq has no way to use our methods...
            Assert.That(cLinq.Contains(2));
            Assert.That(!containsCalled, "Linq use the enumerator to do the job.");
            Assert.That(!cLinq.Contains(56));
            Assert.That(!containsCalled);
            // Linq Contains() accept only parameter of the generic type.
            //Assert.That( !cLinq.Contains( null ), "Contains should accept ANY object without any error." );
        }