Skip to content

Commit c89ea3a

Browse files
committed
add test, remove some dead code
1 parent 528d756 commit c89ea3a

File tree

3 files changed

+113
-95
lines changed

3 files changed

+113
-95
lines changed
Lines changed: 109 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using System;
45
using System.Threading.Tasks;
56
using FluentAssertions;
67
using FluentAssertions.Execution;
@@ -20,95 +21,122 @@ public KernelCommandNestingTests(ITestOutputHelper output) : base(output)
2021
{
2122
}
2223

23-
[Fact]
24-
public async Task Commands_sent_within_the_code_of_another_command_do_not_publish_CommandSucceeded_to_the_outer_result()
24+
public class KernelEvents
2525
{
26-
using var kernel = new CompositeKernel
26+
[Fact]
27+
public async Task Commands_sent_within_the_code_of_another_command_publish_error_events_on_CompositeKernel_for_failures()
2728
{
28-
new CSharpKernel ( "cs1" ),
29-
new CSharpKernel ( "cs2" )
30-
};
31-
var kernelEvents = kernel.KernelEvents.ToSubscribedList();
32-
var command = new SubmitCode(@$"
29+
using var kernel = new CompositeKernel
30+
{
31+
new CSharpKernel("cs1"),
32+
new CSharpKernel("cs2")
33+
};
34+
var kernelEvents = kernel.KernelEvents.ToSubscribedList();
35+
var command = new SubmitCode($@"
3336
#!cs1
3437
using {typeof(Kernel).Namespace};
3538
using {typeof(KernelCommand).Namespace};
36-
await Kernel.Root.SendAsync(new SubmitCode(""1+1"", ""cs2""));
39+
await Kernel.Root.SendAsync(new SubmitCode(""error"", ""cs2""));
3740
");
38-
await kernel.SendAsync(command);
3941

40-
using var _ = new AssertionScope();
41-
kernelEvents.Should()
42-
.ContainSingle<CommandSucceeded>(e => e.Command == command);
42+
await kernel.SendAsync(command);
4343

44-
kernelEvents.Should()
45-
.NotContain(e =>
46-
e is CommandSucceeded &&
47-
e.Command.TargetKernelName == "cs2");
44+
kernelEvents.Should()
45+
.ContainSingle<ErrorProduced>()
46+
.Which
47+
.Message
48+
.Should()
49+
.Be("(1,1): error CS0103: The name 'error' does not exist in the current context");
50+
}
4851
}
4952

50-
[Fact]
51-
public async Task Commands_sent_within_the_code_of_another_command_do_not_publish_CommandFailed_to_the_outer_result()
53+
public class KernelCommandResultEvents
5254
{
53-
using var kernel = new CompositeKernel
55+
[Fact]
56+
public async Task Commands_sent_within_the_code_of_another_command_do_not_publish_CommandSucceeded_to_the_outer_result()
5457
{
55-
new CSharpKernel ( "cs1" ),
56-
new CSharpKernel ( "cs2" )
57-
};
58-
var kernelEvents = kernel.KernelEvents.ToSubscribedList();
59-
var command = new SubmitCode($@"
58+
using var kernel = new CompositeKernel
59+
{
60+
new CSharpKernel("cs1"),
61+
new CSharpKernel("cs2")
62+
};
63+
var command = new SubmitCode(@$"
6064
#!cs1
6165
using {typeof(Kernel).Namespace};
6266
using {typeof(KernelCommand).Namespace};
63-
await Kernel.Root.SendAsync(new SubmitCode(""error"", ""cs2""));
67+
await Kernel.Root.SendAsync(new SubmitCode(""1+1"", ""cs2""));
6468
");
65-
await kernel.SendAsync(command);
69+
var result = await kernel.SendAsync(command);
6670

67-
kernelEvents.Should()
68-
.ContainSingle<CommandSucceeded>(e => e.Command == command);
71+
using var _ = new AssertionScope();
6972

70-
kernelEvents
71-
.Should()
72-
.NotContain(e => e is CommandFailed);
73-
}
73+
result.Events.Should().ContainSingle<CommandSucceeded>(e => e.Command == command);
7474

75-
[Fact]
76-
public async Task Commands_sent_within_the_code_of_another_command_publish_error_events_on_CompositeKernel_for_failures()
77-
{
78-
using var kernel = new CompositeKernel
79-
{
80-
new CSharpKernel ( "cs1" ),
81-
new CSharpKernel ( "cs2" )
82-
};
75+
result.Events.Should()
76+
.NotContain(e =>
77+
e is CommandSucceeded &&
78+
e.Command.TargetKernelName == "cs2");
79+
}
8380

84-
var command = new SubmitCode($@"
81+
[Fact]
82+
public async Task Commands_sent_within_the_code_of_another_command_do_not_publish_CommandFailed_to_the_outer_result()
83+
{
84+
using var kernel = new CompositeKernel
85+
{
86+
new CSharpKernel("cs1"),
87+
new CSharpKernel("cs2")
88+
};
89+
var command = new SubmitCode($@"
8590
#!cs1
8691
using {typeof(Kernel).Namespace};
8792
using {typeof(KernelCommand).Namespace};
8893
await Kernel.Root.SendAsync(new SubmitCode(""error"", ""cs2""));
8994
");
90-
var result = await kernel.SendAsync(command);
91-
92-
result.Events.Should()
93-
.ContainSingle<ErrorProduced>()
94-
.Which
95-
.Message
96-
.Should()
97-
.Be("(1,1): error CS0103: The name 'error' does not exist in the current context");
98-
}
95+
var result = await kernel.SendAsync(command);
9996

100-
[Fact]
101-
public async Task Commands_sent_within_the_code_of_another_command_publish_CommandSucceeded_to_the_inner_result()
102-
{
103-
using var kernel = new CompositeKernel
104-
{
105-
new CSharpKernel(),
106-
new FSharpKernel()
107-
};
108-
kernel.DefaultKernelName = "csharp";
97+
result.Events.Should()
98+
.ContainSingle<CommandSucceeded>(e => e.Command == command);
10999

110-
var result = await kernel.SubmitCodeAsync(
111-
@"
100+
result.Events
101+
.Should()
102+
.NotContain(e => e is CommandFailed);
103+
}
104+
105+
[Fact]
106+
public async Task Commands_sent_within_the_code_of_another_command_do_not_publish_events_to_the_outer_result()
107+
{
108+
using var kernel = new CompositeKernel
109+
{
110+
new CSharpKernel("cs1"),
111+
new CSharpKernel("cs2")
112+
};
113+
114+
var command = new SubmitCode($"""
115+
using {typeof(Kernel).Namespace};
116+
using {typeof(KernelCommand).Namespace};
117+
var result = await Kernel.Root.SendAsync(new SubmitCode("123.Display();\n456", "cs2"));
118+
""", "cs1");
119+
120+
var result = await kernel.SendAsync(command);
121+
122+
using var _ = new AssertionScope();
123+
result.Events.Should().NotContainErrors();
124+
result.Events.Should().NotContain(e => e is DisplayedValueProduced);
125+
result.Events.Should().NotContain(e => e is ReturnValueProduced);
126+
}
127+
128+
[Fact]
129+
public async Task Commands_sent_within_the_code_of_another_command_publish_CommandSucceeded_to_the_inner_result()
130+
{
131+
using var kernel = new CompositeKernel
132+
{
133+
new CSharpKernel(),
134+
new FSharpKernel()
135+
};
136+
kernel.DefaultKernelName = "csharp";
137+
138+
var result = await kernel.SubmitCodeAsync(
139+
@"
112140
using System.Reactive.Linq;
113141
using Microsoft.DotNet.Interactive;
114142
using Microsoft.DotNet.Interactive.Commands;
@@ -118,24 +146,24 @@ public async Task Commands_sent_within_the_code_of_another_command_publish_Comma
118146
result.Events.Last()
119147
");
120148

121-
result.Events.Should().NotContainErrors();
149+
result.Events.Should().NotContainErrors();
122150

123-
result.Events
124-
.Should()
125-
.ContainSingle<ReturnValueProduced>(e => e.Value is CommandSucceeded);
126-
}
151+
result.Events
152+
.Should()
153+
.ContainSingle<ReturnValueProduced>(e => e.Value is CommandSucceeded);
154+
}
127155

128-
[Fact]
129-
public async Task Commands_sent_within_the_code_of_another_command_publish_CommandFailed_to_the_inner_result()
130-
{
131-
using var kernel = new CompositeKernel
156+
[Fact]
157+
public async Task Commands_sent_within_the_code_of_another_command_publish_CommandFailed_to_the_inner_result()
132158
{
133-
new CSharpKernel ( "cs1" ),
134-
new CSharpKernel ( "cs2" )
135-
};
136-
137-
var result = await kernel.SendAsync(new SubmitCode(
138-
@"
159+
using var kernel = new CompositeKernel
160+
{
161+
new CSharpKernel("cs1"),
162+
new CSharpKernel("cs2")
163+
};
164+
165+
var result = await kernel.SendAsync(new SubmitCode(
166+
@"
139167
using System.Reactive.Linq;
140168
using Microsoft.DotNet.Interactive;
141169
using Microsoft.DotNet.Interactive.Commands;
@@ -145,8 +173,9 @@ public async Task Commands_sent_within_the_code_of_another_command_publish_Comma
145173
result.Events.Last()
146174
", "cs1"));
147175

148-
result.Events
149-
.Should()
150-
.ContainSingle<ReturnValueProduced>(e => e.Value is CommandFailed);
176+
result.Events
177+
.Should()
178+
.ContainSingle<ReturnValueProduced>(e => e.Value is CommandFailed);
179+
}
151180
}
152181
}

src/Microsoft.DotNet.Interactive/KernelCommandResult.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation and contributors. All rights reserved.
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System.Collections.Generic;
@@ -11,17 +11,11 @@ namespace Microsoft.DotNet.Interactive;
1111
[TypeFormatterSource(typeof(MessageDiagnosticsFormatterSource))]
1212
public class KernelCommandResult
1313
{
14-
private readonly List<KernelEvent> _events;
14+
private readonly List<KernelEvent> _events = new();
1515

16-
public KernelCommandResult(KernelCommand command, IEnumerable<KernelEvent> events = null)
16+
public KernelCommandResult(KernelCommand command)
1717
{
1818
Command = command;
19-
20-
_events = new();
21-
if (events is not null)
22-
{
23-
_events.AddRange(events);
24-
}
2519
}
2620

2721
public KernelCommand Command { get; }

src/Microsoft.DotNet.Interactive/KernelInvocationContext.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,12 @@ private void SucceedOrFail(
157157
TryCancel();
158158

159159
IsFailed = true;
160-
161160
}
162161
else
163162
{
164163
if (message is not null)
165164
{
166-
if (command.Parent is null)
167-
{
168-
Publish(new ErrorProduced(message, command), publishOnAmbientContextOnly: true);
169-
}
170-
else if (command.IsSelfOrDescendantOf(Command))
165+
if (command.IsSelfOrDescendantOf(Command))
171166
{
172167
Publish(new ErrorProduced(message, command), publishOnAmbientContextOnly: true);
173168
}

0 commit comments

Comments
 (0)