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");
63 xtw.WriteCData ("foo");
64 Fail ("WriteCData after Close Should have thrown an InvalidOperationException.");
66 catch (InvalidOperationException e) {
67 AssertEquals ("InvalidOperationException message incorrect.", "The Writer is closed.", e.Message);
71 xtw.WriteComment ("foo");
72 Fail ("WriteComment after Close Should have thrown an InvalidOperationException.");
74 catch (InvalidOperationException e) {
75 AssertEquals ("InvalidOperationException message incorrect.", "The Writer is closed.", e.Message);
79 xtw.WriteProcessingInstruction ("foo", "bar");
80 Fail ("WriteProcessingInstruction after Close Should have thrown an InvalidOperationException.");
82 catch (InvalidOperationException e) {
83 AssertEquals ("InvalidOperationException message incorrect.", "The Writer is closed.", e.Message);
87 public void TestCommentValid ()
89 xtw.WriteComment ("foo");
90 AssertEquals ("WriteComment had incorrect output.", "<!--foo-->", sw.GetStringBuilder().ToString());
93 public void TestCommentInvalid ()
96 xtw.WriteComment("foo-");
97 Fail("Should have thrown an ArgumentException.");
99 catch (ArgumentException) { }
102 xtw.WriteComment("foo-->bar");
103 Fail("Should have thrown an ArgumentException.");
105 catch (ArgumentException) { }
108 public void TestElementEmpty ()
110 xtw.WriteStartElement ("foo");
111 xtw.WriteEndElement ();
112 AssertEquals ("Incorrect output.", "<foo />", sw.GetStringBuilder().ToString());
115 public void TestElementWriteElementString ()
117 xtw.WriteElementString ("foo", "bar");
118 AssertEquals ("WriteElementString has incorrect output.", "<foo>bar</foo>", sw.GetStringBuilder().ToString());
121 public void TestProcessingInstructionValid ()
123 xtw.WriteProcessingInstruction("foo", "bar");
124 AssertEquals ("WriteProcessingInstruction had incorrect output.", "<?foo bar?>", sw.GetStringBuilder().ToString());
127 public void TestProcessingInstructionInvalid ()
130 xtw.WriteProcessingInstruction("fo?>o", "bar");
131 Fail("Should have thrown an ArgumentException.");
132 } catch (ArgumentException) { }
135 xtw.WriteProcessingInstruction("foo", "ba?>r");
136 Fail("Should have thrown an ArgumentException.");
137 } catch (ArgumentException) { }
140 xtw.WriteProcessingInstruction("", "bar");
141 Fail("Should have thrown an ArgumentException.");
142 } catch (ArgumentException) { }
145 xtw.WriteProcessingInstruction(null, "bar");
146 Fail("Should have thrown an ArgumentException.");
147 } catch (ArgumentException) { }
150 public void TestNamespacesNoNamespaceClearsDefaultNamespace ()
152 xtw.WriteStartElement(String.Empty, "foo", "http://netsack.com/");
153 xtw.WriteStartElement(String.Empty, "bar", String.Empty);
154 xtw.WriteElementString("baz", String.Empty, String.Empty);
155 xtw.WriteEndElement();
156 xtw.WriteEndElement();
157 AssertEquals ("XmlTextWriter is incorrectly outputting namespaces.",
158 "<foo xmlns=\"http://netsack.com/\"><bar xmlns=\"\"><baz /></bar></foo>",
159 sw.GetStringBuilder().ToString());
162 public void TestNamespacesPrefix ()
164 xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
165 xtw.WriteStartElement ("foo", "baz", "http://netsack.com/");
166 xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
167 xtw.WriteEndElement ();
168 xtw.WriteEndElement ();
169 AssertEquals ("XmlTextWriter is incorrectly outputting prefixes.",
170 "<foo:bar xmlns:foo=\"http://netsack.com/\"><foo:baz><foo:qux /></foo:baz></foo:bar>",
171 sw.GetStringBuilder ().ToString ());
174 public void TestNamespacesPrefixWithEmptyNamespace ()
177 xtw.WriteStartElement ("foo", "bar", "");
178 Fail ("Should have thrown an ArgumentException.");
180 catch (ArgumentException e) {
181 AssertEquals ("ArgumentException text is incorrect.",
182 "Cannot use a prefix with an empty namespace.", e.Message);