Skip to content

Commit c263982

Browse files
committed
Add initial language service tests
This change adds new tests for the LanguageService class. It also establishes a new shared library for test assemblies so that both the core library tests and the host process tests can use the same test data. Usage of the shared test data in the host tests will be added in a later commit.
1 parent e39f2b3 commit c263982

File tree

13 files changed

+390
-62
lines changed

13 files changed

+390
-62
lines changed

PowerShellEditorServices.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{E51470
3232
.nuget\NuGet.targets = .nuget\NuGet.targets
3333
EndProjectSection
3434
EndProject
35+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PowerShellEditorServices.Test.Shared", "test\PowerShellEditorServices.Test.Shared\PowerShellEditorServices.Test.Shared.csproj", "{6A20B9E9-DE66-456E-B4F5-ACFD1A95C3CA}"
36+
EndProject
3537
Global
3638
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3739
Debug|Any CPU = Debug|Any CPU
@@ -70,6 +72,10 @@ Global
7072
{8ED116F4-9DDF-4C49-AB96-AE462E3D64C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
7173
{8ED116F4-9DDF-4C49-AB96-AE462E3D64C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
7274
{8ED116F4-9DDF-4C49-AB96-AE462E3D64C3}.Release|Any CPU.Build.0 = Release|Any CPU
75+
{6A20B9E9-DE66-456E-B4F5-ACFD1A95C3CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
76+
{6A20B9E9-DE66-456E-B4F5-ACFD1A95C3CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
77+
{6A20B9E9-DE66-456E-B4F5-ACFD1A95C3CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
78+
{6A20B9E9-DE66-456E-B4F5-ACFD1A95C3CA}.Release|Any CPU.Build.0 = Release|Any CPU
7379
EndGlobalSection
7480
GlobalSection(SolutionProperties) = preSolution
7581
HideSolutionNode = FALSE
@@ -83,5 +89,6 @@ Global
8389
{3A5DDD20-5BD0-42F4-89F4-ACC0CE554028} = {422E561A-8118-4BE7-A54F-9309E4F03AAE}
8490
{E3A5CF5D-6E41-44AC-AE0A-4C227E4BACD4} = {422E561A-8118-4BE7-A54F-9309E4F03AAE}
8591
{8ED116F4-9DDF-4C49-AB96-AE462E3D64C3} = {422E561A-8118-4BE7-A54F-9309E4F03AAE}
92+
{6A20B9E9-DE66-456E-B4F5-ACFD1A95C3CA} = {422E561A-8118-4BE7-A54F-9309E4F03AAE}
8693
EndGlobalSection
8794
EndGlobal

src/PowerShellEditorServices/Language/CompletionResults.cs

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
44
//
55

6+
using Microsoft.PowerShell.EditorServices.Utility;
67
using System.Collections.Generic;
78
using System.Linq;
89
using System.Management.Automation;
@@ -13,22 +14,33 @@ namespace Microsoft.PowerShell.EditorServices.Language
1314
/// <summary>
1415
/// Provides the results of a single code completion request.
1516
/// </summary>
16-
public class CompletionResults
17+
public sealed class CompletionResults
1718
{
18-
public int CurrentMatchIndex { get; private set; }
19+
#region Properties
1920

21+
/// <summary>
22+
/// Gets the completions that were found during the
23+
/// completion request.
24+
/// </summary>
2025
public CompletionDetails[] Completions { get; private set; }
2126

27+
#endregion
28+
29+
#region Constructors
30+
2231
internal static CompletionResults Create(
2332
CommandCompletion commandCompletion)
2433
{
2534
return new CompletionResults
2635
{
2736
Completions = GetCompletionsArray(commandCompletion),
28-
CurrentMatchIndex = commandCompletion.CurrentMatchIndex
2937
};
3038
}
3139

40+
#endregion
41+
42+
#region Private Methods
43+
3244
private static CompletionDetails[] GetCompletionsArray(
3345
CommandCompletion commandCompletion)
3446
{
@@ -38,6 +50,8 @@ private static CompletionDetails[] GetCompletionsArray(
3850

3951
return completionList.ToArray();
4052
}
53+
54+
#endregion
4155
}
4256

4357
/// <summary>
@@ -96,8 +110,10 @@ public enum CompletionType
96110
/// <summary>
97111
/// Provides the details about a single completion result.
98112
/// </summary>
99-
public class CompletionDetails
113+
public sealed class CompletionDetails
100114
{
115+
#region Properties
116+
101117
/// <summary>
102118
/// Gets the text that will be used to complete the statement
103119
/// at the requested file offset.
@@ -122,22 +138,90 @@ public class CompletionDetails
122138
/// </summary>
123139
public CompletionType CompletionType { get; private set; }
124140

141+
#endregion
142+
143+
#region Constructors
144+
125145
internal static CompletionDetails Create(CompletionResult completionResult)
126146
{
127-
//completionResult.ToolTip;
128-
//completionResult.ListItemText;
147+
Validate.IsNotNull("completionResult", completionResult);
148+
149+
// Some tooltips may have newlines or whitespace for unknown reasons
150+
string toolTipText = completionResult.ToolTip;
151+
if (toolTipText != null)
152+
{
153+
toolTipText = toolTipText.Trim();
154+
}
129155

130156
return new CompletionDetails
131157
{
132158
CompletionText = completionResult.CompletionText,
133-
ToolTipText = completionResult.ToolTip,
159+
ToolTipText = toolTipText,
134160
SymbolTypeName = ExtractSymbolTypeNameFromToolTip(completionResult.ToolTip),
135161
CompletionType =
136162
ConvertCompletionResultType(
137163
completionResult.ResultType)
138164
};
139165
}
140166

167+
internal static CompletionDetails Create(
168+
string completionText,
169+
CompletionType completionType,
170+
string toolTipText = null,
171+
string symbolTypeName = null)
172+
{
173+
return new CompletionDetails
174+
{
175+
CompletionText = completionText,
176+
CompletionType = completionType,
177+
ToolTipText = toolTipText,
178+
SymbolTypeName = symbolTypeName
179+
};
180+
}
181+
182+
#endregion
183+
184+
#region Public Methods
185+
186+
/// <summary>
187+
/// Compares two CompletionResults instances for equality.
188+
/// </summary>
189+
/// <param name="obj">The potential CompletionResults instance to compare.</param>
190+
/// <returns>True if the CompletionResults instances have the same details.</returns>
191+
public override bool Equals(object obj)
192+
{
193+
CompletionDetails otherDetails = obj as CompletionDetails;
194+
if (otherDetails == null)
195+
{
196+
return false;
197+
}
198+
199+
return
200+
string.Equals(this.CompletionText, otherDetails.CompletionText) &&
201+
this.CompletionType == otherDetails.CompletionType &&
202+
string.Equals(this.ToolTipText, otherDetails.ToolTipText) &&
203+
string.Equals(this.SymbolTypeName, otherDetails.SymbolTypeName);
204+
}
205+
206+
/// <summary>
207+
/// Returns the hash code for this CompletionResults instance.
208+
/// </summary>
209+
/// <returns>The hash code for this CompletionResults instance.</returns>
210+
public override int GetHashCode()
211+
{
212+
return
213+
string.Format(
214+
"{0}{1}{2}{3}",
215+
this.CompletionText,
216+
this.CompletionType,
217+
this.ToolTipText,
218+
this.SymbolTypeName).GetHashCode();
219+
}
220+
221+
#endregion
222+
223+
#region Private Methods
224+
141225
private static CompletionType ConvertCompletionResultType(
142226
CompletionResultType completionResultType)
143227
{
@@ -187,5 +271,7 @@ private static string ExtractSymbolTypeNameFromToolTip(string toolTipText)
187271

188272
return null;
189273
}
274+
275+
#endregion
190276
}
191277
}

src/PowerShellEditorServices/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@
3434
// [assembly: AssemblyVersion("1.0.*")]
3535
[assembly: AssemblyVersion("1.0.0.0")]
3636
[assembly: AssemblyFileVersion("1.0.0.0")]
37+
38+
[assembly: InternalsVisibleTo("Microsoft.PowerShell.EditorServices.Test.Shared")]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
//
4+
5+
using Microsoft.PowerShell.EditorServices.Language;
6+
using Microsoft.PowerShell.EditorServices.Session;
7+
8+
namespace Microsoft.PowerShell.EditorServices.Test.Shared.Completion
9+
{
10+
public class CompleteCommandFromModule
11+
{
12+
public static readonly ScriptRegion SourceDetails =
13+
new ScriptRegion
14+
{
15+
File = @"Completion\CompletionExamples.ps1",
16+
StartLineNumber = 13,
17+
StartColumnNumber = 11
18+
};
19+
20+
public static readonly CompletionDetails ExpectedCompletion =
21+
CompletionDetails.Create(
22+
"Install-Module",
23+
CompletionType.Command,
24+
"Install-Module");
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
//
4+
5+
using Microsoft.PowerShell.EditorServices.Language;
6+
using Microsoft.PowerShell.EditorServices.Session;
7+
8+
namespace Microsoft.PowerShell.EditorServices.Test.Shared.Completion
9+
{
10+
public class CompleteCommandInFile
11+
{
12+
public static readonly ScriptRegion SourceDetails =
13+
new ScriptRegion
14+
{
15+
File = @"Completion\CompletionExamples.ps1",
16+
StartLineNumber = 8,
17+
StartColumnNumber = 7
18+
};
19+
20+
public static readonly CompletionDetails ExpectedCompletion =
21+
CompletionDetails.Create(
22+
"Get-Something",
23+
CompletionType.Command,
24+
"Get-Something");
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
//
4+
5+
using Microsoft.PowerShell.EditorServices.Language;
6+
using Microsoft.PowerShell.EditorServices.Session;
7+
8+
namespace Microsoft.PowerShell.EditorServices.Test.Shared.Completion
9+
{
10+
public class CompleteVariableInFile
11+
{
12+
public static readonly ScriptRegion SourceDetails =
13+
new ScriptRegion
14+
{
15+
File = @"Completion\CompletionExamples.ps1",
16+
StartLineNumber = 10,
17+
StartColumnNumber = 9
18+
};
19+
20+
public static readonly CompletionDetails ExpectedCompletion =
21+
CompletionDetails.Create(
22+
"$testVar1",
23+
CompletionType.Variable,
24+
"testVar1");
25+
}
26+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function Get-Something
2+
{
3+
$testVar2 = "Shouldn't find this variable"
4+
}
5+
6+
$testVar1 = "Should find this variable"
7+
8+
Get-So
9+
10+
$testVar
11+
12+
Import-Module PowerShellGet
13+
Install-Mo
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{6A20B9E9-DE66-456E-B4F5-ACFD1A95C3CA}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>Microsoft.PowerShell.EditorServices.Test.Shared</RootNamespace>
11+
<AssemblyName>Microsoft.PowerShell.EditorServices.Test.Shared</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\Debug\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<OutputPath>bin\Release\</OutputPath>
28+
<DefineConstants>TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<ItemGroup>
33+
<Reference Include="System" />
34+
<Reference Include="System.Core" />
35+
<Reference Include="System.Xml.Linq" />
36+
<Reference Include="System.Data.DataSetExtensions" />
37+
<Reference Include="Microsoft.CSharp" />
38+
<Reference Include="System.Data" />
39+
<Reference Include="System.Xml" />
40+
</ItemGroup>
41+
<ItemGroup>
42+
<Compile Include="Completion\CompleteCommandFromModule.cs" />
43+
<Compile Include="Completion\CompleteCommandInFile.cs" />
44+
<Compile Include="Completion\CompleteVariableInFile.cs" />
45+
<Compile Include="Properties\AssemblyInfo.cs" />
46+
<Compile Include="Utility\ResourceFileLoader.cs" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<EmbeddedResource Include="Completion\CompletionExamples.ps1" />
50+
</ItemGroup>
51+
<ItemGroup>
52+
<ProjectReference Include="..\..\src\PowerShellEditorServices\PowerShellEditorServices.csproj">
53+
<Project>{81e8cbcd-6319-49e7-9662-0475bd0791f4}</Project>
54+
<Name>PowerShellEditorServices</Name>
55+
</ProjectReference>
56+
</ItemGroup>
57+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
58+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
59+
Other similar extension points exist, see Microsoft.Common.targets.
60+
<Target Name="BeforeBuild">
61+
</Target>
62+
<Target Name="AfterBuild">
63+
</Target>
64+
-->
65+
</Project>

0 commit comments

Comments
 (0)