2003-02-09 Piers Haken <piersh@friskit.com>
[mono.git] / mcs / class / System.XML / Test / XmlCharacterDataTests.cs
1 //
2 // System.Xml.XmlTextWriterTests
3 //
4 // Author:
5 //   Kral Ferch <kral_ferch@hotmail.com>
6 //
7 // (C) 2002 Kral Ferch
8 //
9
10 using System;
11 using System.Xml;
12
13 using NUnit.Framework;
14
15 namespace MonoTests.System.Xml
16 {
17         public class XmlCharacterDataTests : TestCase
18         {
19                 public XmlCharacterDataTests () : base ("MonoTests.System.Xml.XmlCharacterDataTests testsuite") {}
20                 public XmlCharacterDataTests (string name) : base (name) {}
21
22                 XmlDocument document;
23                 XmlComment comment;
24                 bool changed;
25                 bool changing;
26
27                 protected override void SetUp ()
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                 public void TestAppendData ()
46                 {
47                         changed = false;
48                         changing = false;
49                         comment.AppendData ("bar");
50                         Assert (changed);
51                         Assert (changing);
52                         AssertEquals ("foobar", comment.Data);
53
54                         comment.Value = "foo";
55                         comment.AppendData (null);
56                         AssertEquals ("foo", comment.Data);
57                 }
58
59                 public void TestDeleteData ()
60                 {
61                         comment.Value = "bar";
62                         changed = false;
63                         changing = false;
64                         comment.DeleteData (1, 1);
65                         Assert (changed);
66                         Assert (changing);
67                         AssertEquals ("br", comment.Data);
68
69                         try 
70                         {
71                                 comment.Value = "foo";
72                                 comment.DeleteData(-1, 1);
73                                 Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
74                         } 
75                         catch (ArgumentOutOfRangeException) {}
76
77                         comment.Value = "foo";
78                         comment.DeleteData(1, 5);
79                         AssertEquals("f", comment.Data);
80
81                         comment.Value = "foo";
82                         comment.DeleteData(3, 10);
83                         AssertEquals("foo", comment.Data);
84                 }
85
86                 public void TestInsertData ()
87                 {
88                         comment.Value = "foobaz";
89                         changed = false;
90                         changing = false;
91                         comment.InsertData (3, "bar");
92                         Assert (changed);
93                         Assert (changing);
94                         AssertEquals ("foobarbaz", comment.Data);
95
96                         try 
97                         {
98                                 comment.Value = "foo";
99                                 comment.InsertData (-1, "bar");
100                                 Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
101                         } 
102                         catch (ArgumentOutOfRangeException) {}
103
104                         comment.Value = "foo";
105                         comment.InsertData (3, "bar");
106                         AssertEquals ("foobar", comment.Data);
107
108                         try 
109                         {
110                                 comment.Value = "foo";
111                                 comment.InsertData (4, "bar");
112                                 Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
113                         } 
114                         catch (ArgumentOutOfRangeException) {}
115
116                         try 
117                         {
118                                 comment.Value = "foo";
119                                 comment.InsertData (1, null);
120                                 Fail ("Expected an ArgumentNullException to be thrown.");
121                         } 
122                         catch (ArgumentNullException) {}
123                 }
124
125                 public void TestReplaceData ()
126                 {
127                         changed = false;
128                         changing = false;
129                         comment.ReplaceData (0, 3, "bar");
130                         Assert (changed);
131                         Assert (changing);
132                         AssertEquals ("bar", comment.Data);
133
134                         comment.Value = "foo";
135                         comment.ReplaceData (2, 3, "bar");
136                         AssertEquals ("fobar", comment.Data);
137
138                         comment.Value = "foo";
139                         comment.ReplaceData (3, 3, "bar");
140                         AssertEquals ("foobar", comment.Data);
141
142                         try 
143                         {
144                                 comment.Value = "foo";
145                                 comment.ReplaceData (4, 3, "bar");
146                                 Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
147                         } 
148                         catch (ArgumentOutOfRangeException) {}
149
150                         try 
151                         {
152                                 comment.Value = "foo";
153                                 comment.ReplaceData (-1, 3, "bar");
154                                 Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
155                         } 
156                         catch (ArgumentOutOfRangeException) {}
157
158                         comment.Value = "foo";
159                         comment.ReplaceData (0, 2, "bar");
160                         AssertEquals ("baro", comment.Data);
161
162                         comment.Value = "foo";
163                         comment.ReplaceData (0, 5, "bar");
164                         AssertEquals ("bar", comment.Data);
165
166                         try 
167                         {
168                                 comment.Value = "foo";
169                                 comment.ReplaceData (1, 1, null);
170                                 Fail ("Expected an ArgumentNullException to be thrown.");
171                         } 
172                         catch (ArgumentNullException) {}
173                 }
174         }
175 }