-
Notifications
You must be signed in to change notification settings - Fork 368
/
KernelTests.RegisteringCommandHandlers.cs
94 lines (82 loc) · 3.65 KB
/
KernelTests.RegisteringCommandHandlers.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
// 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 Microsoft.DotNet.Interactive.Connection;
using Microsoft.DotNet.Interactive.Tests.Utility;
using Xunit;
namespace Microsoft.DotNet.Interactive.Tests;
public partial class KernelTests
{
public class RegisteringCommandHandlers
{
[Theory]
[InlineData(true)]
[InlineData(false)]
public void When_command_type_registered_then_kernel_registers_envelope_type_for_serialization(bool withHandler)
{
KernelCommandEnvelope.RegisterDefaults();
using var kernel = new FakeKernel();
if (withHandler)
{
kernel.RegisterCommandHandler<CustomCommandTypes.FirstSubmission.MyCommand>(
(_, _) => Task.CompletedTask);
}
else
{
kernel.RegisterCommandType<CustomCommandTypes.FirstSubmission.MyCommand>();
}
var originalCommand = new CustomCommandTypes.FirstSubmission.MyCommand("xyzzy");
string envelopeJson = KernelCommandEnvelope.Serialize(originalCommand);
var roundTrippedCommandEnvelope = KernelCommandEnvelope.Deserialize(envelopeJson);
roundTrippedCommandEnvelope
.Command
.Should()
.BeOfType<CustomCommandTypes.FirstSubmission.MyCommand>()
.Which
.Info
.Should()
.Be(originalCommand.Info);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void When_command_type_reregistered_with_changed_type_command_then_kernel_registers_updated_envelope_type_for_serialization(bool withHandler)
{
// Notebook authors should be able to develop their custom commands experimentally and progressively,
// so we don't want any "you have to restart your kernel now" situations just because you already
// called RegisterCommandHandler once for a particular command type.
KernelCommandEnvelope.RegisterDefaults();
using var kernel = new FakeKernel();
if (withHandler)
{
kernel.RegisterCommandHandler<CustomCommandTypes.FirstSubmission.MyCommand>(
(_, _) => Task.CompletedTask);
kernel.RegisterCommandHandler<CustomCommandTypes.SecondSubmission.MyCommand>(
(_, _) => Task.CompletedTask);
}
else
{
kernel.RegisterCommandType<CustomCommandTypes.FirstSubmission.MyCommand>();
kernel.RegisterCommandType<CustomCommandTypes.SecondSubmission.MyCommand>();
}
var originalCommand = new CustomCommandTypes.SecondSubmission.MyCommand("xyzzy", 42);
string envelopeJson = KernelCommandEnvelope.Serialize(originalCommand);
var roundTrippedCommandEnvelope = KernelCommandEnvelope.Deserialize(envelopeJson);
roundTrippedCommandEnvelope
.Command
.Should()
.BeOfType<CustomCommandTypes.SecondSubmission.MyCommand>()
.Which
.Info
.Should()
.Be(originalCommand.Info);
roundTrippedCommandEnvelope
.Command
.As<CustomCommandTypes.SecondSubmission.MyCommand>()
.AdditionalProperty
.Should()
.Be(originalCommand.AdditionalProperty);
}
}
}