-
Notifications
You must be signed in to change notification settings - Fork 368
/
KernelCommandNestingTests.cs
207 lines (174 loc) · 6.9 KB
/
KernelCommandNestingTests.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// 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.Threading.Tasks;
using FluentAssertions;
using FluentAssertions.Execution;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.CSharp;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.FSharp;
using Microsoft.DotNet.Interactive.Tests.Utility;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.DotNet.Interactive.Tests;
public class KernelCommandNestingTests : LanguageKernelTestBase
{
public KernelCommandNestingTests(ITestOutputHelper output) : base(output)
{
}
public class Kernel_KernelEvents
{
[Fact]
public async Task Commands_sent_within_the_code_of_another_command_publish_error_events_on_CompositeKernel_for_failures()
{
using var kernel = new CompositeKernel
{
new CSharpKernel("cs1"),
new CSharpKernel("cs2")
};
var kernelEvents = kernel.KernelEvents.ToSubscribedList();
var command = new SubmitCode($@"
#!cs1
using {typeof(Kernel).Namespace};
using {typeof(KernelCommand).Namespace};
await Kernel.Root.SendAsync(new SubmitCode(""error"", ""cs2""));
");
await kernel.SendAsync(command);
kernelEvents.Should()
.ContainSingle<ErrorProduced>()
.Which
.Message
.Should()
.Be("(1,1): error CS0103: The name 'error' does not exist in the current context");
}
}
public class KernelCommandResult_Events
{
[Fact]
public async Task Commands_sent_within_the_code_of_another_command_do_not_publish_CommandSucceeded_to_the_outer_result()
{
using var kernel = new CompositeKernel
{
new CSharpKernel("cs1"),
new CSharpKernel("cs2")
};
var command = new SubmitCode(@$"
#!cs1
using {typeof(Kernel).Namespace};
using {typeof(KernelCommand).Namespace};
await Kernel.Root.SendAsync(new SubmitCode(""1+1"", ""cs2""));
");
var result = await kernel.SendAsync(command);
using var _ = new AssertionScope();
result.Events.Should().ContainSingle<CommandSucceeded>(e => e.Command == command);
result.Events.Should()
.NotContain(e =>
e is CommandSucceeded &&
e.Command.TargetKernelName == "cs2");
}
[Fact]
public async Task Commands_sent_within_the_code_of_another_command_do_not_publish_CommandFailed_to_the_outer_result()
{
using var kernel = new CompositeKernel
{
new CSharpKernel("cs1"),
new CSharpKernel("cs2")
};
var command = new SubmitCode($@"
#!cs1
using {typeof(Kernel).Namespace};
using {typeof(KernelCommand).Namespace};
await Kernel.Root.SendAsync(new SubmitCode(""error"", ""cs2""));
");
var result = await kernel.SendAsync(command);
result.Events.Should()
.ContainSingle<CommandSucceeded>(e => e.Command == command);
result.Events
.Should()
.NotContain(e => e is CommandFailed);
}
[Fact]
public async Task Commands_sent_within_the_code_of_another_command_do_not_publish_events_to_the_outer_result()
{
using var kernel = new CompositeKernel
{
new CSharpKernel("cs1"),
new CSharpKernel("cs2")
};
var command = new SubmitCode($"""
using {typeof(Kernel).Namespace};
using {typeof(KernelCommand).Namespace};
var result = await Kernel.Root.SendAsync(new SubmitCode("123.Display();\n456", "cs2"));
""", "cs1");
var result = await kernel.SendAsync(command);
using var _ = new AssertionScope();
result.Events.Should().NotContainErrors();
result.Events.Should().NotContain(e => e is DisplayedValueProduced);
result.Events.Should().NotContain(e => e is ReturnValueProduced);
}
[Fact]
public async Task Commands_sent_via_API_within_a_split_submission_do_not_bubble_events()
{
using var kernel = new CompositeKernel
{
new CSharpKernel("cs1"),
new CSharpKernel("cs2")
};
var command = new SubmitCode($"""
#!cs1
using {typeof(Kernel).Namespace};
using {typeof(KernelCommand).Namespace};
var result = await Kernel.Root.SendAsync(new SubmitCode("123.Display();\n456", "cs2"));
#!cs2
Console.WriteLine(789);
""");
var result = await kernel.SendAsync(command);
using var _ = new AssertionScope();
result.Events.Should().NotContainErrors();
result.Events.Should().NotContain(e => e is DisplayedValueProduced);
result.Events.Should().NotContain(e => e is ReturnValueProduced);
}
[Fact]
public async Task Commands_sent_within_the_code_of_another_command_publish_CommandSucceeded_to_the_inner_result()
{
using var kernel = new CompositeKernel
{
new CSharpKernel(),
new FSharpKernel()
};
kernel.DefaultKernelName = "csharp";
var result = await kernel.SubmitCodeAsync(
@"
using System.Reactive.Linq;
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Commands;
var result = await Kernel.Root.SendAsync(new SubmitCode(""123"", ""fsharp""));
result.Events.Last()
");
result.Events.Should().NotContainErrors();
result.Events
.Should()
.ContainSingle<ReturnValueProduced>(e => e.Value is CommandSucceeded);
}
[Fact]
public async Task Commands_sent_within_the_code_of_another_command_publish_CommandFailed_to_the_inner_result()
{
using var kernel = new CompositeKernel
{
new CSharpKernel("cs1"),
new CSharpKernel("cs2")
};
var result = await kernel.SendAsync(new SubmitCode(
@"
using System.Reactive.Linq;
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Commands;
var result = await Kernel.Root.SendAsync(new SubmitCode(""nope"", ""cs2""));
result.Events.Last()
", "cs1"));
result.Events
.Should()
.ContainSingle<ReturnValueProduced>(e => e.Value is CommandFailed);
}
}
}