Skip to content

Commit d4c6c5d

Browse files
committed
feat: add AskAsync and ConfirmAsync methods with tests for interactive console input
1 parent fb136be commit d4c6c5d

3 files changed

Lines changed: 66 additions & 1 deletion

File tree

src/SpectreConsoleLogSink.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public SpectreConsoleLogSink() : base()
3232
{
3333
DefaultLogMessageWriter = new SpectreConsoleLogMessageWriter<object>(this);
3434

35-
RegisterLogMessageWriter<WidgetPayloadLogMessageWriter>();
35+
RegisterLogMessageWriter<AskConsoleMessageWriter>();
36+
RegisterLogMessageWriter<ConfirmConsoleMessageWriter>();
3637
RegisterLogMessageWriter<ProgressConsoleMessageWriter>();
3738
RegisterLogMessageWriter<StatusConsoleMessageWriter>();
3839
RegisterLogMessageWriter<WidgetPayloadLogMessageWriter>();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Threading.Tasks;
2+
using AwesomeAssertions;
3+
using Spectre.Console.Testing;
4+
using WB.Logging;
5+
using WB.Logging.LogSinks.Console.Spectre;
6+
7+
namespace ExtensionsTests.ILoggerExtensionsTests.AskAsyncMethodTests;
8+
9+
public sealed class TheAskAsyncMethod
10+
{
11+
[Test]
12+
public async Task ShouldAskAQuestion()
13+
{
14+
// Arrange
15+
string input = "42";
16+
TestConsole testConsole = new();
17+
testConsole.Input.PushTextWithEnter(input);
18+
Logger logger = new("test");
19+
logger.AttachSpectreConsole(logSink =>
20+
{
21+
logSink.Console = testConsole;
22+
});
23+
24+
// Act
25+
string result = await logger.AskAsync<string>("What is the answer to the Ultimate Question of Life, The Universe, and Everything?").ConfigureAwait(false);
26+
await logger.FlushAsync().ConfigureAwait(false);
27+
28+
// Assert
29+
result.Should().Be(input, because: "the AskAsync method should return the input provided by the user");
30+
}
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Threading.Tasks;
2+
using AwesomeAssertions;
3+
using Spectre.Console.Testing;
4+
using WB.Logging;
5+
using WB.Logging.LogSinks.Console.Spectre;
6+
7+
namespace ExtensionsTests.ILoggerExtensionsTests.ConfirmAsyncMethodTests;
8+
9+
public sealed class TheConfirmAsyncMethod
10+
{
11+
[Test]
12+
[Arguments(true)]
13+
[Arguments(false)]
14+
public async Task ShouldAskAQuestion(bool answer)
15+
{
16+
// Arrange
17+
string input = answer ? "y" : "n";
18+
TestConsole testConsole = new();
19+
testConsole.Input.PushTextWithEnter(input);
20+
Logger logger = new("test");
21+
logger.AttachSpectreConsole(logSink =>
22+
{
23+
logSink.Console = testConsole;
24+
});
25+
26+
// Act
27+
bool result = await logger.ConfirmAsync("What is the answer to the Ultimate Question of Life, The Universe, and Everything?").ConfigureAwait(false);
28+
await logger.FlushAsync().ConfigureAwait(false);
29+
30+
// Assert
31+
result.Should().Be(answer, because: "the ConfirmAsync method should return the input provided by the user");
32+
}
33+
}

0 commit comments

Comments
 (0)