Skip to content

Commit e2549ad

Browse files
committed
Add some html coverage
1 parent d636a6f commit e2549ad

File tree

1 file changed

+64
-11
lines changed

1 file changed

+64
-11
lines changed

src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/Cohost/CohostFoldingRangeEndpointTest.cs

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

4-
using System;
54
using System.Collections.Immutable;
65
using System.Linq;
76
using System.Text;
87
using System.Threading.Tasks;
98
using Microsoft.AspNetCore.Razor;
109
using Microsoft.AspNetCore.Razor.Language;
10+
using Microsoft.AspNetCore.Razor.PooledObjects;
1111
using Microsoft.CodeAnalysis.Razor.Workspaces;
1212
using Microsoft.CodeAnalysis.Testing;
1313
using Microsoft.CodeAnalysis.Text;
@@ -197,40 +197,93 @@ public void M() {[|
197197
}|]
198198
""");
199199

200+
[Fact]
201+
public Task HtmlAndCSharp()
202+
=> VerifyFoldingRangesAsync("""
203+
<div>{|html:
204+
Hello @_name
205+
206+
<div>{|html:
207+
Nests aren't just for birds!
208+
</div>|}
209+
</div>|}
210+
211+
@code {[|
212+
private string _name = "Dave";
213+
214+
public void M() {[|
215+
}|]
216+
}|]
217+
""");
218+
200219
private async Task VerifyFoldingRangesAsync(string input, string? fileKind = null)
201220
{
202-
TestFileMarkupParser.GetSpans(input, out var source, out ImmutableArray<TextSpan> expected);
221+
TestFileMarkupParser.GetSpans(input, out var source, out ImmutableDictionary<string, ImmutableArray<TextSpan>> spans);
203222
var document = CreateProjectAndRazorDocument(source, fileKind);
204-
205-
var requestInvoker = new TestLSPRequestInvoker([(Methods.TextDocumentFoldingRangeName, Array.Empty<FoldingRange>())]);
223+
var inputText = await document.GetTextAsync(DisposalToken);
224+
225+
var htmlSpans = spans.GetValueOrDefault("html").NullToEmpty();
226+
var htmlRanges = htmlSpans
227+
.Select(span =>
228+
{
229+
inputText.GetLineAndOffset(span.Start, out var startLine, out var startCharacter);
230+
inputText.GetLineAndOffset(span.End, out var endLine, out var endCharacter);
231+
return new FoldingRange()
232+
{
233+
StartLine = startLine,
234+
StartCharacter = startCharacter,
235+
EndLine = endLine,
236+
EndCharacter = endCharacter
237+
};
238+
})
239+
.ToArray();
240+
241+
var requestInvoker = new TestLSPRequestInvoker([(Methods.TextDocumentFoldingRangeName, htmlRanges)]);
206242

207243
var endpoint = new CohostFoldingRangeEndpoint(RemoteServiceInvoker, TestHtmlDocumentSynchronizer.Instance, requestInvoker, LoggerFactory);
208244

209245
var result = await endpoint.GetTestAccessor().HandleRequestAsync(document, DisposalToken);
210246

211-
if (expected.Length == 0)
247+
if (spans.Count == 0)
212248
{
213249
Assert.Null(result);
214250
return;
215251
}
216252

217-
// Rather than comparing numbers and spans, its nicer to reconstruct the test input data and use string comparisons so we can
218-
// more easily understand what went wrong.
253+
var actual = GenerateTestInput(inputText, htmlSpans, result.AssumeNotNull());
219254

220-
var inputText = SourceText.From(source);
255+
AssertEx.EqualOrDiff(input, actual);
256+
}
257+
258+
private static string GenerateTestInput(SourceText inputText, ImmutableArray<TextSpan> htmlSpans, FoldingRange[] result)
259+
{
221260
var markerPositions = result
222261
.SelectMany(r =>
223262
new[] {
224263
(index: inputText.GetRequiredAbsoluteIndex(r.StartLine, r.StartCharacter.AssumeNotNull()), isStart: true),
225264
(index: inputText.GetRequiredAbsoluteIndex(r.EndLine, r.EndCharacter.AssumeNotNull()), isStart: false)
226265
});
227266

228-
var actual = new StringBuilder(source);
267+
var actual = new StringBuilder(inputText.ToString());
229268
foreach (var marker in markerPositions.OrderByDescending(p => p.index))
230269
{
231-
actual.Insert(marker.index, marker.isStart ? "[|" : "|]");
270+
actual.Insert(marker.index, GetMarker(marker.index, marker.isStart, htmlSpans));
271+
}
272+
273+
static string GetMarker(int index, bool isStart, ImmutableArray<TextSpan> htmlSpans)
274+
{
275+
if (isStart)
276+
{
277+
return htmlSpans.Any(r => r.Start == index)
278+
? "{|html:"
279+
: "[|";
280+
}
281+
282+
return htmlSpans.Any(r => r.End == index)
283+
? "|}"
284+
: "|]";
232285
}
233286

234-
AssertEx.EqualOrDiff(input, actual.ToString());
287+
return actual.ToString();
235288
}
236289
}

0 commit comments

Comments
 (0)