public void ChangeItemKey_NullNewKey_Success()
        {
            var collection = new StringKeyedCollection <int>(null, 0);

            collection.GetKeyForItemHandler = item => item.ToString();
            collection.Add(1);
            collection.Add(2);

            // Don't add even numbers.
            collection.GetKeyForItemHandler = item => item % 2 == 0 ? null : item.ToString();

            // Change null key.
            collection.ChangeItemKey(2, "6");
            Assert.Equal(new int[] { 1, 2 }, collection.Items.Cast <int>());
            Assert.Equal(new Dictionary <string, int>
            {
                { "1", 1 },
                { "2", 2 },
                { "6", 2 }
            }, collection.Dictionary
                         );

            // Change non-null key.
            collection.ChangeItemKey(1, "5");
            Assert.Equal(new int[] { 1, 2 }, collection.Items.Cast <int>());
            Assert.Equal(new Dictionary <string, int>
            {
                { "5", 1 },
                { "2", 2 },
                { "6", 2 }
            }, collection.Dictionary
                         );
        }
        public void ChangeItemKey_DifferentKeyAfterCreation_ThrowsArgumentException(string newKey)
        {
            var collection = new StringKeyedCollection <int>(null, 3);

            collection.GetKeyForItemHandler = item => item.ToString();
            collection.Add(1);
            collection.Add(2);
            collection.Add(3);
            Assert.Null(collection.Dictionary);

            collection.GetKeyForItemHandler = item => (item * 2).ToString();

            // Without dictionary.
            collection.ChangeItemKey(2, "10");
            Assert.Equal(new Dictionary <string, int>
            {
                { "2", 1 },
                { "10", 2 },
                { "6", 3 }
            }, collection.Dictionary
                         );

            // With dictionary.
            collection.Add(4);
            AssertExtensions.Throws <ArgumentException>("item", null, () => collection.ChangeItemKey(2, newKey));
            Assert.Equal(new Dictionary <string, int>
            {
                { "2", 1 },
                { "10", 2 },
                { "6", 3 },
                { "8", 4 }
            }, collection.Dictionary
                         );
        }
        public void ChangeItemKey_DuplicateKey_ThrowsArgumentException(IEqualityComparer <string> comparer, string newKey)
        {
            var collection = new StringKeyedCollection <string>(comparer, 3);

            collection.GetKeyForItemHandler = item => item + "_key";
            collection.Add("first");
            collection.Add("second");

            AssertExtensions.Throws <ArgumentException>("key", null, () => collection.ChangeItemKey("first", newKey));
        }
        public void ChangeItemKey_Invoke_Success(IEqualityComparer <string> comparer, int dictionaryCreationThreshold, string item, string newKey, Dictionary <string, string> expectedDictionary)
        {
            var collection = new StringKeyedCollection <string>(comparer, dictionaryCreationThreshold);

            collection.GetKeyForItemHandler = i => i + "_key";
            collection.Add("first");
            collection.Add("second");

            collection.ChangeItemKey(item, newKey);
            Assert.Equal(new string[] { "first", "second" }, collection.Items.Cast <string>());
            Assert.Equal(expectedDictionary, collection.Dictionary);
        }
        public void SetItem_SameValue_Success(IEqualityComparer <string> comparer, int dictionaryCreationThreshold, string value, Dictionary <string, string> expectedDictionary)
        {
            var collection = new StringKeyedCollection <string>(comparer, dictionaryCreationThreshold);

            collection.GetKeyForItemHandler = item => item + "_key";
            collection.Add("first");
            collection.Add("second");
            collection.Add("third");

            collection[0] = value;
            Assert.Equal(new string[] { value, "second", "third" }, collection.Items.Cast <string>());
            Assert.Equal(expectedDictionary, collection.Dictionary);
        }
        public void Contains_DifferentKeyForItemResult_Success(int dictionaryCreationThreshold, bool expected)
        {
            var collection = new StringKeyedCollection <int>(null, dictionaryCreationThreshold);

            collection.GetKeyForItemHandler = i => i.ToString();
            collection.Add(1);
            collection.Add(2);
            collection.Add(3);
            collection.Add(4);

            collection.GetKeyForItemHandler = i => (i * 2).ToString();

            Assert.Equal(expected, collection.Contains("6"));
        }
        public void ClearItems_WithDictionary_Success()
        {
            var collection = new StringKeyedCollection <string>(null, 3);

            collection.GetKeyForItemHandler = item => item + "_key";
            collection.Add("first");
            collection.Add("second");
            collection.Add("third");
            collection.Add("fourth");
            Assert.NotNull(collection.Dictionary);

            collection.ClearItems();
            Assert.Empty(collection);
            Assert.Empty(collection.Dictionary);
        }
        public void Remove_ValidKeyWithDictionary_Success(IEqualityComparer <string> comparer, string key, bool expected, string[] expectedItems, Dictionary <string, string> expectedDictionary)
        {
            var collection = new StringKeyedCollection <string>(comparer, 3);

            collection.GetKeyForItemHandler = i => i + "_key";
            collection.Add("first");
            collection.Add("second");
            collection.Add("third");
            collection.Add("fourth");
            Assert.NotNull(collection.Dictionary);

            Assert.Equal(expected, collection.Remove(key));
            Assert.Equal(expectedItems, collection.Items.Cast <string>());
            Assert.Equal(expectedDictionary, collection.Dictionary);
        }
        public void TryGetValue_DifferentKeyForItemResult_Success(int dictionaryCreationThreshold, bool expected, int expectedItem)
        {
            var collection = new StringKeyedCollection <int>(null, dictionaryCreationThreshold);

            collection.GetKeyForItemHandler = i => i.ToString();
            collection.Add(1);
            collection.Add(2);
            collection.Add(3);
            collection.Add(4);

            collection.GetKeyForItemHandler = i => (i * 2).ToString();

            Assert.Equal(expected, collection.TryGetValue("6", out int item));
            Assert.Equal(expectedItem, item);
        }
        public void Item_GetNoSuchItem_ThrowsKeyNotFoundException()
        {
            var collection = new StringKeyedCollection <string>(null, 3);

            collection.GetKeyForItemHandler = item => item + "_key";

            // Without dictionary.
            collection.Add("first");
            Assert.Throws <KeyNotFoundException>(() => collection["NoSuchKey"]);

            // With dictionary.
            collection.Add("second");
            collection.Add("third");
            collection.Add("fourth");
            Assert.Throws <KeyNotFoundException>(() => collection["NoSuchKey"]);
        }
        public void RemoveItem_InvokeWithDictionary_Success()
        {
            var collection = new StringKeyedCollection <string>(null, 3);

            collection.GetKeyForItemHandler = item => item + "_key";
            collection.Add("first");
            collection.Add("second");
            collection.Add("third");
            collection.Add("fourth");
            Assert.NotNull(collection.Dictionary);

            // Remove from start.
            collection.RemoveItem(0);
            Assert.Equal(new string[] { "second", "third", "fourth" }, collection.Items.Cast <string>());
            Assert.Equal(new Dictionary <string, string>
            {
                { "second_key", "second" },
                { "third_key", "third" },
                { "fourth_key", "fourth" }
            }, collection.Dictionary
                         );

            // Remove from middle.
            collection.RemoveItem(1);
            Assert.Equal(new string[] { "second", "fourth" }, collection.Items.Cast <string>());
            Assert.Equal(new Dictionary <string, string>
            {
                { "second_key", "second" },
                { "fourth_key", "fourth" }
            }, collection.Dictionary
                         );

            // Remove from end.
            collection.RemoveItem(1);
            Assert.Equal(new string[] { "second" }, collection.Items.Cast <string>());
            Assert.Equal(new Dictionary <string, string>
            {
                { "second_key", "second" }
            }, collection.Dictionary
                         );

            // Remove last.
            collection.RemoveItem(0);
            Assert.Empty(collection);
            Assert.Empty(collection.Dictionary);
        }
        public void Item_GetWithComparer_Success()
        {
            var collection = new StringKeyedCollection <string>(StringComparer.OrdinalIgnoreCase, 3);

            collection.GetKeyForItemHandler = item => item + "_key";

            // Without dictionary.
            collection.Add("first");
            Assert.Equal("first", collection["first_key"]);
            Assert.Equal("first", collection["FIRST_KEY"]);

            // With dictionary.
            collection.Add("second");
            collection.Add("third");
            collection.Add("fourth");
            Assert.Equal("first", collection["first_key"]);
            Assert.Equal("first", collection["FIRST_KEY"]);
        }
        public void RemoveItem_DifferentKeyForItemResult_Success()
        {
            var collection = new StringKeyedCollection <int>();

            collection.GetKeyForItemHandler = item => item.ToString();
            collection.Add(1);
            collection.Add(2);

            collection.GetKeyForItemHandler = item => (item * 2).ToString();

            collection.RemoveItem(1);
            Assert.Equal(new int[] { 1 }, collection.Items.Cast <int>());
            Assert.Equal(new Dictionary <string, int>
            {
                { "1", 1 },
                { "2", 2 },
            }, collection.Dictionary
                         );
        }
        public void Contains_NullKeyForItemResult_Success(int dictionaryCreationThreshold, bool expected)
        {
            var collection = new StringKeyedCollection <int>(null, dictionaryCreationThreshold);

            collection.GetKeyForItemHandler = i => i.ToString();
            collection.Add(1);
            collection.Add(2);
            collection.Add(3);
            collection.Add(4);

            // Don't get even numbers.
            collection.GetKeyForItemHandler = i => i % 2 == 0 ? null : i.ToString();

            // Get null key.
            Assert.Equal(expected, collection.Contains("2"));

            // Get non null key.
            Assert.True(collection.Contains("1"));
        }
        public void TryGetValue_NullKeyForItemResult_Success(int dictionaryCreationThreshold, bool expected, int expectedItem)
        {
            var collection = new StringKeyedCollection <int>(null, dictionaryCreationThreshold);

            collection.GetKeyForItemHandler = i => i.ToString();
            collection.Add(1);
            collection.Add(2);
            collection.Add(3);
            collection.Add(4);

            // Don't get even numbers.
            collection.GetKeyForItemHandler = i => i % 2 == 0 ? null : i.ToString();

            // Get null key.
            Assert.Equal(expected, collection.TryGetValue("2", out int item));
            Assert.Equal(expectedItem, item);

            // Get non null key.
            Assert.True(collection.TryGetValue("1", out item));
            Assert.Equal(1, item);
        }
        public void ClearItems_Invoke_ResetsCurrentThresholdCount()
        {
            var collection = new StringKeyedCollection <string>(null, 3);

            collection.GetKeyForItemHandler = item => item + "_key";
            collection.Add("first");
            collection.ClearItems();

            // Add more items - make sure the current count has been reset.
            collection.Add("first");
            Assert.Null(collection.Dictionary);

            collection.Add("second");
            Assert.Null(collection.Dictionary);

            collection.Add("third");
            Assert.Null(collection.Dictionary);

            collection.Add("fourth");
            Assert.NotEmpty(collection.Dictionary);
        }
        public void RemoveItem_InvokeWithoutDictionary_Success()
        {
            var collection = new StringKeyedCollection <string>(null, 3);

            collection.GetKeyForItemHandler = item => item + "_key";
            collection.Add("first");
            Assert.Null(collection.Dictionary);

            collection.RemoveItem(0);
            Assert.Empty(collection);
            Assert.Null(collection.Dictionary);
        }
        public void ChangeItemKey_OnThresholdOfCreation_Success()
        {
            var collection = new StringKeyedCollection <int>(null, 3);

            collection.GetKeyForItemHandler = item => item.ToString();
            collection.Add(1);
            collection.Add(2);
            collection.Add(3);
            Assert.Null(collection.Dictionary);

            collection.GetKeyForItemHandler = item => (item * 2).ToString();

            collection.ChangeItemKey(2, "10");
            Assert.Equal(new Dictionary <string, int>
            {
                { "2", 1 },
                { "10", 2 },
                { "6", 3 }
            }, collection.Dictionary
                         );
        }
        public void ChangeItemKey_NoSuchItem_ThrowsArgumentException()
        {
            var collection = new StringKeyedCollection <string>(StringComparer.OrdinalIgnoreCase, 3);

            collection.GetKeyForItemHandler = item => item + "_key";

            // Empty.
            AssertExtensions.Throws <ArgumentException>("item", null, () => collection.ChangeItemKey("NoSuchItem", "other_key"));
            AssertExtensions.Throws <ArgumentException>("item", null, () => collection.ChangeItemKey("FIRST", "other_key"));

            // Without dictionary.
            collection.Add("first");
            AssertExtensions.Throws <ArgumentException>("item", null, () => collection.ChangeItemKey("NoSuchItem", "other_key"));
            AssertExtensions.Throws <ArgumentException>("item", null, () => collection.ChangeItemKey("FIRST", "other_key"));

            // With dictionary.
            collection.Add("second");
            collection.Add("third");
            collection.Add("fourth");
            AssertExtensions.Throws <ArgumentException>("item", null, () => collection.ChangeItemKey("NoSuchItem", "other_key"));
            AssertExtensions.Throws <ArgumentException>("item", null, () => collection.ChangeItemKey("FIRST", "other_key"));
        }
