Merge pull request #274 from iainlane/master
[mono.git] / mcs / class / System / Test / System.Collections.Specialized / NameValueCollectionTest.cs
index fc38042cf486163f1dfd813cd1d92d6ae6ce8633..60abadc3387c06c686e5d2d01325029e115037eb 100644 (file)
@@ -10,6 +10,7 @@
 
 using System;
 using System.Collections;
+using System.Collections.Generic;
 using System.Collections.Specialized;
 using System.Text;
 
@@ -18,17 +19,17 @@ using NUnit.Framework;
 namespace MonoTests.System.Collections.Specialized {
 
        [TestFixture]
-        public class NameValueCollectionTest : Assertion {
+        public class NameValueCollectionTest {
 
                [Test]
                public void GetValues ()
                {
                        NameValueCollection col = new NameValueCollection ();
                        col.Add ("foo1", "bar1");
-                       AssertEquals ("#1", null, col.GetValues (null));
-                       AssertEquals ("#2", null, col.GetValues (""));
-                       AssertEquals ("#3", null, col.GetValues ("NotExistent"));
-                       AssertEquals ("#4", 1, col.GetValues (0).Length);
+                       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]
@@ -37,7 +38,7 @@ namespace MonoTests.System.Collections.Specialized {
                {
                        NameValueCollection c = new NameValueCollection ();
                        c.Add ("foo1", "bar1");
-                       AssertEquals ("#5", null, c.GetValues (1));
+                       Assert.AreEqual (null, c.GetValues (1), "#5");
                }
 
                [Test]
@@ -45,11 +46,11 @@ namespace MonoTests.System.Collections.Specialized {
                {
                        NameValueCollection col = new NameValueCollection (5);
                        col.Add ("foo1", "bar1");
-                       AssertEquals ("#1", null, col.Get (null));
-                       AssertEquals ("#2", null, col.Get (""));
-                       AssertEquals ("#3", null, col.Get ("NotExistent"));
-                       AssertEquals ("#4", "bar1", col.Get ("foo1"));
-                       AssertEquals ("#5", "bar1", col.Get (0));
+                       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]
@@ -58,7 +59,7 @@ namespace MonoTests.System.Collections.Specialized {
                {
                        NameValueCollection c = new NameValueCollection ();
                        c.Add ("foo1", "bar1");
-                       AssertEquals ("#6", null, c.Get (1));
+                       Assert.AreEqual (null, c.Get (1), "#6");
                }
 
                [Test]
@@ -66,7 +67,7 @@ namespace MonoTests.System.Collections.Specialized {
                {
                        NameValueCollection c = new NameValueCollection (CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant);
                        c.Add ("foo1", "bar1");
-                       AssertEquals ("#1", "foo1", c.GetKey (0));
+                       Assert.AreEqual ("foo1", c.GetKey (0), "#1");
                }
 
                [Test]
@@ -75,27 +76,27 @@ namespace MonoTests.System.Collections.Specialized {
                {
                        NameValueCollection c = new NameValueCollection ();
                        c.Add ("foo1", "bar1");
-                       AssertEquals ("#2", null, c.GetKey (1));
+                       Assert.AreEqual (null, c.GetKey (1), "#2");
                }
 
                [Test]
                public void HasKeys ()
                {
                        NameValueCollection c = new NameValueCollection (5, CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant);
-                       Assert ("#1", !c.HasKeys ());
+                       Assert.IsTrue (!c.HasKeys (), "#1");
                        c.Add ("foo1", "bar1");
-                       Assert ("#2", c.HasKeys ());
+                       Assert.IsTrue (c.HasKeys (), "#2");
                }
 
                [Test]
                public void Clear ()
                {
                        NameValueCollection c = new NameValueCollection ();
-                       AssertEquals ("#1", 0, c.Count);
+                       Assert.AreEqual (0, c.Count, "#1");
                        c.Add ("foo1", "bar1");
-                       AssertEquals ("#2", 1, c.Count);
+                       Assert.AreEqual (1, c.Count, "#2");
                        c.Clear ();
-                       AssertEquals ("#3", 0, c.Count);
+                       Assert.AreEqual (0, c.Count, "#3");
                }
 
                [Test]
@@ -105,12 +106,57 @@ namespace MonoTests.System.Collections.Specialized {
                        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]);
+                       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_Calls ()
+               {
+                       var nvc1 = new MyNVC ();
+                       var nvc2 = new MyNVC ();
+                       nvc1.Add ("one", "1");
+                       nvc1.Add ("one", "one");
+                       nvc1.Add ("two", null);
+                       nvc2.Add (nvc1);
+
+                       string[] values;
+                       Assert.AreEqual (8, nvc1.Log.Count, "#A1-1");
+                       Assert.AreEqual ("Add (string, string)", nvc1.Log [0], "#A1-2");
+                       Assert.AreEqual ("Add (string, string)", nvc1.Log [1], "#A1-3");
+                       Assert.AreEqual ("Add (string, string)", nvc1.Log [2], "#A1-4");
+                       Assert.AreEqual ("get_Count", nvc1.Log [3], "#A1-5");
+                       Assert.AreEqual ("GetKey (int)", nvc1.Log [4], "#A1-6");
+                       Assert.AreEqual ("GetValues (int)", nvc1.Log [5], "#A1-7");
+                       Assert.AreEqual ("GetKey (int)", nvc1.Log [6], "#A1-8");
+                       Assert.AreEqual ("GetValues (int)", nvc1.Log [7], "#A1-9");
+
+                       Assert.AreEqual (2, nvc1.Count, "#A2-1");
+                       values = nvc1.GetValues (0);
+                       Assert.AreEqual ("one", nvc1.GetKey (0), "#A2-2");
+                       Assert.AreEqual ("1", values [0], "#A2-3");
+                       Assert.AreEqual ("one", values [1], "#A2-4");
+                       values = nvc1.GetValues (1);
+                       Assert.AreEqual ("two", nvc1.GetKey (1), "#A2-5");
+                       Assert.IsTrue (values == null, "#A2-6");
+
+                       Assert.AreEqual (3, nvc2.Log.Count, "#B1-1");
+                       Assert.AreEqual ("Add (string, string)", nvc2.Log [0], "#B1-2");
+                       Assert.AreEqual ("Add (string, string)", nvc2.Log [1], "#B1-3");
+                       Assert.AreEqual ("Add (string, string)", nvc2.Log [2], "#B1-4");
+
+                       Assert.AreEqual (2, nvc2.Count, "#B2-1");
+                       values = nvc2.GetValues (0);
+                       Assert.AreEqual ("one", nvc2.GetKey (0), "#B2-2");
+                       Assert.AreEqual ("1", values [0], "#B2-3");
+                       Assert.AreEqual ("one", values [1], "#B2-4");
+                       values = nvc2.GetValues (1);
+                       Assert.AreEqual ("two", nvc2.GetKey (1), "#B2-5");
+                       Assert.IsTrue (values == null, "#B2-6");
+               }
+               
                [Test]
                public void Add_Multiples ()
                {
@@ -118,8 +164,8 @@ namespace MonoTests.System.Collections.Specialized {
                        c.Add ("mono", "mono");
                        c.Add ("mono", "mono");
                        c.Add ("mono", "mono");
-                       AssertEquals ("Count", 1, c.Count);
-                       AssertEquals ("mono", "mono,mono,mono", c ["mono"]);
+                       Assert.AreEqual (1, c.Count, "Count");
+                       Assert.AreEqual ("mono,mono,mono", c ["mono"], "mono");
                }
 
                [Test]
@@ -129,8 +175,8 @@ namespace MonoTests.System.Collections.Specialized {
                        c.Add ("mono", "mono");
                        c.Add ("mono", null);
                        c.Add ("mono", "mono");
-                       AssertEquals ("Count", 1, c.Count);
-                       AssertEquals ("mono", "mono,mono", c ["mono"]);
+                       Assert.AreEqual (1, c.Count, "Count");
+                       Assert.AreEqual ("mono,mono", c ["mono"], "mono");
                }
 
                [Test]
@@ -140,18 +186,18 @@ namespace MonoTests.System.Collections.Specialized {
                        NameValueCollection c2 = new NameValueCollection (c1);
 
                        c2.Add (c1);
-                       AssertEquals ("c1.Count", 0, c1.Count);
-                       AssertEquals ("c2.Count", 0, c2.Count);
+                       Assert.AreEqual (0, c1.Count, "c1.Count");
+                       Assert.AreEqual (0, c2.Count, "c2.Count");
 
                        c1.Add ("foo", "bar");
                        c2.Add ("bar", "foo");
 
-                       AssertEquals ("c1.Count", 1, c1.Count);
-                       AssertEquals ("c2.Count", 1, c2.Count);
+                       Assert.AreEqual (1, c1.Count, "c1.Count");
+                       Assert.AreEqual (1, c2.Count, "c2.Count");
 
                        c2.Add (c1);
-                       AssertEquals ("c1.Count", 1, c1.Count);
-                       AssertEquals ("c2.Count", 2, c2.Count);
+                       Assert.AreEqual (1, c1.Count, "c1.Count");
+                       Assert.AreEqual (2, c2.Count, "c2.Count");
                }
 
                [Test]
@@ -173,7 +219,7 @@ namespace MonoTests.System.Collections.Specialized {
 
                        b.Add ("Test", null);
                        a.Add (b);
-                       AssertEquals ("Count", 1, a.Count);
+                       Assert.AreEqual (1, a.Count, "Count");
                }
 
                [Test]
@@ -183,10 +229,10 @@ namespace MonoTests.System.Collections.Specialized {
                        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]);
+                       Assert.AreEqual (3, c.Count, "Count");
+                       Assert.AreEqual ("mono", c ["mono"], "mono");
+                       Assert.IsNull (c ["!mono"], "!mono");
+                       Assert.AreEqual ("mono!", c [null], "mono!");
                }
 
                [Test]
@@ -196,19 +242,19 @@ namespace MonoTests.System.Collections.Specialized {
                        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!"]);
+                       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!");
-                       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]);
+                       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]
@@ -220,7 +266,7 @@ namespace MonoTests.System.Collections.Specialized {
                        c.Add ("MoNo", "MoNo");
                        c.Add ("mOnO", "mOnO");
                        c.Add ("MONO", "MONO");
-                       AssertEquals ("Count", 1, c.Count);
+                       Assert.AreEqual (1, c.Count, "Count");
                }
 
                [Test]
@@ -292,6 +338,21 @@ namespace MonoTests.System.Collections.Specialized {
                        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 () 
@@ -303,33 +364,55 @@ namespace MonoTests.System.Collections.Specialized {
                                string add = "Add-" + i.ToString () + "-Count";
 
                                c.Add (items [i], add);
-                               AssertEquals (add, 1, c.Count);
+                               Assert.AreEqual (1, c.Count, add);
                                c.Remove (items [0]);
-                               AssertEquals ("Remove-0-Count", 0, c.Count);
+                               Assert.AreEqual (0, c.Count, "Remove-0-Count");
 
                                c.Add (items [i], add);
-                               AssertEquals (add, 1, c.Count);
+                               Assert.AreEqual (1, c.Count, add);
                                c.Remove (items [1]);
-                               AssertEquals ("Remove-1-Count", 0, c.Count);
+                               Assert.AreEqual (0, c.Count, "Remove-1-Count");
 
                                c.Add (items [i], add);
-                               AssertEquals (add, 1, c.Count);
+                               Assert.AreEqual (1, c.Count, add);
                                c.Remove (items [2]);
-                               AssertEquals ("Remove-2-Count", 0, c.Count);
+                               Assert.AreEqual (0, c.Count, "Remove-2-Count");
 
                                c.Add (items [i], add);
-                               AssertEquals (add , 1, c.Count);
+                               Assert.AreEqual (1, c.Count, add);
                                c.Remove (items [3]);
-                               AssertEquals ("Remove-3-Count", 0, c.Count);
+                               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");
-                       AssertEquals ("#1", 1, coll.Count);
+                       Assert.AreEqual (1, coll.Count, "#1");
                }
 
                [Test]
@@ -337,7 +420,7 @@ namespace MonoTests.System.Collections.Specialized {
                {
                        NameValueCollection coll = new NameValueCollection (5, new EqualityComparer ());
                        coll.Add ("a", "1");
-                       AssertEquals ("#1", 1, coll.Count);
+                       Assert.AreEqual (1, coll.Count, "#1");
                }
 
                [Test]
@@ -346,6 +429,100 @@ namespace MonoTests.System.Collections.Specialized {
                {
                        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
+               class MyNVC : NameValueCollection
+               {
+                       List<string> log;
+
+                       public List<string> Log {
+                               get {
+                                       if (log == null)
+                                               log = new List<string> ();
+                                       return log;
+                               }
+                       }
+
+                       public override KeysCollection Keys {
+                               get {
+                                       Log.Add ("get_Keys");
+                                       return base.Keys;
+                               }
+                       }
+
+                       public override int Count {
+                               get {
+                                       Log.Add ("get_Count");
+                                       return base.Count;
+                               }
+                       }
+
+                       public override string[] AllKeys {
+                               get {
+                                       Log.Add ("get_AllKeys");
+                                       return base.AllKeys;
+                               }
+                       }
+                       
+                       public override string Get (int index)
+                       {
+                               Log.Add ("Get (int)");
+                               return base.Get (index);
+                       }
+
+                       public override string Get (string name)
+                       {
+                               Log.Add ("Get (string)");
+                               return base.Get (name);
+                       }
+
+                       public override string GetKey (int index)
+                       {
+                               Log.Add ("GetKey (int)");
+                               return base.GetKey (index);
+                       }
+
+                       public override string[] GetValues (int index)
+                       {
+                               Log.Add ("GetValues (int)");
+                               return base.GetValues (index);
+                       }
+
+                       public override string[] GetValues (string name)
+                       {
+                               Log.Add ("GetValues (string)");
+                               return base.GetValues (name);
+                       }
+
+                       public override void Add (string name, string value)
+                       {
+                               Log.Add ("Add (string, string)");
+                               base.Add (name, value);
+                       }
+
+                       public override void Set (string name, string value)
+                       {
+                               Log.Add ("Set (string, string)");
+                               base.Set (name, value);
+                       }
+               }
        }
 }