Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit c8028f4

Browse files
committed
Merge pull request #2067 from stephentoub/console_tests
Add more tests for System.Console
2 parents 1348fab + 6179876 commit c8028f4

File tree

9 files changed

+286
-45
lines changed

9 files changed

+286
-45
lines changed

src/System.Console/src/System/IO/SyncTextReader.cs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,23 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using System;
5-
using System.IO;
6-
using System.Text;
7-
using System.Threading;
8-
using System.Threading.Tasks;
9-
using System.Runtime.CompilerServices;
10-
using System.Runtime.InteropServices;
11-
using System.Globalization;
12-
using System.Diagnostics.CodeAnalysis;
4+
using System.Diagnostics;
135
using System.Diagnostics.Contracts;
6+
using System.Runtime.InteropServices;
7+
using System.Threading.Tasks;
148

159
namespace System.IO
1610
{
1711
internal sealed class SyncTextReader : TextReader
1812
{
19-
internal TextReader _in;
13+
private readonly TextReader _in;
2014
private readonly object _methodLock = new object();
2115

22-
public static TextReader GetSynchronizedTextReader(TextReader reader)
16+
public static SyncTextReader GetSynchronizedTextReader(TextReader reader)
2317
{
24-
if (reader == null)
25-
throw new ArgumentNullException("reader");
26-
Contract.Ensures(Contract.Result<TextReader>() != null);
27-
Contract.EndContractBlock();
28-
29-
if (reader is SyncTextReader)
30-
return reader;
31-
32-
return new SyncTextReader(reader);
18+
Debug.Assert(reader != null);
19+
return reader as SyncTextReader ??
20+
new SyncTextReader(reader);
3321
}
3422

3523
internal SyncTextReader(TextReader t)

src/System.Console/src/System/IO/SyncTextWriter.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using System.Diagnostics.CodeAnalysis;
5-
using System.Diagnostics.Contracts;
6-
using System.Runtime.InteropServices;
4+
using System.Diagnostics;
75
using System.Text;
86
using System.Threading.Tasks;
97

@@ -14,15 +12,10 @@ internal sealed class SyncTextWriter : TextWriter, IDisposable
1412
private readonly object _methodLock = new object();
1513
internal readonly TextWriter _out;
1614

17-
internal static TextWriter GetSynchronizedTextWriter(TextWriter writer)
15+
internal static SyncTextWriter GetSynchronizedTextWriter(TextWriter writer)
1816
{
19-
if (writer == null)
20-
throw new ArgumentNullException("writer");
21-
Contract.Ensures(Contract.Result<TextWriter>() != null);
22-
Contract.EndContractBlock();
23-
24-
return writer is SyncTextWriter ?
25-
writer :
17+
Debug.Assert(writer != null);
18+
return writer as SyncTextWriter ??
2619
new SyncTextWriter(writer);
2720
}
2821

src/System.Console/src/System/Text/ConsoleEncoding.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ public override byte[] GetPreamble()
2222
return Array.Empty<byte>();
2323
}
2424

25+
public override int CodePage
26+
{
27+
get { return _encoding.CodePage; }
28+
}
29+
30+
public override bool IsSingleByte
31+
{
32+
get { return _encoding.IsSingleByte; }
33+
}
34+
35+
public override string EncodingName
36+
{
37+
get { return _encoding.EncodingName; }
38+
}
39+
2540
public override string WebName
2641
{
2742
get { return _encoding.WebName; }
@@ -32,6 +47,11 @@ public override int GetByteCount(char[] chars)
3247
return _encoding.GetByteCount(chars);
3348
}
3449

50+
public override unsafe int GetByteCount(char* chars, int count)
51+
{
52+
return _encoding.GetByteCount(chars, count);
53+
}
54+
3555
public override int GetByteCount(char[] chars, int index, int count)
3656
{
3757
return _encoding.GetByteCount(chars, index, count);
@@ -42,6 +62,11 @@ public override int GetByteCount(string s)
4262
return _encoding.GetByteCount(s);
4363
}
4464

65+
public override unsafe int GetBytes(char* chars, int charCount, byte* bytes, int byteCount)
66+
{
67+
return _encoding.GetBytes(chars, charCount, bytes, byteCount);
68+
}
69+
4570
public override byte[] GetBytes(char[] chars)
4671
{
4772
return _encoding.GetBytes(chars);
@@ -67,6 +92,11 @@ public override int GetBytes(string s, int charIndex, int charCount, byte[] byte
6792
return _encoding.GetBytes(s, charIndex, charCount, bytes, byteIndex);
6893
}
6994

95+
public override unsafe int GetCharCount(byte* bytes, int count)
96+
{
97+
return _encoding.GetCharCount(bytes, count);
98+
}
99+
70100
public override int GetCharCount(byte[] bytes)
71101
{
72102
return _encoding.GetCharCount(bytes);
@@ -77,6 +107,11 @@ public override int GetCharCount(byte[] bytes, int index, int count)
77107
return _encoding.GetCharCount(bytes, index, count);
78108
}
79109

110+
public override unsafe int GetChars(byte* bytes, int byteCount, char* chars, int charCount)
111+
{
112+
return _encoding.GetChars(bytes, byteCount, chars, charCount);
113+
}
114+
80115
public override char[] GetChars(byte[] bytes)
81116
{
82117
return _encoding.GetChars(bytes);
@@ -112,9 +147,14 @@ public override int GetMaxCharCount(int byteCount)
112147
return _encoding.GetMaxCharCount(byteCount);
113148
}
114149

150+
public override string GetString(byte[] bytes)
151+
{
152+
return _encoding.GetString(bytes);
153+
}
154+
115155
public override string GetString(byte[] bytes, int index, int count)
116156
{
117157
return _encoding.GetString(bytes, index, count);
118158
}
119159
}
120-
}
160+
}

