// // System.Xml.XmlTextWriterTests // // Authors: // Kral Ferch // Martin Willemoes Hansen // // (C) 2002 Kral Ferch // (C) 2003 Martin Willemoes Hansen // using System; using System.IO; using System.Text; using System.Xml; using NUnit.Framework; namespace MonoTests.System.Xml { [TestFixture] public class XmlTextWriterTests : Assertion { StringWriter sw; XmlTextWriter xtw; [SetUp] public void GetReady () { sw = new StringWriter (); xtw = new XmlTextWriter (sw); xtw.QuoteChar = '\''; } private string StringWriterText { get { return sw.GetStringBuilder ().ToString (); } } [Test] public void AttributeNamespacesNonNamespaceAttributeBefore () { xtw.WriteStartElement ("foo"); xtw.WriteAttributeString("bar", "baz"); xtw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def"); AssertEquals ("", StringWriterText); } [Test] [ExpectedException (typeof (ArgumentException))] public void CDataInvalid () { xtw.WriteCData("foo]]>bar"); } [Test] public void CloseOpenElements () { xtw.WriteStartElement("foo"); xtw.WriteStartElement("bar"); xtw.WriteStartElement("baz"); xtw.Close(); AssertEquals ("Close didn't write out end elements properly.", "", StringWriterText); } [Test] public void CloseWriteAfter () { 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) { // Don't rely on English message assertion. // It is enough to check an exception occurs. // 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) { // 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) { // 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) { // AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message); } try { xtw.WriteAttributeString ("foo", "bar"); Fail ("WriteAttributeString after Close Should have thrown an InvalidOperationException."); } catch (InvalidOperationException) { // 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) { // AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message); } } [Test] public void CommentValid () { xtw.WriteComment ("foo"); AssertEquals ("WriteComment had incorrect output.", "", StringWriterText); } [Test] public void CommentInvalid () { 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) { } } [Test] public void ConstructorsAndBaseStream () { Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (null, this.xtw.BaseStream)); MemoryStream ms; StreamReader sr; XmlTextWriter xtw; ms = new MemoryStream (); xtw = new XmlTextWriter (ms, new UnicodeEncoding ()); xtw.WriteStartDocument (); xtw.Flush (); ms.Seek (0, SeekOrigin.Begin); sr = new StreamReader (ms, Encoding.Unicode); string expectedXmlDeclaration = ""; string actualXmlDeclaration = sr.ReadToEnd(); AssertEquals (expectedXmlDeclaration, actualXmlDeclaration); Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (ms, xtw.BaseStream)); ms = new MemoryStream (); xtw = new XmlTextWriter (ms, new UnicodeEncoding ()); xtw.WriteStartDocument (true); xtw.Flush (); ms.Seek (0, SeekOrigin.Begin); sr = new StreamReader (ms, Encoding.Unicode); AssertEquals ("", sr.ReadToEnd ()); ms = new MemoryStream (); xtw = new XmlTextWriter (ms, new UTF8Encoding ()); xtw.WriteStartDocument (); xtw.Flush (); ms.Seek (0, SeekOrigin.Begin); sr = new StreamReader (ms, Encoding.UTF8); AssertEquals ("", sr.ReadToEnd ()); ms = new MemoryStream (); xtw = new XmlTextWriter (ms, null); xtw.WriteStartDocument (); xtw.Flush (); ms.Seek (0, SeekOrigin.Begin); sr = new StreamReader (ms, Encoding.UTF8); AssertEquals ("", sr.ReadToEnd ()); ms = new MemoryStream (); xtw = new XmlTextWriter (ms, null); xtw.WriteStartDocument (true); xtw.Flush (); ms.Seek (0, SeekOrigin.Begin); sr = new StreamReader (ms, Encoding.UTF8); AssertEquals ("", sr.ReadToEnd ()); Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (ms, xtw.BaseStream)); } [Test] public void DocumentStart () { xtw.WriteStartDocument (); AssertEquals ("XmlDeclaration is incorrect.", "", StringWriterText); try { xtw.WriteStartDocument (); Fail("Should have thrown an InvalidOperationException."); } catch (InvalidOperationException) { // Don't rely on English message assertion. // It is enough to check an exception occurs. // AssertEquals ("Exception message is incorrect.", // "WriteStartDocument should be the first call.", e.Message); } xtw = new XmlTextWriter (sw = new StringWriter ()); xtw.QuoteChar = '\''; xtw.WriteStartDocument (true); AssertEquals ("", StringWriterText); xtw = new XmlTextWriter (sw = new StringWriter ()); xtw.QuoteChar = '\''; xtw.WriteStartDocument (false); AssertEquals ("", StringWriterText); } [Test] public void ElementAndAttributeSameXmlns () { xtw.WriteStartElement ("ped", "foo", "urn:foo"); xtw.WriteStartAttribute ("ped", "foo", "urn:foo"); xtw.WriteEndElement (); AssertEquals ("", StringWriterText); } [Test] public void ElementXmlnsNeedEscape () { xtw.WriteStartElement ("test", "foo", "'"); xtw.WriteEndElement (); // MS.NET fails this case. AssertEquals ("", StringWriterText); } [Test] public void ElementEmpty () { xtw.WriteStartElement ("foo"); xtw.WriteEndElement (); AssertEquals ("Incorrect output.", "", StringWriterText); } [Test] public void ElementWriteElementString () { xtw.WriteElementString ("foo", "bar"); AssertEquals ("WriteElementString has incorrect output.", "bar", StringWriterText); xtw.WriteElementString ("baz", ""); AssertEquals ("bar", StringWriterText); xtw.WriteElementString ("quux", null); AssertEquals ("bar", StringWriterText); xtw.WriteElementString ("", "quuux"); AssertEquals ("bar<>quuux", StringWriterText); xtw.WriteElementString (null, "quuuux"); AssertEquals ("bar<>quuux<>quuuux", StringWriterText); } [Test] public void FormattingTest () { xtw.Formatting = Formatting.Indented; xtw.WriteStartDocument (); xtw.WriteStartElement ("foo"); xtw.WriteElementString ("bar", ""); xtw.Close (); AssertEquals (String.Format ("{0}{0} {0}", Environment.NewLine), StringWriterText); } [Test] public void FormattingInvalidXmlForFun () { xtw.Formatting = Formatting.Indented; xtw.IndentChar = 'x'; xtw.WriteStartDocument (); xtw.WriteStartElement ("foo"); xtw.WriteStartElement ("bar"); xtw.WriteElementString ("baz", ""); xtw.Close (); AssertEquals (String.Format ("{0}{0}xx{0}xxxx{0}xx{0}", Environment.NewLine), StringWriterText); } [Test] public void FormattingFromRemarks () { // Remarks section of on-line help for XmlTextWriter.Formatting suggests this test. xtw.Formatting = Formatting.Indented; xtw.WriteStartElement ("ol"); xtw.WriteStartElement ("li"); xtw.WriteString ("The big "); // This means "li" now has a mixed content model. xtw.WriteElementString ("b", "E"); xtw.WriteElementString ("i", "lephant"); xtw.WriteString (" walks slowly."); xtw.WriteEndElement (); xtw.WriteEndElement (); AssertEquals (String.Format ("
    {0}
  1. The big Elephant walks slowly.
  2. {0}
