public void RemoveShouldRemoveValues() { const string Value = "test value"; const string OtherValue = "other value"; var dict = new MultivalueDictionary<int, string>( () => new HashSet<string>() ); dict.Add( 1, Value ); dict.Add( 2, OtherValue ); Assert.Equal( dict.Count, 2 ); Assert.Equal( dict.CountValues( 1 ), 1 ); Assert.Equal( dict.CountValues( 2 ), 1 ); dict.Remove( 1 ); Assert.Equal( dict.Count, 1 ); Assert.Equal( dict.CountValues( 2 ), 1 ); Assert.False( dict.Remove( 2, Value ) ); Assert.True( dict.Remove( 2, OtherValue ) ); }
public void RemoveMethodRaisesEvents() { IList<PropertyChangedEventArgs> propertyArgs = new List<PropertyChangedEventArgs>(); NotifyCollectionChangedEventArgs collectionArgs = null; MultivalueDictionary<string, string> dictionary = new MultivalueDictionary<string, string>(); dictionary.Add( "key", "value1" ); dictionary.Add( "key", "value2" ); dictionary.Add( "other key", "value" ); dictionary.PropertyChanged += ( sender, e ) => propertyArgs.Add( e ); dictionary.CollectionChanged += ( sender, e ) => collectionArgs = e; // removing an nonexisting item should not raise an event dictionary.Remove( "made up key" ); Assert.Equal( propertyArgs.Count, 0 ); Assert.Null( collectionArgs ); // reset propertyArgs.Clear(); collectionArgs = null; // removing an existing item with a nonexisting value should not raise an event dictionary.Remove( "key", "made up value" ); Assert.Equal( propertyArgs.Count, 0 ); Assert.Null( collectionArgs ); // reset propertyArgs.Clear(); collectionArgs = null; // removing an entire existing collection by key should raise an event dictionary.Remove( "other key" ); Assert.Equal( propertyArgs.Count, 2 ); Assert.Equal( propertyArgs[0].PropertyName, "Count" ); Assert.Equal( propertyArgs[1].PropertyName, "Item[]" ); Assert.NotNull( collectionArgs ); Assert.Equal( collectionArgs.Action, NotifyCollectionChangedAction.Remove ); Assert.Null( collectionArgs.NewItems ); Assert.Equal( collectionArgs.NewStartingIndex, -1 ); Assert.NotNull( collectionArgs.OldItems ); Assert.Equal( 1, collectionArgs.OldItems.Count ); Assert.True( AreEquivalent( collectionArgs.OldItems[0], "other key", "value" ) ); Assert.Equal( collectionArgs.OldStartingIndex, 1 ); // reset propertyArgs.Clear(); collectionArgs = null; // removing an existing item with an existing value should raise an event dictionary.Remove( "key", "value1" ); Assert.Equal( propertyArgs.Count, 1 ); Assert.NotNull( collectionArgs ); Assert.Equal( collectionArgs.Action, NotifyCollectionChangedAction.Replace ); Assert.NotNull( collectionArgs.NewItems ); Assert.Equal( 1, collectionArgs.NewItems.Count ); Assert.True( AreEquivalent( collectionArgs.NewItems[0], "key", "value2" ) ); Assert.Equal( collectionArgs.NewStartingIndex, 0 ); Assert.NotNull( collectionArgs.OldItems ); Assert.Equal( 1, collectionArgs.OldItems.Count ); Assert.True( AreEquivalent( collectionArgs.OldItems[0], "key", "value1", "value2" ) ); Assert.Equal( collectionArgs.OldStartingIndex, 0 ); // reset propertyArgs.Clear(); collectionArgs = null; // removing an existing item with for the last existing value should raise an event dictionary.Remove( "key", "value2" ); Assert.Equal( propertyArgs.Count, 3 ); Assert.Equal( propertyArgs[0].PropertyName, "Item[]" ); Assert.Equal( propertyArgs[1].PropertyName, "Count" ); Assert.Equal( propertyArgs[2].PropertyName, "Item[]" ); Assert.NotNull( collectionArgs ); Assert.Equal( collectionArgs.Action, NotifyCollectionChangedAction.Remove ); Assert.Null( collectionArgs.NewItems ); Assert.Equal( collectionArgs.NewStartingIndex, -1 ); Assert.NotNull( collectionArgs.OldItems ); Assert.Equal( 1, collectionArgs.OldItems.Count ); Assert.True( AreEquivalent( collectionArgs.OldItems[0], "key", new string[0] ) ); Assert.Equal( collectionArgs.OldStartingIndex, 0 ); }
public void ShouldRemoveKeyWhenLastItemInValueIsRemoved() { const string Value = "test value"; var dict = new MultivalueDictionary<int, string>(); dict.Add( 1, Value ); Assert.Equal( dict.Count, 1 ); Assert.Equal( dict[1].Count, 1 ); dict.Remove( 1, Value ); Assert.Equal( dict.Count, 0 ); }