public void TestDictionary()
        {
            // Default ctor.
            var data = Enumerable.Range(1, 5).ToDictionary(x => x, x => x.ToString());
            var dict = new ReadOnlyDictionary <int, string>(data);

            Assert.AreEqual(5, dict.Count);
            Assert.IsTrue(dict.IsReadOnly);
            CollectionAssert.AreEqual(data, dict);
            CollectionAssert.AreEqual(data.Keys, dict.Keys);
            CollectionAssert.AreEqual(data.Values, dict.Values);

            Assert.IsTrue(dict.GetEnumerator().MoveNext());
            Assert.IsTrue(((IEnumerable)dict).GetEnumerator().MoveNext());

            Assert.IsTrue(dict.ContainsKey(1));
            Assert.IsTrue(dict.Contains(new KeyValuePair <int, string>(4, "4")));
            Assert.AreEqual("3", dict[3]);

            string val;

            Assert.IsTrue(dict.TryGetValue(2, out val));
            Assert.AreEqual("2", val);

            var arr = new KeyValuePair <int, string> [5];

            dict.CopyTo(arr, 0);
            CollectionAssert.AreEqual(data, arr);

            Assert.Throws <NotSupportedException>(() => dict.Add(1, "2"));
            Assert.Throws <NotSupportedException>(() => dict.Add(new KeyValuePair <int, string>(1, "2")));
            Assert.Throws <NotSupportedException>(() => dict.Clear());
            Assert.Throws <NotSupportedException>(() => dict.Remove(1));
            Assert.Throws <NotSupportedException>(() => dict.Remove(new KeyValuePair <int, string>(1, "2")));
        }
Esempio n. 2
0
        public void TestAddKeyValuePair()
        {
            IDictionary <int, bool> toTest = new ReadOnlyDictionary <int, bool>(new Dictionary <int, bool>());

            Assert.IsTrue(toTest.IsReadOnly);
            toTest.Add(new KeyValuePair <int, bool>(100, false));
        }
        public void AddTest2()
        {
            ICollection <KeyValuePair <string, string> > target = new ReadOnlyDictionary <string, string>(testDictionary);

            AssertExtras.Raises <NotSupportedException>(delegate() {
                target.Add(new KeyValuePair <string, string>("123", "abc"));
            });
        }
        public void AddTest1()
        {
            IDictionary <string, string> target = new ReadOnlyDictionary <string, string>(testDictionary);

            AssertExtras.Raises <NotSupportedException>(delegate()
            {
                target.Add("123", "abc");
            });
        }
        public void ChangingStateShouldNotBeSupported()
        {
            Assert.IsTrue(dict.IsReadOnly);
            Assert.Throws <NotSupportedException>(() => dict.Add("key3", "value3"));
            Assert.Throws <NotSupportedException>(() => dict.Remove("key1"));
            Assert.Throws <NotSupportedException>(() => dict.Clear());
            Assert.Throws <NotSupportedException>(() => dict["key1"] = "something");

            Assert.IsInstanceOf <ReadOnlyCollection <string> >(dict.Keys);
            Assert.IsInstanceOf <ReadOnlyCollection <string> >(dict.Values);
        }
Esempio n. 6
0
        public void AddKeyValuePairThrowsOnAReadOnlyDictionary()
        {
            var dict = new Dictionary <int, int>();

            var readOnlyDict = new ReadOnlyDictionary <int, int>(dict);
            var kvp          = new KeyValuePair <int, int>(0, 1234);

            Assert.Throws <NotSupportedException>(() =>
                                                  readOnlyDict.Add(kvp)
                                                  );
        }
Esempio n. 7
0
        public void ReadOnlyDictionary_TryModify()
        {
            var dictionary = new Dictionary <string, string>();
            var readOnly   = new ReadOnlyDictionary <string, string>(dictionary);

            ExtendedAssert.Throws <NotSupportedException>(() => readOnly.Clear());
            ExtendedAssert.Throws <NotSupportedException>(() => readOnly.Add("hello", "world"));
            ExtendedAssert.Throws <NotSupportedException>(() => readOnly["test"] = "fail");
            ExtendedAssert.Throws <NotSupportedException>(() => readOnly.Remove("hello"));
            ExtendedAssert.Throws <NotSupportedException>(() => readOnly.Remove(new KeyValuePair <string, string>("hello", "world")));
        }
Esempio n. 8
0
        public void ReadOnlyDictionary_Unit_Add1_Optimal()
        {
            IDictionary<String, String> dictionary = new Dictionary<String, String>() {
                { "Key1", "Value1" },
                { "Key2", "Value2" },
                { "Key3", "Value3" }
            };
            IDictionary<String, String> target = new ReadOnlyDictionary<String, String>(dictionary);
            String key = "MyKey";
            String value = "MyValue";

            target.Add(key, value);
        }
Esempio n. 9
0
        public void ReadOnlyDictionary_Unit_Add2_Optimal()
        {
            IDictionary<String, String> dictionary = new Dictionary<String, String>() {
                { "Key1", "Value1" },
                { "Key2", "Value2" },
                { "Key3", "Value3" }
            };
            ICollection<KeyValuePair<String, String>> target = new ReadOnlyDictionary<String, String>(dictionary);
            String key = "MyKey";
            String value = "MyValue";
            KeyValuePair<String, String> item = new KeyValuePair<String, String>(key, value);

            target.Add(item);
        }
Esempio n. 10
0
        public void TestConstructFromKeyValuePairs()
        {
            var dic = new ReadOnlyDictionary<string, string>(new[] {
            new KeyValuePair<string, string>("key1", "val1"),
            new KeyValuePair<string, string>("key2", "val2"),
            new KeyValuePair<string, string>("key3", "val3"),
              });

              Assert.IsTrue(dic.IsReadOnly);
              Assert.AreEqual("val1", dic["key1"]);

              try {
            dic.Add("newkey", "newvalue");
            Assert.Fail("NotSupportedException");
              }
              catch (NotSupportedException) {
              }
        }
        public void TestAdd_Throws()
        {
            ReadOnlyDictionary <int, int> dictionary = new ReadOnlyDictionary <int, int>(new Dictionary <int, int>());

            dictionary.Add(0, 0);
        }
        public void TestAdd_Explicit_Throws()
        {
            ICollection <KeyValuePair <int, int> > dictionary = new ReadOnlyDictionary <int, int>(new Dictionary <int, int>());

            dictionary.Add(new KeyValuePair <int, int>());
        }
Esempio n. 13
0
        public void TestConstructFromKeyValuePairsWithSpecifiedEqualityComaperer()
        {
            var dic = new ReadOnlyDictionary<string, string>(new[] {
            new KeyValuePair<string, string>("key1", "val1"),
            new KeyValuePair<string, string>("key2", "val2"),
            new KeyValuePair<string, string>("key3", "val3"),
              }, StringComparer.OrdinalIgnoreCase);

              Assert.IsTrue(dic.IsReadOnly);
              Assert.AreEqual("val1", dic["key1"]);
              Assert.AreEqual("val1", dic["Key1"]);
              Assert.AreEqual("val1", dic["KEY1"]);

              try {
            dic.Add("newkey", "newvalue");
            Assert.Fail("NotSupportedException");
              }
              catch (NotSupportedException) {
              }
        }