Skip to content

Commit 15ecd2b

Browse files
committed
added utilities for common renaming functions updated tests
1 parent d08775d commit 15ecd2b

File tree

6 files changed

+206
-1
lines changed

6 files changed

+206
-1
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Management.Automation.Language;
7+
8+
namespace Microsoft.PowerShell.EditorServices.Refactoring
9+
{
10+
internal class Utilities
11+
{
12+
public static Ast GetAst(int StartLineNumber, int StartColumnNumber, Ast Ast)
13+
{
14+
Ast token = null;
15+
16+
token = Ast.Find(ast =>
17+
{
18+
return StartLineNumber == ast.Extent.StartLineNumber &&
19+
ast.Extent.EndColumnNumber >= StartColumnNumber &&
20+
StartColumnNumber >= ast.Extent.StartColumnNumber;
21+
}, true);
22+
23+
IEnumerable<Ast> tokens = token.FindAll(ast =>
24+
{
25+
return ast.Extent.EndColumnNumber >= StartColumnNumber
26+
&& StartColumnNumber >= ast.Extent.StartColumnNumber;
27+
}, true);
28+
if (tokens.Count() > 1)
29+
{
30+
token = tokens.LastOrDefault();
31+
}
32+
return token;
33+
}
34+
}
35+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function New-User {
2+
param (
3+
[string]$Username,
4+
[string]$password
5+
)
6+
write-host $username + $password
7+
8+
$splat= @{
9+
Username = "JohnDeer"
10+
Password = "SomePassword"
11+
}
12+
New-User @splat
13+
}
14+
15+
$UserDetailsSplat= @{
16+
Username = "JohnDoe"
17+
Password = "SomePassword"
18+
}
19+
New-User @UserDetailsSplat
20+
21+
New-User -Username "JohnDoe" -Password "SomePassword"

test/PowerShellEditorServices.Test.Shared/Refactoring/Variables/RefactorsVariablesData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ internal static class RenameVariableData
146146
{
147147
FileName = "VarableCommandParameterSplatted.ps1",
148148
Column = 5,
149-
Line = 10,
149+
Line = 16,
150150
RenameTo = "Renamed"
151151
};
152152
}

test/PowerShellEditorServices.Test.Shared/Refactoring/Variables/VarableCommandParameterSplatted.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ function New-User {
44
[string]$password
55
)
66
write-host $username + $password
7+
8+
$splat= @{
9+
Username = "JohnDeer"
10+
Password = "SomePassword"
11+
}
12+
New-User @splat
713
}
814

915
$UserDetailsSplat= @{

test/PowerShellEditorServices.Test.Shared/Refactoring/Variables/VarableCommandParameterSplattedRenamed.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ function New-User {
44
[string]$password
55
)
66
write-host $Renamed + $password
7+
8+
$splat= @{
9+
Renamed = "JohnDeer"
10+
Password = "SomePassword"
11+
}
12+
New-User @splat
713
}
814