", Environment.NewLine), StringWriterText); } [Test] public void LookupPrefix () { xtw.WriteStartElement ("root"); xtw.WriteStartElement ("one"); xtw.WriteAttributeString ("xmlns", "foo", null, "http://abc.def"); xtw.WriteAttributeString ("xmlns", "bar", null, "http://ghi.jkl"); AssertEquals ("foo", xtw.LookupPrefix ("http://abc.def")); AssertEquals ("bar", xtw.LookupPrefix ("http://ghi.jkl")); xtw.WriteEndElement (); xtw.WriteStartElement ("two"); xtw.WriteAttributeString ("xmlns", "baz", null, "http://mno.pqr"); xtw.WriteString("quux"); AssertEquals ("baz", xtw.LookupPrefix ("http://mno.pqr")); AssertNull (xtw.LookupPrefix ("http://abc.def")); AssertNull (xtw.LookupPrefix ("http://ghi.jkl")); AssertNull (xtw.LookupPrefix ("http://bogus")); } [Test] public void NamespacesAttributesPassingInNamespaces () { xtw.Namespaces = false; xtw.WriteStartElement ("foo"); // These shouldn't throw any exceptions since they don't pass in // a namespace. xtw.WriteAttributeString ("bar", "baz"); xtw.WriteAttributeString ("", "a", "", "b"); xtw.WriteAttributeString (null, "c", "", "d"); xtw.WriteAttributeString ("", "e", null, "f"); xtw.WriteAttributeString (null, "g", null, "h"); AssertEquals ("bar
", StringWriterText); } [Test] public void NamespacesPrefix () { 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.", "", StringWriterText); } [Test] [ExpectedException (typeof (ArgumentException))] public void NamespacesPrefixWithEmptyAndNullNamespaceEmpty () { xtw.WriteStartElement ("foo", "bar", ""); } [Test] [ExpectedException (typeof (ArgumentException))] public void NamespacesPrefixWithEmptyAndNullNamespaceNull () { xtw.WriteStartElement ("foo", "bar", null); } [Test] public void NamespacesSettingWhenWriteStateNotStart () { xtw.WriteStartElement ("foo"); try { xtw.Namespaces = false; Fail ("Expected an InvalidOperationException."); } catch (InvalidOperationException) {} AssertEquals (true, xtw.Namespaces); } [Test] public void ProcessingInstructionValid () { xtw.WriteProcessingInstruction("foo", "bar"); AssertEquals ("WriteProcessingInstruction had incorrect output.", "", StringWriterText); } [Test] public void ProcessingInstructionInvalid () { 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) { } } [Test] public void QuoteCharDoubleQuote () { xtw.QuoteChar = '"'; // version, encoding, standalone xtw.WriteStartDocument (true); // namespace declaration xtw.WriteElementString ("foo", "http://netsack.com", "bar"); AssertEquals ("bar", StringWriterText); } [Test] [ExpectedException (typeof (ArgumentException))] public void QuoteCharInvalid () { xtw.QuoteChar = 'x'; } [Test] public void WriteBase64 () { UTF8Encoding encoding = new UTF8Encoding(); byte[] fooBar = encoding.GetBytes("foobar"); xtw.WriteBase64 (fooBar, 0, 6); AssertEquals("Zm9vYmFy", StringWriterText); try { xtw.WriteBase64 (fooBar, 3, 6); Fail ("Expected an Argument Exception to be thrown."); } catch (ArgumentException) {} try { xtw.WriteBase64 (fooBar, -1, 6); Fail ("Expected an Argument Exception to be thrown."); } catch (ArgumentOutOfRangeException) {} try { xtw.WriteBase64 (fooBar, 3, -1); Fail ("Expected an Argument Exception to be thrown."); } catch (ArgumentOutOfRangeException) {} try { xtw.WriteBase64 (null, 0, 6); Fail ("Expected an Argument Exception to be thrown."); } catch (ArgumentNullException) {} } [Test] public void WriteBinHex () { byte [] bytes = new byte [] {4,14,34, 54,94,114, 134,194,255, 0,5}; xtw.WriteBinHex (bytes, 0, 11); AssertEquals ("040E22365E7286C2FF0005", StringWriterText); } [Test] public void WriteCharEntity () { xtw.WriteCharEntity ('a'); AssertEquals ("a", StringWriterText); xtw.WriteCharEntity ('A'); AssertEquals ("aA", StringWriterText); xtw.WriteCharEntity ('1'); AssertEquals ("aA1", StringWriterText); xtw.WriteCharEntity ('K'); AssertEquals ("aA1K", StringWriterText); try { xtw.WriteCharEntity ((char)0xd800); } catch (ArgumentException) {} } [Test] [ExpectedException (typeof (InvalidOperationException))] public void WriteEndAttribute () { xtw.WriteEndAttribute (); } [Test] public void WriteEndDocument () { try { xtw.WriteEndDocument (); Fail ("Expected an ArgumentException."); } catch (ArgumentException) {} xtw.WriteStartDocument (); try { xtw.WriteEndDocument (); Fail ("Expected an ArgumentException."); } catch (ArgumentException) {} xtw.WriteStartElement ("foo"); xtw.WriteStartAttribute ("bar", null); AssertEquals ("", StringWriterText); AssertEquals (WriteState.Start, xtw.WriteState); } [Test] public void WriteEndElement () { try { xtw.WriteEndElement (); Fail ("Should have thrown an InvalidOperationException."); } catch (InvalidOperationException) { // Don't rely on English message assertion. // It is enough to check an exception occurs. // AssertEquals ("Exception message is incorrect.", "There was no XML start tag open.", e.Message); } xtw.WriteStartElement ("foo"); xtw.WriteEndElement (); AssertEquals ("", StringWriterText); xtw.WriteStartElement ("bar"); xtw.WriteStartAttribute ("baz", null); xtw.WriteEndElement (); AssertEquals ("", StringWriterText); } [Test] public void FullEndElement () { xtw.WriteStartElement ("foo"); xtw.WriteFullEndElement (); AssertEquals ("", StringWriterText); xtw.WriteStartElement ("bar"); xtw.WriteAttributeString ("foo", "bar"); xtw.WriteFullEndElement (); AssertEquals ("", StringWriterText); xtw.WriteStartElement ("baz"); xtw.WriteStartAttribute ("bar", null); xtw.WriteFullEndElement (); AssertEquals ("", StringWriterText); } [Test] public void WriteQualifiedName () { xtw.WriteStartElement (null, "test", null); xtw.WriteAttributeString ("xmlns", "me", null, "http://localhost/"); xtw.WriteQualifiedName ("bob", "http://localhost/"); xtw.WriteEndElement (); AssertEquals ("me:bob", StringWriterText); } [Test] public void WriteRaw () { xtw.WriteRaw("&<>\"'"); AssertEquals ("&<>\"'", StringWriterText); xtw.WriteRaw(null); AssertEquals ("&<>\"'", StringWriterText); xtw.WriteRaw(""); AssertEquals ("&<>\"'", StringWriterText); } [Test] public void WriteRawInvalidInAttribute () { xtw.WriteStartElement ("foo"); xtw.WriteStartAttribute ("bar", null); xtw.WriteRaw ("&<>\"'"); xtw.WriteEndAttribute (); xtw.WriteEndElement (); AssertEquals ("", StringWriterText); } [Test] public void WriteStateTest () { AssertEquals (WriteState.Start, xtw.WriteState); xtw.WriteStartDocument (); AssertEquals (WriteState.Prolog, xtw.WriteState); xtw.WriteStartElement ("root"); AssertEquals (WriteState.Element, xtw.WriteState); xtw.WriteElementString ("foo", "bar"); AssertEquals (WriteState.Content, xtw.WriteState); xtw.Close (); AssertEquals (WriteState.Closed, xtw.WriteState); } [Test] public void WriteString () { xtw.WriteStartDocument (); try { xtw.WriteString("foo"); } catch (InvalidOperationException) {} // Testing attribute values xtw.WriteStartElement ("foo"); xtw.WriteAttributeString ("bar", "&<>"); AssertEquals ("\"'"); AssertEquals ("&<>\"'", StringWriterText); } [Test] public void XmlLang () { AssertNull (xtw.XmlLang); xtw.WriteStartElement ("foo"); xtw.WriteAttributeString ("xml", "lang", null, "langfoo"); AssertEquals ("langfoo", xtw.XmlLang); AssertEquals ("baz", StringWriterText); xtw.WriteString("baz"); AssertEquals ("langfoo", xtw.XmlLang); AssertEquals ("bazbaz", StringWriterText); xtw.WriteStartElement ("quux"); xtw.WriteStartAttribute ("xml", "lang", null); AssertEquals ("langfoo", xtw.XmlLang); AssertEquals ("bazbazbazbazbazbazbazbazbazbazsquonk", StringWriterText); xtw.WriteEndElement (); xtw.WriteEndElement (); AssertEquals ("langfoo", xtw.XmlLang); AssertEquals ("bazbazsquonk", StringWriterText); xtw.WriteEndElement (); AssertNull (xtw.XmlLang); AssertEquals ("bazbazsquonk", StringWriterText); xtw.Close (); AssertNull (xtw.XmlLang); } // TODO: test operational aspects [Test] public void XmlSpaceTest () { xtw.WriteStartElement ("foo"); AssertEquals (XmlSpace.None, xtw.XmlSpace); xtw.WriteStartElement ("bar"); xtw.WriteAttributeString ("xml", "space", null, "preserve"); AssertEquals (XmlSpace.Preserve, xtw.XmlSpace); AssertEquals ("", XmlNodeType.Document, ctx); xtr.Read(); // read XMLDecl wr.WriteAttributes(xtr, false); // This method don't always have to take this double-quoted style... AssertEquals("#WriteAttributes.XmlDecl.1", "version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"", sw.ToString().Trim()); sb.Remove(0, sb.Length); // init ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default); xtr = new XmlTextReader("", XmlNodeType.Document, ctx); xtr.Read(); // read XMLDecl AssertEquals (XmlNodeType.XmlDeclaration, xtr.NodeType); sw = new StringWriter (); wr = new XmlTextWriter (sw); // This block raises an error on MS.NET 1.0. wr.WriteAttributes(xtr, false); // This method don't always have to take this double-quoted style... AssertEquals("#WriteAttributes.XmlDecl.2", "version=\"1.0\" standalone=\"no\"", sw.ToString().Trim()); sw = new StringWriter (); wr = new XmlTextWriter (sw); sb.Remove(0, sb.Length); // init xtr.Read(); // read root AssertEquals (XmlNodeType.Element, xtr.NodeType); wr.WriteStartElement(xtr.LocalName, xtr.NamespaceURI); wr.WriteAttributes(xtr, false); wr.WriteEndElement(); wr.Close(); // This method don't always have to take this double-quoted style... AssertEquals("#WriteAttributes.Element", "", sw.ToString().Trim()); xtr.Close (); } [Test] public void WriteWhitespace () { xtw.WriteStartElement ("a"); xtw.WriteWhitespace ("\n\t"); xtw.WriteStartElement ("b"); xtw.WriteWhitespace ("\n\t"); xtw.WriteEndElement (); xtw.WriteWhitespace ("\n"); xtw.WriteEndElement (); xtw.WriteWhitespace ("\n"); xtw.Flush (); AssertEquals ("\n\t\n\t\n\n", StringWriterText); } [Test] public void FlushDoesntCloseTag () { xtw.WriteStartElement ("foo"); xtw.WriteAttributeString ("bar", "baz"); xtw.Flush (); AssertEquals (" ", StringWriterText); } [Test] public void DontOutputMultipleXmlns () { XmlDocument doc = new XmlDocument(); doc.LoadXml(""); XmlDocument doc2 = new XmlDocument(); doc2.LoadXml(doc.InnerXml); AssertEquals ("", doc2.OuterXml); } [Test] public void DontOutputNonDeclaredXmlns () { string xml = ""; XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); XmlDocument doc2 = new XmlDocument(); doc2.LoadXml(doc.InnerXml); AssertEquals (xml.Replace ('\'', '"'), doc2.OuterXml); } [Test] public void DontOutputRemovalDefaultNSDeclaration () { xtw.WriteStartDocument (); xtw.WriteStartElement ("foo"); xtw.WriteAttributeString ("xmlns", "probe"); AssertEquals (String.Empty, xtw.LookupPrefix ("probe")); xtw.WriteStartElement ("b"); AssertEquals (String.Empty, xtw.LookupPrefix ("probe")); xtw.WriteStartElement (null, "b2", null); // *Don't* output xmlns="" xtw.WriteEndElement (); // b2 xtw.WriteStartElement (null, "b2", ""); // *Do* output xmlns="" xtw.WriteEndElement (); // b2 xtw.WriteEndElement (); // b xtw.WriteEndElement (); // foo xtw.WriteEndDocument (); xtw.Close (); AssertEquals ("", StringWriterText); } [Test] public void DontOutputRemovalDefaultNSDeclaration2 () { xtw.WriteStartDocument (); // IMPORTANT DIFFERENCE!! ns = "", not null xtw.WriteStartElement ("foo", ""); xtw.WriteAttributeString ("xmlns", "probe"); AssertNull (xtw.LookupPrefix ("probe")); xtw.WriteStartElement ("b"); AssertNull (xtw.LookupPrefix ("probe")); xtw.WriteStartElement (null, "b2", null); // *Don't* output xmlns="" xtw.WriteEndElement (); // b2 xtw.WriteStartElement (null, "b2", ""); // *Do* output xmlns="" xtw.WriteEndElement (); // b2 xtw.WriteEndElement (); // b xtw.WriteEndElement (); // foo xtw.WriteEndDocument (); xtw.Close (); AssertEquals ("", StringWriterText); } [Test] public void DoOutputRemovalDefaultNSDeclaration () { xtw.WriteStartElement ("docelem", "a-namespace"); XmlDocument doc = new XmlDocument (); doc.CreateElement ("hola").WriteTo (xtw); // This means, WriteTo never passes null NamespaceURI argument to XmlWriter. xtw.WriteEndElement (); xtw.Close (); AssertEquals ("", StringWriterText); } [Test] public void WriteAttributeTakePrecedenceOnXmlns () { xtw.WriteStartElement ("root", "urn:foo"); xtw.WriteAttributeString ("xmlns", "urn:bar"); xtw.WriteEndElement (); xtw.Close (); AssertEquals ("", StringWriterText); } [Test] [ExpectedException (typeof (ArgumentException))] public void LookupPrefixNull () { xtw.LookupPrefix (null); } [Test] [ExpectedException (typeof (ArgumentException))] public void LookupPrefixEmpty () { xtw.LookupPrefix (String.Empty); } [Test] public void LookupPrefixIgnoresXmlnsAttribute () { AssertNull (xtw.LookupPrefix ("urn:foo")); xtw.WriteStartElement ("root"); AssertNull (xtw.LookupPrefix ("urn:foo")); xtw.WriteAttributeString ("xmlns", "urn:foo"); // Surprisingly to say, it is ignored!! AssertEquals (String.Empty, xtw.LookupPrefix ("urn:foo")); xtw.WriteStartElement ("hoge"); // (still after flushing previous start element.) AssertEquals (String.Empty, xtw.LookupPrefix ("urn:foo")); xtw.WriteStartElement ("fuga", "urn:foo"); // Is this testing on the correct way? Yes, here it is. AssertEquals (String.Empty, xtw.LookupPrefix ("urn:foo")); } [Test] public void WriteInvalidNames () { xtw.WriteStartElement ("foo<>"); xtw.WriteAttributeString ("ho<>ge", "value"); } [Test] [ExpectedException (typeof (ArgumentException))] public void AttributeWriteStartAttributePrefixWithoutNS () { xtw.WriteStartAttribute ("some", "foo", null); } [Test] public void AttributeWriteStartAttributeXmlnsNullNS () { xtw.WriteStartAttribute ("xmlns", "foo", null); } [Test] [ExpectedException (typeof (ArgumentException))] public void AttributeWriteEndAttributeXmlnsNullNs () { // Compare with the test AttributeWriteStartAttributeXmlnsNullNS(). xtw.WriteStartAttribute ("xmlns", "foo", null); xtw.WriteEndAttribute (); } [Test] [ExpectedException (typeof (ArgumentException))] public void AttributeWriteStartAttributePrefixXmlnsNonW3CNS () { xtw.WriteStartAttribute ("xmlns", "foo", "urn:foo"); } [Test] [ExpectedException (typeof (ArgumentException))] public void AttributeWriteStartAttributeLocalXmlnsNonW3CNS () { xtw.WriteStartAttribute ("", "xmlns", "urn:foo"); } } }