forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleStreamTests.cs
More file actions
99 lines (81 loc) · 3.43 KB
/
ConsoleStreamTests.cs
File metadata and controls
99 lines (81 loc) · 3.43 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
// 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 System.Threading;
using System.Threading.Tasks;
using Xunit;
public class ConsoleStreamTests
{
[Fact]
public void WriteToOutputStream_EmptyArray()
{
Stream outStream = Console.OpenStandardOutput();
outStream.Write(new byte[] { }, 0, 0);
}
[ConditionalFact(typeof(Helpers), nameof(Helpers.IsConsoleInSupported))]
public void ReadAsyncRespectsCancellation()
{
Stream inStream = Console.OpenStandardInput();
CancellationTokenSource cts = new CancellationTokenSource();
cts.Cancel();
byte[] buffer = new byte[1024];
Task result = inStream.ReadAsync(buffer, 0, buffer.Length, cts.Token);
Assert.True(result.IsCanceled);
ValueTask<int> valueTaskResult = inStream.ReadAsync(buffer.AsMemory(), cts.Token);
Assert.True(valueTaskResult.IsCanceled);
}
[ConditionalFact(typeof(Helpers), nameof(Helpers.IsConsoleInSupported))]
public void ReadAsyncHandlesInvalidParams()
{
Stream inStream = Console.OpenStandardInput();
byte[] buffer = new byte[1024];
Assert.Throws<ArgumentNullException>(() => { inStream.ReadAsync(null, 0, buffer.Length); });
Assert.Throws<ArgumentOutOfRangeException>(() => { inStream.ReadAsync(buffer, -1, buffer.Length); });
Assert.Throws<ArgumentOutOfRangeException>(() => { inStream.ReadAsync(buffer, 0, buffer.Length + 1); });
}
[Fact]
public void WriteAsyncRespectsCancellation()
{
Stream outStream = Console.OpenStandardOutput();
CancellationTokenSource cts = new CancellationTokenSource();
cts.Cancel();
byte[] bytes = Encoding.ASCII.GetBytes("Hi");
Task result = outStream.WriteAsync(bytes, 0, bytes.Length, cts.Token);
Assert.True(result.IsCanceled);
ValueTask valueTaskResult = outStream.WriteAsync(bytes.AsMemory(), cts.Token);
Assert.True(valueTaskResult.IsCanceled);
}
[Fact]
public void WriteAsyncHandlesInvalidParams()
{
Stream outStream = Console.OpenStandardOutput();
byte[] bytes = Encoding.ASCII.GetBytes("Hi");
Assert.Throws<ArgumentNullException>(() => { outStream.WriteAsync(null, 0, bytes.Length); });
Assert.Throws<ArgumentOutOfRangeException>(() => { outStream.WriteAsync(bytes, -1, bytes.Length); });
Assert.Throws<ArgumentOutOfRangeException>(() => { outStream.WriteAsync(bytes, 0, bytes.Length + 1); });
}
[ConditionalFact(typeof(Helpers), nameof(Helpers.IsConsoleInSupported))]
public void InputCannotWriteAsync()
{
Stream inStream = Console.OpenStandardInput();
byte[] bytes = Encoding.ASCII.GetBytes("Hi");
Assert.Throws<NotSupportedException>(() => { inStream.WriteAsync(bytes, 0, bytes.Length); });
Assert.Throws<NotSupportedException>(() => { inStream.WriteAsync(bytes.AsMemory()); });
}
[Fact]
public void OutputCannotReadAsync()
{
Stream outStream = Console.OpenStandardOutput();
byte[] buffer = new byte[1024];
Assert.Throws<NotSupportedException>(() =>
{
outStream.ReadAsync(buffer, 0, buffer.Length);
});
Assert.Throws<NotSupportedException>(() =>
{
outStream.ReadAsync(buffer.AsMemory());
});
}
}