// // System.Xml.XmlTextWriterTests // // Author: // Kral Ferch // // (C) 2002 Kral Ferch // using System; using System.IO; using System.Xml; using NUnit.Framework; namespace Ximian.Mono.Tests { public class XmlTextWriterTests : TestCase { public XmlTextWriterTests () : base ("Ximian.Mono.Tests.XmlTextWriterTests testsuite") {} public XmlTextWriterTests (string name) : base (name) {} StringWriter sw; XmlTextWriter xtw; protected override void SetUp () { sw = new StringWriter (); xtw = new XmlTextWriter (sw); } public void TestCDataValid () { xtw.WriteCData ("foo"); AssertEquals ("WriteCData had incorrect output.", "", sw.GetStringBuilder().ToString()); } 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.", "", sw.GetStringBuilder().ToString()); } public void TestCloseWriteAfter () { xtw.WriteElementString("foo", "bar"); xtw.Close(); try { xtw.WriteCData ("foo"); Fail ("WriteCData after Close Should have thrown an InvalidOperationException."); } catch (InvalidOperationException e) { AssertEquals ("InvalidOperationException message incorrect.", "The Writer is closed.", e.Message); } try { xtw.WriteComment ("foo"); Fail ("WriteComment after Close Should have thrown an InvalidOperationException."); } catch (InvalidOperationException e) { AssertEquals ("InvalidOperationException message 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 ("InvalidOperationException message incorrect.", "The Writer is closed.", e.Message); } } public void TestCommentValid () { xtw.WriteComment ("foo"); AssertEquals ("WriteComment had incorrect output.", "", 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 TestElementEmpty () { xtw.WriteStartElement ("foo"); xtw.WriteEndElement (); AssertEquals ("Incorrect output.", "", sw.GetStringBuilder().ToString()); } public void TestElementWriteElementString () { xtw.WriteElementString ("foo", "bar"); AssertEquals ("WriteElementString has incorrect output.", "bar", sw.GetStringBuilder().ToString()); } public void TestProcessingInstructionValid () { xtw.WriteProcessingInstruction("foo", "bar"); AssertEquals ("WriteProcessingInstruction had incorrect output.", "", sw.GetStringBuilder().ToString()); } public void TestProcessingInstructionInvalid () { try { xtw.WriteProcessingInstruction("fo?>o", "bar"); Fail("Should have thrown an ArgumentException."); } catch (ArgumentException) { } try { xtw.WriteProcessingInstruction("foo", "ba?>r"); Fail("Should have thrown an ArgumentException."); } catch (ArgumentException) { } try { xtw.WriteProcessingInstruction("", "bar"); Fail("Should have thrown an ArgumentException."); } catch (ArgumentException) { } try { xtw.WriteProcessingInstruction(null, "bar"); 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.", "", 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.", "", sw.GetStringBuilder ().ToString ()); } public void TestNamespacesPrefixWithEmptyNamespace () { try { xtw.WriteStartElement ("foo", "bar", ""); Fail ("Should have thrown an ArgumentException."); } catch (ArgumentException e) { AssertEquals ("ArgumentException text is incorrect.", "Cannot use a prefix with an empty namespace.", e.Message); } } } }