915
$UserDetailsSplat= @{
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.IO;
6+
using Microsoft.Extensions.Logging.Abstractions;
7+
using Microsoft.PowerShell.EditorServices.Services;
8+
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
9+
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
10+
using Microsoft.PowerShell.EditorServices.Test;
11+
using Microsoft.PowerShell.EditorServices.Test.Shared;
12+
using Microsoft.PowerShell.EditorServices.Handlers;
13+
using Xunit;
14+
using Microsoft.PowerShell.EditorServices.Refactoring;
15+
using System.Management.Automation.Language;
16+
17+
namespace PowerShellEditorServices.Test.Refactoring
18+
{
19+
[Trait("Category", "RefactorUtilities")]
20+
public class RefactorUtilitiesTests : IDisposable
21+
{
22+
private readonly PsesInternalHost psesHost;
23+
private readonly WorkspaceService workspace;
24+
25+
public RefactorUtilitiesTests()
26+
{
27+
psesHost = PsesHostFactory.Create(NullLoggerFactory.Instance);
28+
workspace = new WorkspaceService(NullLoggerFactory.Instance);
29+
}
30+
31+
public void Dispose()
32+
{
33+
#pragma warning disable VSTHRD002
34+
psesHost.StopAsync().Wait();
35+
#pragma warning restore VSTHRD002
36+
GC.SuppressFinalize(this);
37+
}
38+
private ScriptFile GetTestScript(string fileName) => workspace.GetFile(TestUtilities.GetSharedPath(Path.Combine("Refactoring\\Utilities", fileName)));
39+
40+
[Fact]
41+
public void GetVariableExpressionAst()
42+
{
43+
RenameSymbolParams request = new(){
44+
Column=11,
45+
Line=15,
46+
RenameTo="Renamed",
47+
FileName="TestDetection.ps1"
48+
};
49+
ScriptFile scriptFile = GetTestScript(request.FileName);
50+
51+
Ast symbol = Utilities.GetAst(request.Line,request.Column,scriptFile.ScriptAst);
52+
Assert.Equal(15,symbol.Extent.StartLineNumber);
53+
Assert.Equal(1,symbol.Extent.StartColumnNumber);
54+
55+
}
56+
[Fact]
57+
public void GetVariableExpressionStartAst()
58+
{
59+
RenameSymbolParams request = new(){
60+
Column=1,
61+
Line=15,
62+
RenameTo="Renamed",
63+
FileName="TestDetection.ps1"
64+
};
65+
ScriptFile scriptFile = GetTestScript(request.FileName);
66+
67+
Ast symbol = Utilities.GetAst(request.Line,request.Column,scriptFile.ScriptAst);
68+
Assert.Equal(15,symbol.Extent.StartLineNumber);
69+
Assert.Equal(1,symbol.Extent.StartColumnNumber);
70+
71+
}
72+
[Fact]
73+
public void GetVariableWithinParameterAst()
74+
{
75+
RenameSymbolParams request = new(){
76+
Column=21,
77+
Line=3,
78+
RenameTo="Renamed",
79+
FileName="TestDetection.ps1"
80+
};
81+
ScriptFile scriptFile = GetTestScript(request.FileName);
82+
83+
Ast symbol = Utilities.GetAst(request.Line,request.Column,scriptFile.ScriptAst);
84+
Assert.Equal(3,symbol.Extent.StartLineNumber);
85+
Assert.Equal(17,symbol.Extent.StartColumnNumber);
86+
87+
}
88+
[Fact]
89+
public void GetHashTableKey()
90+
{
91+
RenameSymbolParams request = new(){
92+
Column=9,
93+
Line=16,
94+
RenameTo="Renamed",
95+
FileName="TestDetection.ps1"
96+
};
97+
ScriptFile scriptFile = GetTestScript(request.FileName);
98+
99+
Ast symbol = Utilities.GetAst(request.Line,request.Column,scriptFile.ScriptAst);
100+
Assert.Equal(16,symbol.Extent.StartLineNumber);
101+
Assert.Equal(5,symbol.Extent.StartColumnNumber);
102+
103+
}
104+
[Fact]
105+
public void GetVariableWithinCommandAst()
106+
{
107+
RenameSymbolParams request = new(){
108+
Column=29,
109+
Line=6,
110+
RenameTo="Renamed",
111+
FileName="TestDetection.ps1"
112+
};
113+
ScriptFile scriptFile = GetTestScript(request.FileName);
114+
115+
Ast symbol = Utilities.GetAst(request.Line,request.Column,scriptFile.ScriptAst);
116+
Assert.Equal(6,symbol.Extent.StartLineNumber);
117+
Assert.Equal(28,symbol.Extent.StartColumnNumber);
118+
119+
}
120+
[Fact]
121+
public void GetCommandParameterAst()
122+
{
123+
RenameSymbolParams request = new(){
124+
Column=12,
125+
Line=21,
126+
RenameTo="Renamed",
127+
FileName="TestDetection.ps1"
128+
};
129+
ScriptFile scriptFile = GetTestScript(request.FileName);
130+
131+
Ast symbol = Utilities.GetAst(request.Line,request.Column,scriptFile.ScriptAst);
132+
Assert.Equal(21,symbol.Extent.StartLineNumber);
133+
Assert.Equal(10,symbol.Extent.StartColumnNumber);
134+
135+
}
136+
}
137+
}

0 commit comments

Comments
 (0)