/PrismLibrary/Desktop/Prism.Tests/ListDictionaryFixture.cs

https://github.com/jeffras/Prism-4-with-WinForms · C# · 267 lines · 201 code · 50 blank · 16 comment · 0 complexity · 4f0c86c9659665c1f5675d244398a1fc MD5 · raw file

  1. //===================================================================================
  2. // Microsoft patterns & practices
  3. // Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4. //===================================================================================
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===================================================================================
  11. // The example companies, organizations, products, domain names,
  12. // e-mail addresses, logos, people, places, and events depicted
  13. // herein are fictitious. No association with any real company,
  14. // organization, product, domain name, email address, logo, person,
  15. // places, or events is intended or should be inferred.
  16. //===================================================================================
  17. using System;
  18. using System.Collections.Generic;
  19. using Microsoft.Practices.Prism;
  20. using Microsoft.VisualStudio.TestTools.UnitTesting;
  21. namespace Microsoft.Practices.Prism.Tests
  22. {
  23. [TestClass]
  24. public class ListDictionaryFixture
  25. {
  26. static ListDictionary<string, object> list;
  27. [TestInitialize]
  28. public void SetUp()
  29. {
  30. list = new ListDictionary<string, object>();
  31. }
  32. [ExpectedException(typeof(ArgumentNullException))]
  33. [TestMethod]
  34. public void AddThrowsIfKeyNull()
  35. {
  36. list.Add(null, new object());
  37. }
  38. [ExpectedException(typeof(ArgumentNullException))]
  39. [TestMethod]
  40. public void AddThrowsIfValueNull()
  41. {
  42. list.Add("", null);
  43. }
  44. [TestMethod]
  45. public void CanAddValue()
  46. {
  47. object value1 = new object();
  48. object value2 = new object();
  49. list.Add("foo", value1);
  50. list.Add("foo", value2);
  51. Assert.AreEqual(2, list["foo"].Count);
  52. Assert.AreSame(value1, list["foo"][0]);
  53. Assert.AreSame(value2, list["foo"][1]);
  54. }
  55. [TestMethod]
  56. public void CanIndexValuesByKey()
  57. {
  58. list.Add("foo", new object());
  59. list.Add("foo", new object());
  60. Assert.AreEqual(2, list["foo"].Count);
  61. }
  62. [ExpectedException(typeof(ArgumentNullException))]
  63. [TestMethod]
  64. public void ThrowsIfRemoveKeyNull()
  65. {
  66. list.Remove(null, new object());
  67. }
  68. [TestMethod]
  69. public void CanRemoveValue()
  70. {
  71. object value = new object();
  72. list.Add("foo", value);
  73. list.Remove("foo", value);
  74. Assert.AreEqual(0, list["foo"].Count);
  75. }
  76. [TestMethod]
  77. public void CanRemoveValueFromAllLists()
  78. {
  79. object value = new object();
  80. list.Add("foo", value);
  81. list.Add("bar", value);
  82. list.Remove(value);
  83. Assert.AreEqual(0, list.Values.Count);
  84. }
  85. [TestMethod]
  86. public void RemoveNonExistingValueNoOp()
  87. {
  88. list.Add("foo", new object());
  89. list.Remove("foo", new object());
  90. }
  91. [TestMethod]
  92. public void RemoveNonExistingKeyNoOp()
  93. {
  94. list.Remove("foo", new object());
  95. }
  96. [ExpectedException(typeof(ArgumentNullException))]
  97. [TestMethod]
  98. public void ThrowsIfRemoveListKeyNull()
  99. {
  100. list.Remove(null);
  101. }
  102. [TestMethod]
  103. public void CanRemoveList()
  104. {
  105. list.Add("foo", new object());
  106. list.Add("foo", new object());
  107. bool removed = list.Remove("foo");
  108. Assert.IsTrue(removed);
  109. Assert.AreEqual(0, list.Keys.Count);
  110. }
  111. [TestMethod]
  112. public void CanSetList()
  113. {
  114. List<object> values = new List<object>();
  115. values.Add(new object());
  116. list.Add("foo", new object());
  117. list.Add("foo", new object());
  118. list["foo"] = values;
  119. Assert.AreEqual(1, list["foo"].Count);
  120. }
  121. [TestMethod]
  122. public void CanEnumerateKeyValueList()
  123. {
  124. int count = 0;
  125. list.Add("foo", new object());
  126. list.Add("foo", new object());
  127. foreach (KeyValuePair<string, IList<object>> pair in list)
  128. {
  129. foreach (object value in pair.Value)
  130. {
  131. count++;
  132. }
  133. Assert.AreEqual("foo", pair.Key);
  134. }
  135. Assert.AreEqual(2, count);
  136. }
  137. [TestMethod]
  138. public void CanGetFlatListOfValues()
  139. {
  140. list.Add("foo", new object());
  141. list.Add("foo", new object());
  142. list.Add("bar", new object());
  143. IList<object> values = list.Values;
  144. Assert.AreEqual(3, values.Count);
  145. }
  146. [TestMethod]
  147. public void IndexerAccessAlwaysSucceeds()
  148. {
  149. IList<object> values = list["foo"];
  150. Assert.IsNotNull(values);
  151. }
  152. [ExpectedException(typeof(ArgumentNullException))]
  153. [TestMethod]
  154. public void ThrowsIfContainsKeyNull()
  155. {
  156. list.ContainsKey(null);
  157. }
  158. [TestMethod]
  159. public void CanAskContainsKey()
  160. {
  161. Assert.IsFalse(list.ContainsKey("foo"));
  162. }
  163. [TestMethod]
  164. public void CanAskContainsValueInAnyList()
  165. {
  166. object obj = new object();
  167. list.Add("foo", new object());
  168. list.Add("bar", new object());
  169. list.Add("baz", obj);
  170. bool contains = list.ContainsValue(obj);
  171. Assert.IsTrue(contains);
  172. }
  173. [TestMethod]
  174. public void CanClearDictionary()
  175. {
  176. list.Add("foo", new object());
  177. list.Add("bar", new object());
  178. list.Add("baz", new object());
  179. list.Clear();
  180. Assert.AreEqual(0, list.Count);
  181. }
  182. [TestMethod]
  183. public void CanGetFilteredValuesByKeys()
  184. {
  185. list.Add("foo", new object());
  186. list.Add("bar", new object());
  187. list.Add("baz", new object());
  188. IEnumerable<object> filtered = list.FindAllValuesByKey(delegate(string key)
  189. {
  190. return key.StartsWith("b");
  191. });
  192. int count = 0;
  193. foreach (object obj in filtered)
  194. {
  195. count++;
  196. }
  197. Assert.AreEqual(2, count);
  198. }
  199. [TestMethod]
  200. public void CanGetFilteredValues()
  201. {
  202. list.Add("foo", DateTime.Now);
  203. list.Add("bar", new object());
  204. list.Add("baz", DateTime.Today);
  205. IEnumerable<object> filtered = list.FindAllValues(delegate(object value)
  206. {
  207. return value is DateTime;
  208. });
  209. int count = 0;
  210. foreach (object obj in filtered)
  211. {
  212. count++;
  213. }
  214. Assert.AreEqual(2, count);
  215. }
  216. }
  217. }