2 // System.Xml.XmlWriterSettingsTests.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
15 using System.Xml.Schema;
16 using NUnit.Framework;
18 namespace MonoTests.System.Xml
21 public class XmlWriterSettingsTests : Assertion
24 public void DefaultValue ()
26 XmlWriterSettings s = new XmlWriterSettings ();
32 private void DefaultValue (XmlWriterSettings s)
34 AssertEquals (true, s.CheckCharacters);
35 AssertEquals (false, s.CloseOutput);
36 AssertEquals (ConformanceLevel.Document, s.ConformanceLevel);
37 AssertEquals (Encoding.UTF8, s.Encoding);
38 AssertEquals (false, s.Indent);
39 AssertEquals (" ", s.IndentChars);
40 AssertEquals (Environment.NewLine, s.NewLineChars);
41 AssertEquals (false, s.NewLineOnAttributes);
42 AssertEquals (true, s.NormalizeNewLines);
43 AssertEquals (false, s.OmitXmlDeclaration);
47 public void EncodingTest ()
49 // For Stream it makes sense
50 XmlWriterSettings s = new XmlWriterSettings ();
51 s.Encoding = Encoding.GetEncoding ("shift_jis");
52 MemoryStream ms = new MemoryStream ();
53 XmlWriter w = XmlWriter.Create (ms, s);
54 w.WriteStartElement ("root");
57 byte [] data = ms.ToArray ();
58 Assert (data.Length != 0);
59 string str = s.Encoding.GetString (data);
60 AssertEquals ("<?xml version=\"1.0\" encoding=\"shift_jis\"?><root />", str);
62 // For TextWriter it does not make sense
63 StringWriter sw = new StringWriter ();
64 w = XmlWriter.Create (sw, s);
65 w.WriteStartElement ("root");
68 AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\"?><root />", sw.ToString ());
72 [ExpectedException (typeof (ArgumentException))]
73 public void CheckCharactersTest ()
75 XmlWriterSettings s = new XmlWriterSettings ();
76 StringWriter sw = new StringWriter ();
77 XmlWriter w = XmlWriter.Create (sw, s);
78 w.WriteStartElement ("root");
79 w.WriteString ("\0"); // invalid
85 public void CheckCharactersFalseTest ()
87 XmlWriterSettings s = new XmlWriterSettings ();
88 s.CheckCharacters = false;
89 StringWriter sw = new StringWriter ();
90 XmlWriter w = XmlWriter.Create (sw, s);
91 w.WriteStartElement ("root");
92 w.WriteString ("\0"); // invalid
97 [ExpectedException (typeof (ObjectDisposedException))]
98 public void CloseOutputTest ()
100 XmlWriterSettings s = new XmlWriterSettings ();
101 s.CloseOutput = true;
102 StringWriter sw = new StringWriter ();
103 XmlWriter w = XmlWriter.Create (sw, s);
104 w.WriteStartElement ("root");
105 w.WriteEndElement ();
107 sw.Write ("more"); // not allowed
111 [Ignore ("Write Test!")]
112 public void ConformanceLevelTest ()
114 throw new NotImplementedException ();
118 public void IndentationAndFormatting ()
120 // Test for Indent, IndentChars, NewLineOnAttributes,
121 // NewLineChars and OmitXmlDeclaration.
122 string output = "<root\n attr=\"value\"\n attr2=\"value\">\n <child>test</child>\n</root>";
123 XmlWriterSettings s = new XmlWriterSettings ();
124 s.OmitXmlDeclaration = true;
127 s.NewLineChars = "\n";
128 s.NewLineOnAttributes = true;
129 StringWriter sw = new StringWriter ();
130 XmlWriter w = XmlWriter.Create (sw, s);
131 w.WriteStartElement ("root");
132 w.WriteAttributeString ("attr", "value");
133 w.WriteAttributeString ("attr2", "value");
134 w.WriteStartElement ("child");
135 w.WriteString ("test");
136 w.WriteEndElement ();
137 w.WriteEndElement ();
139 AssertEquals (output, sw.ToString ());
143 [Ignore ("Write Test!")]
144 public void NormalizeNewLinesTest ()
146 throw new NotImplementedException ();