src/System.Console/tests/CancelKeyPress.cs

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,66 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System;
5+
using System.Runtime.InteropServices;
6+
using System.Threading;
57
using Xunit;
68

79
public class CancelKeyPress
810
{
11+
private const int WaitFailTestTimeoutSeconds = 30;
12+
913
[Fact]
1014
public static void CanAddAndRemoveHandler()
1115
{
12-
ConsoleCancelEventHandler handler = new ConsoleCancelEventHandler(Console_CancelKeyPress);
13-
16+
ConsoleCancelEventHandler handler = (sender, e) =>
17+
{
18+
// We don't actually want to do anything here. This will only get called on the off chance
19+
// that someone CTRL+C's the test run while the handler is hooked up. This is just used to
20+
// validate that we can add and remove a handler, we don't care about exercising it.
21+
};
1422
Console.CancelKeyPress += handler;
1523
Console.CancelKeyPress -= handler;
1624
}
1725

18-
static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
26+
[Fact]
27+
[PlatformSpecific(PlatformID.AnyUnix)]
28+
public static void InvokingCancelKeyPressHandler()
1929
{
20-
// We don't actually want to do anything here. This will only get called on the off chance
21-
// that someone CTRL+C's the test run while the handler is hooked up. This is just used to
22-
// validate that we can add and remove a handler, we don't care about exercising it.
30+
// On Windows we could use GenerateConsoleCtrlEvent to send a ctrl-C to the process,
31+
// however that'll apply to all processes associated with the same group, which will
32+
// include processes like the code coverage tool when doing code coverage runs, causing
33+
// those other processes to exit. As such, we test this only on Unix, where we can
34+
// send a SIGINT signal to this specific process only.
35+
36+
using (var mres = new ManualResetEventSlim())
37+
{
38+
ConsoleCancelEventHandler handler = (sender, e) => {
39+
e.Cancel = true;
40+
mres.Set();
41+
};
42+
43+
Console.CancelKeyPress += handler;
44+
try
45+
{
46+
Assert.Equal(0, kill(getpid(), SIGINT));
47+
Assert.True(mres.Wait(WaitFailTestTimeoutSeconds));
48+
}
49+
finally
50+
{
51+
Console.CancelKeyPress -= handler;
52+
}
53+
}
2354
}
55+
56+
#region Unix Imports
57+
// P/Invokes included here rather than being pulled from Interop\Unix
58+
// to avoid platform-dependent includes in csproj
59+
[DllImport("libc", SetLastError = true)]
60+
private static extern int kill(int pid, int sig);
61+
62+
[DllImport("libc")]
63+
private static extern int getpid();
64+
65+
private const int SIGINT = 2;
66+
#endregion
2467
}

src/System.Console/tests/Color.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,32 @@
99

1010
public class Color
1111
{
12+
[Fact]
13+
public static void InvalidColors()
14+
{
15+
Assert.Throws<ArgumentException>(() => Console.BackgroundColor = (ConsoleColor)42);
16+
Assert.Throws<ArgumentException>(() => Console.ForegroundColor = (ConsoleColor)42);
17+
}
18+
19+
[Fact]
20+
public static void RoundtrippingColor()
21+
{
22+
if (Interop.IsWindows)
23+
{
24+
Console.BackgroundColor = Console.BackgroundColor;
25+
Console.ForegroundColor = Console.ForegroundColor;
26+
// Changing color on Windows doesn't have effect in some testing environments
27+
// when there is no associated console, such as when run under a profiler like
28+
// our code coverage tools, so we don't assert that the change took place and
29+
// simple ensure that getting/setting doesn't throw.
30+
}
31+
else
32+
{
33+
Assert.Throws<PlatformNotSupportedException>(() => Console.BackgroundColor);
34+
Assert.Throws<PlatformNotSupportedException>(() => Console.ForegroundColor);
35+
}
36+
}
37+
1238
[Fact]
1339
[PlatformSpecific(PlatformID.AnyUnix)]
1440
public static void RedirectedOutputDoesNotUseAnsiSequences()

src/System.Console/tests/Helpers.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ public static void SetAndReadHelper(Action<TextWriter> setHelper, Func<TextWrite
1111
{
1212
const string TestString = "Test";
1313

14-
TextWriter oldErrorToRestore = getHelper();
14+
TextWriter oldWriterToRestore = getHelper();
15+
Assert.NotNull(oldWriterToRestore);
16+
1517
try
1618
{
1719
using (MemoryStream memStream = new MemoryStream())
@@ -40,7 +42,7 @@ public static void SetAndReadHelper(Action<TextWriter> setHelper, Func<TextWrite
4042
}
4143
finally
4244
{
43-
setHelper(oldErrorToRestore);
45+
setHelper(oldWriterToRestore);
4446
}
4547
}
4648
}

0 commit comments

Comments
 (0)