using System; using System.Collections.Generic; using System.Linq; using NUnit.Framework; namespace Chernobyl.Creation.Collections { [TestFixture, Description("Tests for the BuilderCollection type.")] public class BuilderCollectionTests : BuilderEnumerableTests { [Test, Description("Ensures that BuilderCollection() constructor " + "builds the item as expected.")] public void DefaultConstructor() { BuilderCollection, int> test = new BuilderCollection, int>(); Assert.False(test.Any()); } [Test, Description("Ensures that when IBuilder instances are added " + "using BuilderCollection{T}.Add(T) they can raise the " + "BuilderCollection{T}.Created event when necessary.")] public void AddedInstancesCanRaiseCreated() { BuilderCollection, int> test = CreateBuilderCollection(Enumerable.Empty>()); test.Created += (sender, e) => Assert.Pass("IBuilder.Created event was raised."); IFactory factory = new DefaultConstructorFactory(); test.Add(factory); factory.Create(); Assert.Fail("IBuilder.Created event was not raised."); } [Test, Description("Ensures that when IBuilder instances are removed " + "using BuilderCollection{T}.Remove(T) they do not raise the " + "BuilderCollection{T}.Created event when applicable.")] public void RemovedInstancesDoNotRaiseCreated() { IFactory factory = new DefaultConstructorFactory(); BuilderCollection, int> test = CreateBuilderCollection(new[] { factory }); test.Created += (sender, e) => Assert.Fail("IBuilder.Created event was raised."); test.Remove(factory); factory.Create(); } [Test, Description("Ensures that when IBuilder instances are removed " + "using BuilderCollection{T}.Clear() they do not raise the " + "BuilderCollection{T}.Created event when applicable.")] public void ClearedInstancesDoNotRaiseCreated() { IFactory factory = new DefaultConstructorFactory(); BuilderCollection, int> test = CreateBuilderCollection(new[] { factory }); test.Created += (sender, e) => Assert.Fail("IBuilder.Created event was raised."); test.Clear(); factory.Create(); } /// /// Creates the instances that are to be tested. This method should be /// overridden by any set of tests that are testing types derived from /// . This method must /// return a new instance every time it is called. /// /// The instances to /// be taken by the types constructor. /// The instance that is to be tested. protected virtual BuilderCollection, int> CreateBuilderCollection(IEnumerable> builders) { return new BuilderCollection, int>(builders); } [TestFixture, Description("Tests for the ICollection portion of the " + "BuilderCollection type.")] public class CollectionTests : CollectionTests> { public CollectionTests() : base(false) {} protected override ICollection> CreateCollection() { BuilderCollection, int> collection = new BuilderCollection, int>(new[] { new DefaultConstructorFactory(), new DefaultConstructorFactory(), new DefaultConstructorFactory(), new DefaultConstructorFactory(), new DefaultConstructorFactory(), new DefaultConstructorFactory(), new DefaultConstructorFactory() }); return collection; } protected override ICollection> CreateReadOnlyCollection() { // Read only BuilderCollection not supported. return null; } protected override IBuilder CreateItem() { return new DefaultConstructorFactory(); } } protected override BuilderEnumerable, int> CreateBuilderEnumerable(IEnumerable> builders) { return CreateBuilderCollection(builders); } } }