Skip to content

Commit 5c5974d

Browse files
committed
Split CompletionHandler tests out and clean up
1 parent d679900 commit 5c5974d

File tree

2 files changed

+160
-513
lines changed

2 files changed

+160
-513
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Threading.Tasks;
6+
using Microsoft.Extensions.Logging.Abstractions;
7+
using Microsoft.PowerShell.EditorServices.Handlers;
8+
using Microsoft.PowerShell.EditorServices.Services;
9+
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
10+
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
11+
using Microsoft.PowerShell.EditorServices.Test.Shared;
12+
using Microsoft.PowerShell.EditorServices.Test.Shared.Completion;
13+
using Microsoft.PowerShell.EditorServices.Utility;
14+
using Xunit;
15+
16+
namespace Microsoft.PowerShell.EditorServices.Test.Language
17+
{
18+
[Trait("Category", "Completions")]
19+
public class CompletionHandlerTests : IDisposable
20+
{
21+
private readonly PsesInternalHost psesHost;
22+
private readonly WorkspaceService workspace;
23+
private readonly PsesCompletionHandler completionHandler;
24+
25+
public CompletionHandlerTests()
26+
{
27+
psesHost = PsesHostFactory.Create(NullLoggerFactory.Instance);
28+
workspace = new WorkspaceService(NullLoggerFactory.Instance);
29+
completionHandler = new PsesCompletionHandler(NullLoggerFactory.Instance, psesHost, psesHost, workspace);
30+
}
31+
32+
public void Dispose()
33+
{
34+
psesHost.StopAsync().GetAwaiter().GetResult();
35+
GC.SuppressFinalize(this);
36+
}
37+
38+
private ScriptFile GetScriptFile(ScriptRegion scriptRegion) => workspace.GetFile(TestUtilities.GetSharedPath(scriptRegion.File));
39+
40+
private async Task<CompletionResults> GetCompletionResults(ScriptRegion scriptRegion)
41+
{
42+
return await completionHandler.GetCompletionsInFileAsync(
43+
GetScriptFile(scriptRegion),
44+
scriptRegion.StartLineNumber,
45+
scriptRegion.StartColumnNumber).ConfigureAwait(true);
46+
}
47+
48+
[Fact]
49+
public async Task CompletesCommandInFile()
50+
{
51+
CompletionResults completionResults = await GetCompletionResults(CompleteCommandInFile.SourceDetails).ConfigureAwait(true);
52+
Assert.NotEmpty(completionResults.Completions);
53+
Assert.Equal(CompleteCommandInFile.ExpectedCompletion, completionResults.Completions[0]);
54+
}
55+
56+
[Fact]
57+
public async Task CompletesCommandFromModule()
58+
{
59+
CompletionResults completionResults = await GetCompletionResults(CompleteCommandFromModule.SourceDetails).ConfigureAwait(true);
60+
61+
Assert.NotEmpty(completionResults.Completions);
62+
63+
Assert.Equal(
64+
CompleteCommandFromModule.ExpectedCompletion.CompletionText,
65+
completionResults.Completions[0].CompletionText);
66+
67+
Assert.Equal(
68+
CompleteCommandFromModule.ExpectedCompletion.CompletionType,
69+
completionResults.Completions[0].CompletionType);
70+
71+
Assert.NotNull(completionResults.Completions[0].ToolTipText);
72+
}
73+
74+
[SkippableFact]
75+
public async Task CompletesTypeName()
76+
{
77+
Skip.If(
78+
!VersionUtils.IsNetCore,
79+
"In Windows PowerShell the CommandCompletion fails in the test harness, but works manually.");
80+
81+
CompletionResults completionResults = await GetCompletionResults(CompleteTypeName.SourceDetails).ConfigureAwait(true);
82+
83+
Assert.NotEmpty(completionResults.Completions);
84+
85+
Assert.Equal(
86+
CompleteTypeName.ExpectedCompletion.CompletionText,
87+
completionResults.Completions[0].CompletionText);
88+
89+
Assert.Equal(
90+
CompleteTypeName.ExpectedCompletion.CompletionType,
91+
completionResults.Completions[0].CompletionType);
92+
93+
Assert.NotNull(completionResults.Completions[0].ToolTipText);
94+
}
95+
96+
[Trait("Category", "Completions")]
97+
[SkippableFact]
98+
public async Task CompletesNamespace()
99+
{
100+
Skip.If(
101+
!VersionUtils.IsNetCore,
102+
"In Windows PowerShell the CommandCompletion fails in the test harness, but works manually.");
103+
104+
CompletionResults completionResults = await GetCompletionResults(CompleteNamespace.SourceDetails).ConfigureAwait(true);
105+
106+
Assert.NotEmpty(completionResults.Completions);
107+
108+
Assert.Equal(
109+
CompleteNamespace.ExpectedCompletion.CompletionText,
110+
completionResults.Completions[0].CompletionText);
111+
112+
Assert.Equal(
113+
CompleteNamespace.ExpectedCompletion.CompletionType,
114+
completionResults.Completions[0].CompletionType);
115+
116+
Assert.NotNull(completionResults.Completions[0].ToolTipText);
117+
}
118+
119+
[Fact]
120+
public async Task CompletesVariableInFile()
121+
{
122+
CompletionResults completionResults = await GetCompletionResults(CompleteVariableInFile.SourceDetails).ConfigureAwait(true);
123+
124+
Assert.Single(completionResults.Completions);
125+
126+
Assert.Equal(
127+
CompleteVariableInFile.ExpectedCompletion,
128+
completionResults.Completions[0]);
129+
}
130+
131+
[Fact]
132+
public async Task CompletesAttributeValue()
133+
{
134+
CompletionResults completionResults = await GetCompletionResults(CompleteAttributeValue.SourceDetails).ConfigureAwait(true);
135+
136+
Assert.NotEmpty(completionResults.Completions);
137+
138+
Assert.Equal(
139+
CompleteAttributeValue.ExpectedRange,
140+
completionResults.ReplacedRange);
141+
}
142+
143+
[Fact]
144+
public async Task CompletesFilePath()
145+
{
146+
CompletionResults completionResults = await GetCompletionResults(CompleteFilePath.SourceDetails).ConfigureAwait(true);
147+
148+
Assert.NotEmpty(completionResults.Completions);
149+
150+
// TODO: Since this is a path completion, this test will need to be
151+
// platform specific. Probably something like:
152+
// - Windows: C:\Program
153+
// - macOS: /User
154+
// - Linux: /hom
155+
//Assert.Equal(
156+
// CompleteFilePath.ExpectedRange,
157+
// completionResults.ReplacedRange);
158+
}
159+
}
160+
}

0 commit comments

Comments
 (0)