From ef3278763ede51acfbb00d0e19aad86609aafef0 Mon Sep 17 00:00:00 2001 From: Hugh Bellamy Date: Tue, 24 Jul 2018 03:50:21 +0100 Subject: [PATCH] Add some Unix console tests (dotnet/corefx#31244) * Add some Unix console tests * Address PR feedback Commit migrated from https://github.com/dotnet/corefx/commit/672255c1de97e2ed2c4791f6526f57e3fdcb883b --- .../System.Console/src/System/ConsolePal.Unix.cs | 2 - src/libraries/System.Console/tests/ReadKey.cs | 14 ++ .../System.Console/tests/WindowAndCursorProps.cs | 238 +++++++++++++++++---- 3 files changed, 215 insertions(+), 39 deletions(-) diff --git a/src/libraries/System.Console/src/System/ConsolePal.Unix.cs b/src/libraries/System.Console/src/System/ConsolePal.Unix.cs index 855aa55..07e2210 100644 --- a/src/libraries/System.Console/src/System/ConsolePal.Unix.cs +++ b/src/libraries/System.Console/src/System/ConsolePal.Unix.cs @@ -240,13 +240,11 @@ namespace System public static int LargestWindowWidth { get { return WindowWidth; } - set { throw new PlatformNotSupportedException(); } } public static int LargestWindowHeight { get { return WindowHeight; } - set { throw new PlatformNotSupportedException(); } } public static int WindowLeft diff --git a/src/libraries/System.Console/tests/ReadKey.cs b/src/libraries/System.Console/tests/ReadKey.cs index 0c7525d..bc3d7d5 100644 --- a/src/libraries/System.Console/tests/ReadKey.cs +++ b/src/libraries/System.Console/tests/ReadKey.cs @@ -37,6 +37,20 @@ public class ReadKey : RemoteExecutorTestBase Assert.Throws(() => new ConsoleKeyInfo('\0', (ConsoleKey)256, false, false, false)); } + [Fact] + [PlatformSpecific(TestPlatforms.AnyUnix)] + public void NumberLock_GetUnix_ThrowsPlatformNotSupportedException() + { + Assert.Throws(() => Console.NumberLock); + } + + [Fact] + [PlatformSpecific(TestPlatforms.AnyUnix)] + public void CapsLock_GetUnix_ThrowsPlatformNotSupportedException() + { + Assert.Throws(() => Console.CapsLock); + } + private static void RunRemote(Func func, ProcessStartInfo psi = null) { var options = new RemoteInvokeOptions(); diff --git a/src/libraries/System.Console/tests/WindowAndCursorProps.cs b/src/libraries/System.Console/tests/WindowAndCursorProps.cs index e10cc1d..b3b41e28 100644 --- a/src/libraries/System.Console/tests/WindowAndCursorProps.cs +++ b/src/libraries/System.Console/tests/WindowAndCursorProps.cs @@ -13,45 +13,59 @@ public class WindowAndCursorProps : RemoteExecutorTestBase { [Fact] [PlatformSpecific(TestPlatforms.AnyUnix)] // Expected behavior specific to Unix - public static void BufferSize_SettingNotSupported() + public static void BufferWidth_GetUnix_ReturnsWindowWidth() { - Assert.Throws(() => Console.BufferWidth = 1); - Assert.Throws(() => Console.BufferHeight = 1); + Assert.Equal(Console.WindowWidth, Console.BufferWidth); } [Fact] [PlatformSpecific(TestPlatforms.AnyUnix)] // Expected behavior specific to Unix - public static void BufferSize_GettingSameAsWindowSize() + public static void BufferWidth_SetUnix_ThrowsPlatformNotSupportedException() { Assert.Throws(() => Console.BufferWidth = 1); - Assert.Throws(() => Console.BufferHeight = 1); + } - Assert.Equal(Console.WindowWidth, Console.BufferWidth); + [Fact] + [PlatformSpecific(TestPlatforms.AnyUnix)] // Expected behavior specific to Unix + public static void BufferHeight_GetUnix_ReturnsWindowHeight() + { Assert.Equal(Console.WindowHeight, Console.BufferHeight); } [Fact] - [PlatformSpecific(TestPlatforms.Windows)] // Expected behavior specific to Windows - public static void WindowWidth_WindowHeight_InvalidSize() + [PlatformSpecific(TestPlatforms.AnyUnix)] // Expected behavior specific to Unix + public static void BufferHeight_SetUnix_ThrowsPlatformNotSupportedException() + { + Assert.Throws(() => Console.BufferHeight = 1); + } + + [Fact] + [PlatformSpecific(TestPlatforms.AnyUnix)] // Expected behavior specific to Unix + public static void SetBufferSize_Unix_ThrowsPlatformNotSupportedException() + { + Assert.Throws(() => Console.SetBufferSize(0, 0)); + } + + [Theory] + [PlatformSpecific(TestPlatforms.Windows)] + [InlineData(0)] + [InlineData(-1)] + public static void WindowWidth_SetInvalid_ThrowsArgumentOutOfRangeException(int value) { if (Console.IsOutputRedirected) { - Assert.Throws(() => Console.WindowWidth = 0); - Assert.Throws(() => Console.WindowHeight = 0); + Assert.Throws(() => Console.WindowWidth = value); } else { - AssertExtensions.Throws("width", () => Console.WindowWidth = 0); - AssertExtensions.Throws("height", () => Console.WindowHeight = 0); + AssertExtensions.Throws("width", () => Console.WindowWidth = value); } } [Fact] [PlatformSpecific(TestPlatforms.AnyUnix)] // Expected behavior specific to Unix - public static void WindowWidth() + public static void WindowWidth_GetUnix_Success() { - Assert.Throws(() => Console.WindowWidth = 100); - // Validate that Console.WindowWidth returns some value in a non-redirected o/p. Helpers.RunInNonRedirectedOutput((data) => Console.WriteLine(Console.WindowWidth)); Helpers.RunInRedirectedOutput((data) => Console.WriteLine(Console.WindowWidth)); @@ -59,10 +73,31 @@ public class WindowAndCursorProps : RemoteExecutorTestBase [Fact] [PlatformSpecific(TestPlatforms.AnyUnix)] // Expected behavior specific to Unix - public static void WindowHeight() + public static void WindowWidth_SetUnix_ThrowsPlatformNotSupportedException() { - Assert.Throws(() => Console.WindowHeight = 100); + Assert.Throws(() => Console.WindowWidth = 100); + } + [Theory] + [PlatformSpecific(TestPlatforms.Windows)] // Expected behavior specific to Windows + [InlineData(0)] + [InlineData(-1)] + public static void WindowHeight_SetInvalid_ThrowsArgumentOutOfRangeException(int value) + { + if (Console.IsOutputRedirected) + { + Assert.Throws(() => Console.WindowHeight = value); + } + else + { + AssertExtensions.Throws("height", () => Console.WindowHeight = value); + } + } + + [Fact] + [PlatformSpecific(TestPlatforms.AnyUnix)] // Expected behavior specific to Unix + public static void WindowHeight_GetUnix_Success() + { // Validate that Console.WindowHeight returns some value in a non-redirected o/p. Helpers.RunInNonRedirectedOutput((data) => Console.WriteLine(Console.WindowHeight)); Helpers.RunInRedirectedOutput((data) => Console.WriteLine(Console.WindowHeight)); @@ -70,11 +105,52 @@ public class WindowAndCursorProps : RemoteExecutorTestBase [Fact] [PlatformSpecific(TestPlatforms.AnyUnix)] // Expected behavior specific to Unix - public static void WindowLeftTop_AnyUnix() + public static void WindowHeight_SetUnix_ThrowsPlatformNotSupportedException() + { + Assert.Throws(() => Console.WindowHeight = 100); + } + + [Fact] + [PlatformSpecific(TestPlatforms.AnyUnix)] // Expected behavior specific to Unix + public static void LargestWindowWidth_UnixGet_ReturnsExpected() + { + Helpers.RunInNonRedirectedOutput((data) => Assert.Equal(Console.WindowWidth, Console.LargestWindowWidth)); + Helpers.RunInRedirectedOutput((data) => Assert.Equal(Console.WindowWidth, Console.LargestWindowWidth)); + } + + [Fact] + [PlatformSpecific(TestPlatforms.AnyUnix)] // Expected behavior specific to Unix + public static void LargestWindowHeight_UnixGet_ReturnsExpected() + { + Helpers.RunInNonRedirectedOutput((data) => Assert.Equal(Console.WindowHeight, Console.LargestWindowHeight)); + Helpers.RunInRedirectedOutput((data) => Assert.Equal(Console.WindowHeight, Console.LargestWindowHeight)); + } + + [Fact] + [PlatformSpecific(TestPlatforms.AnyUnix)] // Expected behavior specific to Unix + public static void WindowLeft_GetUnix_ReturnsZero() { Assert.Equal(0, Console.WindowLeft); - Assert.Equal(0, Console.WindowTop); + } + + [Fact] + [PlatformSpecific(TestPlatforms.AnyUnix)] // Expected behavior specific to Unix + public static void WindowLeft_SetUnix_ThrowsPlatformNotSupportedException() + { Assert.Throws(() => Console.WindowLeft = 0); + } + + [Fact] + [PlatformSpecific(TestPlatforms.AnyUnix)] // Expected behavior specific to Unix + public static void WindowTop_GetUnix_ReturnsZero() + { + Assert.Equal(0, Console.WindowTop); + } + + [Fact] + [PlatformSpecific(TestPlatforms.AnyUnix)] // Expected behavior specific to Unix + public static void WindowTop_SetUnix_ThrowsPlatformNotSupportedException() + { Assert.Throws(() => Console.WindowTop = 0); } @@ -109,20 +185,34 @@ public class WindowAndCursorProps : RemoteExecutorTestBase [Fact] [PlatformSpecific(TestPlatforms.AnyUnix)] // Expected behavior specific to Unix - public static void CursorVisible() + public static void CursorVisible_GetUnix_ThrowsPlatformNotSupportedExeption() { - Assert.Throws(() => { bool unused = Console.CursorVisible; }); + Assert.Throws(() => Console.CursorVisible); + } - // Validate that the Console.CursorVisible does nothing in a redirected stream. - Helpers.RunInRedirectedOutput((data) => { Console.CursorVisible = false; Assert.Equal(0, data.ToArray().Length); }); - Helpers.RunInRedirectedOutput((data) => { Console.CursorVisible = true; Assert.Equal(0, data.ToArray().Length); }); + [Theory] + [PlatformSpecific(TestPlatforms.AnyUnix)] // Expected behavior specific to Unix + [InlineData(true)] + [InlineData(false)] + public static void CursorVisible_SetUnixRedirected_Nop(bool value) + { + Helpers.RunInRedirectedOutput((data) => { + Console.CursorVisible = value; + Assert.Equal(0, data.ToArray().Length); + }); } [Fact] [PlatformSpecific(TestPlatforms.AnyUnix)] // Expected behavior specific to Unix - public static void Title_GetSet_Unix() + public static void Title_GetUnix_ThrowPlatformNotSupportedException() { Assert.Throws(() => Console.Title); + } + + [Fact] + [PlatformSpecific(TestPlatforms.AnyUnix)] // Expected behavior specific to Unix + public static void Title_SetUnix_Success() + { RemoteInvoke(() => { Console.Title = "Title set by unit test"; @@ -134,7 +224,7 @@ public class WindowAndCursorProps : RemoteExecutorTestBase [PlatformSpecific(TestPlatforms.Windows)] [SkipOnTargetFramework(TargetFrameworkMonikers.Uap, "In appcontainer, the stream cannot be opened: there is no Console")] [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "// NETFX does not have the fix https://github.com/dotnet/corefx/pull/28905")] - public static void Title_Get_Windows() + public static void Title_GetWindows_ReturnsNonNull() { Assert.NotNull(Console.Title); } @@ -194,26 +284,26 @@ public class WindowAndCursorProps : RemoteExecutorTestBase } [SkipOnTargetFramework(~TargetFrameworkMonikers.Uap)] // In appcontainer, the stream cannot be opened: there is no Console - public static void Title_Get_Windows_Uap() + public static void Title_GetWindowsUap_ThrowsIOException() { Assert.Throws(() => Console.Title); } [SkipOnTargetFramework(~TargetFrameworkMonikers.Uap)] // In appcontainer, the stream cannot be opened: there is no Console - public static void Title_Set_Windows_Uap(int lengthOfTitle) + public static void Title_SetWindowsUap_ThrowsIOException(int lengthOfTitle) { Assert.Throws(() => Console.Title = "x"); } [Fact] - public static void Title_Set_Windows_Null_ThrowsArgumentNullException() + public static void Title_SetNull_ThrowsArgumentNullException() { AssertExtensions.Throws("value", () => Console.Title = null); } [Fact] [SkipOnTargetFramework(~TargetFrameworkMonikers.NetFramework)] - public static void Title_Set_Windows_GreaterThan24500Chars_ThrowsArgumentOutOfRangeException() + public static void Title_SetGreaterThan24500Chars_ThrowsArgumentOutOfRangeException() { // We don't explicitly throw on Core as this isn't technically correct string newTitle = new string('a', 24501); @@ -222,7 +312,7 @@ public class WindowAndCursorProps : RemoteExecutorTestBase [Fact] [OuterLoop] // makes noise, not very inner-loop friendly - public static void Beep() + public static void Beep_Invoke_Success() { // Nothing to verify; just run the code. Console.Beep(); @@ -231,7 +321,7 @@ public class WindowAndCursorProps : RemoteExecutorTestBase [Fact] [OuterLoop] // makes noise, not very inner-loop friendly [PlatformSpecific(TestPlatforms.Windows)] - public static void BeepWithFrequency() + public static void BeepWithFrequency_Invoke_Success() { // Nothing to verify; just run the code. Console.Beep(800, 200); @@ -264,7 +354,7 @@ public class WindowAndCursorProps : RemoteExecutorTestBase [Fact] [OuterLoop] // clears the screen, not very inner-loop friendly - public static void Clear() + public static void Clear_Invoke_Success() { if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || (!Console.IsInputRedirected && !Console.IsOutputRedirected)) { @@ -274,7 +364,7 @@ public class WindowAndCursorProps : RemoteExecutorTestBase } [Fact] - public static void SetCursorPosition() + public static void SetCursorPosition_Invoke_Success() { if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || (!Console.IsInputRedirected && !Console.IsOutputRedirected)) { @@ -300,7 +390,7 @@ public class WindowAndCursorProps : RemoteExecutorTestBase } [Fact] - public static void GetCursorPosition() + public static void GetCursorPosition_Invoke_ReturnsExpected() { if (!Console.IsInputRedirected && !Console.IsOutputRedirected) { @@ -322,8 +412,76 @@ public class WindowAndCursorProps : RemoteExecutorTestBase } [Fact] + public void CursorLeft_Set_GetReturnsExpected() + { + if (!Console.IsInputRedirected && !Console.IsOutputRedirected) + { + int origLeft = Console.CursorLeft; + + Console.CursorLeft = 10; + Assert.Equal(10, Console.CursorLeft); + + Console.CursorLeft = origLeft; + Assert.Equal(origLeft, Console.CursorLeft); + } + else if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + Assert.Equal(0, Console.CursorLeft); + } + } + + [Theory] + [InlineData(-1)] + [InlineData(short.MaxValue + 1)] + public void CursorLeft_SetInvalid_ThrowsArgumentOutOfRangeException(int value) + { + if (PlatformDetection.IsWindows && Console.IsOutputRedirected) + { + Assert.Throws(() => Console.CursorLeft = value); + } + else + { + AssertExtensions.Throws("left", () => Console.CursorLeft = value); + } + } + + [Fact] + public void CursorTop_Set_GetReturnsExpected() + { + if (!Console.IsInputRedirected && !Console.IsOutputRedirected) + { + int origTop = Console.CursorTop; + + Console.CursorTop = 10; + Assert.Equal(10, Console.CursorTop); + + Console.CursorTop = origTop; + Assert.Equal(origTop, Console.CursorTop); + } + else if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + Assert.Equal(0, Console.CursorTop); + } + } + + [Theory] + [InlineData(-1)] + [InlineData(short.MaxValue + 1)] + public void CursorTop_SetInvalid_ThrowsArgumentOutOfRangeException(int value) + { + if (PlatformDetection.IsWindows & Console.IsOutputRedirected) + { + Assert.Throws(() => Console.CursorTop = value); + } + else + { + AssertExtensions.Throws("top", () => Console.CursorTop = value); + } + } + + [Fact] [PlatformSpecific(TestPlatforms.Windows)] - public void CursorSize_SetGet_ReturnsExpected() + public void CursorSize_Set_GetReturnsExpected() { if (!Console.IsInputRedirected && !Console.IsOutputRedirected) { @@ -351,9 +509,15 @@ public class WindowAndCursorProps : RemoteExecutorTestBase [Fact] [PlatformSpecific(TestPlatforms.AnyUnix)] - public void CursorSize_SetUnix_ThrowsPlatformNotSupportedException() + public void CursorSize_GetUnix_ReturnsExpected() { Assert.Equal(100, Console.CursorSize); + } + + [Fact] + [PlatformSpecific(TestPlatforms.AnyUnix)] + public void CursorSize_SetUnix_ThrowsPlatformNotSupportedException() + { Assert.Throws(() => Console.CursorSize = 1); } -- 2.7.4