xmltextwriter writestartdocument and writeendelement.
[mono.git] / mcs / class / System.XML / Test / XmlTextWriterTests.cs
index cb3068a3a55878a9aebf2b299e7127a446604bc7..1cd2aa83bfb20e2f4cd241baa6276cfb8a12eb51 100644 (file)
@@ -29,18 +29,119 @@ namespace Ximian.Mono.Tests
                        xtw = new XmlTextWriter (sw);
                }
 
-               public void TestCData ()
+               public void TestCDataValid ()
                {
                        xtw.WriteCData ("foo");
-                       AssertEquals ("WriteCData had incorrect output.", sw.GetStringBuilder().ToString(), "<![CDATA[foo]]>");
+                       AssertEquals ("WriteCData had incorrect output.", "<![CDATA[foo]]>", sw.GetStringBuilder().ToString());
                }
 
-               public void TestComment ()
+               public void TestCDataInvalid ()
+               {
+                       try {
+                               xtw.WriteCData("foo]]>bar");
+                               Fail("Should have thrown an ArgumentException.");
+                       } 
+                       catch (ArgumentException) { }
+               }
+
+               public void TestCloseOpenElements ()
+               {
+                       xtw.WriteStartElement("foo");
+                       xtw.WriteStartElement("bar");
+                       xtw.WriteStartElement("baz");
+                       xtw.Close();
+                       AssertEquals ("Close didn't write out end elements properly.", "<foo><bar><baz /></bar></foo>",
+                               sw.GetStringBuilder().ToString());
+               }
+
+               public void TestCloseWriteAfter ()
+               {
+                       xtw.WriteElementString ("foo", "bar");
+                       xtw.Close ();
+
+                       // WriteEndElement and WriteStartDocument aren't tested here because
+                       // they will always throw different exceptions besides 'The Writer is closed.'
+                       // and there are already tests for those exceptions.
+
+                       try {
+                               xtw.WriteCData ("foo");
+                               Fail ("WriteCData after Close Should have thrown an InvalidOperationException.");
+                       } 
+                       catch (InvalidOperationException e) {
+                               AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
+                       }
+
+                       try {
+                               xtw.WriteComment ("foo");
+                               Fail ("WriteComment after Close Should have thrown an InvalidOperationException.");
+                       } 
+                       catch (InvalidOperationException e) {
+                               AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
+                       }
+
+                       try {
+                               xtw.WriteProcessingInstruction ("foo", "bar");
+                               Fail ("WriteProcessingInstruction after Close Should have thrown an InvalidOperationException.");
+                       } 
+                       catch (InvalidOperationException e) {
+                               AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
+                       }
+
+                       try {
+                               xtw.WriteStartElement ("foo", "bar", "baz");
+                               Fail ("WriteStartElement after Close Should have thrown an InvalidOperationException.");
+                       } 
+                       catch (InvalidOperationException e) {
+                               AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
+                       }
+
+                       try {
+                               xtw.WriteString ("foo");
+                               Fail ("WriteString after Close Should have thrown an InvalidOperationException.");
+                       } 
+                       catch (InvalidOperationException e) {
+                               AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
+                       }
+               }
+
+               public void TestCommentValid ()
                {
                        xtw.WriteComment ("foo");
                        AssertEquals ("WriteComment had incorrect output.", "<!--foo-->", sw.GetStringBuilder().ToString());
                }
 
+               public void TestCommentInvalid ()
+               {
+                       try {
+                               xtw.WriteComment("foo-");
+                               Fail("Should have thrown an ArgumentException.");
+                       } 
+                       catch (ArgumentException) { }
+
+                       try {
+                               xtw.WriteComment("foo-->bar");
+                               Fail("Should have thrown an ArgumentException.");
+                       } 
+                       catch (ArgumentException) { }
+               }
+
+               public void TestDocumentStart ()
+               {
+                       xtw.WriteStartDocument ();
+                       AssertEquals ("XmlDeclaration is incorrect.", "<?xml version=\"1.0\" encoding=\"utf-16\"?>",
+                               sw.GetStringBuilder ().ToString ());
+
+                       try 
+                       {
+                               xtw.WriteStartDocument ();
+                               Fail("Should have thrown an InvalidOperationException.");
+                       } 
+                       catch (InvalidOperationException e) {
+                               AssertEquals ("Exception message is incorrect.",
+                                       "WriteStartDocument should be the first call.", e.Message);
+                       }
+               }
+
                public void TestElementEmpty ()
                {
                        xtw.WriteStartElement ("foo");
@@ -54,6 +155,12 @@ namespace Ximian.Mono.Tests
                        AssertEquals ("WriteElementString has incorrect output.", "<foo>bar</foo>", sw.GetStringBuilder().ToString());
                }
 
+               public void TestProcessingInstructionValid ()
+               {
+                       xtw.WriteProcessingInstruction("foo", "bar");
+                       AssertEquals ("WriteProcessingInstruction had incorrect output.", "<?foo bar?>", sw.GetStringBuilder().ToString());
+               }
+
                public void TestProcessingInstructionInvalid ()
                {
                        try {
@@ -76,5 +183,54 @@ namespace Ximian.Mono.Tests
                                Fail("Should have thrown an ArgumentException.");
                        } catch (ArgumentException) { }
                }
+
+               public void TestNamespacesNoNamespaceClearsDefaultNamespace ()
+               {
+                       xtw.WriteStartElement(String.Empty, "foo", "http://netsack.com/");
+                       xtw.WriteStartElement(String.Empty, "bar", String.Empty);
+                       xtw.WriteElementString("baz", String.Empty, String.Empty);
+                       xtw.WriteEndElement();
+                       xtw.WriteEndElement();
+                       AssertEquals ("XmlTextWriter is incorrectly outputting namespaces.",
+                               "<foo xmlns=\"http://netsack.com/\"><bar xmlns=\"\"><baz /></bar></foo>",
+                               sw.GetStringBuilder().ToString());
+               }
+
+               public void TestNamespacesPrefix ()
+               {
+                       xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
+                       xtw.WriteStartElement ("foo", "baz", "http://netsack.com/");
+                       xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
+                       xtw.WriteEndElement ();
+                       xtw.WriteEndElement ();
+                       AssertEquals ("XmlTextWriter is incorrectly outputting prefixes.",
+                               "<foo:bar xmlns:foo=\"http://netsack.com/\"><foo:baz><foo:qux /></foo:baz></foo:bar>",
+                               sw.GetStringBuilder ().ToString ());
+               }
+
+               public void TestNamespacesPrefixWithEmptyNamespace ()
+               {
+                       try {
+                               xtw.WriteStartElement ("foo", "bar", "");
+                               Fail ("Should have thrown an ArgumentException.");
+                       }
+                       catch (ArgumentException e) {
+                               AssertEquals ("Exception message is incorrect.",
+                                       "Cannot use a prefix with an empty namespace.", e.Message);
+                       }
+               }
+
+               public void TestWriteEndElement ()
+               {
+                       try 
+                       {
+                               xtw.WriteEndElement ();
+                               Fail ("Should have thrown an InvalidOperationException.");
+                       }
+                       catch (InvalidOperationException e) {
+                               AssertEquals ("Exception message is incorrect.",
+                                       "There was no XML start tag open.", e.Message);
+                       }
+               }
        }
 }