public void Add() { IDictionary<string, string> dict = new Dictionary<string, string>(); dict = dict.ToReadOnlyDictionary(); Assert.Throws<NotSupportedException>(() => dict.Add("", "")); }
public void Remove() { IDictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("Key", "Value"); dict = dict.ToReadOnlyDictionary(); Assert.Throws<NotSupportedException>(() => dict.Remove("Key")); }
public void Keys() { IDictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("Key", "Value"); dict.Add("OtherKey", "OtherValue"); dict = dict.ToReadOnlyDictionary(); Assert.IsTrue(dict.Keys.SequenceEqual(new string[] { "Key", "OtherKey" })); }
public void ContainsKey() { IDictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("Key", "Value"); dict = dict.ToReadOnlyDictionary(); Assert.IsTrue(dict.ContainsKey("Key")); Assert.IsFalse(dict.ContainsKey("OtherKey")); }
public void TryGetValue() { IDictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("Key", "Value"); dict = dict.ToReadOnlyDictionary(); string value = null; Assert.IsTrue(dict.TryGetValue("Key", out value)); Assert.AreEqual("Value", value); Assert.IsFalse(dict.TryGetValue("OtherKey", out value)); Assert.IsNull(value); }
public void GetEnumerator() { IDictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("Key", "Value"); dict.Add("OtherKey", "OtherValue"); dict = dict.ToReadOnlyDictionary(); var values = new[] { KeyValuePair.Create("Key", "Value"), KeyValuePair.Create("OtherKey", "OtherValue") }; IEnumerable enumerable = dict; Assert.IsTrue(enumerable.OfType<KeyValuePair<string, string>>().SequenceEqual(values)); }
public void IsReadOnly() { IDictionary<string, string> dict = new Dictionary<string, string>(); dict = dict.ToReadOnlyDictionary(); Assert.IsTrue(dict.IsReadOnly); }
public void RemoveViaCollectionInterface() { IDictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("Key", "Value"); dict = dict.ToReadOnlyDictionary(); Assert.Throws<NotSupportedException>(() => dict.Remove(KeyValuePair.Create("Key", "Value"))); }
public void Count() { IDictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("Key", "Value"); dict.Add("OtherKey", "OtherValue"); dict = dict.ToReadOnlyDictionary(); Assert.AreEqual(2, dict.Count); }
public void CopyTo() { IDictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("Key", "Value"); dict.Add("OtherKey", "OtherValue"); dict = dict.ToReadOnlyDictionary(); var values = new KeyValuePair<string, string>[3]; var expectedValues = new[] { KeyValuePair.Create("Key", "Value"), KeyValuePair.Create("OtherKey", "OtherValue"), default(KeyValuePair<string, string>) }; dict.CopyTo(values, 0); Assert.IsTrue(values.SequenceEqual(expectedValues)); dict.CopyTo(values, 1); expectedValues[2] = expectedValues[1]; expectedValues[1] = expectedValues[0]; Assert.IsTrue(values.SequenceEqual(expectedValues)); }
public void Contains() { IDictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("Key", "Value"); dict.Add("OtherKey", "OtherValue"); dict = dict.ToReadOnlyDictionary(); Assert.IsTrue(dict.Contains(KeyValuePair.Create("Key", "Value"))); Assert.IsTrue(dict.Contains(KeyValuePair.Create("OtherKey", "OtherValue"))); Assert.IsFalse(dict.Contains(KeyValuePair.Create("NonExistent", "NonExistentValue"))); }
public void SetViaKeyIndexer() { IDictionary<string, string> dict = new Dictionary<string, string>(); dict = dict.ToReadOnlyDictionary(); Assert.Throws<NotSupportedException>(() => dict["Key"] = "Value"); }
public void GetViaKeyIndexer() { IDictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("Key", "Value"); dict.Add("OtherKey", "OtherValue"); dict = dict.ToReadOnlyDictionary(); Assert.AreEqual("Value", dict["Key"]); Assert.AreEqual("OtherValue", dict["OtherKey"]); Assert.Throws<KeyNotFoundException>(() => { string val = dict["NonExistentValue"]; }); }