forked from dotnet/interactive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializationTests.cs
More file actions
234 lines (192 loc) · 7.85 KB
/
Copy pathSerializationTests.cs
File metadata and controls
234 lines (192 loc) · 7.85 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// 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.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using Assent;
using FluentAssertions;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.CSharpProject.Commands;
using Microsoft.DotNet.Interactive.CSharpProject.Events;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.Connection;
using Microsoft.DotNet.Interactive.Tests.Utility;
using Microsoft.DotNet.Interactive.ValueSharing;
using Pocket;
using Xunit;
using Xunit.Abstractions;
using System.Text.Encodings.Web;
namespace Microsoft.DotNet.Interactive.CSharpProject.Tests;
public class SerializationTests
{
private readonly ITestOutputHelper _output;
public SerializationTests(ITestOutputHelper output)
{
_output = output;
KernelCommandEnvelope.RegisterCommand<OpenProject>();
KernelCommandEnvelope.RegisterCommand<OpenDocument>();
KernelCommandEnvelope.RegisterCommand<CompileProject>();
KernelEventEnvelope.RegisterEvent<ProjectOpened>();
KernelEventEnvelope.RegisterEvent<DocumentOpened>();
KernelEventEnvelope.RegisterEvent<AssemblyProduced>();
}
[Theory]
[MemberData(nameof(Commands))]
public void All_command_types_are_round_trip_serializable(KernelCommand command)
{
var originalEnvelope = KernelCommandEnvelope.Create(command);
var json = KernelCommandEnvelope.Serialize(originalEnvelope);
_output.WriteLine(json);
var deserializedEnvelope = KernelCommandEnvelope.Deserialize(json);
deserializedEnvelope
.Should()
.BeEquivalentToRespectingRuntimeTypes(
originalEnvelope,
o => o.Excluding(e => e.Command.Properties)
.Excluding(e => e.Command.Handler));
}
[Theory]
[MemberData(nameof(Events))]
public void All_event_types_are_round_trip_serializable(KernelEvent @event)
{
var originalEnvelope = KernelEventEnvelope.Create(@event);
var json = KernelEventEnvelope.Serialize(originalEnvelope);
_output.WriteLine($"{Environment.NewLine}{@event.GetType().Name}: {Environment.NewLine}{json}");
var deserializedEnvelope = KernelEventEnvelope.Deserialize(json);
// ignore these specific properties because they're not serialized
var ignoredProperties = new HashSet<string>
{
$"{nameof(CommandFailed)}.{nameof(CommandFailed.Exception)}",
$"{nameof(DisplayEvent)}.{nameof(DisplayEvent.Value)}",
$"{nameof(ValueProduced)}.{nameof(ValueProduced.Value)}",
$"{nameof(KernelValueInfo)}.{nameof(KernelValueInfo.Type)}",
};
deserializedEnvelope
.Should()
.BeEquivalentToRespectingRuntimeTypes(
originalEnvelope,
o => o.Excluding(envelope => envelope.Event.Command.Properties)
.Excluding(memberInfo => ignoredProperties.Contains($"{memberInfo.DeclaringType.Name}.{memberInfo.Name}"))
);
}
[Theory]
[MemberData(nameof(Commands))]
public void Command_contract_has_not_been_broken(KernelCommand command)
{
var _configuration = new Configuration()
.UsingExtension($"{command.GetType().Name}.json")
.SetInteractive(Debugger.IsAttached);
command.SetToken("the-token");
var json = KernelCommandEnvelope.Serialize(command);
this.Assent(Indent(json), _configuration);
}
[Theory]
[MemberData(nameof(EventsUniqueByType))]
public void Event_contract_has_not_been_broken(KernelEvent @event)
{
var configuration = new Configuration()
.UsingExtension($"{@event.GetType().Name}.json")
.SetInteractive(Debugger.IsAttached);
@event.Command?.SetToken("the-token");
var json = KernelEventEnvelope.Serialize(@event);
this.Assent(Indent(json), configuration);
}
[Fact]
public void All_command_types_are_tested_for_round_trip_serialization()
{
var projectKernelCommands = typeof(CSharpProjectKernel)
.Assembly
.ExportedTypes
.Concrete()
.DerivedFrom(typeof(KernelCommand));
Commands()
.Select(e => e[0].GetType())
.Distinct()
.Should()
.BeEquivalentTo(projectKernelCommands);
}
[Fact]
public void All_event_types_are_tested_for_round_trip_serialization()
{
var projectKernelEvents = typeof(CSharpProjectKernel)
.Assembly
.ExportedTypes
.Concrete()
.DerivedFrom(typeof(KernelEvent));
Events()
.Select(e => e[0].GetType())
.Distinct()
.Should()
.BeEquivalentTo(projectKernelEvents);
}
public static IEnumerable<object[]> Commands()
{
foreach (var command in commands().Select(c =>
{
c.RoutingSlip.StampAsArrived(new Uri("kernel://somelocation/kernelA"));
c.RoutingSlip.StampAsArrived(new Uri("kernel://somelocation/kernelName"));
c.RoutingSlip.Stamp(new Uri("kernel://somelocation/kernelName"));
return c;
}))
{
yield return new object[] { command };
}
IEnumerable<KernelCommand> commands()
{
yield return new CompileProject();
yield return new OpenDocument("path");
yield return new OpenProject(new Project(new[] { new ProjectFile("Program.cs", "// file contents") }));
}
}
public static IEnumerable<object[]> Events()
{
foreach (var @event in events().Select(e =>
{
e.Command.RoutingSlip.StampAsArrived(new Uri("kernel://somelocation/kernelA"));
e.Command.RoutingSlip.StampAsArrived(new Uri("kernel://somelocation/kernelName"));
e.Command.RoutingSlip.Stamp(new Uri("kernel://somelocation/kernelName"));
e.RoutingSlip.Stamp(new Uri("kernel://somelocation/kernelName"));
return e;
}))
{
yield return new object[] { @event };
}
IEnumerable<KernelEvent> events()
{
var compileProject = new CompileProject();
yield return new AssemblyProduced(compileProject, new Base64EncodedAssembly("01020304"));
var openDocument = new OpenDocument("path");
yield return new DocumentOpened(openDocument, new RelativeFilePath("path"), null, "file contents");
yield return new ProjectOpened(
new OpenProject(new Project(new[]
{
new ProjectFile("Program.cs", "#region some-region\n#endregion"),
})),
new[]
{
new ProjectItem("./Program.cs", new[] { "some-region" }, new Dictionary<string, string>{["some-region"] = string.Empty})
});
}
}
public static IEnumerable<object[]> EventsUniqueByType()
{
var dictionary = new Dictionary<Type, KernelEvent>();
foreach (var e in Events().SelectMany(e => e).OfType<KernelEvent>())
{
dictionary[e.GetType()] = e;
}
return dictionary.Values.Select(e => new[] { e });
}
private static string Indent(string json)
{
json = JsonNode.Parse(json).ToJsonString(new JsonSerializerOptions
{
WriteIndented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
});
return json;
}
}