2003-08-03 Gonzalo Paniagua Javier <gonzalo@ximian.com>
authorGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Mon, 4 Aug 2003 00:05:26 +0000 (00:05 -0000)
committerGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Mon, 4 Aug 2003 00:05:26 +0000 (00:05 -0000)
* CollectionBaseTet.cs: added a few more tests. Now derives from
Assertion.

svn path=/trunk/mcs/; revision=17035

mcs/class/corlib/Test/System.Collections/CollectionBaseTest.cs

index c42d8e876aedc059e809f0021afb6c38a80ca64d..126ca704662dad6b6dff29691c80483c935f1681 100644 (file)
@@ -2,10 +2,12 @@
 // System.Collections.CollectionBase\r
 // Test suite for System.Collections.CollectionBase\r
 //\r
-// Author:\r
+// Authors:\r
 //    Nick D. Drochak II\r
+//    Gonzalo Paniagua Javier (gonzalo@ximian.com)\r
 //\r
 // (C) 2001 Nick D. Drochak II\r
+// (c) 2003 Ximian, Inc. (http://www.ximian.com)\r
 //\r
 \r
 \r
@@ -17,7 +19,7 @@ namespace MonoTests.System.Collections
 {\r
 \r
 [TestFixture]\r
-public class CollectionBaseTest\r
+public class CollectionBaseTest : Assertion\r
 {\r
        // We need a concrete class to test the abstract base class\r
        public class ConcreteCollection : CollectionBase \r
@@ -42,6 +44,8 @@ public class CollectionBaseTest
                public bool onSetCompleteFired;\r
                public int onSetCompleteOldValue;\r
                public int onSetCompleteNewValue;\r
+               public int mustThrowException;\r
+               public bool onValidateFired;\r
 \r
                // This constructor is used to test OnValid()\r
                public ConcreteCollection()     \r
@@ -62,6 +66,15 @@ public class CollectionBaseTest
                        }\r
                }\r
 \r
+               void CheckIfThrow ()\r
+               {\r
+                       if (mustThrowException > 0) {\r
+                               mustThrowException--;\r
+                               if (mustThrowException == 0)\r
+                                       throw new Exception ();\r
+                       }\r
+               }\r
+               \r
                // A helper method to look at a value in the list at a specific index\r
                public int PeekAt(int index)\r
                {\r
@@ -70,15 +83,23 @@ public class CollectionBaseTest
                        return (int) listObj[index];\r
                }\r
 \r
+               protected override void OnValidate (object value) {\r
+                       this.onValidateFired = true;\r
+                       CheckIfThrow ();\r
+                       base.OnValidate (value);\r
+               }\r
+\r
                // Mark the flag if this hook is fired\r
                protected override void OnClear() {\r
                        this.onClearFired = true;\r
+                       CheckIfThrow ();\r
                }\r
 \r
                // Mark the flag if this hook is fired\r
                protected override void OnClearComplete() \r
                {\r
                        this.onClearCompleteFired = true;\r
+                       CheckIfThrow ();\r
                }\r
 \r
                // Mark the flag, and save the paramter if this hook is fired\r
@@ -86,6 +107,7 @@ public class CollectionBaseTest
                {\r
                        this.onInsertFired = true;\r
                        this.onInsertIndex = index;\r
+                       CheckIfThrow ();\r
                }\r
 \r
                // Mark the flag, and save the paramter if this hook is fired\r
@@ -93,6 +115,7 @@ public class CollectionBaseTest
                {\r
                        this.onInsertCompleteFired = true;\r
                        this.onInsertCompleteIndex = index;\r
+                       CheckIfThrow ();\r
                }\r
                \r
                // Mark the flag, and save the paramter if this hook is fired\r
@@ -100,6 +123,7 @@ public class CollectionBaseTest
                {\r
                        this.onRemoveFired = true;\r
                        this.onRemoveIndex = index;\r
+                       CheckIfThrow ();\r
                }\r
                \r
                // Mark the flag, and save the paramter if this hook is fired\r
