diff options
author | Eric Maupin <ermaup@microsoft.com> | 2018-12-11 00:06:38 +0300 |
---|---|---|
committer | Eric Maupin <ermaup@microsoft.com> | 2018-12-13 21:44:25 +0300 |
commit | 6ae9f0fbd0a55b6bf9b927b1ae9bb38ed3e0741b (patch) | |
tree | 7db947d7ff16795fc6cc50cff278efc09ed85f3c /Xamarin.PropertyEditing | |
parent | 10f7d6c6f419396f0d7ce0d250ef285fbec932c9 (diff) |
[Core/Tests] Add Ordered tests from Cadenza, fix set indexer
Diffstat (limited to 'Xamarin.PropertyEditing')
-rw-r--r-- | Xamarin.PropertyEditing/OrderedDictionary.cs | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/Xamarin.PropertyEditing/OrderedDictionary.cs b/Xamarin.PropertyEditing/OrderedDictionary.cs index fc0b574..4b2ab91 100644 --- a/Xamarin.PropertyEditing/OrderedDictionary.cs +++ b/Xamarin.PropertyEditing/OrderedDictionary.cs @@ -156,10 +156,15 @@ namespace Cadenza.Collections set { TKey existingKey = this.keyOrder[index]; - if (!Equals (existingKey, value.Key)) - this.keyOrder[index] = value.Key; + TValue existingValue = this.dict[existingKey]; + if (!Equals (existingKey, value.Key)) { + if (this.dict.ContainsKey (value.Key)) + throw new ArgumentException ("Existing keys can't be moved by setting them into another index", $"{nameof(value)}.{nameof(value.Key)}"); - TValue existingValue = this.dict[value.Key]; + this.keyOrder[index] = value.Key; + this.dict.Remove (existingKey); + } + this.dict[value.Key] = value.Value; this.roKeys.OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Replace, existingKey, value.Key, index)); @@ -331,8 +336,11 @@ namespace Cadenza.Collections /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is less than 0 or greater than <see cref="Count"/></exception> public void Insert (int index, TKey key, TValue value) { - this.keyOrder.Insert (index, key); + if (index < 0 || index > this.keyOrder.Count) + throw new ArgumentOutOfRangeException (nameof(index)); + this.dict.Add (key, value); + this.keyOrder.Insert (index, key); OnCollectionChanged (NotifyCollectionChangedAction.Add, index, key, value); } @@ -350,6 +358,9 @@ namespace Cadenza.Collections /// <exception cref="ArgumentNullException"><paramref name="key"/> is <c>null</c>.</exception> public bool Remove (TKey key) { + if (key == null) + throw new ArgumentNullException (nameof(key)); + int index = this.keyOrder.IndexOf (key); if (index > -1) { RemoveAt (index); @@ -374,6 +385,7 @@ namespace Cadenza.Collections /// Removes they key and associated value from the dictionary located at <paramref name="index"/>. /// </summary> /// <param name="index">The index at which to remove an item.</param> + /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is less than 0 or greater than <see cref="Count"/></exception> public void RemoveAt (int index) { TKey key = this.keyOrder[index]; |