NameValueCollectionTest.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // created on 7/21/2001 at 2:36 PM
  2. //
  3. // Authors:
  4. // Martin Willemoes Hansen ([email protected])
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // (C) 2003 Martin Willemoes Hansen
  8. // Copyright (C) 2004 Novell (http://www.novell.com)
  9. //
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Specialized;
  13. using System.Text;
  14. using NUnit.Framework;
  15. namespace MonoTests.System.Collections.Specialized {
  16. [TestFixture]
  17. public class NameValueCollectionTest : Assertion {
  18. [Test]
  19. public void GetValues ()
  20. {
  21. NameValueCollection col = new NameValueCollection ();
  22. col.Add ("foo1", "bar1");
  23. Assertion.AssertEquals ("#1", null, col.GetValues (null));
  24. Assertion.AssertEquals ("#2", null, col.GetValues (""));
  25. Assertion.AssertEquals ("#3", null, col.GetValues ("NotExistent"));
  26. }
  27. [Test]
  28. public void Add ()
  29. {
  30. NameValueCollection c = new NameValueCollection ();
  31. c.Add ("mono", "mono");
  32. c.Add ("!mono", null);
  33. c.Add (null, "mono!");
  34. AssertEquals ("Count", 3, c.Count);
  35. AssertEquals ("mono", "mono", c ["mono"]);
  36. AssertNull ("!mono", c ["!mono"]);
  37. AssertEquals ("mono!", "mono!", c [null]);
  38. }
  39. [Test]
  40. public void Add_Multiples ()
  41. {
  42. NameValueCollection c = new NameValueCollection ();
  43. c.Add ("mono", "mono");
  44. c.Add ("mono", "mono");
  45. c.Add ("mono", "mono");
  46. AssertEquals ("Count", 1, c.Count);
  47. AssertEquals ("mono", "mono,mono,mono", c ["mono"]);
  48. }
  49. [Test]
  50. public void Add_Multiples_Null ()
  51. {
  52. NameValueCollection c = new NameValueCollection ();
  53. c.Add ("mono", "mono");
  54. c.Add ("mono", null);
  55. c.Add ("mono", "mono");
  56. AssertEquals ("Count", 1, c.Count);
  57. AssertEquals ("mono", "mono,mono", c ["mono"]);
  58. }
  59. [Test]
  60. public void Add_NVC ()
  61. {
  62. NameValueCollection c1 = new NameValueCollection ();
  63. NameValueCollection c2 = new NameValueCollection ();
  64. c2.Add (c1);
  65. AssertEquals ("c1.Count", 0, c1.Count);
  66. AssertEquals ("c2.Count", 0, c2.Count);
  67. c1.Add ("foo", "bar");
  68. c2.Add ("bar", "foo");
  69. AssertEquals ("c1.Count", 1, c1.Count);
  70. AssertEquals ("c2.Count", 1, c2.Count);
  71. c2.Add (c1);
  72. AssertEquals ("c1.Count", 1, c1.Count);
  73. AssertEquals ("c2.Count", 2, c2.Count);
  74. }
  75. [Test]
  76. // [ExpectedException (typeof (ArgumentNullException))]
  77. [ExpectedException (typeof (NullReferenceException))]
  78. public void Add_NVC_Null ()
  79. {
  80. new NameValueCollection ().Add (null);
  81. }
  82. [Test]
  83. public void Add_NVC_Null2 ()
  84. {
  85. NameValueCollection a = new NameValueCollection ();
  86. NameValueCollection b = new NameValueCollection ();
  87. b.Add ("Test", null);
  88. a.Add (b);
  89. AssertEquals ("Count", 1, a.Count);
  90. }
  91. [Test]
  92. public void Set_New ()
  93. {
  94. NameValueCollection c = new NameValueCollection ();
  95. c.Set ("mono", "mono");
  96. c.Set ("!mono", null);
  97. c.Set (null, "mono!");
  98. AssertEquals ("Count", 3, c.Count);
  99. AssertEquals ("mono", "mono", c ["mono"]);
  100. AssertNull ("!mono", c ["!mono"]);
  101. AssertEquals ("mono!", "mono!", c [null]);
  102. }
  103. [Test]
  104. public void Set_Replace ()
  105. {
  106. NameValueCollection c = new NameValueCollection ();
  107. c.Add ("mono", "mono");
  108. c.Add ("!mono", "!mono");
  109. c.Add ("mono!", "mono!");
  110. AssertEquals ("Count", 3, c.Count);
  111. AssertEquals ("mono", "mono", c ["mono"]);
  112. AssertEquals ("!mono", "!mono", c ["!mono"]);
  113. AssertEquals ("mono!", "mono!", c ["mono!"]);
  114. c.Set ("mono", "nomo");
  115. c.Set ("!mono", null);
  116. c.Set (null, "mono!");
  117. AssertEquals ("Count", 4, c.Count); // mono! isn't removed
  118. AssertEquals ("mono", "nomo", c ["mono"]);
  119. AssertNull ("!mono", c ["!mono"]);
  120. AssertEquals ("mono!1", "mono!", c ["mono!"]);
  121. AssertEquals ("mono!2", "mono!", c [null]);
  122. }
  123. [Test]
  124. public void CaseInsensitive ()
  125. {
  126. // default constructor is case insensitive
  127. NameValueCollection c = new NameValueCollection ();
  128. c.Add ("mono", "mono");
  129. c.Add ("MoNo", "MoNo");
  130. c.Add ("mOnO", "mOnO");
  131. c.Add ("MONO", "MONO");
  132. AssertEquals ("Count", 1, c.Count);
  133. }
  134. [Test]
  135. public void CopyTo ()
  136. {
  137. string [] array = new string [4];
  138. NameValueCollection c = new NameValueCollection ();
  139. c.Add ("1", "mono");
  140. c.Add ("2", "MoNo");
  141. c.Add ("3", "mOnO");
  142. c.Add ("4", "MONO");
  143. c.CopyTo (array, 0);
  144. }
  145. [Test]
  146. [ExpectedException (typeof (ArgumentNullException))]
  147. public void CopyTo_Null ()
  148. {
  149. NameValueCollection c = new NameValueCollection ();
  150. c.CopyTo (null, 0);
  151. }
  152. [Test]
  153. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  154. public void CopyTo_NegativeIndex ()
  155. {
  156. string [] array = new string [4];
  157. NameValueCollection c = new NameValueCollection ();
  158. c.Add ("1", "mono");
  159. c.Add ("2", "MoNo");
  160. c.Add ("3", "mOnO");
  161. c.Add ("4", "MONO");
  162. c.CopyTo (array, -1);
  163. }
  164. [Test]
  165. [ExpectedException (typeof (ArgumentException))]
  166. public void CopyTo_NotEnoughSpace ()
  167. {
  168. string [] array = new string [4];
  169. NameValueCollection c = new NameValueCollection ();
  170. c.Add ("1", "mono");
  171. c.Add ("2", "MoNo");
  172. c.Add ("3", "mOnO");
  173. c.Add ("4", "MONO");
  174. c.CopyTo (array, 2);
  175. }
  176. [Test]
  177. // Note: not a RankException
  178. [ExpectedException (typeof (ArgumentException))]
  179. public void CopyTo_MultipleDimensionStringArray ()
  180. {
  181. string [,,] matrix = new string [2,3,4];
  182. NameValueCollection c = new NameValueCollection ();
  183. c.Add ("1", "mono");
  184. c.Add ("2", "MoNo");
  185. c.Add ("3", "mOnO");
  186. c.Add ("4", "MONO");
  187. c.CopyTo (matrix, 0);
  188. }
  189. [Test]
  190. // Note: not a RankException
  191. [ExpectedException (typeof (ArgumentException))]
  192. public void CopyTo_MultipleDimensionArray ()
  193. {
  194. Array a = Array.CreateInstance (typeof (string), 1, 2, 3);
  195. NameValueCollection c = new NameValueCollection ();
  196. c.CopyTo (a, 0);
  197. }
  198. [Test]
  199. public void Remove ()
  200. {
  201. string[] items = { "mono", "MoNo", "mOnO", "MONO" };
  202. // default constructor is case insensitive
  203. NameValueCollection c = new NameValueCollection ();
  204. for (int i=0; i < items.Length; i++) {
  205. string add = "Add-" + i.ToString () + "-Count";
  206. c.Add (items [i], add);
  207. AssertEquals (add, 1, c.Count);
  208. c.Remove (items [0]);
  209. AssertEquals ("Remove-0-Count", 0, c.Count);
  210. c.Add (items [i], add);
  211. AssertEquals (add, 1, c.Count);
  212. c.Remove (items [1]);
  213. AssertEquals ("Remove-1-Count", 0, c.Count);
  214. c.Add (items [i], add);
  215. AssertEquals (add, 1, c.Count);
  216. c.Remove (items [2]);
  217. AssertEquals ("Remove-2-Count", 0, c.Count);
  218. c.Add (items [i], add);
  219. AssertEquals (add , 1, c.Count);
  220. c.Remove (items [3]);
  221. AssertEquals ("Remove-3-Count", 0, c.Count);
  222. }
  223. }
  224. }
  225. }