@@ -107,6 +131,7 @@ public class CollectionBaseTest
                {\r
                        this.onRemoveCompleteFired = true;\r
                        this.onRemoveCompleteIndex = index;\r
+                       CheckIfThrow ();\r
                }\r
                \r
                // Mark the flag, and save the paramters if this hook is fired\r
@@ -115,6 +140,7 @@ public class CollectionBaseTest
                        this.onSetFired = true;\r
                        this.onSetOldValue = (int) oldValue;\r
                        this.onSetNewValue = (int) newValue;\r
+                       CheckIfThrow ();\r
                }\r
                \r
                // Mark the flag, and save the paramters if this hook is fired\r
@@ -123,6 +149,11 @@ public class CollectionBaseTest
                        this.onSetCompleteFired = true;\r
                        this.onSetCompleteOldValue = (int) oldValue;\r
                        this.onSetCompleteNewValue = (int) newValue;\r
+                       CheckIfThrow ();\r
+               }\r
+\r
+               public IList BaseList {\r
+                       get { return base.List; }\r
                }\r
        }  // public class ConcreteCollection\r
 \r
@@ -131,7 +162,7 @@ public class CollectionBaseTest
        public void Count() {\r
                ConcreteCollection myCollection;\r
                myCollection = new ConcreteCollection(4);\r
-               Assertion.Assert(4 == myCollection.Count);\r
+               Assert(4 == myCollection.Count);\r
        }\r
 \r
        // Make sure GetEnumerator returns an object\r
@@ -139,7 +170,7 @@ public class CollectionBaseTest
        public void GetEnumerator() {\r
                ConcreteCollection myCollection;\r
                myCollection = new ConcreteCollection(4);\r
-               Assertion.Assert(null != myCollection.GetEnumerator());\r
+               Assert(null != myCollection.GetEnumerator());\r
        }\r
 \r
        // OnValid disallows nulls\r
@@ -158,15 +189,15 @@ public class CollectionBaseTest
                numberOfItems = 3;\r
                // The constructor inserts\r
                myCollection = new ConcreteCollection(numberOfItems);\r
-               Assertion.Assert(myCollection.onInsertFired);\r
-               Assertion.Assert(myCollection.onInsertCompleteFired);\r
+               Assert(myCollection.onInsertFired);\r
+               Assert(myCollection.onInsertCompleteFired);\r
 \r
                // Using the IList interface, check inserts in the middle\r
                IList listObj = myCollection;\r
                listObj.Insert(1, 9);\r
-               Assertion.Assert(myCollection.onInsertIndex == 1);\r
-               Assertion.Assert(myCollection.onInsertCompleteIndex == 1);\r
-               Assertion.Assert(myCollection.PeekAt(1) == 9);\r
+               Assert(myCollection.onInsertIndex == 1);\r
+               Assert(myCollection.onInsertCompleteIndex == 1);\r
+               Assert(myCollection.PeekAt(1) == 9);\r
        }\r
 \r
        // Test Clear and it's hooks\r
@@ -178,9 +209,9 @@ public class CollectionBaseTest
                numberOfItems = 1;\r
                myCollection = new ConcreteCollection(numberOfItems);\r
                myCollection.Clear();\r
-               Assertion.Assert(myCollection.Count == 0);\r
-               Assertion.Assert(myCollection.onClearFired);\r
-               Assertion.Assert(myCollection.onClearCompleteFired);\r
+               Assert(myCollection.Count == 0);\r
+               Assert(myCollection.onClearFired);\r
+               Assert(myCollection.onClearCompleteFired);\r
        }\r
 \r
        // Test RemoveAt, other removes and the hooks\r
@@ -197,16 +228,16 @@ public class CollectionBaseTest
                myCollection.RemoveAt(1);\r
 \r
                // We should see the original third one in it's place\r
