1 // StreamWriterTest.cs - NUnit Test Cases for the SystemIO.StreamWriter class
3 // David Brandt (bucky@keystreams.com)
5 // (C) Ximian, Inc. http://www.ximian.com
13 namespace MonoTests.System.IO
17 public class StreamWriterTest
19 static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
20 private string _codeFileName = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
21 private string _thisCodeFileName = TempFolder + Path.DirectorySeparatorChar + "StreamWriterTest.temp";
26 if (Directory.Exists (TempFolder))
27 Directory.Delete (TempFolder, true);
28 Directory.CreateDirectory (TempFolder);
30 if (!File.Exists (_thisCodeFileName))
31 File.Create (_thisCodeFileName).Close ();
35 public void TearDown ()
37 if (Directory.Exists (TempFolder))
38 Directory.Delete (TempFolder, true);
41 [Test] // .ctor (Stream)
42 public void Constructor1 ()
44 FileStream f = new FileStream(_codeFileName,
47 StreamWriter r = new StreamWriter (f);
48 Assert.IsFalse (r.AutoFlush, "#1");
49 Assert.AreSame (f, r.BaseStream, "#2");
50 Assert.IsNotNull (r.Encoding, "#3");
51 Assert.AreEqual (typeof (UTF8Encoding), r.Encoding.GetType (), "#4");
56 [Test] // .ctor (Stream)
57 public void Constructor1_Stream_NotWritable ()
59 FileStream f = new FileStream (_thisCodeFileName, FileMode.Open,
64 } catch (ArgumentException ex) {
65 // Stream was not writable
66 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
67 Assert.IsNull (ex.InnerException, "#3");
68 Assert.IsNotNull (ex.Message, "#4");
69 Assert.IsNull (ex.ParamName, "#5");
75 [Test] // .ctor (Stream)
76 public void Constructor1_Stream_Null ()
79 new StreamWriter((Stream) null);
81 } catch (ArgumentNullException ex) {
82 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
83 Assert.IsNull (ex.InnerException, "#3");
84 Assert.IsNotNull (ex.Message, "#4");
85 Assert.AreEqual ("stream", ex.ParamName, "#5");
89 [Test] // .ctor (String)
90 public void Constructor2 ()
92 // TODO - Security/Auth exceptions
93 using (StreamWriter r = new StreamWriter (_codeFileName)) {
94 Assert.IsFalse (r.AutoFlush, "#1");
95 Assert.IsNotNull (r.BaseStream, "#2");
96 Assert.AreEqual (typeof (FileStream), r.BaseStream.GetType (), "#3");
97 Assert.IsNotNull (r.Encoding, "#4");
98 Assert.AreEqual (typeof (UTF8Encoding), r.Encoding.GetType (), "#5");
103 [Test] // .ctor (String)
104 public void Constructor2_Path_DirectoryNotFound ()
106 Directory.Delete (TempFolder, true);
109 new StreamWriter (_codeFileName);
111 } catch (DirectoryNotFoundException ex) {
112 // Could not find a part of the path '...'
113 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
114 Assert.IsNull (ex.InnerException, "#3");
115 Assert.IsNotNull (ex.Message, "#4");
116 Assert.IsTrue (ex.Message.IndexOf (TempFolder) != -1, "#5");
120 [Test] // .ctor (String)
121 public void Constructor2_Path_Empty ()
124 new StreamWriter (string.Empty);
126 } catch (ArgumentException ex) {
127 // Empty path name is not legal
128 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
129 Assert.IsNull (ex.InnerException, "#3");
130 Assert.IsNotNull (ex.Message, "#4");
131 Assert.IsNull (ex.ParamName, "#5");
135 [Test] // .ctor (String)
136 public void Constructor2_Path_IllegalChars ()
139 new StreamWriter ("!$what? what? Huh? !$*#" + Path.InvalidPathChars [0]);
141 } catch (ArgumentException ex) {
142 // Illegal characters in path
143 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
144 Assert.IsNull (ex.InnerException, "#3");
145 Assert.IsNotNull (ex.Message, "#4");
146 Assert.IsNull (ex.ParamName, "#5");
150 [Test] // .ctor (String)
151 public void Constructor2_Path_Null ()
154 new StreamWriter ((string) null);
156 } catch (ArgumentNullException ex) {
157 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
158 Assert.IsNull (ex.InnerException, "#3");
159 Assert.IsNotNull (ex.Message, "#4");
160 Assert.AreEqual ("path", ex.ParamName, "#5");
164 [Test] // .ctor (Stream, Encoding)
165 public void Constructor3 ()
167 FileStream f = new FileStream (_codeFileName,
170 StreamWriter r = new StreamWriter (f, Encoding.ASCII);
171 Assert.IsFalse (r.AutoFlush, "#1");
172 Assert.AreSame (f, r.BaseStream, "#2");
173 Assert.IsNotNull (r.Encoding, "#3");
174 Assert.AreEqual (typeof (ASCIIEncoding), r.Encoding.GetType (), "#4");
179 [Test] // .ctor (Stream, Encoding)
180 public void Constructor3_Encoding_Null ()
182 MemoryStream m = new MemoryStream ();
184 new StreamWriter (m, (Encoding) null);
186 } catch (ArgumentNullException ex) {
187 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
188 Assert.IsNull (ex.InnerException, "#3");
189 Assert.IsNotNull (ex.Message, "#4");
190 Assert.AreEqual ("encoding", ex.ParamName, "#5");
194 [Test] // .ctor (Stream, Encoding)
195 public void Constructor3_Stream_NotWritable ()
197 FileStream f = new FileStream (_thisCodeFileName, FileMode.Open,
200 new StreamWriter (f, Encoding.UTF8);
202 } catch (ArgumentException ex) {
203 // Stream was not writable
204 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
205 Assert.IsNull (ex.InnerException, "#3");
206 Assert.IsNotNull (ex.Message, "#4");
207 Assert.IsNull (ex.ParamName, "#5");
213 [Test] // .ctor (Stream, Encoding)
214 public void Constructor3_Stream_Null ()
217 new StreamWriter ((Stream) null, Encoding.UTF8);
219 } catch (ArgumentNullException ex) {
220 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
221 Assert.IsNull (ex.InnerException, "#3");
222 Assert.IsNotNull (ex.Message, "#4");
223 Assert.AreEqual ("stream", ex.ParamName, "#5");
227 [Test] // .ctor (String, Boolean)
228 public void Constructor4 ()
230 using (StreamWriter r = new StreamWriter (_codeFileName, false)) {
231 Assert.IsFalse (r.AutoFlush, "#A1");
232 Assert.IsNotNull (r.BaseStream, "#A2");
233 Assert.AreEqual (typeof (FileStream), r.BaseStream.GetType (), "#A3");
234 Assert.IsNotNull (r.Encoding, "#A4");
235 Assert.AreEqual (typeof (UTF8Encoding), r.Encoding.GetType (), "#A5");
239 using (StreamWriter r = new StreamWriter(_codeFileName, true)) {
240 Assert.IsFalse (r.AutoFlush, "#B1");
241 Assert.IsNotNull (r.BaseStream, "#B2");
242 Assert.AreEqual (typeof (FileStream), r.BaseStream.GetType (), "#B3");
243 Assert.IsNotNull (r.Encoding, "#B4");
244 Assert.AreEqual (typeof (UTF8Encoding), r.Encoding.GetType (), "#B5");
249 [Test] // .ctor (String, Boolean)
250 public void Constructor4_Path_DirectoryNotFound ()
252 Directory.Delete (TempFolder, true);
255 new StreamWriter (_codeFileName, false);
257 } catch (DirectoryNotFoundException ex) {
258 // Could not find a part of the path '...'
259 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#A2");
260 Assert.IsNull (ex.InnerException, "#A3");
261 Assert.IsNotNull (ex.Message, "#A4");
262 Assert.IsTrue (ex.Message.IndexOf (TempFolder) != -1, "#A5");
266 new StreamWriter (_codeFileName, true);
268 } catch (DirectoryNotFoundException ex) {
269 // Could not find a part of the path '...'
270 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#B2");
271 Assert.IsNull (ex.InnerException, "#B3");
272 Assert.IsNotNull (ex.Message, "#B4");
273 Assert.IsTrue (ex.Message.IndexOf (TempFolder) != -1, "#B5");
277 [Test] // .ctor (String, Boolean)
278 public void Constructor4_Path_Empty ()
281 new StreamWriter (string.Empty, false);
283 } catch (ArgumentException ex) {
284 // Empty path name is not legal
285 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
286 Assert.IsNull (ex.InnerException, "#A3");
287 Assert.IsNotNull (ex.Message, "#A4");
288 Assert.IsNull (ex.ParamName, "#A5");
292 new StreamWriter (string.Empty, true);
294 } catch (ArgumentException ex) {
295 // Empty path name is not legal
296 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
297 Assert.IsNull (ex.InnerException, "#B3");
298 Assert.IsNotNull (ex.Message, "#B4");
299 Assert.IsNull (ex.ParamName, "#B5");
303 [Test] // .ctor (String, Boolean)
304 public void Constructor4_Path_InvalidChars ()
307 new StreamWriter ("!$what? what? Huh? !$*#" + Path.InvalidPathChars [0], false);
309 } catch (ArgumentException ex) {
310 // Illegal characters in path
311 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
312 Assert.IsNull (ex.InnerException, "#A3");
313 Assert.IsNotNull (ex.Message, "#A4");
314 Assert.IsNull (ex.ParamName, "#A5");
318 new StreamWriter ("!$what? what? Huh? !$*#" + Path.InvalidPathChars [0], true);
320 } catch (ArgumentException ex) {
321 // Illegal characters in path
322 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
323 Assert.IsNull (ex.InnerException, "#B3");
324 Assert.IsNotNull (ex.Message, "#B4");
325 Assert.IsNull (ex.ParamName, "#B5");
329 [Test] // .ctor (String, Boolean)
330 public void Constructor4_Path_Null ()
333 new StreamWriter ((string) null, false);
335 } catch (ArgumentNullException ex) {
336 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
337 Assert.IsNull (ex.InnerException, "#A3");
338 Assert.IsNotNull (ex.Message, "#A4");
339 Assert.AreEqual ("path", ex.ParamName, "#A5");
343 new StreamWriter ((string) null, true);
345 } catch (ArgumentNullException ex) {
346 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
347 Assert.IsNull (ex.InnerException, "#B3");
348 Assert.IsNotNull (ex.Message, "#B4");
349 Assert.AreEqual ("path", ex.ParamName, "#B5");
353 [Test] // .ctor (Stream, Encoding, Int32)
354 public void Constructor5 ()
359 m = new MemoryStream ();
360 r = new StreamWriter (m, Encoding.ASCII, 10);
361 Assert.IsFalse (r.AutoFlush, "#A1");
362 Assert.AreSame (m, r.BaseStream, "#A2");
363 Assert.IsNotNull (r.Encoding, "#A3");
364 Assert.AreEqual (typeof (ASCIIEncoding), r.Encoding.GetType (), "#A4");
368 m = new MemoryStream ();
369 r = new StreamWriter (m, Encoding.UTF8, 1);
370 Assert.IsFalse (r.AutoFlush, "#B1");
371 Assert.AreSame (m, r.BaseStream, "#B2");
372 Assert.IsNotNull (r.Encoding, "#B3");
373 Assert.AreEqual (typeof (UTF8Encoding), r.Encoding.GetType (), "#B4");
378 [Test] // .ctor (Stream, Encoding, Int32)
379 public void Constructor5_BufferSize_NotPositive ()
381 MemoryStream m = new MemoryStream ();
384 new StreamWriter (m, Encoding.UTF8, 0);
386 } catch (ArgumentOutOfRangeException ex) {
387 // Positive number required
388 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
389 Assert.IsNull (ex.InnerException, "#A3");
390 Assert.IsNotNull (ex.Message, "#A4");
392 Assert.AreEqual ("bufferSize", ex.ParamName, "#A5");
397 new StreamWriter (m, Encoding.UTF8, -1);
399 } catch (ArgumentOutOfRangeException ex) {
400 // Positive number required
401 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
402 Assert.IsNull (ex.InnerException, "#B3");
403 Assert.IsNotNull (ex.Message, "#B4");
405 Assert.AreEqual ("bufferSize", ex.ParamName, "#B5");
410 [Test] // .ctor (Stream, Encoding, Int32)
411 public void Constructor5_Encoding_Null ()
413 MemoryStream m = new MemoryStream ();
415 new StreamWriter (m, (Encoding) null, 10);
417 } catch (ArgumentNullException ex) {
418 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
419 Assert.IsNull (ex.InnerException, "#3");
420 Assert.IsNotNull (ex.Message, "#4");
421 Assert.AreEqual ("encoding", ex.ParamName, "#5");
425 [Test] // .ctor (Stream, Encoding, Int32)
426 public void Constructor5_Stream_NotWritable ()
428 FileStream f = new FileStream (_thisCodeFileName, FileMode.Open,
431 new StreamWriter (f, Encoding.UTF8, 10);
433 } catch (ArgumentException ex) {
434 // Stream was not writable
435 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
436 Assert.IsNull (ex.InnerException, "#3");
437 Assert.IsNotNull (ex.Message, "#4");
438 Assert.IsNull (ex.ParamName, "#5");
444 [Test] // .ctor (Stream, Encoding, Int32)
445 public void Constructor5_Stream_Null ()
448 new StreamWriter ((Stream) null, Encoding.UTF8, 10);
450 } catch (ArgumentNullException ex) {
451 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
452 Assert.IsNull (ex.InnerException, "#3");
453 Assert.IsNotNull (ex.Message, "#4");
454 Assert.AreEqual ("stream", ex.ParamName, "#5");
458 [Test] // .ctor (String, Boolean, Encoding)
459 public void Constructor6 ()
461 using (StreamWriter r = new StreamWriter (_codeFileName, false, Encoding.ASCII)) {
462 Assert.IsFalse (r.AutoFlush, "#A1");
463 Assert.IsNotNull (r.BaseStream, "#A2");
464 Assert.AreEqual (typeof (FileStream), r.BaseStream.GetType (), "#A3");
465 Assert.IsNotNull (r.Encoding, "#A4");
466 Assert.AreEqual (typeof (ASCIIEncoding), r.Encoding.GetType (), "#A5");
470 using (StreamWriter r = new StreamWriter (_codeFileName, true, Encoding.UTF8)) {
471 Assert.IsFalse (r.AutoFlush, "#B1");
472 Assert.IsNotNull (r.BaseStream, "#B2");
473 Assert.AreEqual (typeof (FileStream), r.BaseStream.GetType (), "#B3");
474 Assert.IsNotNull (r.Encoding, "#B4");
475 Assert.AreEqual (typeof (UTF8Encoding), r.Encoding.GetType (), "#B5");
480 [Test] // .ctor (String, Boolean, Encoding)
481 public void Constructor6_Encoding_Null ()
484 new StreamWriter (_codeFileName, false, (Encoding) null);
486 } catch (ArgumentNullException ex) {
487 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
488 Assert.IsNull (ex.InnerException, "#A3");
489 Assert.IsNotNull (ex.Message, "#A4");
490 Assert.AreEqual ("encoding", ex.ParamName, "#A5");
494 new StreamWriter (_codeFileName, true, (Encoding) null);
496 } catch (ArgumentNullException ex) {
497 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
498 Assert.IsNull (ex.InnerException, "#B3");
499 Assert.IsNotNull (ex.Message, "#B4");
500 Assert.AreEqual ("encoding", ex.ParamName, "#B5");
504 [Test] // .ctor (String, Boolean, Encoding)
505 public void Constructor6_Path_DirectoryNotFound ()
507 Directory.Delete (TempFolder, true);
510 new StreamWriter (_codeFileName, false, Encoding.UTF8);
512 } catch (DirectoryNotFoundException ex) {
513 // Could not find a part of the path '...'
514 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#A2");
515 Assert.IsNull (ex.InnerException, "#A3");
516 Assert.IsNotNull (ex.Message, "#A4");
517 Assert.IsTrue (ex.Message.IndexOf (TempFolder) != -1, "#A5");
521 new StreamWriter (_codeFileName, true, Encoding.UTF8);
523 } catch (DirectoryNotFoundException ex) {
524 // Could not find a part of the path '...'
525 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#B2");
526 Assert.IsNull (ex.InnerException, "#B3");
527 Assert.IsNotNull (ex.Message, "#B4");
528 Assert.IsTrue (ex.Message.IndexOf (TempFolder) != -1, "#B5");
532 [Test] // .ctor (String, Boolean, Encoding)
533 public void Constructor6_Path_Empty ()
536 new StreamWriter (string.Empty, false, Encoding.UTF8);
538 } catch (ArgumentException ex) {
539 // Empty path name is not legal
540 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
541 Assert.IsNull (ex.InnerException, "#A3");
542 Assert.IsNotNull (ex.Message, "#A4");
543 Assert.IsNull (ex.ParamName, "#A5");
547 new StreamWriter (string.Empty, true, Encoding.UTF8);
549 } catch (ArgumentException ex) {
550 // Empty path name is not legal
551 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
552 Assert.IsNull (ex.InnerException, "#B3");
553 Assert.IsNotNull (ex.Message, "#B4");
554 Assert.IsNull (ex.ParamName, "#B5");
558 [Test] // .ctor (String, Boolean, Encoding)
559 public void Constructor6_Path_InvalidChars ()
562 new StreamWriter ("!$what? what? Huh? !$*#" +
563 Path.InvalidPathChars [0], false,
566 } catch (ArgumentException ex) {
567 // Illegal characters in path
568 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
569 Assert.IsNull (ex.InnerException, "#A3");
570 Assert.IsNotNull (ex.Message, "#A4");
571 Assert.IsNull (ex.ParamName, "#A5");
575 new StreamWriter ("!$what? what? Huh? !$*#" +
576 Path.InvalidPathChars [0], true,
579 } catch (ArgumentException ex) {
580 // Illegal characters in path
581 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
582 Assert.IsNull (ex.InnerException, "#B3");
583 Assert.IsNotNull (ex.Message, "#B4");
584 Assert.IsNull (ex.ParamName, "#B5");
588 [Test] // .ctor (String, Boolean, Encoding)
589 public void Constructor6_Path_Null ()
592 new StreamWriter ((string) null, false, Encoding.UTF8);
594 } catch (ArgumentNullException ex) {
595 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
596 Assert.IsNull (ex.InnerException, "#A3");
597 Assert.IsNotNull (ex.Message, "#A4");
598 Assert.AreEqual ("path", ex.ParamName, "#A5");
602 new StreamWriter ((string) null, true, Encoding.UTF8);
604 } catch (ArgumentNullException ex) {
605 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
606 Assert.IsNull (ex.InnerException, "#B3");
607 Assert.IsNotNull (ex.Message, "#B4");
608 Assert.AreEqual ("path", ex.ParamName, "#B5");
612 [Test] // .ctor (String, Boolean, Encoding, Int32)
613 public void Constructor7 ()
615 using (StreamWriter r = new StreamWriter (_codeFileName, false, Encoding.ASCII, 10)) {
616 Assert.IsFalse (r.AutoFlush, "#A1");
617 Assert.IsNotNull (r.BaseStream, "#A2");
618 Assert.AreEqual (typeof (FileStream), r.BaseStream.GetType (), "#A3");
619 Assert.IsNotNull (r.Encoding, "#A4");
620 Assert.AreEqual (typeof (ASCIIEncoding), r.Encoding.GetType (), "#A5");
624 using (StreamWriter r = new StreamWriter (_codeFileName, true, Encoding.UTF8, 1)) {
625 Assert.IsFalse (r.AutoFlush, "#B1");
626 Assert.IsNotNull (r.BaseStream, "#B2");
627 Assert.AreEqual (typeof (FileStream), r.BaseStream.GetType (), "#B3");
628 Assert.IsNotNull (r.Encoding, "#B4");
629 Assert.AreEqual (typeof (UTF8Encoding), r.Encoding.GetType (), "#B5");
634 [Test] // .ctor (String, Boolean, Encoding, Int32)
635 public void Constructor7_BufferSize_NotPositive ()
638 new StreamWriter (_codeFileName, false, Encoding.UTF8, 0);
640 } catch (ArgumentOutOfRangeException ex) {
641 // Positive number required
642 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
643 Assert.IsNull (ex.InnerException, "#A3");
644 Assert.IsNotNull (ex.Message, "#A4");
646 Assert.AreEqual ("bufferSize", ex.ParamName, "#A5");
651 new StreamWriter (_codeFileName, false, Encoding.UTF8, -1);
653 } catch (ArgumentOutOfRangeException ex) {
654 // Positive number required
655 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
656 Assert.IsNull (ex.InnerException, "#B3");
657 Assert.IsNotNull (ex.Message, "#B4");
659 Assert.AreEqual ("bufferSize", ex.ParamName, "#B5");
664 [Test] // .ctor (String, Boolean, Encoding, Int32)
665 public void Constructor7_Encoding_Null ()
668 new StreamWriter (_codeFileName, false, (Encoding) null, 10);
670 } catch (ArgumentNullException ex) {
671 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
672 Assert.IsNull (ex.InnerException, "#A3");
673 Assert.IsNotNull (ex.Message, "#A4");
674 Assert.AreEqual ("encoding", ex.ParamName, "#A5");
678 new StreamWriter (_codeFileName, true, (Encoding) null, 10);
680 } catch (ArgumentNullException ex) {
681 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
682 Assert.IsNull (ex.InnerException, "#B3");
683 Assert.IsNotNull (ex.Message, "#B4");
684 Assert.AreEqual ("encoding", ex.ParamName, "#B5");
688 [Test] // .ctor (String, Boolean, Encoding, Int32)
689 public void Constructor7_Path_DirectoryNotFound ()
691 Directory.Delete (TempFolder, true);
694 new StreamWriter (_codeFileName, false, Encoding.UTF8, 10);
696 } catch (DirectoryNotFoundException ex) {
697 // Could not find a part of the path '...'
698 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#A2");
699 Assert.IsNull (ex.InnerException, "#A3");
700 Assert.IsNotNull (ex.Message, "#A4");
701 Assert.IsTrue (ex.Message.IndexOf (TempFolder) != -1, "#A5");
705 new StreamWriter (_codeFileName, true, Encoding.UTF8, 10);
707 } catch (DirectoryNotFoundException ex) {
708 // Could not find a part of the path '...'
709 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#B2");
710 Assert.IsNull (ex.InnerException, "#B3");
711 Assert.IsNotNull (ex.Message, "#B4");
712 Assert.IsTrue (ex.Message.IndexOf (TempFolder) != -1, "#B5");
716 [Test] // .ctor (String, Boolean, Encoding, Int32)
717 public void Constructor7_Path_Empty ()
720 new StreamWriter (string.Empty, false, Encoding.UTF8, 10);
722 } catch (ArgumentException ex) {
723 // Empty path name is not legal
724 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
725 Assert.IsNull (ex.InnerException, "#A3");
726 Assert.IsNotNull (ex.Message, "#A4");
727 Assert.IsNull (ex.ParamName, "#A5");
731 new StreamWriter (string.Empty, true, Encoding.UTF8, 10);
733 } catch (ArgumentException ex) {
734 // Empty path name is not legal
735 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
736 Assert.IsNull (ex.InnerException, "#B3");
737 Assert.IsNotNull (ex.Message, "#B4");
738 Assert.IsNull (ex.ParamName, "#B5");
742 [Test] // .ctor (String, Boolean, Encoding, Int32)
743 public void Constructor7_Path_InvalidChars ()
746 new StreamWriter ("!$what? what? Huh? !$*#" +
747 Path.InvalidPathChars [0], false,
750 } catch (ArgumentException ex) {
751 // Illegal characters in path
752 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
753 Assert.IsNull (ex.InnerException, "#A3");
754 Assert.IsNotNull (ex.Message, "#A4");
755 Assert.IsNull (ex.ParamName, "#A5");
759 new StreamWriter ("!$what? what? Huh? !$*#" +
760 Path.InvalidPathChars [0], true,
763 } catch (ArgumentException ex) {
764 // Illegal characters in path
765 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
766 Assert.IsNull (ex.InnerException, "#B3");
767 Assert.IsNotNull (ex.Message, "#B4");
768 Assert.IsNull (ex.ParamName, "#B5");
772 [Test] // .ctor (String, Boolean, Encoding, Int32)
773 public void Constructor7_Path_Null ()
776 new StreamWriter ((string) null, false, Encoding.UTF8, 10);
778 } catch (ArgumentNullException ex) {
779 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
780 Assert.IsNull (ex.InnerException, "#A3");
781 Assert.IsNotNull (ex.Message, "#A4");
782 Assert.AreEqual ("path", ex.ParamName, "#A5");
786 new StreamWriter ((string) null, true, Encoding.UTF8, 10);
788 } catch (ArgumentNullException ex) {
789 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
790 Assert.IsNull (ex.InnerException, "#B3");
791 Assert.IsNotNull (ex.Message, "#B4");
792 Assert.AreEqual ("path", ex.ParamName, "#B5");
797 public void AutoFlush ()
802 m = new MemoryStream ();
803 w = new StreamWriter (m);
808 Assert.AreEqual (0, m.Length, "#A1");
810 Assert.IsTrue (w.AutoFlush, "#A2");
811 Assert.AreEqual (4, m.Length, "#A3");
813 Assert.AreEqual (4, m.Length, "#A4");
815 m = new MemoryStream ();
816 w = new StreamWriter(m);
818 Assert.IsTrue (w.AutoFlush, "#B1");
823 Assert.AreEqual (4, m.Length, "#B2");
825 Assert.AreEqual (4, m.Length, "#B3");
827 Assert.IsFalse (w.AutoFlush, "#B4");
829 Assert.AreEqual (4, m.Length, "#B5");
831 Assert.AreEqual (5, m.Length, "#B6");
835 public void AutoFlush_Disposed ()
839 w = new StreamWriter (new MemoryStream ());
842 Assert.IsFalse (w.AutoFlush, "#A1");
846 } catch (ObjectDisposedException) {
848 Assert.IsTrue (w.AutoFlush, "#A3");
850 w = new StreamWriter (new MemoryStream ());
853 Assert.IsTrue (w.AutoFlush, "#B1");
857 } catch (ObjectDisposedException) {
859 Assert.IsTrue (w.AutoFlush, "#B3");
861 Assert.IsFalse (w.AutoFlush, "#B4");
867 Encoding encoding = Encoding.ASCII;
868 MemoryStream m = new MemoryStream ();
869 StreamWriter w = new StreamWriter (m, encoding);
871 Assert.AreEqual (0, m.Length, "#1");
873 Assert.IsFalse (m.CanWrite, "#2");
874 Assert.AreEqual (50, m.GetBuffer () [0], "#3");
875 Assert.IsNull (w.BaseStream, "#4");
876 Assert.IsNull (w.Encoding, "#5");
882 MemoryStream m = new MemoryStream();
883 StreamWriter w = new StreamWriter(m);
888 Assert.AreEqual (0L, m.Length, "#1");
890 Assert.AreEqual (4L, m.Length, "#2");
894 public void Flush_Disposed ()
896 StreamWriter w = new StreamWriter(new MemoryStream ());
901 } catch (ObjectDisposedException) {
906 [ExpectedException (typeof (ObjectDisposedException))]
907 public void WriteChar_Disposed ()
909 StreamWriter w = new StreamWriter (new MemoryStream ());
915 [ExpectedException (typeof (ObjectDisposedException))]
916 public void WriteCharArray_Disposed ()
918 char[] c = new char [2] { 'a', 'b' };
919 StreamWriter w = new StreamWriter (new MemoryStream ());
925 public void WriteCharArray_Null ()
928 StreamWriter w = new StreamWriter (new MemoryStream ());
933 [ExpectedException (typeof (ArgumentException))]
934 public void WriteCharArray_IndexOverflow ()
936 char[] c = new char [2] { 'a', 'b' };
937 StreamWriter w = new StreamWriter (new MemoryStream ());
938 w.Write (c, Int32.MaxValue, 2);
942 [ExpectedException (typeof (ArgumentException))]
943 public void WriteCharArray_CountOverflow ()
945 char[] c = new char [2] { 'a', 'b' };
946 StreamWriter w = new StreamWriter (new MemoryStream ());
947 w.Write (c, 1, Int32.MaxValue);
951 [ExpectedException (typeof (ObjectDisposedException))]
952 public void WriteString_Disposed ()
954 StreamWriter w = new StreamWriter (new MemoryStream ());
960 public void WriteString_Null ()
963 StreamWriter w = new StreamWriter (new MemoryStream ());
968 public void NoPreambleOnAppend ()
970 MemoryStream ms = new MemoryStream ();
971 StreamWriter w = new StreamWriter (ms, Encoding.UTF8);
974 Assert.AreEqual (4, ms.Position, "#1");
976 // Append 1 byte, should skip the preamble now.
979 w = new StreamWriter (ms, Encoding.UTF8);
980 Assert.AreEqual (5, ms.Position, "#2");
983 // TODO - Write - test errors, functionality tested in TestFlush.