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() : base ("MonoTests.System.ConsoleTest testsuite") {}
\r
19 public ConsoleTest(string name) : base(name) {}
\r
21 protected override void SetUp()
\r
25 protected override void TearDown()
\r
29 public static ITest Suite {
\r
31 return new TestSuite(typeof(ConsoleTest));
\r
35 public void TestError() {
\r
36 AssertNotNull("No error", Console.Error);
\r
39 public void TestIn() {
\r
40 AssertNotNull("No in", Console.In);
\r
43 public void TestOut() {
\r
44 AssertNotNull("No out", Console.Out);
\r
47 public void TestOpenStandardError() {
\r
49 Stream err = Console.OpenStandardError();
\r
50 AssertNotNull("Can't open error", err);
\r
53 Stream err = Console.OpenStandardError(512);
\r
54 AssertNotNull("Can't open error", err);
\r
56 // Spec says these are here, MS implementation says no.
\r
58 //bool errorThrown = false;
\r
60 //Stream err = Console.OpenStandardError(-1);
\r
61 //} catch (ArgumentOutOfRangeException) {
\r
62 //errorThrown = true;
\r
64 //Assert("negative buffer error not thrown",
\r
68 //bool errorThrown = false;
\r
70 //Stream err = Console.OpenStandardError(0);
\r
71 //} catch (ArgumentOutOfRangeException) {
\r
72 //errorThrown = true;
\r
74 //Assert("zero buffer error not thrown", errorThrown);
\r
78 public void TestOpenStandardInput() {
\r
80 Stream in1 = Console.OpenStandardInput();
\r
81 AssertNotNull("Can't open input", in1);
\r
84 Stream in1 = Console.OpenStandardInput(512);
\r
85 AssertNotNull("Can't open input", in1);
\r
87 // see commented-out tests in TestOpenStandardError
\r
90 public void TestOpenStandardOutput() {
\r
92 Stream out1 = Console.OpenStandardOutput();
\r
93 AssertNotNull("Can't open output", out1);
\r
96 Stream out1 = Console.OpenStandardOutput(512);
\r
97 AssertNotNull("Can't open output", out1);
\r
99 // see commented-out tests in TestOpenStandardError
\r
102 public void TestRead() {
\r
103 String testStr = "This is a readline test";
\r
104 Stream s = new MemoryStream();
\r
105 TextWriter w = new StreamWriter(s);
\r
106 ((StreamWriter)w).AutoFlush = true;
\r
107 TextReader r = new StreamReader(s);
\r
109 w.WriteLine(testStr);
\r
111 char val = (char) Console.Read();
\r
112 AssertEquals("Wrong read", 'T', val);
\r
115 public void TestReadLine() {
\r
116 String testStr = "This is a readline test";
\r
117 Stream s = new MemoryStream();
\r
118 TextWriter w = new StreamWriter(s);
\r
119 ((StreamWriter)w).AutoFlush = true;
\r
120 TextReader r = new StreamReader(s);
\r
122 w.WriteLine(testStr);
\r
124 String line = Console.ReadLine();
\r
125 AssertEquals("Wrong line", testStr, line);
\r
128 public void TestSetError() {
\r
130 bool errorThrown = false;
\r
132 Console.SetError(null);
\r
133 } catch (ArgumentNullException) {
\r
134 errorThrown = true;
\r
136 Assert("null error error not thrown", errorThrown);
\r
139 String testStr = "This is a stderr test";
\r
140 Stream s = new MemoryStream();
\r
141 TextWriter w = new StreamWriter(s);
\r
142 ((StreamWriter)w).AutoFlush = true;
\r
143 TextReader r = new StreamReader(s);
\r
144 Console.SetError(w);
\r
145 Console.Error.WriteLine(testStr);
\r
147 String line = r.ReadLine();
\r
148 AssertEquals("Wrong line", testStr, line);
\r
152 public void TestSetIn() {
\r
154 bool errorThrown = false;
\r
156 Console.SetIn(null);
\r
157 } catch (ArgumentNullException) {
\r
158 errorThrown = true;
\r
160 Assert("null in error not thrown", errorThrown);
\r
163 String testStr = "This is a stdin test";
\r
164 Stream s = new MemoryStream();
\r
165 TextWriter w = new StreamWriter(s);
\r
166 ((StreamWriter)w).AutoFlush = true;
\r
167 TextReader r = new StreamReader(s);
\r
169 w.WriteLine(testStr);
\r
171 String line = Console.In.ReadLine();
\r
172 AssertEquals("Wrong line", testStr, line);
\r
176 public void TestSetOut() {
\r
178 bool errorThrown = false;
\r
180 Console.SetOut(null);
\r
181 } catch (ArgumentNullException) {
\r
182 errorThrown = true;
\r
184 Assert("null out error not thrown", errorThrown);
\r
187 String testStr = "This is a stdout test";
\r
188 Stream s = new MemoryStream();
\r
189 TextWriter w = new StreamWriter(s);
\r
190 ((StreamWriter)w).AutoFlush = true;
\r
191 TextReader r = new StreamReader(s);
\r
193 Console.Out.WriteLine(testStr);
\r
195 String line = r.ReadLine();
\r
196 AssertEquals("Wrong line", testStr, line);
\r
200 public void TestWrite() {
\r
201 Stream s = new MemoryStream();
\r
202 TextWriter w = new StreamWriter(s);
\r
203 ((StreamWriter)w).AutoFlush = true;
\r
204 TextReader r = new StreamReader(s);
\r
209 String testStr = "This is a stdout write test";
\r
210 Console.Write(testStr);
\r
211 s.Position = endPos;
\r
212 String line = r.ReadToEnd();
\r
213 AssertEquals("Wrong line", testStr, line);
\r
214 endPos = s.Position;
\r
216 Boolean[] booleans = {true, false};
\r
217 foreach (bool b in booleans ) {
\r
219 s.Position = endPos;
\r
220 line = r.ReadToEnd();
\r
221 AssertEquals("Wrong boolean", b.ToString(), line);
\r
222 endPos = s.Position;
\r
225 Char[] chars = {'a', ';', '?'};
\r
226 foreach (Char c in chars ) {
\r
228 s.Position = endPos;
\r
229 line = r.ReadToEnd();
\r
230 AssertEquals("Wrong char", c.ToString(), line);
\r
231 endPos = s.Position;
\r
234 // TODO - Likewise for char[], decimal, double, int, long, object, single, uint32, uint64
\r
235 // TODO - write with format string
\r
238 public void TestWriteLine() {
\r
239 Stream s = new MemoryStream();
\r
240 TextWriter w = new StreamWriter(s);
\r
241 ((StreamWriter)w).AutoFlush = true;
\r
242 TextReader r = new StreamReader(s);
\r
247 String testStr = "This is a stdout writeline test";
\r
248 Console.WriteLine(testStr);
\r
249 s.Position = endPos;
\r
250 String line = r.ReadLine();
\r
251 AssertEquals("Wrong line", testStr, line);
\r
252 endPos = s.Position;
\r
254 Boolean[] booleans = {true, false};
\r
255 foreach (bool b in booleans ) {
\r
256 Console.WriteLine(b);
\r
257 s.Position = endPos;
\r
258 line = r.ReadLine();
\r
259 AssertEquals("Wrong boolean", b.ToString(), line);
\r
260 endPos = s.Position;
\r
263 Char[] chars = {'a', ';', '?'};
\r
264 foreach (Char c in chars ) {
\r
265 Console.WriteLine(c);
\r
266 s.Position = endPos;
\r
267 line = r.ReadLine();
\r
268 AssertEquals("Wrong char", c.ToString(), line);
\r
269 endPos = s.Position;
\r
272 // TODO - Likewise for char[], decimal, double, int, long, object, single, uint32, uint64
\r
273 // TODO - write with format string
\r