using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
namespace Chernobyl.Collections.Generic.Event
{
///
/// Tests for types.
///
/// The type held by the
/// being tested.
public abstract class EventCollectionTests : CollectionTests
{
///
/// Initializes a new instance of the
/// class.
///
/// True if the order of iteration by
/// the should be tested, false if otherwise.
/// If you specify true, be sure to implement the
/// property.
protected EventCollectionTests(bool testIterationOrder)
: base(testIterationOrder)
{}
[Test, Description("Ensures ICollection.Add(T) reports the items " +
"added through IEventCollection.ItemsAdded.")]
public void AddReportsItems()
{
IEventCollection collection = CreateSingleItemEventCollection();
T itemToAdd = CreateItem();
collection.ItemsAdded += (sender, args) =>
{
Assert.AreEqual(1, args.Items.Length, "Only one items was " +
"added yet IEventCollection.ItemsAdded does not report " +
"this.");
Assert.AreEqual(itemToAdd, args.Items[0], "The value added is not " +
"the value being reported by IEventCollection.ItemsAdded.");
Assert.Pass();
};
collection.Add(itemToAdd);
Assert.Fail("IEventCollection.ItemsAdded was not raised.");
}
[Test, Description("Ensures ICollection.Remove(T) reports the items " +
"removed through IEventCollection.ItemsRemoved.")]
public void RemoveReportsItems()
{
IEventCollection collection = CreateSingleItemEventCollection();
T itemToAdd = CreateItem();
collection.Add(itemToAdd);
collection.ItemsRemoved += (sender, args) =>
{
Assert.AreEqual(1, args.Items.Length, "Only one items was " +
"removed yet IEventCollection.ItemsRemoved does not report " +
"this.");
Assert.AreEqual(itemToAdd, args.Items[0], "The value removed is not " +
"the value being reported by IEventCollection.ItemsRemoved.");
Assert.Pass();
};
collection.Remove(itemToAdd);
Assert.Fail("IEventCollection.ItemsRemoved was not raised.");
}
[Test, Description("Ensures ICollection.Clear() reports the items " +
"removed through IEventCollection.ItemsRemoved.")]
public void ClearReportsItems()
{
IEventCollection collection = CreateSingleItemEventCollection();
T itemToRemove = collection.First();
collection.ItemsRemoved += (sender, args) =>
{
Assert.AreEqual(1, args.Items.Length, "Only one items was " +
"removed yet IEventCollection.ItemsRemoved does not report " +
"this.");
Assert.AreEqual(itemToRemove, args.Items[0], "The value removed is not " +
"the value being reported by IEventCollection.ItemsRemoved.");
Assert.Pass();
};
collection.Clear();
Assert.Fail("IEventCollection.ItemsRemoved was not raised.");
}
///
/// This method should return a new
/// that can be iterated over exactly once. This method should create a
/// new every time it is invoked and
/// not reuse instances.
///
/// The containing a single item.
protected abstract IEventCollection CreateSingleItemEventCollection();
///
/// This method should return a new
/// that can be iterated over five or more times. The items will be
/// compared to the items in
/// but only if the
/// property is set to true. This method should create a new
/// every time it is invoked and not
/// reuse instances. All instances returned through iteration of the
/// should be unique.
///
/// The containing many items.
protected abstract IEventCollection CreateManyItemEventCollection();
protected override ICollection CreateCollection()
{
return CreateManyItemEventCollection();
}
protected override ICollection CreateReadOnlyCollection()
{
// Read only IEventCollection not supported.
return null;
}
protected override IEnumerable CreateSingleItemEnumerable()
{
return CreateSingleItemEventCollection();
}
protected override IEnumerable CreateManyItemEnumerable()
{
return CreateManyItemEventCollection();
}
}
}