XmlCharacterDataTests.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // System.Xml.XmlTextWriterTests
  3. //
  4. // Author: Kral Ferch <[email protected]>
  5. // Author: Martin Willemoes Hansen <[email protected]>
  6. //
  7. // (C) 2002 Kral Ferch
  8. // (C) 2003 Martin Willemoes Hansen
  9. //
  10. using System;
  11. using System.Xml;
  12. using NUnit.Framework;
  13. namespace MonoTests.System.Xml
  14. {
  15. [TestFixture]
  16. public class XmlCharacterDataTests : Assertion
  17. {
  18. XmlDocument document;
  19. XmlComment comment;
  20. bool changed;
  21. bool changing;
  22. [SetUp]
  23. public void GetReady ()
  24. {
  25. document = new XmlDocument ();
  26. document.NodeChanged += new XmlNodeChangedEventHandler (this.EventNodeChanged);
  27. document.NodeChanging += new XmlNodeChangedEventHandler (this.EventNodeChanging);
  28. comment = document.CreateComment ("foo");
  29. }
  30. private void EventNodeChanged(Object sender, XmlNodeChangedEventArgs e)
  31. {
  32. changed = true;
  33. }
  34. private void EventNodeChanging (Object sender, XmlNodeChangedEventArgs e)
  35. {
  36. changing = true;
  37. }
  38. [Test]
  39. public void AppendData ()
  40. {
  41. changed = false;
  42. changing = false;
  43. comment.AppendData ("bar");
  44. Assert (changed);
  45. Assert (changing);
  46. AssertEquals ("foobar", comment.Data);
  47. comment.Value = "foo";
  48. comment.AppendData (null);
  49. AssertEquals ("foo", comment.Data);
  50. }
  51. [Test]
  52. public void DeleteData ()
  53. {
  54. comment.Value = "bar";
  55. changed = false;
  56. changing = false;
  57. comment.DeleteData (1, 1);
  58. Assert (changed);
  59. Assert (changing);
  60. AssertEquals ("br", comment.Data);
  61. try
  62. {
  63. comment.Value = "foo";
  64. comment.DeleteData(-1, 1);
  65. Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  66. }
  67. catch (ArgumentOutOfRangeException) {}
  68. comment.Value = "foo";
  69. comment.DeleteData(1, 5);
  70. AssertEquals("f", comment.Data);
  71. comment.Value = "foo";
  72. comment.DeleteData(3, 10);
  73. AssertEquals("foo", comment.Data);
  74. }
  75. [Test]
  76. #if NET_2_0
  77. [Category ("NotDotNet")] // enbug in 2.0
  78. #endif
  79. public void InsertData ()
  80. {
  81. comment.Value = "foobaz";
  82. changed = false;
  83. changing = false;
  84. comment.InsertData (3, "bar");
  85. Assert (changed);
  86. Assert (changing);
  87. AssertEquals ("foobarbaz", comment.Data);
  88. try
  89. {
  90. comment.Value = "foo";
  91. comment.InsertData (-1, "bar");
  92. Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  93. }
  94. catch (ArgumentOutOfRangeException) {}
  95. comment.Value = "foo";
  96. comment.InsertData (3, "bar");
  97. AssertEquals ("foobar", comment.Data);
  98. try
  99. {
  100. comment.Value = "foo";
  101. comment.InsertData (4, "bar");
  102. Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  103. }
  104. catch (ArgumentOutOfRangeException) {}
  105. try
  106. {
  107. comment.Value = "foo";
  108. comment.InsertData (1, null);
  109. Fail ("Expected an ArgumentNullException to be thrown.");
  110. }
  111. catch (ArgumentNullException) {}
  112. }
  113. [Test]
  114. #if NET_2_0
  115. [Category ("NotDotNet")] // enbug in 2.0
  116. #endif
  117. public void ReplaceData ()
  118. {
  119. changed = false;
  120. changing = false;
  121. comment.ReplaceData (0, 3, "bar");
  122. Assert (changed);
  123. Assert (changing);
  124. AssertEquals ("bar", comment.Data);
  125. comment.Value = "foo";
  126. comment.ReplaceData (2, 3, "bar");
  127. AssertEquals ("fobar", comment.Data);
  128. comment.Value = "foo";
  129. comment.ReplaceData (3, 3, "bar");
  130. AssertEquals ("foobar", comment.Data);
  131. try
  132. {
  133. comment.Value = "foo";
  134. comment.ReplaceData (4, 3, "bar");
  135. Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  136. }
  137. catch (ArgumentOutOfRangeException) {}
  138. try
  139. {
  140. comment.Value = "foo";
  141. comment.ReplaceData (-1, 3, "bar");
  142. Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  143. }
  144. catch (ArgumentOutOfRangeException) {}
  145. comment.Value = "foo";
  146. comment.ReplaceData (0, 2, "bar");
  147. AssertEquals ("baro", comment.Data);
  148. comment.Value = "foo";
  149. comment.ReplaceData (0, 5, "bar");
  150. AssertEquals ("bar", comment.Data);
  151. try
  152. {
  153. comment.Value = "foo";
  154. comment.ReplaceData (1, 1, null);
  155. Fail ("Expected an ArgumentNullException to be thrown.");
  156. }
  157. catch (ArgumentNullException) {}
  158. }
  159. [Test]
  160. public void Substring ()
  161. {
  162. comment.Value = "test string";
  163. AssertEquals ("test string", comment.Substring (0, 50));
  164. }
  165. [Test]
  166. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  167. public void SubstringStartOutOfRange ()
  168. {
  169. comment.Value = "test string";
  170. comment.Substring (-5, 10);
  171. }
  172. [Test]
  173. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  174. public void SubstringCountOutOfRange ()
  175. {
  176. comment.Value = "test string";
  177. comment.Substring (10, -5);
  178. }
  179. }
  180. }