1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
// created on 7/21/2001 at 2:36 PM
//
// Authors:
// Martin Willemoes Hansen (mwh@sysrq.dk)
// Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2003 Martin Willemoes Hansen
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
//
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using NUnit.Framework;
namespace MonoTests.System.Collections.Specialized {
[TestFixture]
public class NameValueCollectionTest {
[Test]
public void GetValues ()
{
NameValueCollection col = new NameValueCollection ();
col.Add ("foo1", "bar1");
Assert.AreEqual (null, col.GetValues (null), "#1");
Assert.AreEqual (null, col.GetValues (""), "#2");
Assert.AreEqual (null, col.GetValues ("NotExistent"), "#3");
Assert.AreEqual (1, col.GetValues (0).Length, "#4");
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void GetValues_OutOfRange ()
{
NameValueCollection c = new NameValueCollection ();
c.Add ("foo1", "bar1");
Assert.AreEqual (null, c.GetValues (1), "#5");
}
[Test]
public void Get ()
{
NameValueCollection col = new NameValueCollection (5);
col.Add ("foo1", "bar1");
Assert.AreEqual (null, col.Get (null), "#1");
Assert.AreEqual (null, col.Get (""), "#2");
Assert.AreEqual (null, col.Get ("NotExistent"), "#3");
Assert.AreEqual ("bar1", col.Get ("foo1"), "#4");
Assert.AreEqual ("bar1", col.Get (0), "#5");
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void Get_OutOfRange ()
{
NameValueCollection c = new NameValueCollection ();
c.Add ("foo1", "bar1");
Assert.AreEqual (null, c.Get (1), "#6");
}
[Test]
public void GetKey ()
{
NameValueCollection c = new NameValueCollection (CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant);
c.Add ("foo1", "bar1");
Assert.AreEqual ("foo1", c.GetKey (0), "#1");
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void GetKey_OutOfRange ()
{
NameValueCollection c = new NameValueCollection ();
c.Add ("foo1", "bar1");
Assert.AreEqual (null, c.GetKey (1), "#2");
}
[Test]
public void HasKeys ()
{
NameValueCollection c = new NameValueCollection (5, CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant);
Assert.IsTrue (!c.HasKeys (), "#1");
c.Add ("foo1", "bar1");
Assert.IsTrue (c.HasKeys (), "#2");
}
[Test]
public void Clear ()
{
NameValueCollection c = new NameValueCollection ();
Assert.AreEqual (0, c.Count, "#1");
c.Add ("foo1", "bar1");
Assert.AreEqual (1, c.Count, "#2");
c.Clear ();
Assert.AreEqual (0, c.Count, "#3");
}
[Test]
public void Add ()
{
NameValueCollection c = new NameValueCollection ();
c.Add ("mono", "mono");
c.Add ("!mono", null);
c.Add (null, "mono!");
Assert.AreEqual (3, c.Count, "Count");
Assert.AreEqual ("mono", c ["mono"], "mono");
Assert.IsNull (c ["!mono"], "!mono");
Assert.AreEqual ("mono!", c [null], "mono!");
}
[Test]
public void Add_Multiples ()
{
NameValueCollection c = new NameValueCollection ();
c.Add ("mono", "mono");
c.Add ("mono", "mono");
c.Add ("mono", "mono");
Assert.AreEqual (1, c.Count, "Count");
Assert.AreEqual ("mono,mono,mono", c ["mono"], "mono");
}
[Test]
public void Add_Multiples_Null ()
{
NameValueCollection c = new NameValueCollection ();
c.Add ("mono", "mono");
c.Add ("mono", null);
c.Add ("mono", "mono");
Assert.AreEqual (1, c.Count, "Count");
Assert.AreEqual ("mono,mono", c ["mono"], "mono");
}
[Test]
public void Add_NVC ()
{
NameValueCollection c1 = new NameValueCollection ();
NameValueCollection c2 = new NameValueCollection (c1);
c2.Add (c1);
Assert.AreEqual (0, c1.Count, "c1.Count");
Assert.AreEqual (0, c2.Count, "c2.Count");
c1.Add ("foo", "bar");
c2.Add ("bar", "foo");
Assert.AreEqual (1, c1.Count, "c1.Count");
Assert.AreEqual (1, c2.Count, "c2.Count");
c2.Add (c1);
Assert.AreEqual (1, c1.Count, "c1.Count");
Assert.AreEqual (2, c2.Count, "c2.Count");
}
[Test]
#if NET_2_0
[ExpectedException (typeof (ArgumentNullException))]
#else
[ExpectedException (typeof (NullReferenceException))]
#endif
public void Add_NVC_Null ()
{
new NameValueCollection ().Add (null);
}
[Test]
public void Add_NVC_Null2 ()
{
NameValueCollection a = new NameValueCollection ();
NameValueCollection b = new NameValueCollection ();
b.Add ("Test", null);
a.Add (b);
Assert.AreEqual (1, a.Count, "Count");
}
[Test]
public void Set_New ()
{
NameValueCollection c = new NameValueCollection ();
c.Set ("mono", "mono");
c.Set ("!mono", null);
c.Set (null, "mono!");
Assert.AreEqual (3, c.Count, "Count");
Assert.AreEqual ("mono", c ["mono"], "mono");
Assert.IsNull (c ["!mono"], "!mono");
Assert.AreEqual ("mono!", c [null], "mono!");
}
[Test]
public void Set_Replace ()
{
NameValueCollection c = new NameValueCollection ();
c.Add ("mono", "mono");
c.Add ("!mono", "!mono");
c.Add ("mono!", "mono!");
Assert.AreEqual (3, c.Count, "Count");
Assert.AreEqual ("mono", c ["mono"], "mono");
Assert.AreEqual ("!mono", c ["!mono"], "!mono");
Assert.AreEqual ("mono!", c ["mono!"], "mono!");
c.Set ("mono", "nomo");
c.Set ("!mono", null);
c.Set (null, "mono!");
Assert.AreEqual (4, c.Count, "Count"); // mono! isn't removed
Assert.AreEqual ("nomo", c ["mono"], "mono");
Assert.IsNull (c ["!mono"], "!mono");
Assert.AreEqual ("mono!", c ["mono!"], "mono!1");
Assert.AreEqual ("mono!", c [null], "mono!2");
}
[Test]
public void CaseInsensitive ()
{
// default constructor is case insensitive
NameValueCollection c = new NameValueCollection ();
c.Add ("mono", "mono");
c.Add ("MoNo", "MoNo");
c.Add ("mOnO", "mOnO");
c.Add ("MONO", "MONO");
Assert.AreEqual (1, c.Count, "Count");
}
[Test]
public void CopyTo ()
{
string [] array = new string [4];
NameValueCollection c = new NameValueCollection ();
c.Add ("1", "mono");
c.Add ("2", "MoNo");
c.Add ("3", "mOnO");
c.Add ("4", "MONO");
c.CopyTo (array, 0);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void CopyTo_Null ()
{
NameValueCollection c = new NameValueCollection ();
c.CopyTo (null, 0);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void CopyTo_NegativeIndex ()
{
string [] array = new string [4];
NameValueCollection c = new NameValueCollection ();
c.Add ("1", "mono");
c.Add ("2", "MoNo");
c.Add ("3", "mOnO");
c.Add ("4", "MONO");
c.CopyTo (array, -1);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void CopyTo_NotEnoughSpace ()
{
string [] array = new string [4];
NameValueCollection c = new NameValueCollection ();
c.Add ("1", "mono");
c.Add ("2", "MoNo");
c.Add ("3", "mOnO");
c.Add ("4", "MONO");
c.CopyTo (array, 2);
}
[Test]
// Note: not a RankException
[ExpectedException (typeof (ArgumentException))]
public void CopyTo_MultipleDimensionStringArray ()
{
string [,,] matrix = new string [2,3,4];
NameValueCollection c = new NameValueCollection ();
c.Add ("1", "mono");
c.Add ("2", "MoNo");
c.Add ("3", "mOnO");
c.Add ("4", "MONO");
c.CopyTo (matrix, 0);
}
[Test]
// Note: not a RankException
[ExpectedException (typeof (ArgumentException))]
public void CopyTo_MultipleDimensionArray ()
{
Array a = Array.CreateInstance (typeof (string), 1, 2, 3);
NameValueCollection c = new NameValueCollection ();
c.CopyTo (a, 0);
}
[Test]
#if NET_2_0
[ExpectedException (typeof (InvalidCastException))]
#else
[ExpectedException (typeof (ArrayTypeMismatchException))]
#endif
public void CopyTo_WrongTypeArray ()
{
Array a = Array.CreateInstance (typeof (DateTime), 3);
NameValueCollection c = new NameValueCollection ();
for (int i = 0; i < 3; i++)
c.Add(i.ToString(), i.ToString());
c.CopyTo(a, 0);
}
[Test]
public void Remove ()
{
string[] items = { "mono", "MoNo", "mOnO", "MONO" };
// default constructor is case insensitive
NameValueCollection c = new NameValueCollection ();
for (int i=0; i < items.Length; i++) {
string add = "Add-" + i.ToString () + "-Count";
c.Add (items [i], add);
Assert.AreEqual (1, c.Count, add);
c.Remove (items [0]);
Assert.AreEqual (0, c.Count, "Remove-0-Count");
c.Add (items [i], add);
Assert.AreEqual (1, c.Count, add);
c.Remove (items [1]);
Assert.AreEqual (0, c.Count, "Remove-1-Count");
c.Add (items [i], add);
Assert.AreEqual (1, c.Count, add);
c.Remove (items [2]);
Assert.AreEqual (0, c.Count, "Remove-2-Count");
c.Add (items [i], add);
Assert.AreEqual (1, c.Count, add);
c.Remove (items [3]);
Assert.AreEqual (0, c.Count, "Remove-3-Count");
}
}
[Test]
#if NET_2_0
[ExpectedException (typeof (ArgumentNullException))]
#else
[ExpectedException (typeof (NullReferenceException))]
#endif
public void Constructor_Null_NVC ()
{
NameValueCollection nvc = new NameValueCollection((NameValueCollection)null);
}
[Test]
#if NET_2_0
[ExpectedException (typeof (ArgumentNullException))]
#else
[ExpectedException (typeof (NullReferenceException))]
#endif
public void Constructor_Capacity_Null_NVC ()
{
NameValueCollection nvc = new NameValueCollection(10, (NameValueCollection)null);
}
#if NET_2_0
[Test]
public void Constructor_IEqualityComparer ()
{
NameValueCollection coll = new NameValueCollection (new EqualityComparer ());
coll.Add ("a", "1");
Assert.AreEqual (1, coll.Count, "#1");
}
[Test]
public void Constructor_Int_IEqualityComparer ()
{
NameValueCollection coll = new NameValueCollection (5, new EqualityComparer ());
coll.Add ("a", "1");
Assert.AreEqual (1, coll.Count, "#1");
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void Constructor_IntNegative_IEqualityComparer ()
{
new NameValueCollection (-1, new EqualityComparer ());
}
[Test]
public void Constructor_IEqualityComparer_Null ()
{
NameValueCollection c1 = new NameValueCollection ((IEqualityComparer)null);
c1.Add ("key", "value");
Assert.AreEqual (c1.Get ("KEY"), "value", "Constructor_IEqualityComparer_Null");
c1.Remove ("key");
}
[Test]
public void Constructor_NameValueCollection ()
{
NameValueCollection c1 = new NameValueCollection (StringComparer.InvariantCultureIgnoreCase);
c1.Add ("key", "value");
NameValueCollection c2 = new NameValueCollection (c1);
Assert.AreEqual (c2.Get ("KEY"), "value", "Constructor_NameValueCollection");
c2.Remove ("key");
}
#endif
}
}
|