-
Notifications
You must be signed in to change notification settings - Fork 367
/
InputsWithinMagicCommandsTests.cs
139 lines (112 loc) · 4.13 KB
/
InputsWithinMagicCommandsTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.CommandLine;
using System.IO;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.CSharp;
using Microsoft.DotNet.Interactive.Events;
using Xunit;
namespace Microsoft.DotNet.Interactive.Tests;
public class InputsWithinMagicCommandsTests : IDisposable
{
private readonly CompositeKernel kernel;
private RequestInput _receivedRequestInput = null;
private string _receivedUserInput = null;
private readonly Command _shimCommand;
public InputsWithinMagicCommandsTests()
{
kernel = CreateKernel();
kernel.RegisterCommandHandler<RequestInput>((requestInput, context) =>
{
_receivedRequestInput = requestInput;
context.Publish(new InputProduced("hello!", requestInput));
return Task.CompletedTask;
});
kernel.SetDefaultTargetKernelNameForCommand(typeof(RequestInput), kernel.Name);
var stringOption = new Option<string>("--string");
_shimCommand = new("#!shim")
{
stringOption
};
_shimCommand.SetHandler(context =>
{
_receivedUserInput = context.ParseResult.GetValueForOption(stringOption);
});
kernel.FindKernelByName("csharp").AddDirective(_shimCommand);
}
public void Dispose()
{
kernel.Dispose();
}
[Fact]
public async Task Input_token_in_magic_command_prompts_user_for_input()
{
await kernel.SendAsync(new SubmitCode("#!shim --string @input:input-please", "csharp"));
_receivedRequestInput.IsPassword.Should().BeFalse();
_receivedRequestInput
.Should()
.BeOfType<RequestInput>()
.Which
.Prompt
.Should()
.Be("Please enter a value for field \"input-please\".");
}
[Fact]
public async Task Input_token_in_magic_command_includes_requested_value_name()
{
await kernel.SendAsync(new SubmitCode("#!shim --string @input:input-please", "csharp"));
_receivedRequestInput.IsPassword.Should().BeFalse();
_receivedRequestInput
.Should()
.BeOfType<RequestInput>()
.Which
.ValueName
.Should()
.Be("input-please");
}
[Fact]
public async Task Input_token_in_magic_command_prompts_user_passes_user_input_to_directive__to_handler()
{
await kernel.SendAsync(new SubmitCode("#!shim --string @input:input-please", "csharp"));
_receivedUserInput.Should().Be("hello!");
}
[Fact]
public async Task Input_token_in_magic_command_prompts_user_for_password()
{
await kernel.SendAsync(new SubmitCode("#!shim --string @password:input-please", "csharp"));
_receivedRequestInput.IsPassword.Should().BeTrue();
_receivedRequestInput
.Should()
.BeOfType<RequestInput>()
.Which
.Prompt
.Should()
.Be("Please enter a value for field \"input-please\".");
}
[Fact]
public async Task An_input_type_hint_is_set_for_file_inputs()
{
_shimCommand.Add(new Option<FileInfo>("--file"));
await kernel.SendAsync(new SubmitCode("#!shim --file @input:file-please\n// some more stuff", "csharp"));
_receivedRequestInput.InputTypeHint.Should().Be("file");
}
[Fact]
public async Task Unknown_types_return_type_hint_of_text()
{
_shimCommand.Add(new Option<CompositeKernel>("--unknown"));
await kernel.SendAsync(new SubmitCode("#!shim --file @input:file-please\n// some more stuff", "csharp"));
_receivedRequestInput.InputTypeHint.Should().Be("text");
}
private static CompositeKernel CreateKernel() =>
new()
{
new CSharpKernel()
.UseNugetDirective()
.UseKernelHelpers()
.UseValueSharing(),
new KeyValueStoreKernel()
};
}