mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-08-03 02:17:59 +08:00
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:
@ -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));
|
||||
|
Reference in New Issue
Block a user