public void SetItem_InvalidIndex_ThrowsArgumentOutOfRangeException(int index)
        {
            var collection = new StringKeyedCollection <string>(null, 3);

            collection.GetKeyForItemHandler = item => item + "_key";
            AssertExtensions.Throws <ArgumentOutOfRangeException>("index", () => collection.SetItem(index, "first"));
        }
        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 TryGetValue_NullKey_ThrowsArgumentNullException()
        {
            var    collection = new StringKeyedCollection <string>();
            string item       = "item";

            AssertExtensions.Throws <ArgumentNullException>("key", () => collection.TryGetValue(null, out item));
            Assert.Equal("item", item);
        }
        public void InsertItem_AddSameKey_ThrowsArgumentException(IEqualityComparer <string> comparer, int dictionaryCreationThreshold, string key)
        {
            var collection = new StringKeyedCollection <string>(comparer, dictionaryCreationThreshold);

            collection.GetKeyForItemHandler = item => item + "_key";
            collection.InsertItem(0, "first");
            AssertExtensions.Throws <ArgumentException>(dictionaryCreationThreshold > 1 ? "key" : null, null, () => collection.InsertItem(0, key));
        }
        public void Ctor_Default()
        {
            var collection = new StringKeyedCollection <int>();

            Assert.Empty(collection);
            Assert.Equal(EqualityComparer <string> .Default, collection.Comparer);
            Assert.Null(collection.Dictionary);
            Assert.Empty(collection.Items);
            Assert.IsType <List <int> >(collection.Items);
        }
        public void Ctor_IEqualityComparer_Int(IEqualityComparer <string> comparer, int dictionaryCreationThreshold)
        {
            var collection = new StringKeyedCollection <int>(comparer, dictionaryCreationThreshold);

            Assert.Empty(collection);
            Assert.Same(comparer ?? EqualityComparer <string> .Default, collection.Comparer);
            Assert.Null(collection.Dictionary);
            Assert.Empty(collection.Items);
            Assert.IsType <List <int> >(collection.Items);
        }
        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 ClearItems_WithoutDictionary_Success()
        {
            var collection = new StringKeyedCollection <string>(null, 3);

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

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

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

            Assert.Equal(expected, collection.Remove(key));
            Assert.Equal(expectedItems, collection.Items.Cast <string>());
            Assert.Null(collection.Dictionary);
        }
        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 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 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);
        }
Esempio n. 16
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 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 Contains_Invoke_ReturnsExpected(IEqualityComparer <string> comparer, string key, bool expected)
        {
            var collection = new StringKeyedCollection <string>(comparer, 3);

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

            // Without dictionary.
            collection.InsertItem(0, "first");
            Assert.Equal(expected, collection.Contains(key));

            // With dictionary.
            collection.InsertItem(0, "second");
            collection.InsertItem(0, "third");
            collection.InsertItem(0, "fourth");
            Assert.Equal(expected, collection.Contains(key));
        }
        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 TryGetValue_Invoke_ReturnsExpected(IEqualityComparer <string> comparer, string key, bool expected, string expectedItem)
        {
            var collection = new StringKeyedCollection <string>(comparer, 3);

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

            // Without dictionary.
            collection.InsertItem(0, "first");
            Assert.Equal(expected, collection.TryGetValue(key, out string item));
            Assert.Equal(expectedItem, item);

            // With dictionary.
            collection.InsertItem(0, "second");
            collection.InsertItem(0, "third");
            collection.InsertItem(0, "fourth");
            Assert.Equal(expected, collection.TryGetValue(key, out item));
            Assert.Equal(expectedItem, item);
        }
        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 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
                         );
        }
        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 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 InsertItem_InvokeSeveralTimesWithoutCustomThreshold_Success()
        {
            var collection = new StringKeyedCollection <int>();

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

            // Add first.
            collection.InsertItem(0, 1);
            Assert.Equal(new int[] { 1 }, collection.Items.Cast <int>());
            Assert.Equal(new Dictionary <string, int>
            {
                { "2", 1 }
            }, collection.Dictionary
                         );
            Assert.Equal(1, collection["2"]);

            // Add another - end.
            collection.InsertItem(1, 3);
            Assert.Equal(new int[] { 1, 3 }, collection.Items.Cast <int>());
            Assert.Equal(new Dictionary <string, int>
            {
                { "2", 1 },
                { "6", 3 }
            }, collection.Dictionary
                         );
            Assert.Equal(1, collection["2"]);
            Assert.Equal(3, collection["6"]);

            // Add another - middle.
            collection.InsertItem(1, 2);
            Assert.Equal(new int[] { 1, 2, 3 }, collection.Items.Cast <int>());
            Assert.Equal(new Dictionary <string, int>
            {
                { "2", 1 },
                { "4", 2 },
                { "6", 3 }
            }, collection.Dictionary
                         );
            Assert.Equal(1, collection["2"]);
            Assert.Equal(2, collection["4"]);
            Assert.Equal(3, collection["6"]);
        }
        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 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 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 InsertItem_NullKeyForItemResult_Success()
        {
            var collection = new StringKeyedCollection <int>(null, 0);

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

            // Add null without dictionary - not added.
            collection.InsertItem(0, 2);
            Assert.Equal(new int[] { 2 }, collection.Items.Cast <int>());
            Assert.Null(collection.Dictionary);

            // Add non null without dictionary - added.
            collection.InsertItem(0, 1);
            Assert.Equal(new int[] { 1, 2 }, collection.Items.Cast <int>());
            Assert.Equal(new Dictionary <string, int>
            {
                { "1", 1 }
            }, collection.Dictionary
                         );

            // Add null with dictionary - not added.
            collection.InsertItem(0, 4);
            Assert.Equal(new int[] { 4, 1, 2 }, collection.Items.Cast <int>());
            Assert.Equal(new Dictionary <string, int>
            {
                { "1", 1 }
            }, collection.Dictionary
                         );

            // Add non null with dictionary - added.
            collection.InsertItem(0, 3);
            Assert.Equal(new int[] { 3, 4, 1, 2 }, collection.Items.Cast <int>());
            Assert.Equal(new Dictionary <string, int>
            {
                { "1", 1 },
                { "3", 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"));
        }
Esempio n. 31
0
 public Menu()
 {
     Options = new StringKeyedCollection<Option>();
     Places = new StringKeyedCollection<Place>();
     Foods = new WeekDayOptionKeyedCollection<MenuItem>();
 }