Async overloads for AnsiConsole Prompt/Ask/Confirm. (#1194)

* Add async overloads for Prompt/Ask/Confirm.

* Added unit test coverage - AskAsync, ConfirmAsync

* Reordered methods to group non-async/async of the same tests together

---------

Co-authored-by: Frank Ray <52075808+FrankRay78@users.noreply.github.com>
This commit is contained in:
Tom Deseyn
2024-11-28 11:56:19 +01:00
committed by GitHub
parent 92daeb739d
commit 29ab313bb9
3 changed files with 169 additions and 11 deletions

View File

@ -2,19 +2,29 @@ namespace Spectre.Console.Tests.Unit;
public partial class AnsiConsoleTests
{
public sealed class Prompt
public sealed class Confirm
{
[Theory]
[InlineData(true, true)]
[InlineData(false, false)]
public void Should_Return_Default_Value_If_Nothing_Is_Entered(bool expected, bool defaultValue)
[InlineData(true, true, true)]
[InlineData(false, true, true)]
[InlineData(true, false, false)]
[InlineData(false, false, false)]
public async Task Should_Return_Default_Value_If_Nothing_Is_Entered(bool async, bool defaultValue, bool expected)
{
// Given
var console = new TestConsole().EmitAnsiSequences();
console.Input.PushKey(ConsoleKey.Enter);
// When
var result = console.Confirm("Want some prompt?", defaultValue);
bool result;
if (async)
{
result = await console.ConfirmAsync("Want some prompt?", defaultValue);
}
else
{
result = console.Confirm("Want some prompt?", defaultValue);
}
// Then
result.ShouldBe(expected);
@ -23,29 +33,49 @@ public partial class AnsiConsoleTests
public sealed class Ask
{
[Fact]
public void Should_Return_Correct_DateTime_When_Asked_PL_Culture()
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task Should_Return_Correct_DateTime_When_Asked_PL_Culture(bool async)
{
// Given
var console = new TestConsole().EmitAnsiSequences();
console.Input.PushTextWithEnter("1/2/1998");
// When
var dateTime = console.Ask<DateTime>(string.Empty, CultureInfo.GetCultureInfo("pl-PL"));
DateTime dateTime;
if (async)
{
dateTime = await console.AskAsync<DateTime>(string.Empty, CultureInfo.GetCultureInfo("pl-PL"));
}
else
{
dateTime = console.Ask<DateTime>(string.Empty, CultureInfo.GetCultureInfo("pl-PL"));
}
// Then
dateTime.ShouldBe(new DateTime(1998, 2, 1));
}
[Fact]
public void Should_Return_Correct_DateTime_When_Asked_US_Culture()
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task Should_Return_Correct_DateTime_When_Asked_US_Culture(bool async)
{
// Given
var console = new TestConsole().EmitAnsiSequences();
console.Input.PushTextWithEnter("2/1/1998");
// When
var dateTime = console.Ask<DateTime>(string.Empty, CultureInfo.GetCultureInfo("en-US"));
DateTime dateTime;
if (async)
{
dateTime = await console.AskAsync<DateTime>(string.Empty, CultureInfo.GetCultureInfo("en-US"));
}
else
{
dateTime = console.Ask<DateTime>(string.Empty, CultureInfo.GetCultureInfo("en-US"));
}
// Then
dateTime.ShouldBe(new DateTime(1998, 2, 1));