1 // ConsoleTest.cs - NUnit Test Cases for the System.Console 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
\r
16 public class ConsoleTest : TestCase
\r
18 public ConsoleTest() {}
\r
23 protected override void SetUp()
\r
25 _err = Console.Error;
\r
30 protected override void TearDown()
\r
32 Console.SetError(_err);
\r
34 Console.SetOut(_out);
\r
37 public void TestError() {
\r
38 AssertNotNull("No error", Console.Error);
\r
41 public void TestIn() {
\r
42 AssertNotNull("No in", Console.In);
\r
45 public void TestOut() {
\r
46 AssertNotNull("No out", Console.Out);
\r
49 public void TestOpenStandardError() {
\r
51 Stream err = Console.OpenStandardError();
\r
52 AssertNotNull("Can't open error", err);
\r
55 Stream err = Console.OpenStandardError(512);
\r
56 AssertNotNull("Can't open error", err);
\r
58 // Spec says these are here, MS implementation says no.
\r
60 //bool errorThrown = false;
\r
62 //Stream err = Console.OpenStandardError(-1);
\r
63 //} catch (ArgumentOutOfRangeException) {
\r
64 //errorThrown = true;
\r
66 //Assert("negative buffer error not thrown",
\r
70 //bool errorThrown = false;
\r
72 //Stream err = Console.OpenStandardError(0);
\r
73 //} catch (ArgumentOutOfRangeException) {
\r
74 //errorThrown = true;
\r
76 //Assert("zero buffer error not thrown", errorThrown);
\r
80 public void TestOpenStandardInput() {
\r
82 Stream in1 = Console.OpenStandardInput();
\r
83 AssertNotNull("Can't open input", in1);
\r
86 Stream in1 = Console.OpenStandardInput(512);
\r
87 AssertNotNull("Can't open input", in1);
\r
89 // see commented-out tests in TestOpenStandardError
\r
92 public void TestOpenStandardOutput() {
\r
94 Stream out1 = Console.OpenStandardOutput();
\r
95 AssertNotNull("Can't open output", out1);
\r
98 Stream out1 = Console.OpenStandardOutput(512);
\r
99 AssertNotNull("Can't open output", out1);
\r
101 // see commented-out tests in TestOpenStandardError
\r
104 public void TestRead() {
\r
105 String testStr = "This is a readline test";
\r
106 Stream s = new MemoryStream();
\r
107 TextWriter w = new StreamWriter(s);
\r
108 ((StreamWriter)w).AutoFlush = true;
\r
109 TextReader r = new StreamReader(s);
\r
111 w.WriteLine(testStr);
\r
113 char val = (char) Console.Read();
\r
114 AssertEquals("Wrong read", 'T', val);
\r
117 public void TestReadLine() {
\r
118 String testStr = "This is a readline test";
\r
119 Stream s = new MemoryStream();
\r
120 TextWriter w = new StreamWriter(s);
\r
121 ((StreamWriter)w).AutoFlush = true;
\r
122 TextReader r = new StreamReader(s);
\r
124 w.WriteLine(testStr);
\r
126 String line = Console.ReadLine();
\r
127 AssertEquals("Wrong line", testStr, line);
\r
130 public void TestSetError() {
\r
132 bool errorThrown = false;
\r
134 Console.SetError(null);
\r
135 } catch (ArgumentNullException) {
\r
136 errorThrown = true;
\r
138 Assert("null error error not thrown", errorThrown);
\r
141 String testStr = "This is a stderr test";
\r
142 Stream s = new MemoryStream();
\r
143 TextWriter w = new StreamWriter(s);
\r
144 ((StreamWriter)w).AutoFlush = true;
\r
145 TextReader r = new StreamReader(s);
\r
146 Console.SetError(w);
\r
147 Console.Error.WriteLine(testStr);
\r
149 String line = r.ReadLine();
\r
150 AssertEquals("Wrong line", testStr, line);
\r
154 public void TestSetIn() {
\r
156 bool errorThrown = false;
\r
158 Console.SetIn(null);
\r
159 } catch (ArgumentNullException) {
\r
160 errorThrown = true;
\r
162 Assert("null in error not thrown", errorThrown);
\r
165 String testStr = "This is a stdin test";
\r
166 Stream s = new MemoryStream();
\r
167 TextWriter w = new StreamWriter(s);
\r
168 ((StreamWriter)w).AutoFlush = true;
\r
169 TextReader r = new StreamReader(s);
\r
171 w.WriteLine(testStr);
\r
173 String line = Console.In.ReadLine();
\r
174 AssertEquals("Wrong line", testStr, line);
\r
178 public void TestSetOut() {
\r
180 bool errorThrown = false;
\r
182 Console.SetOut(null);
\r
183 } catch (ArgumentNullException) {
\r
184 errorThrown = true;
\r
186 Assert("null out error not thrown", errorThrown);
\r
189 String testStr = "This is a stdout test";
\r
190 Stream s = new MemoryStream();
\r
191 TextWriter w = new StreamWriter(s);
\r
192 ((StreamWriter)w).AutoFlush = true;
\r
193 TextReader r = new StreamReader(s);
\r
195 Console.Out.WriteLine(testStr);
\r
197 String line = r.ReadLine();
\r
198 AssertEquals("Wrong line", testStr, line);
\r
202 public void TestWrite() {
\r
203 Stream s = new MemoryStream();
\r
204 TextWriter w = new StreamWriter(s);
\r
205 ((StreamWriter)w).AutoFlush = true;
\r
206 TextReader r = new StreamReader(s);
\r
211 String testStr = "This is a stdout write test";
\r
212 Console.Write(testStr);
\r
213 s.Position = endPos;
\r
214 String line = r.ReadToEnd();
\r
215 AssertEquals("Wrong line", testStr, line);
\r
216 endPos = s.Position;
\r
218 Boolean[] booleans = {true, false};
\r
219 foreach (bool b in booleans ) {
\r
221 s.Position = endPos;
\r
222 line = r.ReadToEnd();
\r
223 AssertEquals("Wrong boolean", b.ToString(), line);
\r
224 endPos = s.Position;
\r
227 Char[] chars = {'a', ';', '?'};
\r
228 foreach (Char c in chars ) {
\r
230 s.Position = endPos;
\r
231 line = r.ReadToEnd();
\r
232 AssertEquals("Wrong char", c.ToString(), line);
\r
233 endPos = s.Position;
\r
236 // test writing a null value
\r
240 // TODO - Likewise for char[], decimal, double, int, long, object, single, uint32, uint64
\r
241 // TODO - write with format string
\r
243 public void TestWriteLine() {
\r
244 Stream s = new MemoryStream();
\r
245 TextWriter w = new StreamWriter(s);
\r
246 ((StreamWriter)w).AutoFlush = true;
\r
247 TextReader r = new StreamReader(s);
\r
252 String testStr = "This is a stdout writeline test";
\r
253 Console.WriteLine(testStr);
\r
254 s.Position = endPos;
\r
255 String line = r.ReadLine();
\r
256 AssertEquals("Wrong line", testStr, line);
\r
257 endPos = s.Position;
\r
259 Boolean[] booleans = {true, false};
\r
260 foreach (bool b in booleans ) {
\r
261 Console.WriteLine(b);
\r
262 s.Position = endPos;
\r
263 line = r.ReadLine();
\r
264 AssertEquals("Wrong boolean", b.ToString(), line);
\r
265 endPos = s.Position;
\r
268 Char[] chars = {'a', ';', '?'};
\r
269 foreach (Char c in chars ) {
\r
270 Console.WriteLine(c);
\r
271 s.Position = endPos;
\r
272 line = r.ReadLine();
\r
273 AssertEquals("Wrong char", c.ToString(), line);
\r
274 endPos = s.Position;
\r
277 // test writing a null value
\r
279 Console.WriteLine (x);
\r
281 // TODO - Likewise for char[], decimal, double, int, long, object, single, uint32, uint64
\r
282 // TODO - write with format string
\r