-               Assertion.Assert(myCollection.PeekAt(1) == 2);\r
-               Assertion.Assert(myCollection.onRemoveFired);\r
-               Assertion.Assert(myCollection.onRemoveIndex == 1);\r
-               Assertion.Assert(myCollection.onRemoveCompleteFired);\r
-               Assertion.Assert(myCollection.onRemoveCompleteIndex == 1);\r
+               Assert(myCollection.PeekAt(1) == 2);\r
+               Assert(myCollection.onRemoveFired);\r
+               Assert(myCollection.onRemoveIndex == 1);\r
+               Assert(myCollection.onRemoveCompleteFired);\r
+               Assert(myCollection.onRemoveCompleteIndex == 1);\r
                IList listObj = myCollection;\r
                listObj.Remove(0);\r
                // Confirm parameters are being passed to the hooks\r
-               Assertion.Assert(myCollection.onRemoveIndex == 0);\r
-               Assertion.Assert(myCollection.onRemoveCompleteIndex == 0);\r
+               Assert(myCollection.onRemoveIndex == 0);\r
+               Assert(myCollection.onRemoveCompleteIndex == 0);\r
        }\r
 \r
        // Test the random access feature\r
@@ -219,13 +250,57 @@ public class CollectionBaseTest
                myCollection = new ConcreteCollection(numberOfItems);\r
                IList listObj = myCollection;\r
                listObj[0] = 99;\r
-               Assertion.Assert((int) listObj[0] == 99);\r
-               Assertion.Assert(myCollection.onSetFired);\r
-               Assertion.Assert(myCollection.onSetCompleteFired);\r
-               Assertion.Assert(myCollection.onSetOldValue == 0);\r
-               Assertion.Assert(myCollection.onSetCompleteOldValue == 0);\r
-               Assertion.Assert(myCollection.onSetNewValue == 99);\r
-               Assertion.Assert(myCollection.onSetCompleteNewValue == 99);\r
+               Assert((int) listObj[0] == 99);\r
+               Assert(myCollection.onSetFired);\r
+               Assert(myCollection.onSetCompleteFired);\r
+               Assert(myCollection.onSetOldValue == 0);\r
+               Assert(myCollection.onSetCompleteOldValue == 0);\r
+               Assert(myCollection.onSetNewValue == 99);\r
+               Assert(myCollection.onSetCompleteNewValue == 99);\r
+       }\r
+\r
+       [Test]\r
+       public void InsertComplete_Add ()\r
+       {\r
+               ConcreteCollection coll = new ConcreteCollection (0);\r
+               coll.mustThrowException = 1;\r
+\r
+               try {\r
+                       coll.BaseList.Add (0);\r
+               } catch {\r
+               }\r
+               AssertEquals (0, coll.Count);\r
+       }\r
+\r
+       [Test]\r
+       [ExpectedException (typeof (ArgumentOutOfRangeException))]\r
+       public void ValidateCalled ()\r
+       {\r
+               ConcreteCollection coll = new ConcreteCollection (0);\r
+               coll.mustThrowException = 1;\r
+\r
+               try {\r
+                       coll.BaseList [5] = 8888;\r
+               } catch (ArgumentOutOfRangeException) {\r
+                       throw;\r
+               } finally {\r
+                       AssertEquals (false, coll.onValidateFired);\r
+               }\r
+       }\r
+\r
+       [Test]\r
+       public void SetCompleteCalled ()\r
+       {\r
+               ConcreteCollection coll = new ConcreteCollection (0);\r
+\r
+               coll.BaseList.Add (88);\r
+               coll.mustThrowException = 1;\r
+               try {\r
+                       coll.BaseList [0] = 11;\r
+               } catch {\r
+               } finally {\r
+                       AssertEquals (false, coll.onSetCompleteFired);\r
+               }\r
        }\r
 }\r
 \r