using System; using System.Collections.Generic; using System.Linq; using Chernobyl.Event; using NUnit.Framework; // ReSharper disable ObjectCreationAsStatement namespace Chernobyl.Creation.Collections { [TestFixture, Description("Tests for the Warehouse type.")] public class WarehouseTests : EnumerationTests { /// /// Initializes a new instance of the class. /// public WarehouseTests() : base(true) {} [Test, Description("Ensures that the Warehouse(IBuilder, ICollection) " + "constructor throws an exception when its first argument " + "is null.")] public void ConstructorExceptionOnNullFirstArgument() { Assert.Throws(() => new Warehouse(null, new List())); } [Test, Description("Ensures that the Warehouse(IBuilder, ICollection) " + "constructor throws an exception when its second argument " + "is null.")] public void ConstructorExceptionOnNullSecondArgument() { Assert.Throws(() => new Warehouse(new Builder(), null)); } [Test, Description("Ensures that the Warehouse does not store an item " + "or cause a crash when construction has been canceled.")] public void DontStoreOnCancellation() { Builder builder = new Builder(); Warehouse warehouse = new Warehouse(builder); builder.Cancel(); Assert.False(warehouse.Any()); } [Test, Description("Ensures that the Warehouse does not store an item " + "or cause a crash when construction has resulted in an " + "error.")] public void DontStoreOnError() { Builder builder = new Builder(); Warehouse warehouse = new Warehouse(builder); builder.Error(); Assert.False(warehouse.Any()); } protected override IEnumerable CreateSingleItemEnumerable() { // The EnumerationTests base class will be used to ensure the // Warehouse properly stores an item from an IBuilder that is // passed to its constructor. Builder builder = new Builder(); Warehouse warehouse = new Warehouse(builder); builder.Create(5); return warehouse; } protected override IEnumerable CreateManyItemEnumerable() { // The EnumerationTests base class will be used to ensure the // Warehouse properly stores an item from an IBuilder that is // passed to its constructor and stores items that are passed to it // using its Warehouse.OnItemCreated(object, CreationEventArgs) // event handler method. // One builder will be passed into the constructor. Builder builder1 = new Builder(); Warehouse warehouse = new Warehouse(builder1); // The other builder will use the Warehouse.OnItemCreated method. Builder builder2 = new Builder(); builder2.Created += warehouse.OnItemCreated; builder2.Create(1); builder1.Create(2); builder2.Create(3); builder1.Create(4); builder2.Create(5); return warehouse; } /// /// Returns the items created by /// are expected to be in. /// protected override IEnumerable OrderedItems => new[] { 1, 2, 3, 4, 5 }; /// /// An used as a helper class to test /// functionality. /// class Builder : Builder { /// /// Causes the event to be raised with the /// passed in values as the created item. /// /// The value to "create". public void Create(int value) { CreatedMethod?.Invoke(this, new CreationEventArgs(value)); } /// /// Causes the event to be raised as if /// construction of an item was canceled. /// public void Cancel() { CreatedMethod?.Invoke(this, new CreationEventArgs(true)); } /// /// Causes the event to be raised as if /// construction of an item resulted in an error. /// public void Error() { CreatedMethod?.Invoke(this, new CreationEventArgs(new Exception())); } } } }