Skip to content

Commit 1b6e035

Browse files
committed
Completed and corrected selected range extraction functionality
FROM MERGE
1 parent 0471dbf commit 1b6e035

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/Razor/ExtractToNewComponentCodeActionProviderTest.cs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,127 @@ @namespace MarketApp.Pages.Product.Home
229229
Assert.Equal(selectionSpan.End, actionParams.ExtractEnd);
230230
}
231231

232+
[Fact]
233+
public async Task Handle_InProperMarkup_ReturnsNull()
234+
{
235+
// Arrange
236+
var documentPath = "c:/Test.razor";
237+
var contents = """
238+
page "/"
239+
240+
<PageTitle>Home</PageTitle>
241+
242+
<div id="parent">
243+
<div>
244+
<h1>Div a title</h1>
245+
<p>Div $$a par</p>
246+
</div>
247+
<div>
248+
<h1>Div b title</h1>
249+
<p>Div b par</p>
250+
</div>
251+
</div
252+
253+
<h1>Hello, world!</h1>
254+
255+
Welcome to your new app.
256+
""";
257+
TestFileMarkupParser.GetPosition(contents, out contents, out var cursorPosition);
258+
259+
var request = new VSCodeActionParams()
260+
{
261+
TextDocument = new VSTextDocumentIdentifier { Uri = new Uri(documentPath) },
262+
Range = new Range(),
263+
Context = new VSInternalCodeActionContext()
264+
};
265+
266+
var location = new SourceLocation(cursorPosition, -1, -1);
267+
var context = CreateRazorCodeActionContext(request, location, documentPath, contents);
268+
269+
var provider = new ExtractToNewComponentCodeActionProvider(LoggerFactory);
270+
271+
// Act
272+
var commandOrCodeActionContainer = await provider.ProvideAsync(context, default);
273+
274+
// Assert
275+
Assert.Null(commandOrCodeActionContainer);
276+
}
277+
278+
[Theory]
279+
[InlineData("""
280+
<div id="parent">
281+
[|<div>
282+
<h1>Div a title</h1>
283+
<p>Div a par</p>
284+
</div>|]
285+
<div>
286+
<h1>Div b title</h1>
287+
<p>Div b par</p>
288+
</div>
289+
</div>
290+
""")]
291+
[InlineData("""
292+
<div id="parent">
293+
<div>
294+
<h1>Div a title</h1>
295+
[|<p>Div a par</p>|]
296+
</div>
297+
<div>
298+
<h1>Div b title</h1>
299+
<p>Div b par</p>
300+
</div>
301+
</div>
302+
""")]
303+
[InlineData("""
304+
<div id="parent">
305+
<div>
306+
<h1>Div a title</h1>
307+
[|<p>Div a par</p>
308+
</div>
309+
<div>
310+
<h1>Div b title</h1>
311+
<p>Div b par</p>|]
312+
</div>
313+
</div>
314+
""")]
315+
public async Task Handle_ValidElementSelection_ReturnsNotNull(string markupElementSelection)
316+
{
317+
// Arrange
318+
var documentPath = "c:/Test.razor";
319+
var contents = $$"""
320+
page "/"
321+
322+
<PageTitle>Home</PageTitle>
323+
324+
{{markupElementSelection}}
325+
326+
<h1>Hello, world!</h1>
327+
328+
Welcome to your new app.
329+
""";
330+
331+
TestFileMarkupParser.GetPositionAndSpans(
332+
contents, out contents, out int cursorPosition, out ImmutableArray<TextSpan> spans);
333+
334+
var request = new VSCodeActionParams()
335+
{
336+
TextDocument = new VSTextDocumentIdentifier { Uri = new Uri(documentPath) },
337+
Range = new Range(),
338+
Context = new VSInternalCodeActionContext()
339+
};
340+
341+
var location = new SourceLocation(cursorPosition, -1, -1);
342+
var context = CreateRazorCodeActionContext(request, location, documentPath, contents, supportsFileCreation: true);
343+
344+
var provider = new ExtractToNewComponentCodeActionProvider(LoggerFactory);
345+
346+
// Act
347+
var commandOrCodeActionContainer = await provider.ProvideAsync(context, default);
348+
349+
// Assert
350+
Assert.NotNull(commandOrCodeActionContainer);
351+
}
352+
232353
private static RazorCodeActionContext CreateRazorCodeActionContext(VSCodeActionParams request, SourceLocation location, string filePath, string text, bool supportsFileCreation = true)
233354
=> CreateRazorCodeActionContext(request, location, filePath, text, relativePath: filePath, supportsFileCreation: supportsFileCreation);
234355

0 commit comments

Comments
 (0)