Added some helper classes for INotifyPropertyChanged; added unit tests for the ObservableSet class; documented the second Count property exposed by the multi dictionary
git-svn-id: file:///srv/devel/repo-conversion/nusu@262 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
parent
df169e376a
commit
1a05bf9d63
13 changed files with 1175 additions and 139 deletions
|
@ -239,9 +239,25 @@ namespace Nuclex.Support.Collections {
|
|||
return this.typedDictionary.GetEnumerator();
|
||||
}
|
||||
|
||||
/// <summary>Removes the specified key/value pair from the dictionary</summary>
|
||||
/// <param name="item">Key/value pair that will be removed</param>
|
||||
/// <returns>True if the key/value pair was contained in the dictionary</returns>
|
||||
/// <summary>Number of unique keys in the dictionary</summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This Count property returns a different value from the main interface of
|
||||
/// the multi dictionary to stay consistent with the implemented interfaces.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// If you cast a multi dictionary to a collection of collections, the count
|
||||
/// property of the outer collection should, of course, be the number of inner
|
||||
/// collections it contains (and not the sum of the items contained in all of
|
||||
/// the inner collections).
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// If you use the count property in the main interface of the multi dictionary,
|
||||
/// the value collections are hidden (it behaves as if the key was in the
|
||||
/// dictionary multiple times), so now the sum of all key-value pairs should
|
||||
/// be returned.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
int ICollection<KeyValuePair<TKey, ICollection<TValue>>>.Count {
|
||||
get { return this.typedDictionary.Count; }
|
||||
}
|
||||
|
|
|
@ -39,22 +39,46 @@ namespace Nuclex.Support.Collections {
|
|||
|
||||
public interface IObservableCollectionSubscriber<TItem> {
|
||||
|
||||
/// <summary>Raised when an item has been added to the collection</summary>
|
||||
event EventHandler<ItemEventArgs<TItem>> ItemAdded;
|
||||
/// <summary>Raised when an item is removed from the collection</summary>
|
||||
event EventHandler<ItemEventArgs<TItem>> ItemRemoved;
|
||||
/// <summary>Raised when an item is replaced in the collection</summary>
|
||||
event EventHandler<ItemReplaceEventArgs<TItem>> ItemReplaced;
|
||||
/// <summary>Raised when the collection is about to be cleared</summary>
|
||||
event EventHandler Clearing;
|
||||
/// <summary>Raised when the collection has been cleared</summary>
|
||||
event EventHandler Cleared;
|
||||
/// <summary>Called when an item has been added to the collection</summary>
|
||||
void ItemAdded(object sender, ItemEventArgs<TItem> arguments);
|
||||
/// <summary>Called when an item is removed from the collection</summary>
|
||||
void ItemRemoved(object sender, ItemEventArgs<TItem> arguments);
|
||||
/// <summary>Called when an item is replaced in the collection</summary>
|
||||
void ItemReplaced(object sender, ItemReplaceEventArgs<TItem> arguments);
|
||||
/// <summary>Called when the collection is about to be cleared</summary>
|
||||
void Clearing(object sender, EventArgs arguments);
|
||||
/// <summary>Called when the collection has been cleared</summary>
|
||||
void Cleared(object sender, EventArgs arguments);
|
||||
|
||||
}
|
||||
|
||||
#endregion // interface IObservableCollectionSubscriber<TItem>
|
||||
|
||||
|
||||
/// <summary>Called before each test is run</summary>
|
||||
[SetUp]
|
||||
public void Setup() {
|
||||
this.mockFactory = new MockFactory();
|
||||
this.observableSet = new ObservableSet<int>();
|
||||
|
||||
this.subscriber = this.mockFactory.CreateMock<IObservableCollectionSubscriber<int>>();
|
||||
this.observableSet.ItemAdded += this.subscriber.MockObject.ItemAdded;
|
||||
this.observableSet.ItemRemoved += this.subscriber.MockObject.ItemRemoved;
|
||||
this.observableSet.ItemReplaced += this.subscriber.MockObject.ItemReplaced;
|
||||
this.observableSet.Clearing += this.subscriber.MockObject.Clearing;
|
||||
this.observableSet.Cleared += this.subscriber.MockObject.Cleared;
|
||||
}
|
||||
|
||||
/// <summary>Called after each test has run</summary>
|
||||
[TearDown]
|
||||
public void Teardown() {
|
||||
if(this.mockFactory != null) {
|
||||
this.mockFactory.VerifyAllExpectationsHaveBeenMet();
|
||||
|
||||
this.subscriber = null;
|
||||
this.mockFactory.Dispose();
|
||||
this.mockFactory = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the observable set has a default constructor
|
||||
|
@ -64,6 +88,119 @@ namespace Nuclex.Support.Collections {
|
|||
Assert.IsNotNull(new ObservableSet<int>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that adding items to the set triggers the 'ItemAdded' event
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void AddingItemsTriggersEvent() {
|
||||
this.subscriber.Expects.One.Method((s) => s.ItemAdded(null, null)).WithAnyArguments();
|
||||
this.observableSet.Add(123);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that adding items to the set triggers the 'ItemAdded' event
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void AddingAlreadyContainedItemDoesNotTriggerEvent() {
|
||||
this.subscriber.Expects.One.Method((s) => s.ItemAdded(null, null)).WithAnyArguments();
|
||||
this.observableSet.Add(123);
|
||||
|
||||
this.subscriber.Expects.No.Method((s) => s.ItemAdded(null, null)).WithAnyArguments();
|
||||
this.observableSet.Add(123);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that excepting the set with itself empties the set
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void ExceptWithSelfEmptiesSet() {
|
||||
var set = new ObservableSet<int>();
|
||||
set.Add(1);
|
||||
set.Add(2);
|
||||
set.Add(3);
|
||||
|
||||
Assert.AreEqual(3, set.Count);
|
||||
set.ExceptWith(set);
|
||||
Assert.AreEqual(0, set.Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that a set can be excepted with a collection
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void SetCanBeExceptedWithCollection() {
|
||||
var set = new ObservableSet<int>();
|
||||
set.Add(1);
|
||||
set.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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that a set can be intersected with a collection
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void SetCanBeIntersectedWithCollection() {
|
||||
var set = new ObservableSet<int>();
|
||||
set.Add(1);
|
||||
set.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));
|
||||
}
|
||||
|
||||
/// <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() {
|
||||
var set1 = new ObservableSet<int>() { 1, 2, 3 };
|
||||
var set2 = new ObservableSet<int>() { 1, 3 };
|
||||
|
||||
Assert.IsTrue(set1.IsProperSupersetOf(set2));
|
||||
Assert.IsTrue(set2.IsProperSubsetOf(set1));
|
||||
|
||||
set2.Add(2);
|
||||
|
||||
Assert.IsFalse(set1.IsProperSupersetOf(set2));
|
||||
Assert.IsFalse(set2.IsProperSubsetOf(set1));
|
||||
}
|
||||
|
||||
/// <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() {
|
||||
var set1 = new ObservableSet<int>() { 1, 2, 3 };
|
||||
var set2 = new ObservableSet<int>() { 1, 2, 3 };
|
||||
|
||||
Assert.IsTrue(set1.IsSupersetOf(set2));
|
||||
Assert.IsTrue(set2.IsSubsetOf(set1));
|
||||
|
||||
set2.Add(4);
|
||||
|
||||
Assert.IsFalse(set1.IsSupersetOf(set2));
|
||||
Assert.IsFalse(set2.IsSubsetOf(set1));
|
||||
}
|
||||
|
||||
/// <summary>Creates mock object for the test</summary>
|
||||
private MockFactory mockFactory;
|
||||
/// <summary>Observable set being tested</summary>
|
||||
private ObservableSet<int> observableSet;
|
||||
/// <summary>Subscriber for the observable set's events</summary>
|
||||
private Mock<IObservableCollectionSubscriber<int>> subscriber;
|
||||
|
||||
}
|
||||
|
||||
} // namespace Nuclex.Support.Collections
|
||||
|
|
|
@ -37,9 +37,9 @@ namespace Nuclex.Support.Collections {
|
|||
ISet<TItem>,
|
||||
ICollection<TItem>,
|
||||
#if !NO_SPECIALIZED_COLLECTIONS
|
||||
INotifyCollectionChanged,
|
||||
INotifyCollectionChanged,
|
||||
#endif
|
||||
IObservableCollection<TItem> {
|
||||
IObservableCollection<TItem> {
|
||||
|
||||
/// <summary>Raised when an item has been added to the collection</summary>
|
||||
public event EventHandler<ItemEventArgs<TItem>> ItemAdded;
|
||||
|
@ -109,12 +109,22 @@ namespace Nuclex.Support.Collections {
|
|||
/// </summary>
|
||||
/// <param name="other">Other set this set will be filtered by</param>
|
||||
public void IntersectWith(IEnumerable<TItem> other) {
|
||||
foreach(TItem item in other) {
|
||||
if(!other.Contains(item)) {
|
||||
this.set.Remove(item);
|
||||
OnRemoved(item);
|
||||
var otherSet = other as ISet<TItem>;
|
||||
if(otherSet == null) {
|
||||
otherSet = new HashSet<TItem>(other);
|
||||
}
|
||||
|
||||
var itemsToRemove = new List<TItem>();
|
||||
foreach(TItem item in this.set) {
|
||||
if(!otherSet.Contains(item)) {
|
||||
itemsToRemove.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
for(int index = 0; index < itemsToRemove.Count; ++index) {
|
||||
this.set.Remove(itemsToRemove[index]);
|
||||
OnRemoved(itemsToRemove[index]);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue