| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- // created on 7/21/2001 at 2:36 PM
- //
- // Authors:
- // Martin Willemoes Hansen ([email protected])
- // Sebastien Pouliot <[email protected]>
- //
- // (C) 2003 Martin Willemoes Hansen
- // Copyright (C) 2004 Novell (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 : Assertion {
- [Test]
- public void GetValues ()
- {
- NameValueCollection col = new NameValueCollection ();
- col.Add ("foo1", "bar1");
- Assertion.AssertEquals ("#1", null, col.GetValues (null));
- Assertion.AssertEquals ("#2", null, col.GetValues (""));
- Assertion.AssertEquals ("#3", null, col.GetValues ("NotExistent"));
- }
- [Test]
- public void Add ()
- {
- NameValueCollection c = new NameValueCollection ();
- c.Add ("mono", "mono");
- c.Add ("!mono", null);
- c.Add (null, "mono!");
- AssertEquals ("Count", 3, c.Count);
- AssertEquals ("mono", "mono", c ["mono"]);
- AssertNull ("!mono", c ["!mono"]);
- AssertEquals ("mono!", "mono!", c [null]);
- }
- [Test]
- public void Add_Multiples ()
- {
- NameValueCollection c = new NameValueCollection ();
- c.Add ("mono", "mono");
- c.Add ("mono", "mono");
- c.Add ("mono", "mono");
- AssertEquals ("Count", 1, c.Count);
- AssertEquals ("mono", "mono,mono,mono", c ["mono"]);
- }
- [Test]
- public void Add_Multiples_Null ()
- {
- NameValueCollection c = new NameValueCollection ();
- c.Add ("mono", "mono");
- c.Add ("mono", null);
- c.Add ("mono", "mono");
- AssertEquals ("Count", 1, c.Count);
- AssertEquals ("mono", "mono,mono", c ["mono"]);
- }
- [Test]
- public void Add_NVC ()
- {
- NameValueCollection c1 = new NameValueCollection ();
- NameValueCollection c2 = new NameValueCollection ();
- c2.Add (c1);
- AssertEquals ("c1.Count", 0, c1.Count);
- AssertEquals ("c2.Count", 0, c2.Count);
- c1.Add ("foo", "bar");
- c2.Add ("bar", "foo");
- AssertEquals ("c1.Count", 1, c1.Count);
- AssertEquals ("c2.Count", 1, c2.Count);
- c2.Add (c1);
- AssertEquals ("c1.Count", 1, c1.Count);
- AssertEquals ("c2.Count", 2, c2.Count);
- }
- [Test]
- // [ExpectedException (typeof (ArgumentNullException))]
- [ExpectedException (typeof (NullReferenceException))]
- 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);
- AssertEquals ("Count", 1, a.Count);
- }
- [Test]
- public void Set_New ()
- {
- NameValueCollection c = new NameValueCollection ();
- c.Set ("mono", "mono");
- c.Set ("!mono", null);
- c.Set (null, "mono!");
- AssertEquals ("Count", 3, c.Count);
- AssertEquals ("mono", "mono", c ["mono"]);
- AssertNull ("!mono", c ["!mono"]);
- AssertEquals ("mono!", "mono!", c [null]);
- }
- [Test]
- public void Set_Replace ()
- {
- NameValueCollection c = new NameValueCollection ();
- c.Add ("mono", "mono");
- c.Add ("!mono", "!mono");
- c.Add ("mono!", "mono!");
- AssertEquals ("Count", 3, c.Count);
- AssertEquals ("mono", "mono", c ["mono"]);
- AssertEquals ("!mono", "!mono", c ["!mono"]);
- AssertEquals ("mono!", "mono!", c ["mono!"]);
- c.Set ("mono", "nomo");
- c.Set ("!mono", null);
- c.Set (null, "mono!");
- AssertEquals ("Count", 4, c.Count); // mono! isn't removed
- AssertEquals ("mono", "nomo", c ["mono"]);
- AssertNull ("!mono", c ["!mono"]);
- AssertEquals ("mono!1", "mono!", c ["mono!"]);
- AssertEquals ("mono!2", "mono!", c [null]);
- }
- [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");
- AssertEquals ("Count", 1, c.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]
- 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);
- AssertEquals (add, 1, c.Count);
- c.Remove (items [0]);
- AssertEquals ("Remove-0-Count", 0, c.Count);
- c.Add (items [i], add);
- AssertEquals (add, 1, c.Count);
- c.Remove (items [1]);
- AssertEquals ("Remove-1-Count", 0, c.Count);
- c.Add (items [i], add);
- AssertEquals (add, 1, c.Count);
- c.Remove (items [2]);
- AssertEquals ("Remove-2-Count", 0, c.Count);
- c.Add (items [i], add);
- AssertEquals (add , 1, c.Count);
- c.Remove (items [3]);
- AssertEquals ("Remove-3-Count", 0, c.Count);
- }
- }
- }
- }
|