Skip to content

Commit

Permalink
add test, remove some dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsequitur committed Sep 14, 2023
1 parent 528d756 commit c89ea3a
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 95 deletions.
189 changes: 109 additions & 80 deletions src/Microsoft.DotNet.Interactive.Tests/KernelCommandNestingTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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.Threading.Tasks;
using FluentAssertions;
using FluentAssertions.Execution;
Expand All @@ -20,95 +21,122 @@ public KernelCommandNestingTests(ITestOutputHelper output) : base(output)
{
}

[Fact]
public async Task Commands_sent_within_the_code_of_another_command_do_not_publish_CommandSucceeded_to_the_outer_result()
public class KernelEvents
{
using var kernel = new CompositeKernel
[Fact]
public async Task Commands_sent_within_the_code_of_another_command_publish_error_events_on_CompositeKernel_for_failures()
{
new CSharpKernel ( "cs1" ),
new CSharpKernel ( "cs2" )
};
var kernelEvents = kernel.KernelEvents.ToSubscribedList();
var command = new SubmitCode(@$"
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(""1+1"", ""cs2""));
await Kernel.Root.SendAsync(new SubmitCode(""error"", ""cs2""));
");
await kernel.SendAsync(command);

using var _ = new AssertionScope();
kernelEvents.Should()
.ContainSingle<CommandSucceeded>(e => e.Command == command);
await kernel.SendAsync(command);

kernelEvents.Should()
.NotContain(e =>
e is CommandSucceeded &&
e.Command.TargetKernelName == "cs2");
kernelEvents.Should()
.ContainSingle<ErrorProduced>()
.Which
.Message
.Should()
.Be("(1,1): error CS0103: The name 'error' does not exist in the current context");
}
}

[Fact]
public async Task Commands_sent_within_the_code_of_another_command_do_not_publish_CommandFailed_to_the_outer_result()
public class KernelCommandResultEvents
{
using var kernel = new CompositeKernel
[Fact]
public async Task Commands_sent_within_the_code_of_another_command_do_not_publish_CommandSucceeded_to_the_outer_result()
{
new CSharpKernel ( "cs1" ),
new CSharpKernel ( "cs2" )
};
var kernelEvents = kernel.KernelEvents.ToSubscribedList();
var command = new SubmitCode($@"
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""));
await Kernel.Root.SendAsync(new SubmitCode(""1+1"", ""cs2""));
");
await kernel.SendAsync(command);
var result = await kernel.SendAsync(command);

kernelEvents.Should()
.ContainSingle<CommandSucceeded>(e => e.Command == command);
using var _ = new AssertionScope();

kernelEvents
.Should()
.NotContain(e => e is CommandFailed);
}
result.Events.Should().ContainSingle<CommandSucceeded>(e => e.Command == command);

[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" )
};
result.Events.Should()
.NotContain(e =>
e is CommandSucceeded &&
e.Command.TargetKernelName == "cs2");
}

var command = new SubmitCode($@"
[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<ErrorProduced>()
.Which
.Message
.Should()
.Be("(1,1): error CS0103: The name 'error' does not exist in the current context");
}
var result = await kernel.SendAsync(command);

[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";
result.Events.Should()
.ContainSingle<CommandSucceeded>(e => e.Command == command);

var result = await kernel.SubmitCodeAsync(
@"
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_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;
Expand All @@ -118,24 +146,24 @@ public async Task Commands_sent_within_the_code_of_another_command_publish_Comma
result.Events.Last()
");

result.Events.Should().NotContainErrors();
result.Events.Should().NotContainErrors();

result.Events
.Should()
.ContainSingle<ReturnValueProduced>(e => e.Value is CommandSucceeded);
}
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
[Fact]
public async Task Commands_sent_within_the_code_of_another_command_publish_CommandFailed_to_the_inner_result()
{
new CSharpKernel ( "cs1" ),
new CSharpKernel ( "cs2" )
};

var result = await kernel.SendAsync(new SubmitCode(
@"
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;
Expand All @@ -145,8 +173,9 @@ public async Task Commands_sent_within_the_code_of_another_command_publish_Comma
result.Events.Last()
", "cs1"));

result.Events
.Should()
.ContainSingle<ReturnValueProduced>(e => e.Value is CommandFailed);
result.Events
.Should()
.ContainSingle<ReturnValueProduced>(e => e.Value is CommandFailed);
}
}
}
12 changes: 3 additions & 9 deletions src/Microsoft.DotNet.Interactive/KernelCommandResult.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// 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.Collections.Generic;
Expand All @@ -11,17 +11,11 @@ namespace Microsoft.DotNet.Interactive;
[TypeFormatterSource(typeof(MessageDiagnosticsFormatterSource))]
public class KernelCommandResult
{
private readonly List<KernelEvent> _events;
private readonly List<KernelEvent> _events = new();

public KernelCommandResult(KernelCommand command, IEnumerable<KernelEvent> events = null)
public KernelCommandResult(KernelCommand command)
{
Command = command;

_events = new();
if (events is not null)
{
_events.AddRange(events);
}
}

public KernelCommand Command { get; }
Expand Down
7 changes: 1 addition & 6 deletions src/Microsoft.DotNet.Interactive/KernelInvocationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,12 @@ private void SucceedOrFail(
TryCancel();

IsFailed = true;

}
else
{
if (message is not null)
{
if (command.Parent is null)
{
Publish(new ErrorProduced(message, command), publishOnAmbientContextOnly: true);
}
else if (command.IsSelfOrDescendantOf(Command))
if (command.IsSelfOrDescendantOf(Command))
{
Publish(new ErrorProduced(message, command), publishOnAmbientContextOnly: true);
}
Expand Down

0 comments on commit c89ea3a

Please sign in to comment.