CodeCatchClauseCollectionTest.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //
  2. // CodeCatchClauseCollectionTest.cs
  3. // - Unit tests for System.CodeDom.CodeCatchClauseCollection
  4. //
  5. // Author:
  6. // Gert Driesen <[email protected]>
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using NUnit.Framework;
  30. using System;
  31. using System.Collections;
  32. using System.CodeDom;
  33. namespace MonoTests.System.CodeDom {
  34. [TestFixture]
  35. public class CodeCatchClauseCollectionTest {
  36. [Test]
  37. public void Constructor0 ()
  38. {
  39. CodeCatchClauseCollection coll = new CodeCatchClauseCollection ();
  40. Assert.IsFalse (((IList) coll).IsFixedSize, "#1");
  41. Assert.IsFalse (((IList) coll).IsReadOnly, "#2");
  42. Assert.AreEqual (0, coll.Count, "#3");
  43. Assert.IsFalse (((ICollection) coll).IsSynchronized, "#4");
  44. Assert.IsNotNull (((ICollection) coll).SyncRoot, "#5");
  45. }
  46. [Test]
  47. public void Constructor1 ()
  48. {
  49. CodeCatchClause cc1 = new CodeCatchClause ();
  50. CodeCatchClause cc2 = new CodeCatchClause ();
  51. CodeCatchClause[] catchClauses = new CodeCatchClause[] { cc1, cc2 };
  52. CodeCatchClauseCollection coll = new CodeCatchClauseCollection (
  53. catchClauses);
  54. Assert.AreEqual (2, coll.Count, "#1");
  55. Assert.AreEqual (0, coll.IndexOf (cc1), "#2");
  56. Assert.AreEqual (1, coll.IndexOf (cc2), "#3");
  57. }
  58. [Test]
  59. [ExpectedException (typeof (ArgumentNullException))]
  60. public void Constructor1_NullItem ()
  61. {
  62. CodeCatchClause[] catchClauses = new CodeCatchClause[] {
  63. new CodeCatchClause (), null };
  64. CodeCatchClauseCollection coll = new CodeCatchClauseCollection (
  65. catchClauses);
  66. }
  67. [Test]
  68. [ExpectedException (typeof (ArgumentNullException))]
  69. public void Constructor1_Null () {
  70. CodeCatchClauseCollection coll = new CodeCatchClauseCollection (
  71. (CodeCatchClause[]) null);
  72. }
  73. [Test]
  74. public void Constructor2 ()
  75. {
  76. CodeCatchClause cc1 = new CodeCatchClause ();
  77. CodeCatchClause cc2 = new CodeCatchClause ();
  78. CodeCatchClauseCollection c = new CodeCatchClauseCollection ();
  79. c.Add (cc1);
  80. c.Add (cc2);
  81. CodeCatchClauseCollection coll = new CodeCatchClauseCollection (c);
  82. Assert.AreEqual (2, coll.Count, "#1");
  83. Assert.AreEqual (0, coll.IndexOf (cc1), "#2");
  84. Assert.AreEqual (1, coll.IndexOf (cc2), "#3");
  85. }
  86. [Test]
  87. [ExpectedException (typeof (ArgumentNullException))]
  88. public void Constructor2_Null ()
  89. {
  90. CodeCatchClauseCollection coll = new CodeCatchClauseCollection (
  91. (CodeCatchClauseCollection) null);
  92. }
  93. [Test]
  94. public void Add ()
  95. {
  96. CodeCatchClause cc1 = new CodeCatchClause ();
  97. CodeCatchClause cc2 = new CodeCatchClause ();
  98. CodeCatchClauseCollection coll = new CodeCatchClauseCollection ();
  99. Assert.AreEqual (0, coll.Add (cc1), "#1");
  100. Assert.AreEqual (1, coll.Count, "#2");
  101. Assert.AreEqual (0, coll.IndexOf (cc1), "#3");
  102. Assert.AreEqual (1, coll.Add (cc2), "#4");
  103. Assert.AreEqual (2, coll.Count, "#5");
  104. Assert.AreEqual (1, coll.IndexOf (cc2), "#6");
  105. }
  106. [Test]
  107. [ExpectedException (typeof (ArgumentNullException))]
  108. public void Add_Null () {
  109. CodeCatchClauseCollection coll = new CodeCatchClauseCollection ();
  110. coll.Add ((CodeCatchClause) null);
  111. }
  112. [Test]
  113. public void Insert ()
  114. {
  115. CodeCatchClause cc1 = new CodeCatchClause ();
  116. CodeCatchClause cc2 = new CodeCatchClause ();
  117. CodeCatchClauseCollection coll = new CodeCatchClauseCollection ();
  118. coll.Add (cc1);
  119. Assert.AreEqual (1, coll.Count, "#1");
  120. Assert.AreEqual (0, coll.IndexOf (cc1), "#2");
  121. coll.Insert (0, cc2);
  122. Assert.AreEqual (2, coll.Count, "#3");
  123. Assert.AreEqual (1, coll.IndexOf (cc1), "#4");
  124. Assert.AreEqual (0, coll.IndexOf (cc2), "#5");
  125. }
  126. [Test]
  127. [ExpectedException (typeof (ArgumentNullException))]
  128. public void Insert_Null ()
  129. {
  130. CodeCatchClauseCollection coll = new CodeCatchClauseCollection ();
  131. coll.Insert (0, (CodeCatchClause) null);
  132. }
  133. [Test]
  134. public void AddRange ()
  135. {
  136. CodeCatchClause cc1 = new CodeCatchClause ();
  137. CodeCatchClause cc2 = new CodeCatchClause ();
  138. CodeCatchClause cc3 = new CodeCatchClause ();
  139. CodeCatchClauseCollection coll1 = new CodeCatchClauseCollection ();
  140. coll1.Add (cc1);
  141. coll1.Add (cc2);
  142. CodeCatchClauseCollection coll2 = new CodeCatchClauseCollection ();
  143. coll2.Add (cc3);
  144. coll2.AddRange (coll1);
  145. Assert.AreEqual (3, coll2.Count, "#1");
  146. Assert.AreEqual (1, coll2.IndexOf (cc1), "#2");
  147. Assert.AreEqual (2, coll2.IndexOf (cc2), "#3");
  148. Assert.AreEqual (0, coll2.IndexOf (cc3), "#4");
  149. CodeCatchClauseCollection coll3 = new CodeCatchClauseCollection ();
  150. coll3.Add (cc3);
  151. coll3.AddRange (new CodeCatchClause[] { cc1, cc2 });
  152. Assert.AreEqual (3, coll2.Count, "#5");
  153. Assert.AreEqual (1, coll2.IndexOf (cc1), "#6");
  154. Assert.AreEqual (2, coll2.IndexOf (cc2), "#7");
  155. Assert.AreEqual (0, coll2.IndexOf (cc3), "#8");
  156. }
  157. [Test]
  158. [ExpectedException (typeof (ArgumentNullException))]
  159. public void AddRange_Null_Array ()
  160. {
  161. CodeCatchClauseCollection coll = new CodeCatchClauseCollection ();
  162. coll.AddRange ((CodeCatchClause[]) null);
  163. }
  164. [Test]
  165. [ExpectedException (typeof (ArgumentNullException))]
  166. public void AddRange_Null_Item ()
  167. {
  168. CodeCatchClauseCollection coll = new CodeCatchClauseCollection ();
  169. coll.AddRange (new CodeCatchClause[] { null });
  170. }
  171. [Test]
  172. [ExpectedException (typeof (ArgumentNullException))]
  173. public void AddRange_Null_Collection ()
  174. {
  175. CodeCatchClauseCollection coll = new CodeCatchClauseCollection ();
  176. coll.AddRange ((CodeCatchClauseCollection) null);
  177. }
  178. [Test]
  179. public void AddRange_Self ()
  180. {
  181. CodeCatchClauseCollection coll = new CodeCatchClauseCollection ();
  182. coll.Add (new CodeCatchClause ());
  183. Assert.AreEqual (1, coll.Count, "#1");
  184. coll.AddRange (coll);
  185. Assert.AreEqual (2, coll.Count, "#2");
  186. }
  187. [Test]
  188. public void Remove ()
  189. {
  190. CodeCatchClause ccc1 = new CodeCatchClause ();
  191. CodeCatchClause ccc2 = new CodeCatchClause ();
  192. CodeCatchClauseCollection coll = new CodeCatchClauseCollection ();
  193. coll.Add (ccc1);
  194. coll.Add (ccc2);
  195. Assert.AreEqual (2, coll.Count, "#1");
  196. Assert.AreEqual (0, coll.IndexOf (ccc1), "#2");
  197. Assert.AreEqual (1, coll.IndexOf (ccc2), "#3");
  198. coll.Remove (ccc1);
  199. Assert.AreEqual (1, coll.Count, "#4");
  200. Assert.AreEqual (-1, coll.IndexOf (ccc1), "#5");
  201. Assert.AreEqual (0, coll.IndexOf (ccc2), "#6");
  202. }
  203. [Test]
  204. [ExpectedException (typeof (ArgumentException))]
  205. public void Remove_NotInCollection ()
  206. {
  207. CodeCatchClauseCollection coll = new CodeCatchClauseCollection ();
  208. coll.Remove (new CodeCatchClause ());
  209. }
  210. [Test]
  211. [ExpectedException (typeof (ArgumentNullException))]
  212. public void Remove_Null ()
  213. {
  214. CodeCatchClauseCollection coll = new CodeCatchClauseCollection ();
  215. coll.Remove ((CodeCatchClause) null);
  216. }
  217. }
  218. }