1 // ConsoleTest.cs - NUnit Test Cases for the System.Console class
3 // David Brandt (bucky@keystreams.com)
5 // (C) Ximian, Inc. http://www.ximian.com
13 namespace MonoTests.System
16 public class ConsoleTest
18 public ConsoleTest() {}
33 public void TearDown()
35 Console.SetError(_err);
41 public void TestError()
43 Assert.IsNotNull (Console.Error, "No error");
49 Assert.IsNotNull (Console.In, "No in");
55 Assert.IsNotNull (Console.Out, "No out");
59 public void TestOpenStandardError()
62 Stream err = Console.OpenStandardError ();
63 Assert.IsNotNull (err, "Can't open error #1");
66 Stream err = Console.OpenStandardError (512);
67 Assert.IsNotNull (err, "Can't open error #2");
69 // Spec says these are here, MS implementation says no.
71 //bool errorThrown = false;
73 //Stream err = Console.OpenStandardError(-1);
74 //} catch (ArgumentOutOfRangeException) {
77 //Assert("negative buffer error not thrown",
81 //bool errorThrown = false;
83 //Stream err = Console.OpenStandardError(0);
84 //} catch (ArgumentOutOfRangeException) {
87 //Assert("zero buffer error not thrown", errorThrown);
92 public void TestOpenStandardInput()
95 Stream in1 = Console.OpenStandardInput ();
96 Assert.IsNotNull (in1, "Can't open input #1");
99 Stream in1 = Console.OpenStandardInput (512);
100 Assert.IsNotNull (in1, "Can't open input #2");
102 // see commented-out tests in TestOpenStandardError
106 public void TestOpenStandardOutput()
109 Stream out1 = Console.OpenStandardOutput ();
110 Assert.IsNotNull(out1, "Can't open output #1");
113 Stream out1 = Console.OpenStandardOutput(512);
114 Assert.IsNotNull (out1, "Can't open output #2");
116 // see commented-out tests in TestOpenStandardError
120 public void TestRead()
122 String testStr = "This is a readline test";
123 Stream s = new MemoryStream();
124 TextWriter w = new StreamWriter(s);
125 ((StreamWriter)w).AutoFlush = true;
126 TextReader r = new StreamReader(s);
128 w.WriteLine(testStr);
130 char val = (char) Console.Read();
131 Assert.AreEqual ('T', val, "Wrong read");
135 public void TestReadLine()
137 String testStr = "This is a readline test";
138 Stream s = new MemoryStream();
139 TextWriter w = new StreamWriter(s);
140 ((StreamWriter)w).AutoFlush = true;
141 TextReader r = new StreamReader(s);
143 w.WriteLine(testStr);
145 String line = Console.ReadLine();
146 Assert.AreEqual (testStr, line, "Wrong line");
150 public void TestSetError()
153 bool errorThrown = false;
155 Console.SetError(null);
156 } catch (ArgumentNullException) {
159 Assert.IsTrue (errorThrown, "null error error not thrown");
162 String testStr = "This is a stderr test";
163 Stream s = new MemoryStream();
164 TextWriter w = new StreamWriter(s);
165 ((StreamWriter)w).AutoFlush = true;
166 TextReader r = new StreamReader(s);
168 Console.Error.WriteLine(testStr);
170 String line = r.ReadLine();
171 Assert.AreEqual (testStr, line, "Wrong line");
176 public void TestSetIn()
179 bool errorThrown = false;
182 } catch (ArgumentNullException) {
185 Assert.IsTrue (errorThrown, "null in error not thrown");
188 String testStr = "This is a stdin test";
189 Stream s = new MemoryStream();
190 TextWriter w = new StreamWriter(s);
191 ((StreamWriter)w).AutoFlush = true;
192 TextReader r = new StreamReader(s);
194 w.WriteLine(testStr);
196 String line = Console.In.ReadLine();
197 Assert.AreEqual (testStr, line, "Wrong line");
202 public void TestSetOut()
205 bool errorThrown = false;
207 Console.SetOut(null);
208 } catch (ArgumentNullException) {
211 Assert.IsTrue (errorThrown, "null out error not thrown");
214 String testStr = "This is a stdout test";
215 Stream s = new MemoryStream();
216 TextWriter w = new StreamWriter(s);
217 ((StreamWriter)w).AutoFlush = true;
218 TextReader r = new StreamReader(s);
220 Console.Out.WriteLine(testStr);
222 String line = r.ReadLine();
223 Assert.AreEqual (testStr, line, "Wrong line");
228 public void TestWrite_Params()
230 Console.Write ("text {0}", (object[]) null);
234 public void TestWrite()
236 Stream s = new MemoryStream();
237 TextWriter w = new StreamWriter(s);
238 ((StreamWriter)w).AutoFlush = true;
239 TextReader r = new StreamReader(s);
244 String testStr = "This is a stdout write test";
245 Console.Write(testStr);
247 String line = r.ReadToEnd();
248 Assert.AreEqual (testStr, line, "Wrong line");
251 Boolean[] booleans = {true, false};
252 foreach (bool b in booleans) {
255 line = r.ReadToEnd();
256 Assert.AreEqual (b.ToString(), line, "Wrong boolean");
260 Char[] chars = {'a', ';', '?'};
261 foreach (Char c in chars) {
264 line = r.ReadToEnd();
265 Assert.AreEqual (c.ToString(), line, "Wrong char");
269 // test writing a null value
273 // TODO - Likewise for char[], decimal, double, int, long, object, single, uint32, uint64
274 // TODO - write with format string
278 public void TestWriteLine()
280 Stream s = new MemoryStream();
281 TextWriter w = new StreamWriter(s);
282 ((StreamWriter)w).AutoFlush = true;
283 TextReader r = new StreamReader(s);
288 String testStr = "This is a stdout writeline test";
289 Console.WriteLine(testStr);
291 String line = r.ReadLine();
292 Assert.AreEqual (testStr, line, "Wrong line");
295 Boolean[] booleans = {true, false};
296 foreach (bool b in booleans) {
297 Console.WriteLine(b);
300 Assert.AreEqual (b.ToString(), line, "Wrong boolean");
304 Char[] chars = {'a', ';', '?'};
305 foreach (Char c in chars) {
306 Console.WriteLine(c);
309 Assert.AreEqual (c.ToString(), line, "Wrong char");
313 // test writing a null value
315 Console.WriteLine (x);
317 // TODO - Likewise for char[], decimal, double, int, long, object, single, uint32, uint64
318 // TODO - write with format string
322 public void TestWriteLine_Params()
324 Stream s = new MemoryStream();
325 TextWriter w = new StreamWriter(s);
326 ((StreamWriter)w).AutoFlush = true;
327 TextReader r = new StreamReader(s);
330 Console.WriteLine ("text {0}", (object[]) null);
337 public void RedirectedTest ()
339 if (Console.IsErrorRedirected) {
340 // Assert.Inconclusive ();
344 Console.SetError (TextWriter.Null);
345 Assert.IsFalse (Console.IsErrorRedirected);
351 public void EncodingTest ()
353 Console.OutputEncoding = Encoding.ASCII;
354 Assert.AreEqual (Console.OutputEncoding, Console.Out.Encoding);
355 Console.OutputEncoding = Encoding.UTF8;
356 Assert.AreEqual (Console.OutputEncoding, Console.Out.Encoding);