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 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 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 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 RemoveItem_Invoke_ResetsCurrentThresholdCount()
        {
            var collection = new StringKeyedCollection <string>(null, 3);

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

            // 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_InvalidIndex_ThrowsArgumentOutOfRangeException(int index)
        {
            var collection = new StringKeyedCollection <string>(null, 3);

            AssertExtensions.Throws <ArgumentOutOfRangeException>("index", () => collection.RemoveItem(index));
        }