using System; using System.Collections.Generic; using System.Linq; using System.Text; using NUnit.Framework; namespace Chernobyl.Collections.Generic.Event { [TestFixture, Description("Tests for the SourceEventEnumerable type.")] public class SourceEventEnumerableTests : EnumerationTests { public SourceEventEnumerableTests() : base(true) {} //------------------------------------------------------------------ // Constructor Tests //------------------------------------------------------------------ [Test, Description("Ensures SourceEventEnumerable(IEventEnumerable) accepts " + "a null argument as a valid argument.")] public void ConstructorNullArgumentAccepted() { SourceEventEnumerable sourceEnumerable = CreateSourceEventEnumerable(null); } //------------------------------------------------------------------ // IEventEnumerable.ItemsAdded Tests //------------------------------------------------------------------ [Test, Description("Ensures SourceEventEnumerable raises its " + "IEventEnumerable.ItemsAdded event when SourceEventEnumerable.Source " + "raises the same event.")] public void ItemsAddedRaisedWhenSourceRaisesSameEvent() { DecoratingEventList items = new DecoratingEventList(); SourceEventEnumerable sourceEnumerable = CreateSourceEventEnumerable(items); sourceEnumerable.ItemsAdded += (sender, args) => Assert.Pass(); items.Add(1); Assert.Fail("IEventEnumerable.ItemsAdded was not raised when it should have been."); } [Test, Description("Ensures SourceEventEnumerable does NOT raise its " + "IEventEnumerable.ItemsAdded event when SourceEventEnumerable.Source " + "is set to another value.")] public void ItemsAddedNotRaisedWhenSourceIsSetToAnotherValue() { DecoratingEventList items = new DecoratingEventList(); SourceEventEnumerable sourceEnumerable = CreateSourceEventEnumerable(items); sourceEnumerable.ItemsAdded += (sender, args) => Assert.Fail("IEventEnumerable.ItemsAdded " + "was raised when it should NOT have been."); sourceEnumerable.Source = null; items.Add(1); } //------------------------------------------------------------------ // IEventEnumerable.ItemsRemoved Tests //------------------------------------------------------------------ [Test, Description("Ensures SourceEventEnumerable raises its " + "IEventEnumerable.ItemsRemoved event when SourceEventEnumerable.Source " + "raises the same event.")] public void ItemsRemovedRaisedWhenSourceRaisesSameEvent() { DecoratingEventList items = new DecoratingEventList(); items.Add(1); SourceEventEnumerable sourceEnumerable = CreateSourceEventEnumerable(items); sourceEnumerable.ItemsRemoved += (sender, args) => Assert.Pass(); items.Remove(1); Assert.Fail("IEventEnumerable.ItemsRemoved was not raised when it should have been."); } [Test, Description("Ensures SourceEventEnumerable does NOT raise its " + "IEventEnumerable.ItemsRemoved event when SourceEventEnumerable.Source " + "is set to another value.")] public void ItemsRemovedNotRaisedWhenSourceIsSetToAnotherValue() { DecoratingEventList items = new DecoratingEventList(); items.Add(1); SourceEventEnumerable sourceEnumerable = CreateSourceEventEnumerable(items); sourceEnumerable.ItemsRemoved += (sender, args) => Assert.Fail("IEventEnumerable.ItemsRemoved " + "was raised when it should NOT have been."); sourceEnumerable.Source = null; items.Remove(1); } //------------------------------------------------------------------ // Utility //------------------------------------------------------------------ /// /// Creates the that is to be /// tested. This method should always create a new /// instance. /// /// The source of the items that are being enumerated /// and the events that are raised. If null is specified then this /// instance will be empty. /// The that is to be tested. protected virtual SourceEventEnumerable CreateSourceEventEnumerable(IEventEnumerable source) { return new SourceEventEnumerable(source); } protected override IEnumerable CreateSingleItemEnumerable() { DecoratingEventList items = new DecoratingEventList(); items.Add(1); return CreateSourceEventEnumerable(items); } protected override IEnumerable CreateManyItemEnumerable() { DecoratingEventList items = new DecoratingEventList(); items.AddRange(_orderedItems); return CreateSourceEventEnumerable(items); } protected override IEnumerable OrderedItems { get { return _orderedItems; } } int[] _orderedItems = new[] { 1, 2, 3, 4, 5, 6, 7 }; } }