XmlCharacterDataTests.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. public void InsertData ()
  77. {
  78. comment.Value = "foobaz";
  79. changed = false;
  80. changing = false;
  81. comment.InsertData (3, "bar");
  82. Assert (changed);
  83. Assert (changing);
  84. AssertEquals ("foobarbaz", comment.Data);
  85. try
  86. {
  87. comment.Value = "foo";
  88. comment.InsertData (-1, "bar");
  89. Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  90. }
  91. catch (ArgumentOutOfRangeException) {}
  92. comment.Value = "foo";
  93. comment.InsertData (3, "bar");
  94. AssertEquals ("foobar", comment.Data);
  95. try
  96. {
  97. comment.Value = "foo";
  98. comment.InsertData (4, "bar");
  99. Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  100. }
  101. catch (ArgumentOutOfRangeException) {}
  102. try
  103. {
  104. comment.Value = "foo";
  105. comment.InsertData (1, null);
  106. Fail ("Expected an ArgumentNullException to be thrown.");
  107. }
  108. catch (ArgumentNullException) {}
  109. }
  110. [Test]
  111. public void ReplaceData ()
  112. {
  113. changed = false;
  114. changing = false;
  115. comment.ReplaceData (0, 3, "bar");
  116. Assert (changed);
  117. Assert (changing);
  118. AssertEquals ("bar", comment.Data);
  119. comment.Value = "foo";
  120. comment.ReplaceData (2, 3, "bar");
  121. AssertEquals ("fobar", comment.Data);
  122. comment.Value = "foo";
  123. comment.ReplaceData (3, 3, "bar");
  124. AssertEquals ("foobar", comment.Data);
  125. try
  126. {
  127. comment.Value = "foo";
  128. comment.ReplaceData (4, 3, "bar");
  129. Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  130. }
  131. catch (ArgumentOutOfRangeException) {}
  132. try
  133. {
  134. comment.Value = "foo";
  135. comment.ReplaceData (-1, 3, "bar");
  136. Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  137. }
  138. catch (ArgumentOutOfRangeException) {}
  139. comment.Value = "foo";
  140. comment.ReplaceData (0, 2, "bar");
  141. AssertEquals ("baro", comment.Data);
  142. comment.Value = "foo";
  143. comment.ReplaceData (0, 5, "bar");
  144. AssertEquals ("bar", comment.Data);
  145. try
  146. {
  147. comment.Value = "foo";
  148. comment.ReplaceData (1, 1, null);
  149. Fail ("Expected an ArgumentNullException to be thrown.");
  150. }
  151. catch (ArgumentNullException) {}
  152. }
  153. [Test]
  154. public void Substring ()
  155. {
  156. comment.Value = "test string";
  157. AssertEquals ("test string", comment.Substring (0, 50));
  158. }
  159. [Test]
  160. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  161. public void SubstringStartOutOfRange ()
  162. {
  163. comment.Value = "test string";
  164. comment.Substring (-5, 10);
  165. }
  166. [Test]
  167. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  168. public void SubstringCountOutOfRange ()
  169. {
  170. comment.Value = "test string";
  171. comment.Substring (10, -5);
  172. }
  173. }
  174. }