1 // StreamWriterTest.cs - NUnit Test Cases for the SystemIO.StreamWriter class
\r
3 // David Brandt (bucky@keystreams.com)
\r
5 // (C) Ximian, Inc. http://www.ximian.com
\r
8 using NUnit.Framework;
\r
13 namespace MonoTests.System.IO
\r
16 public class StreamWriterTest : TestCase
\r
18 public StreamWriterTest() :
\r
19 base ("MonoTests.System.StreamWriterTest testsuite") {}
\r
20 public StreamWriterTest(string name) : base(name) {}
\r
22 protected override void SetUp()
\r
26 protected override void TearDown()
\r
30 public static ITest Suite {
\r
32 return new TestSuite(typeof(StreamWriterTest));
\r
36 private string _codeFileName = "StreamWriterTest.temp";
\r
37 private string _thisCodeFileName = "System.IO/StreamReaderTest.cs";
\r
40 public void TestCtor1() {
\r
42 bool errorThrown = false;
\r
44 StreamWriter r = new StreamWriter((Stream)null);
\r
45 } catch (ArgumentNullException) {
\r
48 Assert("null string error not thrown", errorThrown);
\r
51 bool errorThrown = false;
\r
52 FileStream f = new FileStream(_thisCodeFileName,
\r
56 StreamWriter r = new StreamWriter(f);
\r
58 } catch (ArgumentException) {
\r
62 Assert("no read error not thrown", errorThrown);
\r
65 FileStream f = new FileStream(_codeFileName,
\r
68 StreamWriter r = new StreamWriter(f);
\r
69 AssertNotNull("no stream writer", r);
\r
74 public void TestCtor2() {
\r
76 bool errorThrown = false;
\r
78 StreamWriter r = new StreamWriter("");
\r
79 } catch (ArgumentException) {
\r
82 Assert("empty string error not thrown", errorThrown);
\r
85 bool errorThrown = false;
\r
87 StreamWriter r = new StreamWriter((string)null);
\r
88 } catch (ArgumentNullException) {
\r
91 Assert("null string error not thrown", errorThrown);
\r
94 bool errorThrown = false;
\r
96 StreamWriter r = new StreamWriter("nonexistentdir/file");
\r
97 } catch (DirectoryNotFoundException) {
\r
100 Assert("dirNotFound error not thrown", errorThrown);
\r
103 bool errorThrown = false;
\r
105 StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#");
\r
106 } catch (IOException) {
\r
107 errorThrown = true;
\r
108 } catch (ArgumentException) {
\r
109 // FIXME - the spec says 'IOExc', but the
\r
110 // compiler says 'ArgExc'...
\r
111 errorThrown = true;
\r
113 Assert("invalid filename error not thrown", errorThrown);
\r
115 // TODO - Security/Auth exceptions
\r
117 StreamWriter r = new StreamWriter(_codeFileName);
\r
118 AssertNotNull("no stream writer", r);
\r
122 public void TestCtor3() {
\r
124 bool errorThrown = false;
\r
126 StreamWriter r = new StreamWriter("", false);
\r
127 } catch (ArgumentException) {
\r
128 errorThrown = true;
\r
130 Assert("empty string error not thrown", errorThrown);
\r
133 bool errorThrown = false;
\r
135 StreamWriter r = new StreamWriter((string)null, false);
\r
136 } catch (ArgumentNullException) {
\r
137 errorThrown = true;
\r
139 Assert("null string error not thrown", errorThrown);
\r
142 bool errorThrown = false;
\r
144 StreamWriter r = new StreamWriter("nonexistentdir/file", false);
\r
145 } catch (DirectoryNotFoundException) {
\r
146 errorThrown = true;
\r
148 Assert("dirNotFound error not thrown", errorThrown);
\r
151 bool errorThrown = false;
\r
153 StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#", false);
\r
154 } catch (IOException) {
\r
155 errorThrown = true;
\r
156 } catch (ArgumentException) {
\r
157 // FIXME - the spec says 'IOExc', but the
\r
158 // compiler says 'ArgExc'...
\r
159 errorThrown = true;
\r
161 Assert("invalid filename error not thrown", errorThrown);
\r
164 StreamWriter r = new StreamWriter(_codeFileName, false);
\r
165 AssertNotNull("no stream writer", r);
\r
169 bool errorThrown = false;
\r
171 StreamWriter r = new StreamWriter("", true);
\r
172 } catch (ArgumentException) {
\r
173 errorThrown = true;
\r
175 Assert("empty string error not thrown", errorThrown);
\r
178 bool errorThrown = false;
\r
180 StreamWriter r = new StreamWriter((string)null, true);
\r
181 } catch (ArgumentNullException) {
\r
182 errorThrown = true;
\r
184 Assert("null string error not thrown", errorThrown);
\r
187 bool errorThrown = false;
\r
189 StreamWriter r = new StreamWriter("nonexistentdir/file", true);
\r
190 } catch (DirectoryNotFoundException) {
\r
191 errorThrown = true;
\r
193 Assert("dirNotFound error not thrown", errorThrown);
\r
196 bool errorThrown = false;
\r
198 StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#", true);
\r
199 } catch (IOException) {
\r
200 errorThrown = true;
\r
201 } catch (ArgumentException) {
\r
202 // FIXME - the spec says 'IOExc', but the
\r
203 // compiler says 'ArgExc'...
\r
204 errorThrown = true;
\r
206 Assert("invalid filename error not thrown", errorThrown);
\r
209 StreamWriter r = new StreamWriter(_codeFileName, true);
\r
210 AssertNotNull("no stream writer", r);
\r
215 // TODO - ctors with Encoding
\r
217 // TODO - AutoFlush
\r
218 public void TestAutoFlush() {
\r
220 MemoryStream m = new MemoryStream();
\r
221 StreamWriter w = new StreamWriter(m);
\r
222 w.AutoFlush = false;
\r
227 AssertEquals("Should be nothing before flush",
\r
230 AssertEquals("Should be something after flush",
\r
234 MemoryStream m = new MemoryStream();
\r
235 StreamWriter w = new StreamWriter(m);
\r
236 w.AutoFlush = true;
\r
241 AssertEquals("Should be something before flush",
\r
244 AssertEquals("Should be something after flush",
\r
249 public void TestBaseStream() {
\r
250 FileStream f = new FileStream(_codeFileName,
\r
253 StreamWriter r = new StreamWriter(f);
\r
254 AssertEquals("wrong base stream ", f, r.BaseStream);
\r
259 public void TestEncoding() {
\r
260 StreamWriter r = new StreamWriter(_codeFileName);
\r
261 AssertEquals("wrong encoding",
\r
262 Encoding.UTF8.GetType(), r.Encoding.GetType());
\r
266 // TODO - Close - not entirely sure how to test Close
\r
267 //public void TestClose() {
\r
269 //MemoryStream m = new MemoryStream();
\r
270 //StreamWriter w = new StreamWriter(m);
\r
271 //StreamReader r = new StreamReader(m);
\r
276 //AssertEquals("Should be nothing before close",
\r
278 //AssertEquals("Should be nothing in reader",
\r
281 //AssertEquals("Should be something after close",
\r
287 public void TestFlush() {
\r
289 bool errorThrown = false;
\r
291 FileStream f = new FileStream(_codeFileName,
\r
294 StreamWriter r = new StreamWriter(f);
\r
297 } catch (ObjectDisposedException) {
\r
298 errorThrown = true;
\r
300 Assert("can't flush closed error not thrown", errorThrown);
\r
303 MemoryStream m = new MemoryStream();
\r
304 StreamWriter w = new StreamWriter(m);
\r
309 AssertEquals("Should be nothing before flush",
\r
312 AssertEquals("Should be something after flush",
\r
317 // TODO - Write - test errors, functionality tested in TestFlush.
\r