-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathCompletionHandlerTests.cs
135 lines (122 loc) · 6.04 KB
/
CompletionHandlerTests.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.PowerShell.EditorServices.Handlers;
using Microsoft.PowerShell.EditorServices.Services;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
using Microsoft.PowerShell.EditorServices.Test;
using Microsoft.PowerShell.EditorServices.Test.Shared;
using Microsoft.PowerShell.EditorServices.Test.Shared.Completion;
using Microsoft.PowerShell.EditorServices.Utility;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using Xunit;
namespace PowerShellEditorServices.Test.Language
{
[Trait("Category", "Completions")]
public class CompletionHandlerTests : IDisposable
{
private readonly PsesInternalHost psesHost;
private readonly WorkspaceService workspace;
private readonly PsesCompletionHandler completionHandler;
public CompletionHandlerTests()
{
psesHost = PsesHostFactory.Create(NullLoggerFactory.Instance);
workspace = new WorkspaceService(NullLoggerFactory.Instance);
completionHandler = new PsesCompletionHandler(NullLoggerFactory.Instance, psesHost, psesHost, workspace);
}
public void Dispose()
{
#pragma warning disable VSTHRD002
psesHost.StopAsync().Wait();
#pragma warning restore VSTHRD002
GC.SuppressFinalize(this);
}
private ScriptFile GetScriptFile(ScriptRegion scriptRegion) => workspace.GetFile(TestUtilities.GetSharedPath(scriptRegion.File));
private Task<CompletionResults> GetCompletionResultsAsync(ScriptRegion scriptRegion)
{
return completionHandler.GetCompletionsInFileAsync(
GetScriptFile(scriptRegion),
scriptRegion.StartLineNumber,
scriptRegion.StartColumnNumber,
CancellationToken.None);
}
[Fact]
public async Task CompletesCommandInFile()
{
(_, IEnumerable<CompletionItem> results) = await GetCompletionResultsAsync(CompleteCommandInFile.SourceDetails).ConfigureAwait(true);
CompletionItem actual = Assert.Single(results);
Assert.Equal(CompleteCommandInFile.ExpectedCompletion, actual);
}
[Fact]
public async Task CompletesCommandFromModule()
{
(_, IEnumerable<CompletionItem> results) = await GetCompletionResultsAsync(CompleteCommandFromModule.SourceDetails).ConfigureAwait(true);
CompletionItem actual = Assert.Single(results);
// NOTE: The tooltip varies across PowerShell and OS versions, so we ignore it.
Assert.Equal(CompleteCommandFromModule.ExpectedCompletion, actual with { Detail = "" });
Assert.StartsWith(CompleteCommandFromModule.GetRandomDetail, actual.Detail);
}
[Fact]
public async Task CompletesTypeName()
{
(_, IEnumerable<CompletionItem> results) = await GetCompletionResultsAsync(CompleteTypeName.SourceDetails).ConfigureAwait(true);
CompletionItem actual = Assert.Single(results);
if (VersionUtils.IsNetCore)
{
Assert.Equal(CompleteTypeName.ExpectedCompletion, actual);
}
else
{
// Windows PowerShell shows ArrayList as a Class.
Assert.Equal(CompleteTypeName.ExpectedCompletion with
{
Kind = CompletionItemKind.Class,
Detail = "System.Collections.ArrayList"
}, actual);
}
}
[Trait("Category", "Completions")]
[Fact]
public async Task CompletesNamespace()
{
(_, IEnumerable<CompletionItem> results) = await GetCompletionResultsAsync(CompleteNamespace.SourceDetails).ConfigureAwait(true);
CompletionItem actual = Assert.Single(results);
Assert.Equal(CompleteNamespace.ExpectedCompletion, actual);
}
[Fact]
public async Task CompletesVariableInFile()
{
(_, IEnumerable<CompletionItem> results) = await GetCompletionResultsAsync(CompleteVariableInFile.SourceDetails).ConfigureAwait(true);
CompletionItem actual = Assert.Single(results);
Assert.Equal(CompleteVariableInFile.ExpectedCompletion, actual);
}
[Fact]
public async Task CompletesAttributeValue()
{
(_, IEnumerable<CompletionItem> results) = await GetCompletionResultsAsync(CompleteAttributeValue.SourceDetails).ConfigureAwait(true);
// NOTE: Since the completions come through un-ordered from PowerShell, their SortText
// (which has an index prepended from the original order) will mis-match our assumed
// order; hence we ignore it.
Assert.Collection(results.OrderBy(c => c.Label),
actual => Assert.Equal(actual with { Data = null, SortText = null }, CompleteAttributeValue.ExpectedCompletion1),
actual => Assert.Equal(actual with { Data = null, SortText = null }, CompleteAttributeValue.ExpectedCompletion2),
actual => Assert.Equal(actual with { Data = null, SortText = null }, CompleteAttributeValue.ExpectedCompletion3));
}
[Fact]
public async Task CompletesFilePath()
{
(_, IEnumerable<CompletionItem> results) = await GetCompletionResultsAsync(CompleteFilePath.SourceDetails).ConfigureAwait(true);
Assert.NotEmpty(results);
CompletionItem actual = results.First();
// Paths are system dependent so we ignore the text and just check the type and range.
Assert.Equal(actual.TextEdit.TextEdit with { NewText = "" }, CompleteFilePath.ExpectedEdit);
Assert.All(results, r => Assert.True(r.Kind is CompletionItemKind.File or CompletionItemKind.Folder));
}
}
}