Unit tests for the ObservableSet not also tests the change notification events resulting from union/intersect/except methods; added unit tests for read-only set

git-svn-id: file:///srv/devel/repo-conversion/nusu@264 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2012-03-03 15:06:02 +00:00
parent 07a9de6283
commit 4c408a56ad
3 changed files with 260 additions and 27 deletions

View File

@ -97,6 +97,18 @@ namespace Nuclex.Support.Collections {
this.observableSet.Add(123);
}
/// <summary>
/// Verifies that removing items from the set triggers the 'ItemRemoved' event
/// </summary>
[Test]
public void RemovingItemsTriggersEvent() {
this.subscriber.Expects.One.Method((s) => s.ItemAdded(null, null)).WithAnyArguments();
this.observableSet.Add(123);
this.subscriber.Expects.One.Method((s) => s.ItemRemoved(null, null)).WithAnyArguments();
this.observableSet.Remove(123);
}
/// <summary>
/// Verifies that adding items to the set triggers the 'ItemAdded' event
/// </summary>
@ -114,14 +126,21 @@ namespace Nuclex.Support.Collections {
/// </summary>
[Test]
public void ExceptWithSelfEmptiesSet() {
var set = new ObservableSet<int>();
set.Add(1);
set.Add(2);
set.Add(3);
this.subscriber.Expects.Exactly(3).Method(
(s) => s.ItemAdded(null, null)
).WithAnyArguments();
Assert.AreEqual(3, set.Count);
set.ExceptWith(set);
Assert.AreEqual(0, set.Count);
this.observableSet.Add(1);
this.observableSet.Add(2);
this.observableSet.Add(3);
Assert.AreEqual(3, this.observableSet.Count);
this.subscriber.Expects.One.Method((s) => s.Clearing(null, null)).WithAnyArguments();
this.subscriber.Expects.One.Method((s) => s.Cleared(null, null)).WithAnyArguments();
this.observableSet.ExceptWith(this.observableSet);
Assert.AreEqual(0, this.observableSet.Count);
}
/// <summary>
@ -129,16 +148,19 @@ namespace Nuclex.Support.Collections {
/// </summary>
[Test]
public void SetCanBeExceptedWithCollection() {
var set = new ObservableSet<int>();
set.Add(1);
set.Add(2);
this.subscriber.Expects.Exactly(2).Method(
(s) => s.ItemAdded(null, null)
).WithAnyArguments();
this.observableSet.Add(1);
this.observableSet.Add(2);
var collection = new List<int>() { 1 };
Assert.AreEqual(2, set.Count);
set.ExceptWith(collection);
Assert.AreEqual(1, set.Count);
Assert.IsTrue(set.Contains(2));
this.subscriber.Expects.One.Method((s) => s.ItemRemoved(null, null)).WithAnyArguments();
this.observableSet.ExceptWith(collection);
Assert.AreEqual(1, this.observableSet.Count);
Assert.IsTrue(this.observableSet.Contains(2));
}
/// <summary>
@ -146,16 +168,19 @@ namespace Nuclex.Support.Collections {
/// </summary>
[Test]
public void SetCanBeIntersectedWithCollection() {
var set = new ObservableSet<int>();
set.Add(1);
set.Add(2);
this.subscriber.Expects.Exactly(2).Method(
(s) => s.ItemAdded(null, null)
).WithAnyArguments();
this.observableSet.Add(1);
this.observableSet.Add(2);
var collection = new List<int>() { 1 };
Assert.AreEqual(2, set.Count);
set.IntersectWith(collection);
Assert.AreEqual(1, set.Count);
Assert.IsTrue(set.Contains(1));
this.subscriber.Expects.One.Method((s) => s.ItemRemoved(null, null)).WithAnyArguments();
this.observableSet.IntersectWith(collection);
Assert.AreEqual(1, this.observableSet.Count);
Assert.IsTrue(this.observableSet.Contains(1));
}
/// <summary>
@ -223,6 +248,41 @@ namespace Nuclex.Support.Collections {
Assert.IsFalse(set2.SetEquals(set1));
}
/// <summary>
/// Verifies that a set can be symmetrically excepted with another set
/// </summary>
[Test]
public void CanBeSymmetricallyExcepted() {
var set1 = new ObservableSet<int>() { 1, 2, 3 };
var set2 = new ObservableSet<int>() { 3, 4, 5 };
set1.SymmetricExceptWith(set2);
Assert.AreEqual(4, set1.Count);
}
/// <summary>
/// Verifies that a union of two sets can be built
/// </summary>
[Test]
public void CanBeUnioned() {
this.subscriber.Expects.Exactly(3).Method(
(s) => s.ItemAdded(null, null)
).WithAnyArguments();
this.observableSet.Add(1);
this.observableSet.Add(2);
this.observableSet.Add(3);
var set2 = new ObservableSet<int>() { 3, 4, 5 };
this.subscriber.Expects.Exactly(2).Method(
(s) => s.ItemAdded(null, null)
).WithAnyArguments();
this.observableSet.UnionWith(set2);
Assert.AreEqual(5, this.observableSet.Count);
}
/// <summary>Creates mock object for the test</summary>
private MockFactory mockFactory;
/// <summary>Observable set being tested</summary>
@ -234,4 +294,4 @@ namespace Nuclex.Support.Collections {
} // namespace Nuclex.Support.Collections
#endif // UNITTEST
#endif // UNITTEST

View File

@ -247,7 +247,11 @@ namespace Nuclex.Support.Collections {
/// True if the item was contained in the set and is now removed
/// </returns>
public bool Remove(TItem item) {
return this.set.Remove(item);
bool wasRemoved = this.set.Remove(item);
if(wasRemoved) {
OnRemoved(item);
}
return wasRemoved;
}
/// <summary>Creates an enumerator for the set's contents</summary>
@ -307,18 +311,26 @@ namespace Nuclex.Support.Collections {
#endif
}
#region ICollection<T> implementation
/// <summary>Adds an item to the set</summary>
/// <param name="item">Item that will be added to the set</param>
void ICollection<TItem>.Add(TItem item) {
this.set.Add(item);
}
#endregion // ICollection<T> implementation
#region IEnumerable implementation
/// <summary>Creates an enumerator for the set's contents</summary>
/// <returns>A new enumerator for the sets contents</returns>
IEnumerator IEnumerable.GetEnumerator() {
return this.set.GetEnumerator();
}
#endregion // IEnumerable implementation
/// <summary>The set being wrapped</summary>
private ISet<TItem> set;

View File

@ -31,13 +31,174 @@ using NMock;
namespace Nuclex.Support.Collections {
#if false
/// <summary>Unit Test for the read-only set wrapper</summary>
/// <summary>Unit Test for the observable set wrapper</summary>
[TestFixture]
internal class ReadOnlySetTest {
/// <summary>Called before each test is run</summary>
[SetUp]
public void Setup() {
this.set = new HashSet<int>();
this.readOnlySet = new ReadOnlySet<int>(this.set);
}
/// <summary>
/// Verifies that the observable set has a default constructor
/// </summary>
[Test]
public void HasDefaultConstructor() {
Assert.IsNotNull(new ReadOnlySet<int>(new HashSet<int>()));
}
/// <summary>
/// Verifies that an exception is thrown upon any attempt to add items
/// to a read-only set
/// </summary>
[Test]
public void AddingThrowsException() {
Assert.Throws<NotSupportedException>(
delegate() { ((ISet<int>)this.readOnlySet).Add(123); }
);
}
/// <summary>
/// Verifies that an exception is thrown upon any attempt to remove items
/// from a read-only set
/// </summary>
[Test]
public void RemovingThrowsException() {
Assert.Throws<NotSupportedException>(
delegate() { ((ISet<int>)this.readOnlySet).Remove(123); }
);
}
/// <summary>
/// Verifies that an exception is thrown upon any attempt to except
/// the set with another set
/// </summary>
[Test]
public void ExceptingThrowsException() {
Assert.Throws<NotSupportedException>(
delegate() { ((ISet<int>)this.readOnlySet).ExceptWith(null); }
);
}
/// <summary>
/// Verifies that an exception is thrown upon any attempt to intersect
/// the set with another set
/// </summary>
[Test]
public void InsersectThrowsException() {
Assert.Throws<NotSupportedException>(
delegate() { ((ISet<int>)this.readOnlySet).IntersectWith(null); }
);
}
/// <summary>
/// Verifies that it's possible to determine whether a set is a proper subset
/// or superset of another set
/// </summary>
[Test]
public void CanDetermineProperSubsetAndSuperset() {
this.set.Add(1);
this.set.Add(2);
this.set.Add(3);
var set2 = new HashSet<int>() { 1, 3 };
Assert.IsTrue(this.readOnlySet.IsProperSupersetOf(set2));
Assert.IsTrue(set2.IsProperSubsetOf(this.readOnlySet));
set2.Add(2);
Assert.IsFalse(this.readOnlySet.IsProperSupersetOf(set2));
Assert.IsFalse(set2.IsProperSubsetOf(this.readOnlySet));
}
/// <summary>
/// Verifies that it's possible to determine whether a set is a subset
/// or a superset of another set
/// </summary>
[Test]
public void CanDetermineSubsetAndSuperset() {
this.set.Add(1);
this.set.Add(2);
this.set.Add(3);
var set2 = new HashSet<int>() { 1, 2, 3 };
Assert.IsTrue(this.readOnlySet.IsSupersetOf(set2));
Assert.IsTrue(set2.IsSubsetOf(this.readOnlySet));
set2.Add(4);
Assert.IsFalse(this.readOnlySet.IsSupersetOf(set2));
Assert.IsFalse(set2.IsSubsetOf(this.readOnlySet));
}
/// <summary>
/// Verifies that a set can determine if another set overlaps with it
/// </summary>
[Test]
public void CanDetermineOverlap() {
this.set.Add(1);
this.set.Add(3);
this.set.Add(5);
var set2 = new HashSet<int>() { 3 };
Assert.IsTrue(this.readOnlySet.Overlaps(set2));
Assert.IsTrue(set2.Overlaps(this.readOnlySet));
}
/// <summary>
/// Verifies that a set can determine if another set contains the same elements
/// </summary>
[Test]
public void CanDetermineSetEquality() {
this.set.Add(1);
this.set.Add(3);
this.set.Add(5);
var set2 = new HashSet<int>() { 3, 1, 5 };
Assert.IsTrue(this.readOnlySet.SetEquals(set2));
Assert.IsTrue(set2.SetEquals(this.readOnlySet));
this.set.Add(7);
Assert.IsFalse(this.readOnlySet.SetEquals(set2));
Assert.IsFalse(set2.SetEquals(this.readOnlySet));
}
/// <summary>
/// Verifies that any attempt to symmetrically except a read-only set
/// causes an exception
/// </summary>
[Test]
public void SymmetricallyExceptingThrowsException() {
Assert.Throws<NotSupportedException>(
delegate() { ((ISet<int>)this.readOnlySet).SymmetricExceptWith(null); }
);
}
/// <summary>
/// Verifies that any attempt to union a read-only set causes an exception
/// </summary>
[Test]
public void UnioningThrowsException() {
Assert.Throws<NotSupportedException>(
delegate() { ((ISet<int>)this.readOnlySet).UnionWith(null); }
);
}
/// <summary>Set being wrapped in a read-only set</summary>
private ISet<int> set;
/// <summary>Read-only wrapper around the set</summary>
private ReadOnlySet<int> readOnlySet;
}
#endif
} // namespace Nuclex.Support.Collections
#endif // UNITTEST
#endif // UNITTEST