Skip to content

Commit ff75355

Browse files
committed
Add support for negative test cases
1 parent 0304587 commit ff75355

File tree

7 files changed

+80
-13
lines changed

7 files changed

+80
-13
lines changed

src/PowerShellEditorServices/Services/TextDocument/RenameService.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ ILanguageServerConfiguration config
6363
Position = request.Position,
6464
TextDocument = request.TextDocument
6565
};
66-
// TODO: Should we cache these resuls and just fetch them on the actual rename, and move the bulk to an implementation method?
66+
67+
// TODO: As a performance optimization, should we cache these results and just fetch them on the actual rename, and move the bulk to an implementation method? Seems pretty fast right now but may slow down on large documents. Need to add a large document test example.
6768
WorkspaceEdit? renameResponse = await RenameSymbol(renameRequest, cancellationToken).ConfigureAwait(false);
6869

6970
// Since LSP 3.16 we can simply basically return a DefaultBehavior true or null to signal to the client that the position is valid for rename and it should use its default selection criteria (which is probably the language semantic highlighting or grammar). For the current scope of the rename provider, this should be fine, but we have the option to supply the specific range in the future for special cases.
@@ -318,7 +319,7 @@ public AstVisitAction Visit(Ast ast)
318319
{
319320
FunctionDefinitionAst f => f,
320321
CommandAst command => CurrentDocument.FindFunctionDefinition(command)
321-
?? throw new TargetSymbolNotFoundException("The command to rename does not have a function definition. Renaming a function is only supported when the function is defined within the same scope"),
322+
?? throw new HandlerErrorException("The command to rename does not have a function definition. Renaming a function is only supported when the function is defined within the same scope"),
322323
_ => throw new Exception($"Unsupported AST type {target.GetType()} encountered")
323324
};
324325
};
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
function SameNameFunction {
22
Write-Host "This is the outer function"
3-
function RenamedSameNameFunction {
4-
Write-Host "This is the inner function"
3+
function Renamed {
4+
Write-Host 'This is the inner function'
55
}
6-
RenamedSameNameFunction
6+
Renamed
77
}
88
SameNameFunction

test/PowerShellEditorServices.Test.Shared/Refactoring/Functions/RefactorFunctionTestCases.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class RefactorFunctionTestCases
1919
new("FunctionMultipleOccurrences.ps1", Line: 5, Column: 3 ),
2020
new("FunctionNestedRedefinition.ps1", Line: 13, Column: 15 ),
2121
new("FunctionOuterHasNestedFunction.ps1", Line: 1, Column: 10 ),
22-
new("FunctionSameName.ps1", Line: 3, Column: 14 , "RenamedSameNameFunction"),
22+
new("FunctionSameName.ps1", Line: 3, Column: 14 ),
2323
new("FunctionScriptblock.ps1", Line: 5, Column: 5 ),
2424
new("FunctionsSingle.ps1", Line: 1, Column: 11 ),
2525
new("FunctionWithInnerFunction.ps1", Line: 5, Column: 5 ),

test/PowerShellEditorServices.Test.Shared/Refactoring/RenameTestTarget.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,22 @@ public class RenameTestTarget
2727
/// </summary>
2828
public string NewName = "Renamed";
2929

30+
public bool ShouldFail;
31+
3032
/// <param name="FileName">The test case file name e.g. testScript.ps1</param>
3133
/// <param name="Line">The line where the cursor should be positioned for the rename</param>
3234
/// <param name="Column">The column/character indent where ther cursor should be positioned for the rename</param>
3335
/// <param name="NewName">What the target symbol represented by the line and column should be renamed to. Defaults to "Renamed" if not specified</param>
34-
public RenameTestTarget(string FileName, int Line, int Column, string NewName = "Renamed")
36+
/// <param name="ShouldFail">This test case should not succeed and return either null or a handler error</param>
37+
public RenameTestTarget(string FileName, int Line, int Column, string NewName = "Renamed", bool ShouldFail = false)
3538
{
3639
this.FileName = FileName;
3740
this.Line = Line;
3841
this.Column = Column;
3942
this.NewName = NewName;
43+
this.ShouldFail = ShouldFail;
4044
}
4145
public RenameTestTarget() { }
4246

43-
public override string ToString() => $"{FileName.Substring(0, FileName.Length - 4)}";
47+
public override string ToString() => $"{FileName} L{Line} C{Column} N:{NewName} F:{ShouldFail}";
4448
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ public class RefactorVariableTestCases
66
public static RenameTestTarget[] TestCases =
77
[
88
new ("SimpleVariableAssignment.ps1", Line: 1, Column: 1),
9+
new ("SimpleVariableAssignment.ps1", Line: 1, Column: 1, NewName: "$Renamed"),
10+
new ("SimpleVariableAssignment.ps1", Line: 2, Column: 1, NewName: "Wrong", ShouldFail: true),
911
new ("VariableCommandParameter.ps1", Line: 3, Column: 17),
1012
new ("VariableCommandParameter.ps1", Line: 10, Column: 10),
1113
new ("VariableCommandParameterSplatted.ps1", Line: 3, Column: 19 ),

test/PowerShellEditorServices.Test/Refactoring/PrepareRenameHandlerTests.cs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,21 @@ public async Task FindsFunction(RenameTestTarget s)
6767
{
6868
PrepareRenameParams testParams = s.ToPrepareRenameParams("Functions");
6969

70-
RangeOrPlaceholderRange? result = await testHandler.Handle(testParams, CancellationToken.None);
70+
RangeOrPlaceholderRange? result;
71+
try
72+
{
73+
result = await testHandler.Handle(testParams, CancellationToken.None);
74+
}
75+
catch (HandlerErrorException)
76+
{
77+
Assert.True(s.ShouldFail);
78+
return;
79+
}
80+
if (s.ShouldFail)
81+
{
82+
Assert.Null(result);
83+
return;
84+
}
7185

7286
Assert.NotNull(result);
7387
Assert.True(result?.DefaultBehavior?.DefaultBehavior);
@@ -79,7 +93,21 @@ public async Task FindsVariable(RenameTestTarget s)
7993
{
8094
PrepareRenameParams testParams = s.ToPrepareRenameParams("Variables");
8195

82-
RangeOrPlaceholderRange? result = await testHandler.Handle(testParams, CancellationToken.None);
96+
RangeOrPlaceholderRange? result;
97+
try
98+
{
99+
result = await testHandler.Handle(testParams, CancellationToken.None);
100+
}
101+
catch (HandlerErrorException)
102+
{
103+
Assert.True(s.ShouldFail);
104+
return;
105+
}
106+
if (s.ShouldFail)
107+
{
108+
Assert.Null(result);
109+
return;
110+
}
83111

84112
Assert.NotNull(result);
85113
Assert.True(result?.DefaultBehavior?.DefaultBehavior);
@@ -184,6 +212,7 @@ public void Serialize(IXunitSerializationInfo info)
184212
info.AddValue(nameof(Line), Line);
185213
info.AddValue(nameof(Column), Column);
186214
info.AddValue(nameof(NewName), NewName);
215+
info.AddValue(nameof(ShouldFail), ShouldFail);
187216
}
188217

189218
public void Deserialize(IXunitSerializationInfo info)
@@ -192,6 +221,7 @@ public void Deserialize(IXunitSerializationInfo info)
192221
Line = info.GetValue<int>(nameof(Line));
193222
Column = info.GetValue<int>(nameof(Column));
194223
NewName = info.GetValue<string>(nameof(NewName));
224+
ShouldFail = info.GetValue<bool>(nameof(ShouldFail));
195225
}
196226

197227
public static RenameTestTargetSerializable FromRenameTestTarget(RenameTestTarget t)
@@ -200,6 +230,7 @@ public static RenameTestTargetSerializable FromRenameTestTarget(RenameTestTarget
200230
FileName = t.FileName,
201231
Column = t.Column,
202232
Line = t.Line,
203-
NewName = t.NewName
233+
NewName = t.NewName,
234+
ShouldFail = t.ShouldFail
204235
};
205236
}

test/PowerShellEditorServices.Test/Refactoring/RenameHandlerTests.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,22 @@ public static TheoryData<RenameTestTargetSerializable> FunctionTestCases()
5656
public async void RenamedFunction(RenameTestTarget s)
5757
{
5858
RenameParams request = s.ToRenameParams("Functions");
59-
WorkspaceEdit response = await testHandler.Handle(request, CancellationToken.None);
59+
WorkspaceEdit response;
60+
try
61+
{
62+
response = await testHandler.Handle(request, CancellationToken.None);
63+
}
64+
catch (HandlerErrorException)
65+
{
66+
Assert.True(s.ShouldFail);
67+
return;
68+
}
69+
if (s.ShouldFail)
70+
{
71+
Assert.Null(response);
72+
return;
73+
}
74+
6075
DocumentUri testScriptUri = request.TextDocument.Uri;
6176

6277
string expected = workspace.GetFile
@@ -78,7 +93,21 @@ public async void RenamedFunction(RenameTestTarget s)
7893
public async void RenamedVariable(RenameTestTarget s)
7994
{
8095
RenameParams request = s.ToRenameParams("Variables");
81-
WorkspaceEdit response = await testHandler.Handle(request, CancellationToken.None);
96+
WorkspaceEdit response;
97+
try
98+
{
99+
response = await testHandler.Handle(request, CancellationToken.None);
100+
}
101+
catch (HandlerErrorException)
102+
{
103+
Assert.True(s.ShouldFail);
104+
return;
105+
}
106+
if (s.ShouldFail)
107+
{
108+
Assert.Null(response);
109+
return;
110+
}
82111
DocumentUri testScriptUri = request.TextDocument.Uri;
83112

84113
string expected = workspace.GetFile

0 commit comments

Comments
 (0)