forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetIn.cs
More file actions
65 lines (58 loc) · 1.77 KB
/
SetIn.cs
File metadata and controls
65 lines (58 loc) · 1.77 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
// 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 Xunit;
//
// System.Console BCL test cases
//
public class SetIn
{
[ConditionalFact(typeof(Helpers), nameof(Helpers.IsConsoleInSupported))]
public static void SetInThrowsOnNull()
{
TextReader savedIn = Console.In;
try
{
Assert.Throws<ArgumentNullException>(() => Console.SetIn(null));
}
finally
{
Console.SetIn(savedIn);
}
}
[ConditionalFact(typeof(Helpers), nameof(Helpers.IsConsoleInSupported))]
public static void SetInReadLine()
{
const string TextStringFormat = "Test {0}";
TextReader oldInToRestore = Console.In;
try
{
using (MemoryStream memStream = new MemoryStream())
{
using (StreamWriter sw = new StreamWriter(memStream))
{
for (int i = 0; i < 20; i++)
{
sw.WriteLine(string.Format(TextStringFormat, i));
}
sw.Flush();
memStream.Seek(0, SeekOrigin.Begin);
using (StreamReader sr = new StreamReader(memStream))
{
Console.SetIn(sr);
Assert.NotNull(Console.In);
for (int i = 0; i < 20; i++)
{
Assert.Equal(string.Format(TextStringFormat, i), Console.ReadLine());
}
}
}
}
}
finally
{
Console.SetIn(oldInToRestore);
}
}
}