-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathClientCapabilityProviderTests.cs
189 lines (157 loc) · 8.73 KB
/
ClientCapabilityProviderTests.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using FluentAssertions;
using NSubstitute;
using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.LanguageServer;
using OmniSharp.Extensions.LanguageServer.Protocol;
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
using OmniSharp.Extensions.LanguageServer.Protocol.Server.Capabilities;
using OmniSharp.Extensions.LanguageServer.Server;
using Xunit;
using HandlerCollection = OmniSharp.Extensions.LanguageServer.Server.HandlerCollection;
namespace Lsp.Tests
{
public class ClientCapabilityProviderTests
{
private static readonly Type[] Capabilities = typeof(ClientCapabilities).Assembly.GetTypes()
.Where(x => x.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ConnectedCapability<>)))
.ToArray();
[Theory, MemberData(nameof(AllowSupportedCapabilities))]
public void Should_AllowSupportedCapabilities(IJsonRpcHandler handler, object instance)
{
var textDocumentSyncHandler = TextDocumentSyncHandlerExtensions.With(DocumentSelector.ForPattern("**/*.cs"));
var collection = new HandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue) { textDocumentSyncHandler, handler };
var provider = new ClientCapabilityProvider(collection);
HasHandler(provider, instance).Should().BeTrue();
}
public static IEnumerable<object[]> AllowSupportedCapabilities()
{
return GetItems(Capabilities, type => {
var handlerTypes = GetHandlerTypes(type);
var handler = Substitute.For(handlerTypes.ToArray(), new object[0]);
return new[] { handler, Activator.CreateInstance(typeof(Supports<>).MakeGenericType(type), true, Activator.CreateInstance(type)) };
});
}
[Theory, MemberData(nameof(AllowUnsupportedCapabilities))]
public void Should_AllowUnsupportedCapabilities(IJsonRpcHandler handler, object instance)
{
var textDocumentSyncHandler = TextDocumentSyncHandlerExtensions.With(DocumentSelector.ForPattern("**/*.cs"));
var collection = new HandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue) { textDocumentSyncHandler, handler };
var provider = new ClientCapabilityProvider(collection);
HasHandler(provider, instance).Should().BeTrue();
}
public static IEnumerable<object[]> AllowUnsupportedCapabilities()
{
return GetItems(Capabilities, type => {
var handlerTypes = GetHandlerTypes(type);
var handler = Substitute.For(handlerTypes.ToArray(), new object[0]);
return new[] { handler, Activator.CreateInstance(typeof(Supports<>).MakeGenericType(type), false) };
});
}
[Fact]
public void Should_Invoke_Get_Delegate()
{
// Given
var stub = Substitute.For<Func<IExecuteCommandOptions, ExecuteCommandOptions>>();
var provider = new ClientCapabilityProviderFixture().GetStaticOptions();
// When
provider.Get(stub);
// Then
stub.Received().Invoke(Arg.Any<IExecuteCommandOptions>());
}
[Fact]
public void Should_Invoke_Reduce_Delegate()
{
// Given
var stub = Substitute.For<Func<IEnumerable<IExecuteCommandOptions>, ExecuteCommandOptions>>();
var provider = new ClientCapabilityProviderFixture().GetStaticOptions();
// When
provider.Reduce(stub);
// Then
stub.Received().Invoke(Arg.Any<IEnumerable<IExecuteCommandOptions>>());
}
[Theory, MemberData(nameof(AllowNullSupportsCapabilities))]
public void Should_AllowNullSupportedCapabilities(IJsonRpcHandler handler, object instance)
{
var textDocumentSyncHandler = TextDocumentSyncHandlerExtensions.With(DocumentSelector.ForPattern("**/*.cs"));
var collection = new HandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue) { textDocumentSyncHandler, handler };
var provider = new ClientCapabilityProvider(collection);
HasHandler(provider, instance).Should().BeTrue();
}
public static IEnumerable<object[]> AllowNullSupportsCapabilities()
{
return GetItems(Capabilities, type => {
var handlerTypes = GetHandlerTypes(type);
var handler = Substitute.For(handlerTypes.ToArray(), new object[0]);
return new[] { handler, Activator.CreateInstance(typeof(Supports<>).MakeGenericType(type), true) };
});
}
[Theory, MemberData(nameof(DisallowDynamicSupportsCapabilities))]
public void Should_DisallowDynamicSupportedCapabilities(IJsonRpcHandler handler, object instance)
{
var textDocumentSyncHandler = TextDocumentSyncHandlerExtensions.With(DocumentSelector.ForPattern("**/*.cs"));
var collection = new HandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue) { textDocumentSyncHandler, handler };
var provider = new ClientCapabilityProvider(collection);
HasHandler(provider, instance).Should().BeFalse();
}
public static IEnumerable<object[]> DisallowDynamicSupportsCapabilities()
{
return GetItems(Capabilities, type => {
var handlerTypes = GetHandlerTypes(type);
var handler = Substitute.For(handlerTypes.ToArray(), new object[0]);
var capability = Activator.CreateInstance(type);
if (capability is DynamicCapability dyn) dyn.DynamicRegistration = true;
return new[] { handler, Activator.CreateInstance(typeof(Supports<>).MakeGenericType(type), true, capability) };
});
}
[Fact]
public void Should_Handle_Mixed_Capabilities()
{
var textDocumentSyncHandler = TextDocumentSyncHandlerExtensions.With(DocumentSelector.ForPattern("**/*.cs"));
var codeActionHandler = Substitute.For<ICodeActionHandler>();
var definitionHandler = Substitute.For<IDefinitionHandler>();
var typeDefinitionHandler = Substitute.For<ITypeDefinitionHandler>();
var collection = new HandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue) { textDocumentSyncHandler, codeActionHandler, definitionHandler, typeDefinitionHandler };
var provider = new ClientCapabilityProvider(collection);
var capabilities = new ClientCapabilities() {
TextDocument = new TextDocumentClientCapabilities() {
CodeAction = new Supports<CodeActionCapability>(true, new CodeActionCapability() {
DynamicRegistration = false,
}),
TypeDefinition = new Supports<TypeDefinitionCapability>(true, new TypeDefinitionCapability() {
DynamicRegistration = true,
})
}
};
provider.GetStaticOptions(capabilities.TextDocument.CodeAction).Get<ICodeActionOptions, CodeActionOptions>(CodeActionOptions.Of).Should().NotBeNull();
provider.HasStaticHandler(capabilities.TextDocument.Definition).Should().BeTrue();
provider.HasStaticHandler(capabilities.TextDocument.TypeDefinition).Should().BeFalse();
}
private static bool HasHandler(ClientCapabilityProvider provider, object instance)
{
return (bool)typeof(ClientCapabilityProviderTests).GetTypeInfo()
.GetMethod(nameof(GenericHasHandler), BindingFlags.Static | BindingFlags.NonPublic)
.MakeGenericMethod(instance.GetType().GetTypeInfo().GetGenericArguments()[0]).Invoke(null, new[] { provider, instance });
}
private static bool GenericHasHandler<T>(ClientCapabilityProvider provider, Supports<T> supports)
where T : DynamicCapability, ConnectedCapability<IJsonRpcHandler>
{
return provider.HasStaticHandler(supports);
}
private static IEnumerable<object[]> GetItems<T>(IEnumerable<T> types, Func<T, IEnumerable<object>> func)
{
return types.Select(x => func(x).ToArray());
}
private static IEnumerable<Type> GetHandlerTypes(Type type)
{
return type.GetTypeInfo().ImplementedInterfaces
.Where(x => x.GetTypeInfo().IsGenericType && x.GetTypeInfo().GetGenericTypeDefinition() == typeof(ConnectedCapability<>))
.Select(x => x.GetTypeInfo().GetGenericArguments()[0]);
}
}
}