public void SetRangeShouldFilterDuplicatesWhenNotAllowed() { 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( 1, OtherValue ); Assert.Equal( dict.CountValues( 1 ), 2 ); dict.SetRange( 1, new[] { Value, Value } ); Assert.Equal( dict.CountValues( 1 ), 1 ); Assert.Equal( dict[1].First(), Value ); }
public void SetRangeMethodRaisesEvents() { IList<PropertyChangedEventArgs> propertyArgs = new List<PropertyChangedEventArgs>(); NotifyCollectionChangedEventArgs collectionArgs = null; MultivalueDictionary<string, string> dictionary = new MultivalueDictionary<string, string>(); dictionary.PropertyChanged += ( sender, e ) => propertyArgs.Add( e ); dictionary.CollectionChanged += ( sender, e ) => collectionArgs = e; // replacing with a new item should raise an event dictionary.SetRange( "key", new[] { "value" } ); Assert.Equal( propertyArgs.Count, 2 ); Assert.NotNull( collectionArgs ); Assert.Equal( collectionArgs.Action, NotifyCollectionChangedAction.Add ); Assert.NotNull( collectionArgs.NewItems ); Assert.Equal( 1, collectionArgs.NewItems.Count ); Assert.True( AreEquivalent( collectionArgs.NewItems[0], "key", "value" ) ); Assert.Equal( collectionArgs.NewStartingIndex, 0 ); Assert.Null( collectionArgs.OldItems ); Assert.Equal( collectionArgs.OldStartingIndex, -1 ); // reset propertyArgs.Clear(); collectionArgs = null; // replacing an existing item with an existing value should raise an event dictionary.Set( "key", "value" ); 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", "value" ) ); Assert.Equal( collectionArgs.NewStartingIndex, 0 ); Assert.NotNull( collectionArgs.OldItems ); Assert.Equal( 1, collectionArgs.OldItems.Count ); Assert.True( AreEquivalent( collectionArgs.OldItems[0], "key", "value" ) ); Assert.Equal( collectionArgs.OldStartingIndex, 0 ); // reset propertyArgs.Clear(); collectionArgs = null; // replacing an existing item with a new value should raise an event dictionary.SetRange( "key", new[] { "other value" } ); 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", "other value" ) ); Assert.Equal( collectionArgs.NewStartingIndex, 0 ); Assert.NotNull( collectionArgs.OldItems ); Assert.Equal( 1, collectionArgs.OldItems.Count ); Assert.True( AreEquivalent( collectionArgs.OldItems[0], "key", "value" ) ); Assert.Equal( collectionArgs.OldStartingIndex, 0 ); }
public void SetRangeShouldReplaceValues() { 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( 1, OtherValue ); Assert.Equal( dict.CountValues( 1 ), 2 ); dict.SetRange( 1, new[] { Guid.NewGuid().ToString() } ); Assert.Equal( dict.CountValues( 1 ), 1 ); }