Skip to content

Commit

Permalink
small test refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsequitur committed Feb 1, 2023
1 parent 0931d22 commit 538bc7c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Xunit;
using Xunit.Abstractions;
using static Microsoft.DotNet.Interactive.Formatting.Tests.Tags;
using static Microsoft.DotNet.Interactive.Jupyter.Tests.RecordingJupyterMessageSender;
using ZeroMQMessage = Microsoft.DotNet.Interactive.Jupyter.Messaging.Message;

namespace Microsoft.DotNet.Interactive.Jupyter.Tests;
Expand Down Expand Up @@ -128,7 +129,7 @@ public async Task does_not_expose_stacktrace_when_code_submission_contains_error
await context.Done().Timeout(5.Seconds());

JupyterMessageSender.PubSubMessages.Should()
.ContainSingle<Protocol.Stream>()
.ContainSingle<Stream>()
.Which
.Text
.Should()
Expand Down Expand Up @@ -314,9 +315,9 @@ public async Task sends_ExecuteReply_message_when_submission_contains_a_language
}

[Theory]
[InlineData("input()", "", "input-value")]
[InlineData("input(\"User: \")", "User: ", "user name")]
[InlineData("await Microsoft.DotNet.Interactive.Kernel.GetInputAsync(\"User: \")", "User: ", "user name")]
[InlineData("input()", "", InputForUnspecifiedPrompt)]
[InlineData($"input(\"{InputPromptForUser}\")", InputPromptForUser, InputForUser)]
[InlineData($"await Microsoft.DotNet.Interactive.Kernel.GetInputAsync(\"{InputPromptForUser}\")", InputPromptForUser, InputForUser)]
public async Task sends_InputRequest_message_when_submission_requests_user_input_in_csharp(string code, string prompt, string expectedDisplayValue)
{
var scheduler = CreateScheduler();
Expand All @@ -334,8 +335,8 @@ public async Task sends_InputRequest_message_when_submission_requests_user_input
}

[Theory]
[InlineData("Read-Host", "", "input-value")]
[InlineData("Read-Host -Prompt User", "User: ", "user name")]
[InlineData("Read-Host", "", InputForUnspecifiedPrompt)]
[InlineData("Read-Host -Prompt User", InputPromptForUser, InputForUser)]
public async Task sends_InputRequest_message_when_submission_requests_user_input_in_powershell(string code, string prompt, string expectedDisplayValue)
{
SetKernelLanguage(Language.PowerShell);
Expand All @@ -349,9 +350,9 @@ public async Task sends_InputRequest_message_when_submission_requests_user_input

JupyterMessageSender.RequestMessages.Should().Contain(r => r.Prompt == prompt && r.Password == false);
JupyterMessageSender.PubSubMessages
.OfType<Protocol.Stream>()
.OfType<Stream>()
.Should()
.Contain(s => s.Name == Protocol.Stream.StandardOutput && s.Text == (expectedDisplayValue + Environment.NewLine));
.Contain(s => s.Name == Stream.StandardOutput && s.Text == (expectedDisplayValue + Environment.NewLine));
}

[Fact]
Expand All @@ -361,13 +362,13 @@ public async Task password_input_should_not_appear_in_diagnostic_logs()
using var _ = LogEvents.Subscribe(e => log.Append(e.ToLogString()));

var scheduler = CreateScheduler();
var request = ZeroMQMessage.Create(new ExecuteRequest("password(\"Password: \")"));
var request = ZeroMQMessage.Create(new ExecuteRequest($"password(\"{InputPromptForPassword}\")"));
var context = new JupyterRequestContext(JupyterMessageSender, request);
await scheduler.Schedule(context);

await context.Done().Timeout(20.Seconds());

log.ToString().Should().NotContain("secret");
log.ToString().Should().NotContain(InputForPassword);
}

[Theory]
Expand All @@ -383,10 +384,12 @@ public async Task sends_InputRequest_message_when_submission_requests_user_passw
await context.Done().Timeout(20.Seconds());

JupyterMessageSender.RequestMessages.Should().Contain(r => r.Prompt == prompt && r.Password);

JupyterMessageSender.PubSubMessages
.OfType<ExecuteResult>()
.Should()
.Contain(dp => (dp.Data["text/html"] as string).RemoveStyleElement() == $"{PlainTextBegin}{typeof(PasswordString).FullName}{PlainTextEnd}");
.OfType<ExecuteResult>()
.Should()
.Contain(dp => (dp.Data["text/html"] as string).Contains($"{SummaryTextBegin}{typeof(PasswordString).FullName}{SummaryTextEnd}"),
because: $" password function returns the {typeof(PasswordString)} instance");
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ public string Send(InputRequest message)

return message.Prompt switch
{
"User: " => "user name",
"Password: " => "secret",
_ => "input-value"
InputPromptForUser => InputForUser,
InputPromptForPassword => InputForPassword,
_ => InputForUnspecifiedPrompt
};
}

public const string InputPromptForUser = "User: ";
public const string InputPromptForPassword = "Password: ";
public const string InputForUser = "user name";
public const string InputForPassword = "secret";
public const string InputForUnspecifiedPrompt = "input-value";
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public static PasswordString password(string prompt = "")
}

var inputReq = new InputRequest(prompt, password: true);
var password1 = context.JupyterMessageSender.Send(inputReq);
var result = new PasswordString(password1);
var password = context.JupyterMessageSender.Send(inputReq);
var result = new PasswordString(password);
return result;
}
}

0 comments on commit 538bc7c

Please sign in to comment.