-
Notifications
You must be signed in to change notification settings - Fork 368
/
VariableSharingWithinMagicCommandsTests.cs
111 lines (87 loc) · 3.29 KB
/
VariableSharingWithinMagicCommandsTests.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
// 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.Threading.Tasks;
using FluentAssertions;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.CSharp;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.Tests.Utility;
using Xunit;
namespace Microsoft.DotNet.Interactive.Tests;
public class VariableSharingWithinMagicCommandsTests : IDisposable
{
private readonly CompositeKernel _kernel;
private string receivedValue = null;
public VariableSharingWithinMagicCommandsTests()
{
_kernel = CreateKernel();
Option<string> valueOption = new("--value");
Command shim = new("#!shim")
{
valueOption
};
shim.SetHandler(
(string value) => receivedValue = value,
valueOption);
_kernel.FindKernelByName("csharp").AddDirective(shim);
}
public void Dispose()
{
_kernel.Dispose();
}
[Fact]
public async Task Magic_commands_can_interpolate_variables_from_the_current_kernel()
{
await _kernel.SendAsync(new SubmitCode("var x = 123;", "csharp"));
var result = await _kernel.SendAsync(new SubmitCode("#!shim --value @x", "csharp"));
result.Events.Should().NotContainErrors();
receivedValue.Should().Be("123");
}
[Fact]
public async Task Magic_commands_can_interpolate_variables_from_a_different_kernel()
{
var valueX = "value from the value kernel";
await _kernel.SendAsync(new SubmitCode($"#!value-kernel --name x\n{valueX}"));
var result = await _kernel.SendAsync(new SubmitCode("#!shim --value @value-kernel:x", "csharp"));
result.Events.Should().NotContainErrors();
receivedValue.Should().Be(valueX);
}
[Fact]
public async Task When_variable_does_not_exist_then_an_error_is_returned()
{
var result = await _kernel.SendAsync(new SubmitCode("#!shim --value @x", "csharp"));
receivedValue.Should().BeNull();
result.Events.Should()
.ContainSingle<CommandFailed>()
.Which
.Message
.Should()
.Contain("Value 'x' not found in kernel csharp");
}
[Fact]
public async Task Language_service_requests_do_not_trigger_value_interpolation()
{
var inputWasRequested = false;
_kernel.RegisterCommandHandler<RequestInput>((input, context) =>
{
inputWasRequested = true;
return Task.CompletedTask;
});
_kernel.SetDefaultTargetKernelNameForCommand(typeof(RequestInput), _kernel.Name);
var code = "#!share @input:stuff";
var result = await _kernel.SendAsync(new RequestCompletions(code, new LinePosition(0, code.Length)));
result.Events.Should().NotContainErrors();
inputWasRequested.Should().BeFalse();
}
private static CompositeKernel CreateKernel() =>
new()
{
new CSharpKernel()
.UseNugetDirective()
.UseKernelHelpers()
.UseValueSharing(),
new KeyValueStoreKernel("value-kernel")
};
}