This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Mocks.cs
255 lines (207 loc) · 7.97 KB
/
Mocks.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#nullable enable
using Microsoft.Extensions.Options;
using Microsoft.Jupyter.Core;
using Microsoft.Jupyter.Core.Protocol;
using Microsoft.Quantum.IQSharp;
using NuGet.Packaging.Core;
using NuGet.Versioning;
using Newtonsoft.Json.Linq;
using NuGet.Protocol.Core.Types;
using NuGet.Configuration;
using System.IO;
// We allow for unused properties and events in this file, as some properties
// must exist for mocking purposes but are not used in this context.
#pragma warning disable CS0067
namespace Tests.IQSharp
{
public class MockKernelOptions : IOptions<KernelContext>
{
public KernelContext Value => new KernelContext();
}
public class MockNugetOptions : IOptions<NugetPackages.Settings>
{
public MockNugetOptions(string[] versions)
{
this.Value = new NugetPackages.Settings()
{
DefaultPackageVersions = versions
};
}
public NugetPackages.Settings Value { get; }
}
public class MockShell : IShellServer
{
public event Action<Message>? KernelInfoRequest;
public event Action<Message>? ExecuteRequest;
public event Action<Message>? ShutdownRequest;
internal void Handle(Message message)
{
(message.Header.MessageType switch {
"kernel_info_request" => KernelInfoRequest,
"execute_request" => ExecuteRequest,
"shutdown_request" => ShutdownRequest,
_ => null
})?.Invoke(message);
}
public void SendIoPubMessage(Message message)
{
throw new NotImplementedException();
}
public void SendShellMessage(Message message)
{
throw new NotImplementedException();
}
public void Start()
{
throw new NotImplementedException();
}
}
public class MockShellRouter : IShellRouter
{
private MockShell shell;
public MockShellRouter(MockShell shell)
{
this.shell = shell;
}
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
public async Task Handle(Message message)
{
shell.Handle(message);
}
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
public void RegisterFallback(Func<Message, Task?> fallback)
{
throw new NotImplementedException();
}
public void RegisterHandler(string messageType, Func<Message, Task?> handler)
{
}
public void RegisterHandlers<TAssembly>()
{
}
}
public class MockCommsRouter : ICommsRouter
{
private MockShell shell;
public MockCommsRouter(MockShell shell)
{
this.shell = shell;
}
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
public async Task<ICommSession> OpenSession(string targetName, object? data) =>
new MockCommSession();
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
public ICommSessionOpen SessionOpenEvent(string targetName) =>
new MockCommSessionOpen();
}
internal class MockCommSessionOpen : ICommSessionOpen
{
public event Func<ICommSession, JToken, Task>? On;
}
internal class MockCommSession : ICommSession
{
public bool IsValid { get; private set; } = true;
private readonly string id = Guid.NewGuid().ToString();
public string Id => id;
public event Func<CommMessageContent, Task>? OnMessage;
public event Func<CommSessionClosedBy, Task>? OnClose;
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
public async Task Close()
{
IsValid = false;
}
public async Task SendMessage(object contents)
{ }
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
}
public class MockUpdatableDisplay : IUpdatableDisplay
{
public void Update(object displayable)
{
}
}
public class MockChannel : IChannel
{
public List<string> errors = new List<string>();
public List<string> msgs = new List<string>();
public List<Message> iopubMessages = new List<Message>();
public ImmutableList<object> Displays { get; private set; } = ImmutableList<object>.Empty;
private readonly ICommsRouter mockRouter = new MockCommsRouter(new MockShell());
public ICommsRouter CommsRouter => mockRouter;
public void Display(object displayable)
{
Microsoft.VisualStudio.TestTools.UnitTesting.Logging.Logger.LogMessage("[III] {0}", displayable);
Displays = Displays.Add(displayable);
}
public IUpdatableDisplay DisplayUpdatable(object displayable)
{
return new MockUpdatableDisplay();
}
public void SendIoPubMessage(Message message) => iopubMessages.Add(message);
public void Stderr(string message)
{
Microsoft.VisualStudio.TestTools.UnitTesting.Logging.Logger.LogMessage("[EEE] {0}", message);
errors.Add(message);
}
public void Stdout(string message)
{
Microsoft.VisualStudio.TestTools.UnitTesting.Logging.Logger.LogMessage("[<--] {0}", message);
msgs.Add(message);
}
}
public class MockOperationResolver : IOperationResolver
{
public OperationInfo Resolve(string input)
{
return new OperationInfo(null, null);
}
}
public class MockNugetPackages : INugetPackages
{
private static readonly AssemblyInfo MockChemistryAssembly = new AssemblyInfo(typeof(Mock.Chemistry.JordanWignerEncodingData).Assembly);
private static readonly AssemblyInfo MockStandardAssembly = new AssemblyInfo(typeof(Mock.Standard.ApplyToEach<QubitState>).Assembly);
List<PackageIdentity> _items = new List<PackageIdentity>();
public IEnumerable<PackageIdentity> Items => _items;
public IEnumerable<AssemblyInfo> Assemblies
{
get
{
var packageIds = _items.Select(p => p.Id);
if (packageIds.Contains("mock.chemistry"))
{
yield return MockChemistryAssembly;
}
else if (packageIds.Contains("mock.standard"))
{
yield return MockStandardAssembly;
}
}
}
public IReadOnlyDictionary<string, NuGetVersion> DefaultVersions => new Dictionary<string, NuGetVersion>();
private readonly Lazy<SourceRepository> globalPackagesSource = new(() =>
Repository.CreateSource(
Repository.Provider.GetCoreV3(),
SettingsUtility.GetGlobalPackagesFolder(
NuGet.Configuration.Settings.LoadDefaultSettings(Directory.GetCurrentDirectory())
)
)
);
public SourceRepository GlobalPackagesSource => globalPackagesSource.Value;
public Task<PackageIdentity> Add(string package, Action<string>? statusCallback = null)
{
if (package == "microsoft.invalid.quantum")
{
throw new NuGet.Resolver.NuGetResolverInputException($"Unable to find package 'microsoft.invalid.quantum'");
}
var pkg = new PackageIdentity(package, NuGetVersion.Parse("0.0.0"));
_items.Add(pkg);
return Task.FromResult(pkg);
}
public async Task<IEnumerable<SourcePackageDependencyInfo>> Get(PackageIdentity pkgId, Action<string>? statusCallback = null)
{
return Enumerable.Empty<SourcePackageDependencyInfo>();
}
}
}