forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncTextReader.cs
More file actions
221 lines (191 loc) · 6.57 KB
/
SyncTextReader.cs
File metadata and controls
221 lines (191 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.IO;
using System.Text;
using Xunit;
[SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS, "Not supported on Browser, iOS, MacCatalyst, or tvOS.")]
public class SyncTextReader
{
// NOTE: These tests test the underlying SyncTextReader by
// accessing the Console.In TextReader (which in fact is a SyncTextReader).
static readonly string[] s_testLines = new string[] {
"3232 Hello32 Hello 5032 Hello 50 532 Hello 50 5 aTrueaabcdbc1.23123.4561.23439505050System.ObjectHello World",
"32",
"",
"32 Hello",
"",
"32 Hello 50",
"",
"32 Hello 50 5",
"",
"32 Hello 50 5 a",
"",
"True",
"a",
"abcd",
"bc",
"1.23",
"123.456",
"1.234",
"39",
"50",
"50",
"50",
"System.Object",
"Hello World",
};
private static void Test(string content, Action action)
{
TextWriter savedStandardOutput = Console.Out;
TextReader savedStandardInput = Console.In;
try
{
using (MemoryStream memStream = new MemoryStream())
{
// Write the content, but leave the stream open.
using (StreamWriter sw = new StreamWriter(memStream, Encoding.Unicode, 1024, true))
{
sw.Write(content);
sw.Flush();
}
memStream.Seek(0, SeekOrigin.Begin);
using (StreamReader sr = new StreamReader(memStream))
{
Console.SetIn(sr);
action();
}
}
}
finally
{
TextWriter oldWriter = Console.Out;
Console.SetOut(savedStandardOutput);
oldWriter.Dispose();
TextReader oldReader = Console.In;
Console.SetIn(savedStandardInput);
oldReader.Dispose();
}
}
[Fact]
public void ReadToEnd()
{
var expected = string.Join(Environment.NewLine, s_testLines);
Test(expected, () => {
// Given, When
var result = Console.In.ReadToEnd();
// Then
Assert.Equal(expected, result);
Assert.Equal(-1, Console.Read()); // We should be at EOF now.
});
}
[Fact]
public void ReadBlock()
{
var expected = new[] { 'H', 'e', 'l', 'l', 'o' };
Test(new string(expected), () => {
// Given
var buffer = new char[expected.Length];
// When
var result = Console.In.ReadBlock(buffer, 0, 5);
// Then
Assert.Equal(5, result);
Assert.Equal(expected, buffer);
Assert.Equal(-1, Console.Read()); // We should be at EOF now.
});
}
[Fact]
public void Read()
{
var expected = new[] { 'H', 'e', 'l', 'l', 'o' };
Test(new string(expected), () => {
// Given
var buffer = new char[expected.Length];
// When
var result = Console.In.Read(buffer, 0, 5);
// Then
Assert.Equal(5, result);
Assert.Equal(expected, buffer);
Assert.Equal(-1, Console.Read()); // We should be at EOF now.
});
}
[Fact]
public void Peek()
{
const string expected = "ABC";
Test(expected, () => {
foreach (char expectedChar in expected)
{
Assert.Equal(expectedChar, Console.In.Peek());
Assert.Equal(expectedChar, Console.In.Read());
}
});
}
[Fact]
public void ReadToEndAsync()
{
var expected = string.Join(Environment.NewLine, s_testLines);
Test(expected, () => {
// Given, When
var result = Console.In.ReadToEndAsync().Result;
// Then
Assert.Equal(expected, result);
Assert.Equal(-1, Console.Read()); // We should be at EOF now.
});
}
[Fact]
public void ReadBlockAsync()
{
var expected = new[] { 'H', 'e', 'l', 'l', 'o' };
Test(new string(expected), () => {
// Given
var buffer = new char[expected.Length];
// When
var result = Console.In.ReadBlockAsync(buffer, 0, 5).Result;
// Then
Assert.Equal(5, result);
Assert.Equal(expected, buffer);
Assert.Equal(-1, Console.Read()); // We should be at EOF now.
// Invalid args
Assert.Throws<ArgumentNullException>(() => { Console.In.ReadBlockAsync(null, 0, 0); });
Assert.Throws<ArgumentOutOfRangeException>(() => { Console.In.ReadBlockAsync(new char[1], -1, 0); });
Assert.Throws<ArgumentOutOfRangeException>(() => { Console.In.ReadBlockAsync(new char[1], 0, -1); });
AssertExtensions.Throws<ArgumentException>(null, () => { Console.In.ReadBlockAsync(new char[1], 1, 1); });
});
}
[Fact]
public void ReadAsync()
{
var expected = new[] { 'H', 'e', 'l', 'l', 'o' };
Test(new string(expected), () => {
// Given
var buffer = new char[expected.Length];
// When
var result = Console.In.ReadAsync(buffer, 0, 5).Result;
// Then
Assert.Equal(5, result);
Assert.Equal(expected, buffer);
Assert.Equal(-1, Console.Read()); // We should be at EOF now.
// Invalid args
Assert.Throws<ArgumentNullException>(() => { Console.In.ReadAsync(null, 0, 0); });
Assert.Throws<ArgumentOutOfRangeException>(() => { Console.In.ReadAsync(new char[1], -1, 0); });
Assert.Throws<ArgumentOutOfRangeException>(() => { Console.In.ReadAsync(new char[1], 0, -1); });
AssertExtensions.Throws<ArgumentException>(null, () => { Console.In.ReadAsync(new char[1], 1, 1); });
});
}
[Fact]
public void ReadLineAsync()
{
var expected = string.Join(Environment.NewLine, s_testLines);
Test(expected, () => {
for (int i = 0; i < s_testLines.Length; i++)
{
// Given, When
var result = Console.In.ReadLineAsync().Result;
// Then
Assert.Equal(s_testLines[i], result);
}
Assert.Equal(-1, Console.Read());
});
}
}