ListItemCollectionTest.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. //
  2. // Tests for System.Web.UI.WebControls.ListItemCollection.cs
  3. //
  4. // Author:
  5. // Peter Dennis Bartok ([email protected])
  6. //
  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.Drawing;
  33. using System.IO;
  34. using System.Globalization;
  35. using System.Web;
  36. using System.Web.UI;
  37. using System.Web.UI.WebControls;
  38. namespace MonoTests.System.Web.UI.WebControls
  39. {
  40. [TestFixture]
  41. public class ListItemCollectionTest
  42. {
  43. [Test]
  44. public void Methods ()
  45. {
  46. ListItemCollection c;
  47. ListItem i;
  48. ListItem i2;
  49. c = new ListItemCollection();
  50. Assert.AreEqual (0, c.Count, "T1");
  51. i = new ListItem("Item 1", "10");
  52. c.Add(i);
  53. Assert.AreEqual (1, c.Count, "T2");
  54. i = new ListItem("This is item 2", "20");
  55. c.Add(i);
  56. Assert.AreEqual (2, c.Count, "T3");
  57. Assert.AreEqual (null, c.FindByText(" is "), "T4");
  58. Assert.AreEqual (i.Text, c.FindByText("This is item 2").Text, "T5");
  59. Assert.AreSame (i, c.FindByText("This is item 2"), "T6");
  60. Assert.AreEqual (1, c.IndexOf(c.FindByText("This is item 2")), "T7");
  61. Assert.AreEqual (1, c.IndexOf(c.FindByValue("20")), "T8");
  62. i = new ListItem("Item 3", "30");
  63. Assert.IsFalse(c.Contains(i), "T9");
  64. c.Add(i);
  65. Assert.IsTrue(c.Contains(i), "T10");
  66. i = new ListItem("Forth", "40");
  67. i2 = new ListItem("Fifth", "50");
  68. c.AddRange(new ListItem[] {i, i2});
  69. Assert.AreEqual (5, c.Count, "T11");
  70. c.RemoveAt(1);
  71. Assert.AreEqual (4, c.Count, "T12");
  72. Assert.AreEqual (null, c.FindByText("This is item 2"), "T13");
  73. c.Clear();
  74. Assert.AreEqual (0, c.Count, "T13");
  75. }
  76. [Test]
  77. public void IListTest ()
  78. {
  79. ListItemCollection c;
  80. ListItem i;
  81. ListItem i2;
  82. c = new ListItemCollection();
  83. i = new ListItem("Item 1", "1");
  84. i2 = new ListItem("Item 2", "2");
  85. Assert.AreEqual(0, c.Count, "I1");
  86. ((IList)c).Add(i);
  87. Assert.AreEqual(1, c.Count, "I2");
  88. ((IList)c).Clear();
  89. Assert.AreEqual(0, c.Count, "I3");
  90. ((IList)c).Add(i);
  91. ((IList)c).Add(i2);
  92. Assert.AreEqual(1, ((IList)c).IndexOf(i2), "I4");
  93. ((IList)c).RemoveAt(1);
  94. Assert.AreEqual(1, c.Count, "I5");
  95. Assert.AreEqual(true, ((IList)c).Contains(i), "I6");
  96. }
  97. [Test]
  98. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  99. public void RemoveInvalidTest ()
  100. {
  101. ListItemCollection c;
  102. ListItem i;
  103. ListItem i2;
  104. c = new ListItemCollection();
  105. i = new ListItem("Item 1", "1");
  106. i2 = new ListItem("Item 2", "2");
  107. c.Add(i);
  108. c.Add(i2);
  109. c.RemoveAt(20);
  110. }
  111. [Test]
  112. [ExpectedException(typeof(InvalidCastException))]
  113. public void AddInvalidTest ()
  114. {
  115. ListItemCollection c;
  116. c = new ListItemCollection();
  117. ((IList)c).Add(5);
  118. }
  119. [Test]
  120. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  121. public void AccessInvalidTest () {
  122. ListItemCollection c;
  123. ListItem i;
  124. ListItem i2;
  125. c = new ListItemCollection();
  126. i = new ListItem("Item 1", "1");
  127. i2 = new ListItem("Item 2", "2");
  128. i = c[3];
  129. }
  130. [Test]
  131. public void AddTest ()
  132. {
  133. ListItemCollection c;
  134. ListItem i;
  135. c = new ListItemCollection();
  136. c.Add("string");
  137. Assert.AreEqual("string", c.FindByText("string").ToString(), "A1");
  138. c.Add((string)null);
  139. c.Add((ListItem)null);
  140. Assert.AreEqual(3, c.Count, "A2");
  141. }
  142. [Test]
  143. public void AssignmentTest () {
  144. ListItemCollection c;
  145. ListItem i;
  146. c = new ListItemCollection();
  147. i = new ListItem("Text", "Value");
  148. c.Add(i);
  149. i = new ListItem("Blah", "Argl");
  150. ((IList)c)[0] = i;
  151. Assert.AreEqual("Blah", c.FindByText("Blah").ToString(), "AS1");
  152. Assert.AreEqual(1, c.Count, "AS2");
  153. }
  154. [Test]
  155. [ExpectedException(typeof(InvalidCastException))]
  156. public void AssignmentExceptionTest () {
  157. ListItemCollection c;
  158. ListItem i;
  159. c = new ListItemCollection();
  160. i = new ListItem("Text", "Value");
  161. c.Add(i);
  162. ((IList)c)[0] = 5;
  163. }
  164. [Test]
  165. public void ContainsTest ()
  166. {
  167. ListItemCollection c;
  168. ListItem i;
  169. ListItem i2;
  170. c = new ListItemCollection();
  171. i = new ListItem("Item 1", "1");
  172. i2 = new ListItem("Item 2", "2");
  173. c.Add(i);
  174. c.Add(i2);
  175. i2 = new ListItem("Item 1", "1");
  176. // test same vs equal
  177. Assert.AreEqual (true, c.Contains(i), "C1");
  178. Assert.AreEqual (true, c.Contains(i2), "C2");
  179. }
  180. [Test]
  181. public void IndexOfTest () {
  182. ListItemCollection c;
  183. ListItem i;
  184. ListItem i2;
  185. c = new ListItemCollection();
  186. i = new ListItem("Item 1", "1");
  187. i2 = new ListItem("Item 2", "2");
  188. c.Add(i);
  189. c.Add(i2);
  190. i = new ListItem("Item 2", "2");
  191. // test same vs equal
  192. Assert.AreEqual (1, c.IndexOf(i), "IO1");
  193. Assert.AreEqual (1, c.IndexOf(i2), "IO2");
  194. }
  195. [Test]
  196. [ExpectedException(typeof(InvalidCastException))]
  197. public void ContainsTypeTest () {
  198. ListItemCollection c;
  199. ListItem i;
  200. ListItem i2;
  201. c = new ListItemCollection();
  202. i = new ListItem("Item 1", "1");
  203. i2 = new ListItem("Item 2", "2");
  204. c.Add(i);
  205. c.Add(i2);
  206. Assert.AreEqual (false, ((IList)c).Contains(5), "CT1");
  207. }
  208. [Test]
  209. public void RemoveTest () {
  210. ListItemCollection c;
  211. ListItem i;
  212. ListItem i2;
  213. c = new ListItemCollection();
  214. i = new ListItem("Item 1", "1");
  215. i2 = new ListItem("Item 2", "2");
  216. c.Add(i);
  217. c.Add(i2);
  218. i = new ListItem("Item 2", "2");
  219. // test same vs equal
  220. c.Remove(i);
  221. Assert.AreEqual (1, c.Count, "R1");
  222. }
  223. [Test]
  224. public void ViewState () {
  225. ListItemCollection c;
  226. ListItemCollection c2;
  227. ListItem i;
  228. ListItem i2;
  229. object state;
  230. c = new ListItemCollection();
  231. i = new ListItem("Item 1", "1");
  232. i2 = new ListItem("Item 2", "2");
  233. ((IStateManager)c).TrackViewState();
  234. c.Add(i);
  235. c.Add(i2);
  236. Assert.AreEqual (2, c.Count, "V1");
  237. state = ((IStateManager)c).SaveViewState();
  238. c2 = new ListItemCollection();
  239. ((IStateManager)c2).LoadViewState(state);
  240. Assert.AreEqual (2, c2.Count, "V2");
  241. Assert.AreEqual ("Item 1", c2.FindByText("Item 1").ToString(), "V3");
  242. Assert.AreEqual ("Item 2", c2.FindByText("Item 2").ToString(), "V4");
  243. Assert.AreEqual (false, c2.IndexOf(i) == c2.IndexOf(i2), "V5");
  244. }
  245. }
  246. }