forked from PowerShell/PowerShellEditorServices
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrepareRenameHandlerTests.cs
More file actions
250 lines (225 loc) · 10.1 KB
/
Copy pathPrepareRenameHandlerTests.cs
File metadata and controls
250 lines (225 loc) · 10.1 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.PowerShell.EditorServices.Handlers;
using Microsoft.PowerShell.EditorServices.Services;
using Microsoft.PowerShell.EditorServices.Test.Shared;
using Newtonsoft.Json.Linq;
using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.LanguageServer.Protocol;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Extensions.LanguageServer.Protocol.Progress;
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
using PowerShellEditorServices.Test.Shared.Refactoring;
using Xunit;
using Xunit.Abstractions;
namespace PowerShellEditorServices.Test.Handlers;
[Trait("Category", "PrepareRename")]
public class PrepareRenameHandlerTests
{
private readonly PrepareRenameHandler testHandler;
public PrepareRenameHandlerTests()
{
WorkspaceService workspace = new(NullLoggerFactory.Instance);
workspace.WorkspaceFolders.Add(new WorkspaceFolder
{
Uri = DocumentUri.FromFileSystemPath(TestUtilities.GetSharedPath("Refactoring"))
});
testHandler = new
(
new RenameService
(
workspace,
new FakeLspSendMessageRequestFacade("I Accept"),
new EmptyConfiguration()
)
{
DisclaimerAcceptedForSession = true //Disables UI prompts
}
);
}
/// <summary>
/// Convert test cases into theory data. This keeps us from needing xunit in the test data project
/// This type has a special ToString to add a data-driven test name which is why we dont convert directly to the param type first
/// </summary>
public static TheoryData<RenameTestTargetSerializable> VariableTestCases()
=> new(RefactorVariableTestCases.TestCases.Select(RenameTestTargetSerializable.FromRenameTestTarget));
public static TheoryData<RenameTestTargetSerializable> FunctionTestCases()
=> new(RefactorFunctionTestCases.TestCases.Select(RenameTestTargetSerializable.FromRenameTestTarget));
[Theory]
[MemberData(nameof(FunctionTestCases))]
public async Task FindsFunction(RenameTestTarget s)
{
PrepareRenameParams testParams = s.ToPrepareRenameParams("Functions");
RangeOrPlaceholderRange? result;
try
{
result = await testHandler.Handle(testParams, CancellationToken.None);
}
catch (HandlerErrorException err)
{
Assert.True(s.ShouldThrow, $"Unexpected HandlerErrorException: {err.Message}");
return;
}
if (s.ShouldFail)
{
Assert.Null(result);
return;
}
Assert.NotNull(result);
Assert.True(result?.DefaultBehavior?.DefaultBehavior);
}
[Theory]
[MemberData(nameof(VariableTestCases))]
public async Task FindsVariable(RenameTestTarget s)
{
PrepareRenameParams testParams = s.ToPrepareRenameParams("Variables");
RangeOrPlaceholderRange? result;
try
{
result = await testHandler.Handle(testParams, CancellationToken.None);
}
catch (HandlerErrorException err)
{
Assert.True(s.ShouldThrow, $"Unexpected HandlerErrorException: {err.Message}");
return;
}
if (s.ShouldFail)
{
Assert.Null(result);
return;
}
Assert.NotNull(result);
Assert.True(result?.DefaultBehavior?.DefaultBehavior);
}
[Fact]
public void GetRegistrationOptionsToleratesOmittedRenameCapability()
{
// Regression for PowerShell/PowerShellEditorServices#2297: when the client's
// initialize omits textDocument.rename, the framework passes a null
// RenameCapability. GetRegistrationOptions must not dereference it -- the
// NullReferenceException hung the initialize handshake. A null capability means
// the client has no prepare support.
RenameRegistrationOptions options = testHandler.GetRegistrationOptions(null!, new());
Assert.NotNull(options);
Assert.False(options.PrepareProvider, "omitted rename capability must not enable PrepareProvider");
}
// TODO: Bad Path Tests (strings, parameters, etc.)
}
public static partial class RenameTestTargetExtensions
{
public static PrepareRenameParams ToPrepareRenameParams(this RenameTestTarget testCase, string baseFolder)
=> new()
{
Position = new ScriptPositionAdapter(Line: testCase.Line, Column: testCase.Column),
TextDocument = new TextDocumentIdentifier
{
Uri = DocumentUri.FromFileSystemPath(
TestUtilities.GetSharedPath($"Refactoring/{baseFolder}/{testCase.FileName}")
)
}
};
}
public class FakeLspSendMessageRequestFacade(string title) : ILanguageServerFacade
{
public async Task<TResponse> SendRequest<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken)
{
if (request is ShowMessageRequestParams)
{
return (TResponse)(object)new MessageActionItem { Title = title };
}
else
{
throw new NotSupportedException();
}
}
public ITextDocumentLanguageServer TextDocument => throw new NotImplementedException();
public INotebookDocumentLanguageServer NotebookDocument => throw new NotImplementedException();
public IClientLanguageServer Client => throw new NotImplementedException();
public IGeneralLanguageServer General => throw new NotImplementedException();
public IWindowLanguageServer Window => throw new NotImplementedException();
public IWorkspaceLanguageServer Workspace => throw new NotImplementedException();
public IProgressManager ProgressManager => throw new NotImplementedException();
public InitializeParams ClientSettings => throw new NotImplementedException();
public InitializeResult ServerSettings => throw new NotImplementedException();
public object GetService(Type serviceType) => throw new NotImplementedException();
public IDisposable Register(Action<ILanguageServerRegistry> registryAction) => throw new NotImplementedException();
public void SendNotification(string method) => throw new NotImplementedException();
public void SendNotification<T>(string method, T @params) => throw new NotImplementedException();
public void SendNotification(IRequest request) => throw new NotImplementedException();
public IResponseRouterReturns SendRequest(string method) => throw new NotImplementedException();
public IResponseRouterReturns SendRequest<T>(string method, T @params) => throw new NotImplementedException();
public bool TryGetRequest(long id, out string method, out TaskCompletionSource<JToken> pendingTask) => throw new NotImplementedException();
}
public class EmptyConfiguration : ConfigurationRoot, ILanguageServerConfiguration, IScopedConfiguration
{
public EmptyConfiguration() : base([]) { }
public bool IsSupported => throw new NotImplementedException();
public ILanguageServerConfiguration AddConfigurationItems(IEnumerable<ConfigurationItem> configurationItems) => throw new NotImplementedException();
public Task<IConfiguration> GetConfiguration(params ConfigurationItem[] items) => throw new NotImplementedException();
public Task<IScopedConfiguration> GetScopedConfiguration(DocumentUri scopeUri, CancellationToken cancellationToken) => Task.FromResult((IScopedConfiguration)this);
public ILanguageServerConfiguration RemoveConfigurationItems(IEnumerable<ConfigurationItem> configurationItems) => throw new NotImplementedException();
public bool TryGetScopedConfiguration(DocumentUri scopeUri, out IScopedConfiguration configuration) => throw new NotImplementedException();
}
public static partial class RenameTestTargetExtensions
{
/// <summary>
/// Extension Method to convert a RenameTestTarget to a RenameParams. Needed because RenameTestTarget is in a separate project.
/// </summary>
public static RenameParams ToRenameParams(this RenameTestTarget testCase, string subPath)
=> new()
{
Position = new ScriptPositionAdapter(Line: testCase.Line, Column: testCase.Column),
TextDocument = new TextDocumentIdentifier
{
Uri = DocumentUri.FromFileSystemPath(
TestUtilities.GetSharedPath($"Refactoring/{subPath}/{testCase.FileName}")
)
},
NewName = testCase.NewName
};
}
/// <summary>
/// This is necessary for the MS test explorer to display the test cases
/// Ref:
/// </summary>
public class RenameTestTargetSerializable : RenameTestTarget, IXunitSerializable
{
public RenameTestTargetSerializable() : base() { }
public void Serialize(IXunitSerializationInfo info)
{
info.AddValue(nameof(FileName), FileName);
info.AddValue(nameof(Line), Line);
info.AddValue(nameof(Column), Column);
info.AddValue(nameof(NewName), NewName);
info.AddValue(nameof(ShouldFail), ShouldFail);
info.AddValue(nameof(ShouldThrow), ShouldThrow);
}
public void Deserialize(IXunitSerializationInfo info)
{
FileName = info.GetValue<string>(nameof(FileName));
Line = info.GetValue<int>(nameof(Line));
Column = info.GetValue<int>(nameof(Column));
NewName = info.GetValue<string>(nameof(NewName));
ShouldFail = info.GetValue<bool>(nameof(ShouldFail));
ShouldThrow = info.GetValue<bool>(nameof(ShouldThrow));
}
public static RenameTestTargetSerializable FromRenameTestTarget(RenameTestTarget t)
=> new RenameTestTargetSerializable()
{
FileName = t.FileName,
Column = t.Column,
Line = t.Line,
NewName = t.NewName,
ShouldFail = t.ShouldFail,
ShouldThrow = t.ShouldThrow
};
}