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")));
        }
        public void ClearTest1()
        {
            ICollection <KeyValuePair <string, string> > target = new ReadOnlyDictionary <string, string>(testDictionary);

            AssertExtras.Raises <NotSupportedException>(delegate() {
                target.Clear();
            });
        }
        public void ClearTest()
        {
            IDictionary target = new ReadOnlyDictionary <string, string>(testDictionary);

            AssertExtras.Raises <NotSupportedException>(delegate()
            {
                target.Clear();
            });
        }
        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);
        }
Exemplo n.º 5
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")));
        }
Exemplo n.º 6
0
        public void TestClear()
        {
            var dict = new Dictionary <int, string>()
            {
                { 1, "One" },
                { 2, "Two" },
                { 3, "Three" }
            };

            IDictionary <int, string> toTest = new ReadOnlyDictionary <int, string>(dict);

            Assert.IsTrue(toTest.IsReadOnly);
            toTest.Clear();
        }
        public void TestClear_Throws()
        {
            ReadOnlyDictionary <int, int> dictionary = new ReadOnlyDictionary <int, int>(new Dictionary <int, int>());

            dictionary.Clear();
        }
Exemplo n.º 8
0
        public void ReadOnlyDictionary_Unit_Clear_Optimal()
        {
            IDictionary<String, String> dictionary = new Dictionary<String, String>() {
                { "Key1", "Value1" },
                { "Key2", "Value2" },
                { "Key3", "Value3" }
            };
            IDictionary<String, String> target = new ReadOnlyDictionary<String, String>(dictionary);

            target.Clear();
        }