Exemple #20
0
        private void PushOperations(XElement element)
        {
            foreach (Operation operation in CreateOperationsFromElement(element))
            {
                _propertyOperations = _propertyOperations ?? new StringKeyedCollection <PropertyOperationCollection>();

                if (!_propertyOperations.TryGetValue(operation.PropertyName, out PropertyOperationCollection propertyOperations))
                {
                    propertyOperations = new PropertyOperationCollection(operation.PropertyDefinition);
                    _propertyOperations.Add(propertyOperations);
                }

                propertyOperations.Add(operation);
            }
        }
        public void SetItem_NullNewKey_RemovesKey()
        {
            var collection = new StringKeyedCollection <int>();

            collection.GetKeyForItemHandler = item => item.ToString();
            collection.Add(1);
            Assert.NotEmpty(collection.Dictionary);

            // Don't add even numbers.
            collection.GetKeyForItemHandler = item => item % 2 == 0 ? null : item.ToString();

            collection[0] = 2;
            Assert.Equal(new int[] { 2 }, collection.Items.Cast <int>());
            Assert.Empty(collection.Dictionary);
        }
        public void RemoveItem_NullKeyForItemResult_Success()
        {
            var collection = new StringKeyedCollection <int>(null, 3);

            collection.GetKeyForItemHandler = item => item.ToString();
            collection.Add(1);
            collection.Add(2);
            collection.Add(3);
            collection.Add(4);

            // Don't add/remove even numbers.
            collection.GetKeyForItemHandler = item => item % 2 == 0 ? null : item.ToString();

            // Remove null key.
            collection.RemoveItem(1);
            Assert.Equal(new int[] { 1, 3, 4 }, collection.Items.Cast <int>());
            Assert.Equal(new Dictionary <string, int>
            {
                { "1", 1 },
                { "2", 2 },
                { "3", 3 },
                { "4", 4 }
            }, collection.Dictionary
                         );

            // Remove non-null key.
            collection.RemoveItem(0);
            Assert.Equal(new int[] { 3, 4 }, collection.Items.Cast <int>());
            Assert.Equal(new Dictionary <string, int>
            {
                { "2", 2 },
                { "3", 3 },
                { "4", 4 }
            }, collection.Dictionary
                         );
        }
        public void SetItem_NullNewAndOldKey_DoesNotAffectDictionary()
        {
            var collection = new StringKeyedCollection <int>();

            collection.GetKeyForItemHandler = item => item.ToString();
            collection.Add(2);
            Assert.NotEmpty(collection.Dictionary);

            // Don't add even numbers.
            collection.GetKeyForItemHandler = item => item % 2 == 0 ? null : item.ToString();

            collection[0] = 4;
            Assert.Equal(new int[] { 4 }, collection.Items.Cast <int>());
            Assert.Equal(new Dictionary <string, int>
            {
                { "2", 2 }
            }, collection.Dictionary
                         );
        }