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_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_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 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"));
        }