Skip to content

Commit 5b1c7e3

Browse files
CopilotT-Gro
andcommitted
Fix test to use SourceText and RoslynHelpers for proper range extraction
Co-authored-by: T-Gro <[email protected]>
1 parent fca333a commit 5b1c7e3

File tree

1 file changed

+42
-51
lines changed

1 file changed

+42
-51
lines changed

vsintegration/tests/FSharp.Editor.Tests/SemanticClassificationServiceTests.fs

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -247,71 +247,62 @@ module ``It should still show up as a keyword even if the type parameter is inva
247247
"""
248248
type TestType() =
249249
member _.memb(?optional:string) = optional
250-
member _.anotherMember(?opt1:int, ?opt2:string) = (opt1, opt2)
251250
"""
252251

253252
let ranges = getRanges sourceText
254253

255-
// Debug: print all ranges to understand what we're getting
256-
let allClassifications =
257-
ranges
258-
|> List.map (fun item ->
259-
let startLine = item.Range.StartLine
260-
let startCol = item.Range.StartColumn
261-
let endLine = item.Range.EndLine
262-
let endCol = item.Range.EndColumn
263-
let lines = sourceText.Split('\n')
264-
265-
let text =
266-
if startLine = endLine && startLine < lines.Length then
267-
let line = lines.[startLine]
268-
269-
if startCol < line.Length && endCol <= line.Length then
270-
line.Substring(startCol, endCol - startCol)
271-
else
272-
sprintf "[out of bounds: %d-%d in line length %d]" startCol endCol line.Length
273-
else
274-
"[multi-line]"
275-
276-
sprintf "Line %d, Col %d-%d: '%s' (%A)" startLine startCol endCol text item.Type)
277-
|> String.concat "\n"
254+
// The issue was that QuickParse returning None for '?' caused misclassification
255+
// This test verifies that we get semantic classification data and nothing is
256+
// incorrectly classified as a type or namespace due to the ? prefix
278257

279-
// The test should verify that optional parameter usage (the return value) is classified
280-
// We check for "optional" identifier in the body (after =)
281-
let optionalUsageRanges =
258+
// Look for any identifier "optional" in the classifications
259+
let text = SourceText.From(sourceText)
260+
261+
let optionalRanges =
282262
ranges
283263
|> List.filter (fun item ->
284-
let startLine = item.Range.StartLine
285-
let startCol = item.Range.StartColumn
286-
let endCol = item.Range.EndColumn
287-
let lines = sourceText.Split('\n')
288-
289-
if startLine < lines.Length then
290-
let line = lines.[startLine]
291-
292-
if startCol < line.Length && endCol <= line.Length then
293-
let text = line.Substring(startCol, endCol - startCol)
294-
text = "optional"
295-
else
296-
false
297-
else
264+
try
265+
// Get the actual text from the source using SourceText
266+
let span = RoslynHelpers.TryFSharpRangeToTextSpan(text, item.Range)
267+
268+
match span with
269+
| ValueSome textSpan ->
270+
let actualText = text.GetSubText(textSpan).ToString()
271+
actualText = "optional"
272+
| ValueNone -> false
273+
with _ ->
298274
false)
299275

300-
// Provide detailed error message if test fails
276+
// Provide detailed diagnostics if test fails
277+
let allClassifications =
278+
ranges
279+
|> List.map (fun item ->
280+
try
281+
let span = RoslynHelpers.TryFSharpRangeToTextSpan(text, item.Range)
282+
283+
let textStr =
284+
match span with
285+
| ValueSome ts -> text.GetSubText(ts).ToString()
286+
| ValueNone -> "[no span]"
287+
288+
sprintf "Range %A: '%s' (%A)" item.Range textStr item.Type
289+
with ex ->
290+
sprintf "Range %A: [error: %s] (%A)" item.Range ex.Message item.Type)
291+
|> String.concat "\n"
292+
301293
let errorMessage =
302294
sprintf
303295
"Should have classification data for 'optional' identifier.\nFound %d ranges total.\nAll classifications:\n%s"
304296
ranges.Length
305297
allClassifications
306298

307-
Assert.True(optionalUsageRanges.Length > 0, errorMessage)
308-
309-
// If we found "optional", verify it's not classified as a type or namespace
310-
let firstOptional = optionalUsageRanges.[0]
299+
Assert.True(optionalRanges.Length > 0, errorMessage)
311300

312-
let classificationType =
313-
FSharpClassificationTypes.getClassificationTypeName firstOptional.Type
301+
// Verify that none of the "optional" occurrences are classified as type/namespace
302+
// (which would indicate the bug is present)
303+
for optionalRange in optionalRanges do
304+
let classificationType =
305+
FSharpClassificationTypes.getClassificationTypeName optionalRange.Type
314306

315-
// Should NOT be classified as a type or namespace
316-
Assert.NotEqual<string>(ClassificationTypeNames.ClassName, classificationType)
317-
Assert.NotEqual<string>(ClassificationTypeNames.NamespaceName, classificationType)
307+
Assert.NotEqual<string>(ClassificationTypeNames.ClassName, classificationType)
308+
Assert.NotEqual<string>(ClassificationTypeNames.NamespaceName, classificationType)

0 commit comments

Comments
 (0)