1+ // Licensed to the .NET Foundation under one or more agreements.
2+ // The .NET Foundation licenses this file to you under the MIT license.
3+
4+ using System ;
5+ using System . Threading . Tasks ;
6+ using Microsoft . CodeAnalysis . Razor . Protocol ;
7+ using Microsoft . CodeAnalysis . Testing ;
8+ using Microsoft . VisualStudio . Razor . LanguageClient . WrapWithTag ;
9+ using Xunit ;
10+ using Xunit . Abstractions ;
11+
12+ namespace Microsoft . VisualStudio . Razor . LanguageClient . Cohost ;
13+
14+ public class CohostWrapWithTagEndpointTest ( ITestOutputHelper testOutputHelper ) : CohostEndpointTestBase ( testOutputHelper )
15+ {
16+ [ Fact ]
17+ public async Task ValidHtmlLocation_ReturnsResult ( )
18+ {
19+ await VerifyWrapWithTagAsync (
20+ input : """
21+ <div>
22+ [||]
23+ </div>
24+ """ ,
25+ htmlResponse : new VSInternalWrapWithTagResponse (
26+ LspFactory . CreateSingleLineRange ( start : ( 0 , 0 ) , length : 10 ) ,
27+ [ LspFactory . CreateTextEdit ( position : ( 0 , 0 ) , "<p></p>" ) ]
28+ ) ,
29+ expected : new VSInternalWrapWithTagResponse (
30+ LspFactory . CreateSingleLineRange ( start : ( 0 , 0 ) , length : 10 ) ,
31+ [ LspFactory . CreateTextEdit ( position : ( 0 , 0 ) , "<p></p>" ) ]
32+ ) ) ;
33+ }
34+
35+ [ Fact ]
36+ public async Task CSharpLocation_ReturnsNull ( )
37+ {
38+ await VerifyWrapWithTagAsync (
39+ input : """
40+ @code {
41+ [||]
42+ }
43+ """ ,
44+ htmlResponse : null ,
45+ expected : null ) ;
46+ }
47+
48+ [ Fact ]
49+ public async Task ImplicitExpression_ReturnsResult ( )
50+ {
51+ await VerifyWrapWithTagAsync (
52+ input : """
53+ <div>
54+ @[||]currentCount
55+ </div>
56+ """ ,
57+ htmlResponse : new VSInternalWrapWithTagResponse (
58+ LspFactory . CreateSingleLineRange ( start : ( 1 , 4 ) , length : 16 ) ,
59+ [ LspFactory . CreateTextEdit ( position : ( 1 , 4 ) , "<span>@currentCount</span>" ) ]
60+ ) ,
61+ expected : new VSInternalWrapWithTagResponse (
62+ LspFactory . CreateSingleLineRange ( start : ( 1 , 4 ) , length : 16 ) ,
63+ [ LspFactory . CreateTextEdit ( position : ( 1 , 4 ) , "<span>@currentCount</span>" ) ]
64+ ) ) ;
65+ }
66+
67+ [ Fact ]
68+ public async Task HtmlWithTildes_FixesTextEdits ( )
69+ {
70+ await VerifyWrapWithTagAsync (
71+ input : """
72+ <div>
73+ [||]
74+ </div>
75+ """ ,
76+ htmlResponse : new VSInternalWrapWithTagResponse (
77+ LspFactory . CreateSingleLineRange ( start : ( 0 , 0 ) , length : 10 ) ,
78+ [ LspFactory . CreateTextEdit ( position : ( 0 , 0 ) , "~~~<p>~~~~</p>~~~" ) ]
79+ ) ,
80+ expected : new VSInternalWrapWithTagResponse (
81+ LspFactory . CreateSingleLineRange ( start : ( 0 , 0 ) , length : 10 ) ,
82+ [ LspFactory . CreateTextEdit ( position : ( 0 , 0 ) , "<p></p>" ) ]
83+ ) ) ;
84+ }
85+
86+ private async Task VerifyWrapWithTagAsync ( string input , VSInternalWrapWithTagResponse ? htmlResponse , VSInternalWrapWithTagResponse ? expected )
87+ {
88+ TestFileMarkupParser . GetSpan ( input , out input , out var span ) ;
89+ var document = CreateProjectAndRazorDocument ( input ) ;
90+ var sourceText = await document . GetTextAsync ( DisposalToken ) ;
91+
92+ var requestInvoker = new TestHtmlRequestInvoker ( [ ( LanguageServerConstants . RazorWrapWithTagEndpoint , htmlResponse ) ] ) ;
93+
94+ var endpoint = new CohostWrapWithTagEndpoint ( RemoteServiceInvoker , FilePathService , requestInvoker ) ;
95+
96+ var request = new VSInternalWrapWithTagParams (
97+ sourceText . GetRange ( span ) ,
98+ "div" ,
99+ new FormattingOptions ( ) ,
100+ new VersionedTextDocumentIdentifier ( )
101+ {
102+ DocumentUri = new ( document . CreateUri ( ) )
103+ } ) ;
104+
105+ var result = await endpoint . GetTestAccessor ( ) . HandleRequestAsync ( request , document , DisposalToken ) ;
106+
107+ if ( expected is null )
108+ {
109+ Assert . Null ( result ) ;
110+ }
111+ else
112+ {
113+ Assert . NotNull ( result ) ;
114+ Assert . Equal ( expected . TagRange , result . TagRange ) ;
115+ Assert . Equal ( expected . TextEdits , result . TextEdits ) ;
116+ }
117+ }
118+ }
0 commit comments