using System; using System.Collections.Generic; using NUnit.Framework; namespace Chernobyl.Collections.Generic.Event { // ReSharper disable ObjectCreationAsStatement [TestFixture, Description("Test for the AddRemove type.")] public class AddRemoveTest { //---------------------------------------------------------------------- // AddRemove(IEnumerable, bool) Tests //---------------------------------------------------------------------- [Test, Description("Ensures AddRemove(IEnumerable, bool) throws an" + "ArgumentNullException when the first argument is null.")] public void ConstructorIEnumerableBoolArgumentNullException() { Assert.Throws(() => new AddRemove(null, true)); } [Test, Description("Ensures that items are added to the value set on " + "AddRemove.Collection when the second argument " + "of AddRemove(IEnumerable, bool) is true.")] public void ItemsAddedWhenSecondArgumentIsTrueAndCollectionIsSet() { int[] items = new[] {1, 2, 3}; AddRemove addRemove = new AddRemove(items, true); addRemove.Collection = new List(); Assert.AreEqual(items.Length, addRemove.Collection.Count, "The items were not added to AddRemove.Collection when they " + "should have been."); } [Test, Description("Ensures that items are added to the value set on " + "AddRemove.Collection when the second argument " + "of AddRemove(IEnumerable, bool) is true.")] public void ItemsNotAddedWhenSecondArgumentIsFalseAndCollectionIsSet() { int[] items = new[] { 1, 2, 3 }; AddRemove addRemove = new AddRemove(items, false); addRemove.Collection = new List(); Assert.AreEqual(0, addRemove.Collection.Count, "The items were added to AddRemove.Collection when they should " + "not have been."); } //---------------------------------------------------------------------- // AddRemove(IEnumerable, ICollection, bool) Tests //---------------------------------------------------------------------- [Test, Description("Ensures AddRemove(IEnumerable, ICollection, bool) " + "throws an ArgumentNullException when the first argument " + "is null.")] public void ConstructorIEnumerableICollectionBoolArgumentNullException() { Assert.Throws(() => new AddRemove(null, new List(), true)); } [Test, Description("Ensures AddRemove(IEnumerable, ICollection, bool) " + "excepts a null second argument.")] public void ConstructorIEnumerableICollectionBoolExceptsNullSecondArgument() { AddRemove addRemove = new AddRemove(new[] { 1, 2, 3 }, null, true); } [Test, Description("Ensures AddRemove(IEnumerable, ICollection, bool) " + "adds items when the third argument is true.")] public void ConstructorIEnumerableICollectionBoolAddsItemsWhenThirdArgumentIsTrue() { int[] items = new[] {1, 2, 3}; AddRemove addRemove = new AddRemove(items, new List(), true); Assert.AreEqual(items.Length, addRemove.Collection.Count, "The items were not added to AddRemove.Collection when they " + "should have been."); } [Test, Description("Ensures AddRemove(IEnumerable, ICollection, bool) " + "does NOT add items when the third argument is false.")] public void ConstructorIEnumerableICollectionBoolDoesNotAddItemsWhenThirdArgumentIsFalse() { AddRemove addRemove = new AddRemove(new[] { 1, 2, 3 }, new List(), false); Assert.AreEqual(0, addRemove.Collection.Count, "The items were added to AddRemove.Collection when they should " + "not have been."); } //---------------------------------------------------------------------- // AddRemove.Collection //---------------------------------------------------------------------- [Test, Description("Ensures AddRemove.Collection removes items from " + "previous value and adds items to new value if " + "AddRemove.Add(object, EventArgs) was invoked previously.")] public void CollectionItemsRemovedFromPreviousValueAndAddedToNewValueIfAddWasInvokedPreviously() { int[] items = new[] { 1, 2, 3 }; ICollection oldCollection = new List(); AddRemove addRemove = new AddRemove(items, oldCollection, false); addRemove.Add(this, EventArgs.Empty); ICollection newCollection = new List(); addRemove.Collection = newCollection; Assert.AreEqual(0, oldCollection.Count, "The items are in the previous value of AddRemove.Collection " + "when they should not be."); Assert.AreEqual(items.Length, newCollection.Count, "The items are not in the previous value of AddRemove.Collection " + "when they should be."); } [Test, Description("Ensures AddRemove.Collection does not remove items " + "from new value if AddRemove.Remove(object, EventArgs) " + "was invoked previously.")] public void CollectionItemsNotRemovedFromNewValueIfRemoveWasInvokedPreviously() { int[] items = new[] { 1, 2, 3 }; ICollection oldCollection = new List(); AddRemove addRemove = new AddRemove(items, oldCollection, true); addRemove.Remove(this, EventArgs.Empty); ICollection newCollection = new List(); newCollection.AddRange(items); addRemove.Collection = newCollection; Assert.AreEqual(0, oldCollection.Count, "The items are in the previous value of AddRemove.Collection " + "when they should not be."); Assert.AreEqual(items.Length, newCollection.Count, "The items are not in the previous value of AddRemove.Collection " + "when they should be."); } [Test, Description("Ensures AddRemove.Collection accepts null value.")] public void CollectionCanBeNull() { AddRemove addRemove = new AddRemove(new[] { 1, 2, 3 }, new List(), true); addRemove.Collection = null; } //---------------------------------------------------------------------- // AddRemove.Add(object, EventArgs) //---------------------------------------------------------------------- [Test, Description("Ensures AddRemove.Add(object, EventArgs) adds " + "items to AddRemove.Collection.")] public void AddAddsItemsToCollection() { int[] items = new[] { 1, 2, 3 }; AddRemove addRemove = new AddRemove(items, new List(), false); addRemove.Add(this, EventArgs.Empty); Assert.AreEqual(items.Length, addRemove.Collection.Count, "The items were not added to AddRemove.Collection when they " + "should have been."); } [Test, Description("Ensures AddRemove.Add(object, EventArgs) does not " + "add items to AddRemove.Collection when it is null.")] public void AddDoesNotAddItemsToNullCollection() { int[] items = new[] { 1, 2, 3 }; AddRemove addRemove = new AddRemove(items, null, false); addRemove.Add(this, EventArgs.Empty); Assert.AreEqual(null, addRemove.Collection, "AddRemove.Collection should be null but it is not."); } //---------------------------------------------------------------------- // AddRemove.Remove(object, EventArgs) //---------------------------------------------------------------------- [Test, Description("Ensures AddRemove.Remove(object, EventArgs) removes " + "items from AddRemove.Collection.")] public void RemoveRemovesItemsFromCollection() { int[] items = new[] { 1, 2, 3 }; AddRemove addRemove = new AddRemove(items, new List(), true); addRemove.Remove(this, EventArgs.Empty); Assert.AreEqual(0, addRemove.Collection.Count, "The items were not removed from AddRemove.Collection when they " + "should have been."); } [Test, Description("Ensures AddRemove.Remove(object, EventArgs) does not " + "remove items from AddRemove.Collection when it is null.")] public void RemoveDoesNotRemoveItemsFromNullCollection() { int[] items = new[] { 1, 2, 3 }; AddRemove addRemove = new AddRemove(items, null, true); addRemove.Remove(this, EventArgs.Empty); Assert.AreEqual(null, addRemove.Collection, "AddRemove.Collection should be null but it is not."); } } // ReSharper restore ObjectCreationAsStatement }