瀏覽代碼

2003-08-03 Gonzalo Paniagua Javier <[email protected]>

	* CollectionBaseTet.cs: added a few more tests. Now derives from
	Assertion.

svn path=/trunk/mcs/; revision=17035
Gonzalo Paniagua Javier 22 年之前
父節點
當前提交
a448c5900a
共有 1 個文件被更改,包括 101 次插入26 次删除
  1. 101 26
      mcs/class/corlib/Test/System.Collections/CollectionBaseTest.cs

+ 101 - 26
mcs/class/corlib/Test/System.Collections/CollectionBaseTest.cs

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