2 // System.Xml.XmlTextWriterTests
5 // Kral Ferch <kral_ferch@hotmail.com>
14 using NUnit.Framework;
16 namespace Ximian.Mono.Tests
18 public class XmlTextWriterTests : TestCase
20 public XmlTextWriterTests () : base ("Ximian.Mono.Tests.XmlTextWriterTests testsuite") {}
21 public XmlTextWriterTests (string name) : base (name) {}
26 protected override void SetUp ()
28 sw = new StringWriter ();
29 xtw = new XmlTextWriter (sw);
32 public void TestCDataValid ()
34 xtw.WriteCData ("foo");
35 AssertEquals ("WriteCData had incorrect output.", "<![CDATA[foo]]>", sw.GetStringBuilder().ToString());
38 public void TestCDataInvalid ()
41 xtw.WriteCData("foo]]>bar");
42 Fail("Should have thrown an ArgumentException.");
44 catch (ArgumentException) { }
47 public void TestCloseOpenElements ()
49 xtw.WriteStartElement("foo");
50 xtw.WriteStartElement("bar");
51 xtw.WriteStartElement("baz");
53 AssertEquals ("Close didn't write out end elements properly.", "<foo><bar><baz /></bar></foo>",
54 sw.GetStringBuilder().ToString());
57 public void TestCloseWriteAfter ()
59 xtw.WriteElementString ("foo", "bar");
62 // WriteEndElement and WriteStartDocument aren't tested here because
63 // they will always throw different exceptions besides 'The Writer is closed.'
64 // and there are already tests for those exceptions.
67 xtw.WriteCData ("foo");
68 Fail ("WriteCData after Close Should have thrown an InvalidOperationException.");
70 catch (InvalidOperationException e) {
71 AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
75 xtw.WriteComment ("foo");
76 Fail ("WriteComment after Close Should have thrown an InvalidOperationException.");
78 catch (InvalidOperationException e) {
79 AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
83 xtw.WriteProcessingInstruction ("foo", "bar");
84 Fail ("WriteProcessingInstruction after Close Should have thrown an InvalidOperationException.");
86 catch (InvalidOperationException e) {
87 AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
91 xtw.WriteStartElement ("foo", "bar", "baz");
92 Fail ("WriteStartElement after Close Should have thrown an InvalidOperationException.");
94 catch (InvalidOperationException e) {
95 AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
99 xtw.WriteString ("foo");
100 Fail ("WriteString after Close Should have thrown an InvalidOperationException.");
102 catch (InvalidOperationException e) {
103 AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
107 public void TestCommentValid ()
109 xtw.WriteComment ("foo");
110 AssertEquals ("WriteComment had incorrect output.", "<!--foo-->", sw.GetStringBuilder().ToString());
113 public void TestCommentInvalid ()
116 xtw.WriteComment("foo-");
117 Fail("Should have thrown an ArgumentException.");
119 catch (ArgumentException) { }
122 xtw.WriteComment("foo-->bar");
123 Fail("Should have thrown an ArgumentException.");
125 catch (ArgumentException) { }
128 public void TestDocumentStart ()
130 xtw.WriteStartDocument ();
131 AssertEquals ("XmlDeclaration is incorrect.", "<?xml version=\"1.0\" encoding=\"utf-16\"?>",
132 sw.GetStringBuilder ().ToString ());
136 xtw.WriteStartDocument ();
137 Fail("Should have thrown an InvalidOperationException.");
139 catch (InvalidOperationException e) {
140 AssertEquals ("Exception message is incorrect.",
141 "WriteStartDocument should be the first call.", e.Message);
145 public void TestElementEmpty ()
147 xtw.WriteStartElement ("foo");
148 xtw.WriteEndElement ();
149 AssertEquals ("Incorrect output.", "<foo />", sw.GetStringBuilder().ToString());
152 public void TestElementWriteElementString ()
154 xtw.WriteElementString ("foo", "bar");
155 AssertEquals ("WriteElementString has incorrect output.", "<foo>bar</foo>", sw.GetStringBuilder().ToString());
158 public void TestProcessingInstructionValid ()
160 xtw.WriteProcessingInstruction("foo", "bar");
161 AssertEquals ("WriteProcessingInstruction had incorrect output.", "<?foo bar?>", sw.GetStringBuilder().ToString());
164 public void TestProcessingInstructionInvalid ()
167 xtw.WriteProcessingInstruction("fo?>o", "bar");
168 Fail("Should have thrown an ArgumentException.");
169 } catch (ArgumentException) { }
172 xtw.WriteProcessingInstruction("foo", "ba?>r");
173 Fail("Should have thrown an ArgumentException.");
174 } catch (ArgumentException) { }
177 xtw.WriteProcessingInstruction("", "bar");
178 Fail("Should have thrown an ArgumentException.");
179 } catch (ArgumentException) { }
182 xtw.WriteProcessingInstruction(null, "bar");
183 Fail("Should have thrown an ArgumentException.");
184 } catch (ArgumentException) { }
187 public void TestNamespacesNoNamespaceClearsDefaultNamespace ()
189 xtw.WriteStartElement(String.Empty, "foo", "http://netsack.com/");
190 xtw.WriteStartElement(String.Empty, "bar", String.Empty);
191 xtw.WriteElementString("baz", String.Empty, String.Empty);
192 xtw.WriteEndElement();
193 xtw.WriteEndElement();
194 AssertEquals ("XmlTextWriter is incorrectly outputting namespaces.",
195 "<foo xmlns=\"http://netsack.com/\"><bar xmlns=\"\"><baz /></bar></foo>",
196 sw.GetStringBuilder().ToString());
199 public void TestNamespacesPrefix ()
201 xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
202 xtw.WriteStartElement ("foo", "baz", "http://netsack.com/");
203 xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
204 xtw.WriteEndElement ();
205 xtw.WriteEndElement ();
206 AssertEquals ("XmlTextWriter is incorrectly outputting prefixes.",
207 "<foo:bar xmlns:foo=\"http://netsack.com/\"><foo:baz><foo:qux /></foo:baz></foo:bar>",
208 sw.GetStringBuilder ().ToString ());
211 public void TestNamespacesPrefixWithEmptyNamespace ()
214 xtw.WriteStartElement ("foo", "bar", "");
215 Fail ("Should have thrown an ArgumentException.");
217 catch (ArgumentException e) {
218 AssertEquals ("Exception message is incorrect.",
219 "Cannot use a prefix with an empty namespace.", e.Message);
223 public void TestWriteEndElement ()
227 xtw.WriteEndElement ();
228 Fail ("Should have thrown an InvalidOperationException.");
230 catch (InvalidOperationException e) {
231 AssertEquals ("Exception message is incorrect.",
232 "There was no XML start tag open.", e.Message);