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