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()
230 Stream s = new MemoryStream();
231 TextWriter w = new StreamWriter(s);
232 ((StreamWriter)w).AutoFlush = true;
233 TextReader r = new StreamReader(s);
238 String testStr = "This is a stdout write test";
239 Console.Write(testStr);
241 String line = r.ReadToEnd();
242 Assert.AreEqual (testStr, line, "Wrong line");
245 Boolean[] booleans = {true, false};
246 foreach (bool b in booleans) {
249 line = r.ReadToEnd();
250 Assert.AreEqual (b.ToString(), line, "Wrong boolean");
254 Char[] chars = {'a', ';', '?'};
255 foreach (Char c in chars) {
258 line = r.ReadToEnd();
259 Assert.AreEqual (c.ToString(), line, "Wrong char");
263 // test writing a null value
267 // TODO - Likewise for char[], decimal, double, int, long, object, single, uint32, uint64
268 // TODO - write with format string
272 public void TestWriteLine()
274 Stream s = new MemoryStream();
275 TextWriter w = new StreamWriter(s);
276 ((StreamWriter)w).AutoFlush = true;
277 TextReader r = new StreamReader(s);
282 String testStr = "This is a stdout writeline test";
283 Console.WriteLine(testStr);
285 String line = r.ReadLine();
286 Assert.AreEqual (testStr, line, "Wrong line");
289 Boolean[] booleans = {true, false};
290 foreach (bool b in booleans) {
291 Console.WriteLine(b);
294 Assert.AreEqual (b.ToString(), line, "Wrong boolean");
298 Char[] chars = {'a', ';', '?'};
299 foreach (Char c in chars) {
300 Console.WriteLine(c);
303 Assert.AreEqual (c.ToString(), line, "Wrong char");
307 // test writing a null value
309 Console.WriteLine (x);
311 // TODO - Likewise for char[], decimal, double, int, long, object, single, uint32, uint64
312 // TODO - write with format string