/src/Manos.Tests/Manos.Http/RequestDataDictionaryTest.cs

http://github.com/jacksonh/manos · C# · 400 lines · 284 code · 93 blank · 23 comment · 0 complexity · f487701ca83f9ab97cca3caff6ffe47a MD5 · raw file

  1. //
  2. // Copyright (C) 2010 Jackson Harper (jackson@manosdemono.com)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. //
  24. using System;
  25. using System.Collections;
  26. using System.Collections.Generic;
  27. using System.Collections.Specialized;
  28. using NUnit.Framework;
  29. using Manos.Collections;
  30. namespace Manos.Server.Tests
  31. {
  32. [TestFixture()]
  33. public class RequestDataDictionaryTest
  34. {
  35. [Test()]
  36. public void Count_NoItems_ReturnsZero ()
  37. {
  38. var rdd = new RequestDataDictionary ();
  39. Assert.AreEqual (0, rdd.Count);
  40. }
  41. [Test]
  42. public void Count_SingleItem_ReturnsOne ()
  43. {
  44. var rdd = new RequestDataDictionary ();
  45. rdd.Add ("foobar", "baz");
  46. Assert.AreEqual (1, rdd.Count);
  47. }
  48. [Test]
  49. public void Count_SingleItemInCollection_ReturnsOne ()
  50. {
  51. var nvc = new DataDictionary ();
  52. var rdd = new RequestDataDictionary ();
  53. nvc.Add ("foobar", "baz");
  54. rdd.AddCollection (nvc);
  55. Assert.AreEqual (1, rdd.Count);
  56. }
  57. [Test]
  58. public void Count_ItemInCollectionAndItemAdded_ReturnsTwo ()
  59. {
  60. var nvc = new DataDictionary ();
  61. var rdd = new RequestDataDictionary ();
  62. nvc.Add ("foobar", "baz");
  63. rdd.AddCollection (nvc);
  64. rdd.Add ("foobar", "baz");
  65. Assert.AreEqual (2, rdd.Count);
  66. }
  67. [Test]
  68. public void Clear_SingleItemAdded_SetsCountZero ()
  69. {
  70. var rdd = new RequestDataDictionary ();
  71. rdd.Add ("foobar", "baz");
  72. rdd.Clear ();
  73. Assert.AreEqual (0, rdd.Count);
  74. }
  75. [Test]
  76. public void Clear_ItemInCollection_SetsCountZero ()
  77. {
  78. var nvc = new DataDictionary ();
  79. var rdd = new RequestDataDictionary ();
  80. nvc.Add ("foobar", "baz");
  81. rdd.AddCollection (nvc);
  82. rdd.Clear ();
  83. Assert.AreEqual (0, rdd.Count);
  84. }
  85. [Test]
  86. public void Clear_ItemInCollectionAndItemAdded_SetsCountZero ()
  87. {
  88. var nvc = new DataDictionary ();
  89. var rdd = new RequestDataDictionary ();
  90. nvc.Add ("foobar", "baz");
  91. rdd.AddCollection (nvc);
  92. rdd.Add ("foobar", "baz");
  93. rdd.Clear ();
  94. Assert.AreEqual (0, rdd.Count);
  95. }
  96. [Test]
  97. public void Clear_SingleItemAdded_RemovesItem ()
  98. {
  99. var rdd = new RequestDataDictionary ();
  100. rdd.Add ("foobar", "baz");
  101. rdd.Clear ();
  102. string dummy;
  103. bool has_item = rdd.ContainsKey ("foobar");
  104. Assert.IsFalse (has_item);
  105. }
  106. [Test]
  107. public void Clear_ItemInCollection_RemovesItem ()
  108. {
  109. var nvc = new DataDictionary ();
  110. var rdd = new RequestDataDictionary ();
  111. nvc.Add ("foobar", "baz");
  112. rdd.AddCollection (nvc);
  113. rdd.Clear ();
  114. bool has_item = rdd.ContainsKey ("foobar");
  115. Assert.IsFalse (has_item);
  116. }
  117. [Test]
  118. public void Clear_ItemInCollectionAndItemAdded_RemovesItem ()
  119. {
  120. var nvc = new DataDictionary ();
  121. var rdd = new RequestDataDictionary ();
  122. nvc.Add ("foobar", "baz");
  123. rdd.AddCollection (nvc);
  124. rdd.Add ("foobar", "baz");
  125. rdd.Clear ();
  126. bool has_item = rdd.ContainsKey ("foobar");
  127. Assert.IsFalse (has_item);
  128. }
  129. [Test]
  130. public void ContainsKey_NoItems_ReturnsFalse ()
  131. {
  132. var rdd = new RequestDataDictionary ();
  133. bool has_item = rdd.ContainsKey ("foobar");
  134. Assert.IsFalse (has_item);
  135. }
  136. [Test]
  137. public void ContainsKey_ItemAdded_ReturnsTrue ()
  138. {
  139. var rdd = new RequestDataDictionary ();
  140. rdd.Add ("foobar", "baz");
  141. bool has_item = rdd.ContainsKey ("foobar");
  142. Assert.IsTrue (has_item);
  143. }
  144. [Test]
  145. public void ContainsKey_ItemInCollection_ReturnsTrue ()
  146. {
  147. var nvc = new DataDictionary ();
  148. var rdd = new RequestDataDictionary ();
  149. nvc.Add ("foobar", "baz");
  150. rdd.AddCollection (nvc);
  151. bool has_item = rdd.ContainsKey ("foobar");
  152. Assert.IsTrue (has_item);
  153. }
  154. [Test]
  155. public void Contains_NoItemsAdded_ReturnsFalse ()
  156. {
  157. var rdd = new RequestDataDictionary ();
  158. var pair = new KeyValuePair<string, string> ("foobar", "baz");
  159. bool has_item = rdd.Contains (pair);
  160. Assert.IsFalse (has_item);
  161. }
  162. [Test]
  163. public void Contains_ItemAddedKeyMatchesValueDoesnt_ReturnsFalse ()
  164. {
  165. var rdd = new RequestDataDictionary ();
  166. var pair = new KeyValuePair<string, string> ("foobar", "baz");
  167. rdd.Add ("foobar", "nope");
  168. bool has_item = rdd.Contains (pair);
  169. Assert.IsFalse (has_item);
  170. }
  171. [Test]
  172. public void Contains_ItemAddedKeyDoesNotMatchValueDoes_ReturnsFalse ()
  173. {
  174. var rdd = new RequestDataDictionary ();
  175. var pair = new KeyValuePair<string, string> ("foobar", "baz");
  176. rdd.Add ("nope", "baz");
  177. bool has_item = rdd.Contains (pair);
  178. Assert.IsFalse (has_item);
  179. }
  180. [Test]
  181. public void Contains_ItemAddedKeyAndValueMatch_ReturnsTrue ()
  182. {
  183. var rdd = new RequestDataDictionary ();
  184. var pair = new KeyValuePair<string, string> ("foobar", "baz");
  185. rdd.Add ("foobar", "baz");
  186. bool has_item = rdd.Contains (pair);
  187. Assert.IsTrue (has_item);
  188. }
  189. [Test]
  190. public void Contains_ItemInCollectionKeyAndValuesMatch_ReturnsTrue ()
  191. {
  192. var nvc = new DataDictionary ();
  193. var rdd = new RequestDataDictionary ();
  194. var pair = new KeyValuePair<string,string> ("foobar", "baz");
  195. nvc.Add ("foobar", "baz");
  196. rdd.AddCollection (nvc);
  197. bool has_item = rdd.Contains (pair);
  198. Assert.IsTrue (has_item);
  199. }
  200. [Test]
  201. public void Contains_MultipleItemsInCollectionFirstKeyAndValuesMatch_ReturnsTrue ()
  202. {
  203. var nvc = new DataDictionary ();
  204. var rdd = new RequestDataDictionary ();
  205. var pair = new KeyValuePair<string,string> ("foobar", "baz");
  206. nvc.Add ("foobar", "baz");
  207. nvc.Add ("foobar", "blah");
  208. rdd.AddCollection (nvc);
  209. bool has_item = rdd.Contains (pair);
  210. Assert.IsTrue (has_item);
  211. }
  212. [Test]
  213. public void Contains_MultipleItemsInCollectionLastKeyAndValuesMatch_ReturnsTrue ()
  214. {
  215. var nvc = new DataDictionary ();
  216. var rdd = new RequestDataDictionary ();
  217. var pair = new KeyValuePair<string,string> ("foobar", "baz");
  218. nvc.Add ("foobar", "blah");
  219. nvc.Add ("foobar", "baz");
  220. rdd.AddCollection (nvc);
  221. bool has_item = rdd.Contains (pair);
  222. Assert.IsTrue (has_item);
  223. }
  224. [Test]
  225. public void Contains_MultipleItemsInCollectionMiddleKeyAndValuesMatch_ReturnsTrue ()
  226. {
  227. var nvc = new DataDictionary ();
  228. var rdd = new RequestDataDictionary ();
  229. var pair = new KeyValuePair<string,string> ("foobar", "baz");
  230. nvc.Add ("foobar", "blah");
  231. nvc.Add ("foobar", "baz");
  232. nvc.Add ("foobar", "burrah");
  233. rdd.AddCollection (nvc);
  234. bool has_item = rdd.Contains (pair);
  235. Assert.IsTrue (has_item);
  236. }
  237. [Test]
  238. public void Contains_MultipleItemsWithPartialStartMatchesInCollection_ReturnsFalse ()
  239. {
  240. var nvc = new DataDictionary ();
  241. var rdd = new RequestDataDictionary ();
  242. var pair = new KeyValuePair<string,string> ("foobar", "baz");
  243. nvc.Add ("foobar", "baza");
  244. nvc.Add ("foobar", "baza");
  245. nvc.Add ("foobar", "baza");
  246. rdd.AddCollection (nvc);
  247. bool has_item = rdd.Contains (pair);
  248. Assert.IsFalse (has_item);
  249. }
  250. [Test]
  251. public void Contains_MultipleItemsWithPartialEndMatchesInCollection_ReturnsFalse ()
  252. {
  253. var nvc = new DataDictionary ();
  254. var rdd = new RequestDataDictionary ();
  255. var pair = new KeyValuePair<string,string> ("foobar", "baz");
  256. nvc.Add ("foobar", "abaz");
  257. nvc.Add ("foobar", "abaz");
  258. nvc.Add ("foobar", "abaz");
  259. rdd.AddCollection (nvc);
  260. bool has_item = rdd.Contains (pair);
  261. Assert.IsFalse (has_item);
  262. }
  263. [Test]
  264. public void TryGetValue_NoValuesAdded_ReturnsFalse ()
  265. {
  266. var rdd = new RequestDataDictionary ();
  267. string dummy;
  268. bool has_value = rdd.TryGetValue ("foobar", out dummy);
  269. Assert.IsFalse (has_value);
  270. }
  271. [Test]
  272. public void TryGetValue_NoValuesAdded_SetsValueNull ()
  273. {
  274. var rdd = new RequestDataDictionary ();
  275. string the_data;
  276. rdd.TryGetValue ("foobar", out the_data);
  277. Assert.IsNull (the_data);
  278. }
  279. [Test]
  280. public void TryGetValue_NonMatchingValueAdded_ReturnsFalse ()
  281. {
  282. var rdd = new RequestDataDictionary ();
  283. rdd.Add ("blizbar", "baz");
  284. string dummy;
  285. bool has_value = rdd.TryGetValue ("foobar", out dummy);
  286. Assert.IsFalse (has_value);
  287. }
  288. [Test]
  289. public void TryGetValue_SingleAddedValueMatches_ReturnsTrue ()
  290. {
  291. var rdd = new RequestDataDictionary ();
  292. rdd.Add ("foobar", "baz");
  293. string dummy;
  294. bool has_value = rdd.TryGetValue ("foobar", out dummy);
  295. Assert.IsTrue (has_value);
  296. }
  297. [Test]
  298. public void TryGetValue_SingleAddedValueMatches_SetsData ()
  299. {
  300. var rdd = new RequestDataDictionary ();
  301. rdd.Add ("foobar", "baz");
  302. string the_data;
  303. rdd.TryGetValue ("foobar", out the_data);
  304. Assert.AreEqual ("baz", the_data);
  305. }
  306. }
  307. }