public void TestReferenceTypes() { TestCollection <ActionSequence> c = new TestCollection <ActionSequence>(); ActionSequence oneElement = new ActionSequence(); 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; }
public void TestImplementationNaked() { 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 dit NOT call our Count."); IEnumerable <int> cLinq = c; Assert.That(cLinq.Count() == 1, "Linq can not use our implementation..."); Assert.That(!countCalled, "...it dit not call our Count property."); // Adressing 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, adressing 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." ); }