diff options
author | Gert Driesen <drieseng@users.sourceforge.net> | 2009-06-06 21:07:34 +0400 |
---|---|---|
committer | Gert Driesen <drieseng@users.sourceforge.net> | 2009-06-06 21:07:34 +0400 |
commit | 3ee30c68d12c8989ea663fcbe294b8b21742abb8 (patch) | |
tree | 8e6a2323fd8617639d4ed362ecd388727a4c2229 /mcs | |
parent | bf070eecb16eae2d70844b7e182073a571a0b2c0 (diff) |
* ContextStack.cs: Added argument check to Type indexer, avoiding a
NRE. Modify System.Int32 indexer to throw ArgumentOutOfRangeException
instead of ArgumentException. Reduce number of times that item count
must be obtained.
* ContextStackTest.cs: Added unit tests for indexers, and argument
(null) checks.
svn path=/trunk/mcs/; revision=135596
Diffstat (limited to 'mcs')
4 files changed, 183 insertions, 31 deletions
diff --git a/mcs/class/System/System.ComponentModel.Design.Serialization/ChangeLog b/mcs/class/System/System.ComponentModel.Design.Serialization/ChangeLog index 3f705c8cadc..3d9e9f98d35 100755 --- a/mcs/class/System/System.ComponentModel.Design.Serialization/ChangeLog +++ b/mcs/class/System/System.ComponentModel.Design.Serialization/ChangeLog @@ -1,3 +1,10 @@ +2009-06-06 Gert Driesen <drieseng@users.sourceforge.net> + + * ContextStack.cs: Added argument check to Type indexer, avoiding a + NRE. Modify System.Int32 indexer to throw ArgumentOutOfRangeException + instead of ArgumentException. Reduce number of times that item count + must be obtained. + 2008-06-05 Ivan N. Zlatev <contact@i-nz.net> * ContextStack.cs: Also check for subclasses in the Type-based diff --git a/mcs/class/System/System.ComponentModel.Design.Serialization/ContextStack.cs b/mcs/class/System/System.ComponentModel.Design.Serialization/ContextStack.cs index 132e28447c8..31b238c969b 100755..100644 --- a/mcs/class/System/System.ComponentModel.Design.Serialization/ContextStack.cs +++ b/mcs/class/System/System.ComponentModel.Design.Serialization/ContextStack.cs @@ -47,18 +47,23 @@ namespace System.ComponentModel.Design.Serialization } public object Current { - get { - if (_contextList.Count > 0) - return _contextList[_contextList.Count-1]; + get { + int context_count = _contextList.Count; + if (context_count > 0) + return _contextList [context_count - 1]; return null; } } public object this[Type type] { get { - for (int i = _contextList.Count - 1; i >= 0; i--) - if (type.IsInstanceOfType (_contextList[i])) - return _contextList[i]; + if (type == null) + throw new ArgumentNullException ("type"); + for (int i = _contextList.Count - 1; i >= 0; i--) { + object context = _contextList [i]; + if (type.IsInstanceOfType (context)) + return context; + } return null; } } @@ -66,9 +71,10 @@ namespace System.ComponentModel.Design.Serialization public object this[int level] { get { if (level < 0) - throw new ArgumentException ("level has to be >= 0","level"); - if (_contextList.Count > 0 && _contextList.Count > level) - return _contextList[_contextList.Count - 1 - level]; + throw new ArgumentOutOfRangeException ("level"); + int context_count = _contextList.Count; + if (context_count > 0 && context_count > level) + return _contextList [context_count - 1 - level]; return null; } } @@ -76,11 +82,12 @@ namespace System.ComponentModel.Design.Serialization public object Pop () { object o = null; - if (_contextList.Count > 0) { - int lastItem = _contextList.Count - 1; - o = _contextList[lastItem]; - _contextList.RemoveAt (lastItem); - } + int context_count = _contextList.Count; + if (context_count > 0) { + int lastItem = context_count - 1; + o = _contextList [lastItem]; + _contextList.RemoveAt (lastItem); + } return o; } @@ -88,7 +95,6 @@ namespace System.ComponentModel.Design.Serialization { if (context == null) throw new ArgumentNullException ("context"); - _contextList.Add (context); } diff --git a/mcs/class/System/Test/System.ComponentModel.Design.Serialization/ChangeLog b/mcs/class/System/Test/System.ComponentModel.Design.Serialization/ChangeLog index c3c2934174e..07a95feb0c7 100644 --- a/mcs/class/System/Test/System.ComponentModel.Design.Serialization/ChangeLog +++ b/mcs/class/System/Test/System.ComponentModel.Design.Serialization/ChangeLog @@ -1,3 +1,8 @@ +2009-06-06 Gert Driesen <drieseng@users.sourceforge.net> + + * ContextStackTest.cs: Added unit tests for indexers, and argument + (null) checks. + 2007-08-20 Gert Driesen <drieseng@users.sourceforge.net> * ContextStackTest.cs: Fix build of unit tests on 1.0 profile. diff --git a/mcs/class/System/Test/System.ComponentModel.Design.Serialization/ContextStackTest.cs b/mcs/class/System/Test/System.ComponentModel.Design.Serialization/ContextStackTest.cs index 250f0d73920..4808a3b9334 100644 --- a/mcs/class/System/Test/System.ComponentModel.Design.Serialization/ContextStackTest.cs +++ b/mcs/class/System/Test/System.ComponentModel.Design.Serialization/ContextStackTest.cs @@ -27,10 +27,11 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // -using NUnit.Framework; - +using System; using System.ComponentModel.Design.Serialization; +using NUnit.Framework; + namespace MonoTests.System.ComponentModel.Design.Serialization { [TestFixture] @@ -45,30 +46,163 @@ namespace MonoTests.System.ComponentModel.Design.Serialization string two = "two"; stack.Push (two); stack.Push (one); - Assert.AreEqual (one, stack[typeof (string)], "#1"); - Assert.AreEqual (one, stack[0], "#2"); - Assert.AreEqual (one, stack.Current, "#3"); + Assert.AreSame (one, stack [typeof (string)], "#1"); + Assert.AreSame (one, stack [0], "#2"); + Assert.AreSame (one, stack.Current, "#3"); - Assert.AreEqual (one, stack.Pop (), "#4"); + Assert.AreSame (one, stack.Pop (), "#4"); - Assert.AreEqual (two, stack[typeof (string)], "#5"); - Assert.AreEqual (two, stack[0], "#6"); - Assert.AreEqual (two, stack.Current, "#7"); + Assert.AreSame (two, stack [typeof (string)], "#5"); + Assert.AreSame (two, stack [0], "#6"); + Assert.AreSame (two, stack.Current, "#7"); #if NET_2_0 string three = "three"; stack.Append (three); - Assert.AreEqual (two, stack[typeof (string)], "#8"); - Assert.AreEqual (two, stack[0], "#9"); - Assert.AreEqual (two, stack.Current, "#10"); + Assert.AreSame (two, stack[typeof (string)], "#8"); + Assert.AreSame (two, stack[0], "#9"); + Assert.AreSame (two, stack.Current, "#10"); - Assert.AreEqual (two, stack.Pop (), "#11"); + Assert.AreSame (two, stack.Pop (), "#11"); - Assert.AreEqual (three, stack[typeof (string)], "#12"); - Assert.AreEqual (three, stack[0], "#13"); - Assert.AreEqual (three, stack.Current, "#14"); + Assert.AreSame (three, stack[typeof (string)], "#12"); + Assert.AreSame (three, stack[0], "#13"); + Assert.AreSame (three, stack.Current, "#14"); + Assert.AreSame (three, stack.Pop (), "#15"); +#else + Assert.AreSame (two, stack.Pop (), "#15"); +#endif + + Assert.IsNull (stack.Pop (), "#16"); + Assert.IsNull (stack.Current, "#17"); + } + +#if NET_2_0 + [Test] + public void Append_Context_Null () + { + ContextStack stack = new ContextStack (); + try { + stack.Append (null); + Assert.Fail ("#1"); + } catch (ArgumentNullException ex) { + Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual ("context", ex.ParamName, "#5"); + } + } #endif + + [Test] // Item (Int32) + public void Indexer1 () + { + ContextStack stack = new ContextStack (); + string one = "one"; + string two = "two"; + + stack.Push (one); + stack.Push (two); + + Assert.AreSame (two, stack [0], "#1"); + Assert.AreSame (one, stack [1], "#2"); + Assert.IsNull (stack [2], "#3"); + Assert.AreSame (two, stack.Pop (), "#4"); + Assert.AreSame (one, stack [0], "#5"); + Assert.IsNull (stack [1], "#6"); + Assert.AreSame (one, stack.Pop (), "#7"); + Assert.IsNull (stack [0], "#8"); + Assert.IsNull (stack [1], "#9"); + } + + [Test] // Item (Int32) + public void Indexer1_Level_Negative () + { + ContextStack stack = new ContextStack (); + stack.Push (new Foo ()); + + try { + object context = stack [-1]; + Assert.Fail ("#A1:" + context); + } catch (ArgumentOutOfRangeException ex) { + Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2"); + Assert.IsNull (ex.InnerException, "#A3"); + Assert.AreEqual (new ArgumentOutOfRangeException ("level").Message, ex.Message, "#A4"); + Assert.AreEqual ("level", ex.ParamName, "#A5"); + } + + try { + object context = stack [-5]; + Assert.Fail ("#B1:" + context); + } catch (ArgumentOutOfRangeException ex) { + Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2"); + Assert.IsNull (ex.InnerException, "#B3"); + Assert.AreEqual (new ArgumentOutOfRangeException ("level").Message, ex.Message, "#B4"); + Assert.AreEqual ("level", ex.ParamName, "#B5"); + } + } + + [Test] // Item (Type) + public void Indexer2 () + { + ContextStack stack = new ContextStack (); + + Foo foo = new Foo (); + FooBar foobar = new FooBar (); + + stack.Push (foobar); + stack.Push (foo); + Assert.AreSame (foo, stack [typeof (Foo)], "#1"); + Assert.AreSame (foo, stack [typeof (IFoo)], "#2"); + Assert.AreSame (foo, stack.Pop (), "#3"); + Assert.AreSame (foobar, stack [typeof (Foo)], "#4"); + Assert.AreSame (foobar, stack [typeof (FooBar)], "#5"); + Assert.AreSame (foobar, stack [typeof (IFoo)], "#6"); + Assert.IsNull (stack [typeof (string)], "#7"); + } + + [Test] // Item (Type) + public void Indexer2_Type_Null () + { + ContextStack stack = new ContextStack (); + stack.Push (new Foo ()); + try { + object context = stack [(Type) null]; + Assert.Fail ("#1:" + context); + } catch (ArgumentNullException ex) { + Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual ("type", ex.ParamName, "#5"); + } + } + + [Test] + public void Push_Context_Null () + { + ContextStack stack = new ContextStack (); + try { + stack.Push (null); + Assert.Fail ("#1"); + } catch (ArgumentNullException ex) { + Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual ("context", ex.ParamName, "#5"); + } + } + + public interface IFoo + { + } + + public class Foo : IFoo + { + } + + public class FooBar : Foo + { } } } |