1
1
// Copyright (c) Microsoft Corporation.
2
2
// Licensed under the MIT License.
3
3
4
- using System ;
5
4
using System . IO ;
6
5
using System . Threading . Tasks ;
7
6
using Microsoft . Extensions . Logging . Abstractions ;
13
12
using Microsoft . PowerShell . EditorServices . Handlers ;
14
13
using Xunit ;
15
14
using PowerShellEditorServices . Test . Shared . Refactoring . Variables ;
15
+ using static PowerShellEditorServices . Test . Refactoring . RefactorUtilities ;
16
16
using Microsoft . PowerShell . EditorServices . Refactoring ;
17
17
18
18
namespace PowerShellEditorServices . Test . Refactoring
@@ -31,33 +31,7 @@ public async Task InitializeAsync()
31
31
public async Task DisposeAsync ( ) => await Task . Run ( psesHost . StopAsync ) ;
32
32
private ScriptFile GetTestScript ( string fileName ) => workspace . GetFile ( TestUtilities . GetSharedPath ( Path . Combine ( "Refactoring" , "Variables" , fileName ) ) ) ;
33
33
34
- internal static string GetModifiedScript ( string OriginalScript , ModifiedFileResponse Modification )
35
- {
36
- Modification . Changes . Sort ( ( a , b ) =>
37
- {
38
- if ( b . StartLine == a . StartLine )
39
- {
40
- return b . EndColumn - a . EndColumn ;
41
- }
42
- return b . StartLine - a . StartLine ;
43
-
44
- } ) ;
45
- string [ ] Lines = OriginalScript . Split (
46
- new string [ ] { Environment . NewLine } ,
47
- StringSplitOptions . None ) ;
48
-
49
- foreach ( TextChange change in Modification . Changes )
50
- {
51
- string TargetLine = Lines [ change . StartLine ] ;
52
- string begin = TargetLine . Substring ( 0 , change . StartColumn ) ;
53
- string end = TargetLine . Substring ( change . EndColumn ) ;
54
- Lines [ change . StartLine ] = begin + change . NewText + end ;
55
- }
56
-
57
- return string . Join ( Environment . NewLine , Lines ) ;
58
- }
59
-
60
- internal static string TestRenaming ( ScriptFile scriptFile , RenameSymbolParams request )
34
+ internal static string TestRenaming ( ScriptFile scriptFile , RenameSymbolParamsSerialized request )
61
35
{
62
36
63
37
IterativeVariableRename iterative = new ( request . RenameTo ,
@@ -71,223 +45,40 @@ internal static string TestRenaming(ScriptFile scriptFile, RenameSymbolParams re
71
45
} ;
72
46
return GetModifiedScript ( scriptFile . Contents , changes ) ;
73
47
}
74
-
75
- [ Fact ]
76
- public void RefactorVariableSingle ( )
77
- {
78
- RenameSymbolParams request = RenameVariableData . SimpleVariableAssignment ;
79
- ScriptFile scriptFile = GetTestScript ( request . FileName ) ;
80
- ScriptFile expectedContent = GetTestScript ( request . FileName . Substring ( 0 , request . FileName . Length - 4 ) + "Renamed.ps1" ) ;
81
-
82
- string modifiedcontent = TestRenaming ( scriptFile , request ) ;
83
-
84
- Assert . Equal ( expectedContent . Contents , modifiedcontent ) ;
85
-
86
- }
87
- [ Fact ]
88
- public void RefactorVariableNestedScopeFunction ( )
89
- {
90
- RenameSymbolParams request = RenameVariableData . VariableNestedScopeFunction ;
91
- ScriptFile scriptFile = GetTestScript ( request . FileName ) ;
92
- ScriptFile expectedContent = GetTestScript ( request . FileName . Substring ( 0 , request . FileName . Length - 4 ) + "Renamed.ps1" ) ;
93
-
94
- string modifiedcontent = TestRenaming ( scriptFile , request ) ;
95
-
96
- Assert . Equal ( expectedContent . Contents , modifiedcontent ) ;
97
-
98
- }
99
- [ Fact ]
100
- public void RefactorVariableInPipeline ( )
101
- {
102
- RenameSymbolParams request = RenameVariableData . VariableInPipeline ;
103
- ScriptFile scriptFile = GetTestScript ( request . FileName ) ;
104
- ScriptFile expectedContent = GetTestScript ( request . FileName . Substring ( 0 , request . FileName . Length - 4 ) + "Renamed.ps1" ) ;
105
-
106
- string modifiedcontent = TestRenaming ( scriptFile , request ) ;
107
-
108
- Assert . Equal ( expectedContent . Contents , modifiedcontent ) ;
109
-
110
- }
111
- [ Fact ]
112
- public void RefactorVariableInScriptBlock ( )
113
- {
114
- RenameSymbolParams request = RenameVariableData . VariableInScriptblock ;
115
- ScriptFile scriptFile = GetTestScript ( request . FileName ) ;
116
- ScriptFile expectedContent = GetTestScript ( request . FileName . Substring ( 0 , request . FileName . Length - 4 ) + "Renamed.ps1" ) ;
117
-
118
- string modifiedcontent = TestRenaming ( scriptFile , request ) ;
119
-
120
- Assert . Equal ( expectedContent . Contents , modifiedcontent ) ;
121
-
122
- }
123
- [ Fact ]
124
- public void RefactorVariableInScriptBlockScoped ( )
125
- {
126
- RenameSymbolParams request = RenameVariableData . VariablewWithinHastableExpression ;
127
- ScriptFile scriptFile = GetTestScript ( request . FileName ) ;
128
- ScriptFile expectedContent = GetTestScript ( request . FileName . Substring ( 0 , request . FileName . Length - 4 ) + "Renamed.ps1" ) ;
129
-
130
- string modifiedcontent = TestRenaming ( scriptFile , request ) ;
131
-
132
- Assert . Equal ( expectedContent . Contents , modifiedcontent ) ;
133
-
134
- }
135
- [ Fact ]
136
- public void VariableNestedFunctionScriptblock ( )
137
- {
138
- RenameSymbolParams request = RenameVariableData . VariableNestedFunctionScriptblock ;
139
- ScriptFile scriptFile = GetTestScript ( request . FileName ) ;
140
- ScriptFile expectedContent = GetTestScript ( request . FileName . Substring ( 0 , request . FileName . Length - 4 ) + "Renamed.ps1" ) ;
141
-
142
- string modifiedcontent = TestRenaming ( scriptFile , request ) ;
143
-
144
- Assert . Equal ( expectedContent . Contents , modifiedcontent ) ;
145
-
146
- }
147
- [ Fact ]
148
- public void VariableWithinCommandAstScriptBlock ( )
149
- {
150
- RenameSymbolParams request = RenameVariableData . VariableWithinCommandAstScriptBlock ;
151
- ScriptFile scriptFile = GetTestScript ( request . FileName ) ;
152
- ScriptFile expectedContent = GetTestScript ( request . FileName . Substring ( 0 , request . FileName . Length - 4 ) + "Renamed.ps1" ) ;
153
-
154
- string modifiedcontent = TestRenaming ( scriptFile , request ) ;
155
-
156
- Assert . Equal ( expectedContent . Contents , modifiedcontent ) ;
157
-
158
- }
159
- [ Fact ]
160
- public void VariableWithinForeachObject ( )
161
- {
162
- RenameSymbolParams request = RenameVariableData . VariableWithinForeachObject ;
163
- ScriptFile scriptFile = GetTestScript ( request . FileName ) ;
164
- ScriptFile expectedContent = GetTestScript ( request . FileName . Substring ( 0 , request . FileName . Length - 4 ) + "Renamed.ps1" ) ;
165
-
166
- string modifiedcontent = TestRenaming ( scriptFile , request ) ;
167
-
168
- Assert . Equal ( expectedContent . Contents , modifiedcontent ) ;
169
-
170
- }
171
- [ Fact ]
172
- public void VariableusedInWhileLoop ( )
173
- {
174
- RenameSymbolParams request = RenameVariableData . VariableusedInWhileLoop ;
175
- ScriptFile scriptFile = GetTestScript ( request . FileName ) ;
176
- ScriptFile expectedContent = GetTestScript ( request . FileName . Substring ( 0 , request . FileName . Length - 4 ) + "Renamed.ps1" ) ;
177
-
178
- string modifiedcontent = TestRenaming ( scriptFile , request ) ;
179
-
180
- Assert . Equal ( expectedContent . Contents , modifiedcontent ) ;
181
-
182
- }
183
- [ Fact ]
184
- public void VariableInParam ( )
185
- {
186
- RenameSymbolParams request = RenameVariableData . VariableInParam ;
187
- ScriptFile scriptFile = GetTestScript ( request . FileName ) ;
188
- ScriptFile expectedContent = GetTestScript ( request . FileName . Substring ( 0 , request . FileName . Length - 4 ) + "Renamed.ps1" ) ;
189
-
190
- string modifiedcontent = TestRenaming ( scriptFile , request ) ;
191
-
192
- Assert . Equal ( expectedContent . Contents , modifiedcontent ) ;
193
-
194
- }
195
- [ Fact ]
196
- public void VariableCommandParameter ( )
197
- {
198
- RenameSymbolParams request = RenameVariableData . VariableCommandParameter ;
199
- ScriptFile scriptFile = GetTestScript ( request . FileName ) ;
200
- ScriptFile expectedContent = GetTestScript ( request . FileName . Substring ( 0 , request . FileName . Length - 4 ) + "Renamed.ps1" ) ;
201
-
202
- string modifiedcontent = TestRenaming ( scriptFile , request ) ;
203
-
204
- Assert . Equal ( expectedContent . Contents , modifiedcontent ) ;
205
-
206
- }
207
- [ Fact ]
208
- public void VariableCommandParameterReverse ( )
209
- {
210
- RenameSymbolParams request = RenameVariableData . VariableCommandParameterReverse ;
211
- ScriptFile scriptFile = GetTestScript ( request . FileName ) ;
212
- ScriptFile expectedContent = GetTestScript ( request . FileName . Substring ( 0 , request . FileName . Length - 4 ) + "Renamed.ps1" ) ;
213
-
214
- string modifiedcontent = TestRenaming ( scriptFile , request ) ;
215
-
216
- Assert . Equal ( expectedContent . Contents , modifiedcontent ) ;
217
-
218
- }
219
- [ Fact ]
220
- public void VariableScriptWithParamBlock ( )
221
- {
222
- RenameSymbolParams request = RenameVariableData . VariableScriptWithParamBlock ;
223
- ScriptFile scriptFile = GetTestScript ( request . FileName ) ;
224
- ScriptFile expectedContent = GetTestScript ( request . FileName . Substring ( 0 , request . FileName . Length - 4 ) + "Renamed.ps1" ) ;
225
-
226
- string modifiedcontent = TestRenaming ( scriptFile , request ) ;
227
-
228
- Assert . Equal ( expectedContent . Contents , modifiedcontent ) ;
229
-
230
- }
231
- [ Fact ]
232
- public void VariableNonParam ( )
233
- {
234
- RenameSymbolParams request = RenameVariableData . VariableNonParam ;
235
- ScriptFile scriptFile = GetTestScript ( request . FileName ) ;
236
- ScriptFile expectedContent = GetTestScript ( request . FileName . Substring ( 0 , request . FileName . Length - 4 ) + "Renamed.ps1" ) ;
237
-
238
- string modifiedcontent = TestRenaming ( scriptFile , request ) ;
239
-
240
- Assert . Equal ( expectedContent . Contents , modifiedcontent ) ;
241
-
242
- }
243
- [ Fact ]
244
- public void VariableParameterCommandWithSameName ( )
245
- {
246
- RenameSymbolParams request = RenameVariableData . VariableParameterCommandWithSameName ;
247
- ScriptFile scriptFile = GetTestScript ( request . FileName ) ;
248
- ScriptFile expectedContent = GetTestScript ( request . FileName . Substring ( 0 , request . FileName . Length - 4 ) + "Renamed.ps1" ) ;
249
-
250
- string modifiedcontent = TestRenaming ( scriptFile , request ) ;
251
-
252
- Assert . Equal ( expectedContent . Contents , modifiedcontent ) ;
253
- }
254
- [ Fact ]
255
- public void VarableCommandParameterSplattedFromCommandAst ( )
256
- {
257
- RenameSymbolParams request = RenameVariableData . VariableCommandParameterSplattedFromCommandAst ;
258
- ScriptFile scriptFile = GetTestScript ( request . FileName ) ;
259
- ScriptFile expectedContent = GetTestScript ( request . FileName . Substring ( 0 , request . FileName . Length - 4 ) + "Renamed.ps1" ) ;
260
-
261
- string modifiedcontent = TestRenaming ( scriptFile , request ) ;
262
-
263
- Assert . Equal ( expectedContent . Contents , modifiedcontent ) ;
264
- }
265
- [ Fact ]
266
- public void VarableCommandParameterSplattedFromSplat ( )
48
+ public class VariableRenameTestData : TheoryData < RenameSymbolParamsSerialized >
267
49
{
268
- RenameSymbolParams request = RenameVariableData . VariableCommandParameterSplattedFromSplat ;
269
- ScriptFile scriptFile = GetTestScript ( request . FileName ) ;
270
- ScriptFile expectedContent = GetTestScript ( request . FileName . Substring ( 0 , request . FileName . Length - 4 ) + "Renamed.ps1" ) ;
271
-
272
- string modifiedcontent = TestRenaming ( scriptFile , request ) ;
273
-
274
- Assert . Equal ( expectedContent . Contents , modifiedcontent ) ;
50
+ public VariableRenameTestData ( )
51
+ {
52
+ Add ( new RenameSymbolParamsSerialized ( RenameVariableData . SimpleVariableAssignment ) ) ;
53
+ Add ( new RenameSymbolParamsSerialized ( RenameVariableData . VariableRedefinition ) ) ;
54
+ Add ( new RenameSymbolParamsSerialized ( RenameVariableData . VariableNestedScopeFunction ) ) ;
55
+ Add ( new RenameSymbolParamsSerialized ( RenameVariableData . VariableInLoop ) ) ;
56
+ Add ( new RenameSymbolParamsSerialized ( RenameVariableData . VariableInPipeline ) ) ;
57
+ Add ( new RenameSymbolParamsSerialized ( RenameVariableData . VariableInScriptblock ) ) ;
58
+ Add ( new RenameSymbolParamsSerialized ( RenameVariableData . VariableInScriptblockScoped ) ) ;
59
+ Add ( new RenameSymbolParamsSerialized ( RenameVariableData . VariablewWithinHastableExpression ) ) ;
60
+ Add ( new RenameSymbolParamsSerialized ( RenameVariableData . VariableNestedFunctionScriptblock ) ) ;
61
+ Add ( new RenameSymbolParamsSerialized ( RenameVariableData . VariableWithinCommandAstScriptBlock ) ) ;
62
+ Add ( new RenameSymbolParamsSerialized ( RenameVariableData . VariableWithinForeachObject ) ) ;
63
+ Add ( new RenameSymbolParamsSerialized ( RenameVariableData . VariableusedInWhileLoop ) ) ;
64
+ Add ( new RenameSymbolParamsSerialized ( RenameVariableData . VariableInParam ) ) ;
65
+ Add ( new RenameSymbolParamsSerialized ( RenameVariableData . VariableCommandParameter ) ) ;
66
+ Add ( new RenameSymbolParamsSerialized ( RenameVariableData . VariableCommandParameterReverse ) ) ;
67
+ Add ( new RenameSymbolParamsSerialized ( RenameVariableData . VariableScriptWithParamBlock ) ) ;
68
+ Add ( new RenameSymbolParamsSerialized ( RenameVariableData . VariableNonParam ) ) ;
69
+ Add ( new RenameSymbolParamsSerialized ( RenameVariableData . VariableParameterCommandWithSameName ) ) ;
70
+ Add ( new RenameSymbolParamsSerialized ( RenameVariableData . VariableCommandParameterSplattedFromCommandAst ) ) ;
71
+ Add ( new RenameSymbolParamsSerialized ( RenameVariableData . VariableCommandParameterSplattedFromSplat ) ) ;
72
+ Add ( new RenameSymbolParamsSerialized ( RenameVariableData . VariableInForeachDuplicateAssignment ) ) ;
73
+ Add ( new RenameSymbolParamsSerialized ( RenameVariableData . VariableInForloopDuplicateAssignment ) ) ;
74
+ }
275
75
}
276
- [ Fact ]
277
- public void VariableInForeachDuplicateAssignment ( )
278
- {
279
- RenameSymbolParams request = RenameVariableData . VariableInForeachDuplicateAssignment ;
280
- ScriptFile scriptFile = GetTestScript ( request . FileName ) ;
281
- ScriptFile expectedContent = GetTestScript ( request . FileName . Substring ( 0 , request . FileName . Length - 4 ) + "Renamed.ps1" ) ;
282
76
283
- string modifiedcontent = TestRenaming ( scriptFile , request ) ;
284
-
285
- Assert . Equal ( expectedContent . Contents , modifiedcontent ) ;
286
- }
287
- [ Fact ]
288
- public void VariableInForloopDuplicateAssignment ( )
77
+ [ Theory ]
78
+ [ ClassData ( typeof ( VariableRenameTestData ) ) ]
79
+ public void Rename ( RenameSymbolParamsSerialized s )
289
80
{
290
- RenameSymbolParams request = RenameVariableData . VariableInForloopDuplicateAssignment ;
81
+ RenameSymbolParamsSerialized request = s ;
291
82
ScriptFile scriptFile = GetTestScript ( request . FileName ) ;
292
83
ScriptFile expectedContent = GetTestScript ( request . FileName . Substring ( 0 , request . FileName . Length - 4 ) + "Renamed.ps1" ) ;
293
84
@@ -296,4 +87,5 @@ public void VariableInForloopDuplicateAssignment()
296
87
Assert . Equal ( expectedContent . Contents , modifiedcontent ) ;
297
88
}
298
89
}
90
+
299
91
}
0 commit comments