Skip to content

Commit d7f1fd8

Browse files
Copilotabonie
authored andcommitted
Add C# interop tests for obsolete field/event/method/property filtering
Fix issue number to #13512. Add ObsoleteMembersClass to CSharp_Analysis with obsolete and non-obsolete members. Add 6 C# interop completion tests verifying all obsolete member types are consistently hidden. Agent-Logs-Url: https://github.com/dotnet/fsharp/sessions/1d21d452-3f55-4d56-898c-0b50980050b5 Co-authored-by: abonie <20281641+abonie@users.noreply.github.com>
1 parent f9aae25 commit d7f1fd8

4 files changed

Lines changed: 87 additions & 3 deletions

File tree

docs/release-notes/.FSharp.Compiler.Service/11.0.100.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* Fix `YieldFromFinal`/`ReturnFromFinal` being incorrectly called in non-tail positions (`for`, `use`, `use!`, `try/with` handler). ([Issue #19402](https://github.com/dotnet/fsharp/issues/19402), [PR #19403](https://github.com/dotnet/fsharp/pull/19403))
1818
* Fixed how the source ranges of warn directives are reported (as trivia) in the parser output (by not reporting leading spaces). ([Issue #19405](https://github.com/dotnet/fsharp/issues/19405), [PR #19408]((https://github.com/dotnet/fsharp/pull/19408)))
1919
* Fix UoM value type `ToString()` returning garbage values when `--checknulls+` is enabled, caused by double address-taking in codegen. ([Issue #19435](https://github.com/dotnet/fsharp/issues/19435), [PR #19440](https://github.com/dotnet/fsharp/pull/19440))
20-
* Fix completion inconsistently showing some obsolete members (fields and events) while hiding others (methods and properties). All obsolete members are now consistently hidden by default. ([Issue #13693](https://github.com/dotnet/fsharp/issues/13693), [PR #19506](https://github.com/dotnet/fsharp/pull/19506))
20+
* Fix completion inconsistently showing some obsolete members (fields and events) while hiding others (methods and properties). All obsolete members are now consistently hidden by default. ([Issue #13512](https://github.com/dotnet/fsharp/issues/13512), [PR #19506](https://github.com/dotnet/fsharp/pull/19506))
2121

2222
### Added
2323

tests/FSharp.Compiler.Service.Tests/Checker.fs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ module Checker =
162162
let parseResults, checkResults = getParseAndCheckResults context.Source
163163
checkResults.GetCodeCompletionSuggestions(context, parseResults, options)
164164

165+
let getCompletionInfoWithCompilerAndCompletionOptions (compilerOptions: string array) (completionOptions: FSharpCodeCompletionOptions) (markedSource: string) =
166+
let context = getCompletionContext markedSource
167+
let parseResults, checkResults = getParseAndCheckResultsWithOptions compilerOptions context.Source
168+
checkResults.GetCodeCompletionSuggestions(context, parseResults, completionOptions)
169+
165170
let getCompletionInfo markedSource =
166171
getCompletionInfoWithOptions FSharpCodeCompletionOptions.Default markedSource
167172

tests/FSharp.Compiler.Service.Tests/CompletionTests.fs

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ exception E
691691
try () with E{caret}
692692
"""
693693

694-
// https://github.com/dotnet/fsharp/issues/13693
694+
// https://github.com/dotnet/fsharp/issues/13512
695695
[<Fact>]
696696
let ``Event - Instance 01`` () =
697697
assertItem "Ev" """
@@ -702,7 +702,7 @@ type T() =
702702
T().{caret}
703703
"""
704704

705-
// https://github.com/dotnet/fsharp/issues/13693
705+
// https://github.com/dotnet/fsharp/issues/13512
706706
[<Fact>]
707707
let ``Event - Static 01`` () =
708708
assertItem "Ev" """
@@ -713,6 +713,64 @@ type T() =
713713
T.{caret}
714714
"""
715715

716+
/// Helper to assert completion with a reference to the CSharp_Analysis assembly
717+
let private assertCSharpInteropItem name source =
718+
let csharpAssembly = PathRelativeToTestAssembly "CSharp_Analysis.dll"
719+
let compilerOptions = [| $"-r:{csharpAssembly}" |]
720+
[allowObsoleteOptions; disallowObsoleteOptions]
721+
|> List.iter (fun completionOptions ->
722+
let contains = completionOptions.SuggestObsoleteSymbols
723+
let info = Checker.getCompletionInfoWithCompilerAndCompletionOptions compilerOptions completionOptions source
724+
assertItemsWithNames contains [name] info
725+
)
726+
727+
// https://github.com/dotnet/fsharp/issues/13512
728+
[<Fact>]
729+
let ``CSharp - Obsolete field is hidden`` () =
730+
assertCSharpInteropItem "ObsoleteField" """
731+
open FSharp.Compiler.Service.Tests
732+
ObsoleteMembersClass.{caret}
733+
"""
734+
735+
// https://github.com/dotnet/fsharp/issues/13512
736+
[<Fact>]
737+
let ``CSharp - Obsolete method is hidden`` () =
738+
assertCSharpInteropItem "ObsoleteMethod" """
739+
open FSharp.Compiler.Service.Tests
740+
ObsoleteMembersClass.{caret}
741+
"""
742+
743+
// https://github.com/dotnet/fsharp/issues/13512
744+
[<Fact>]
745+
let ``CSharp - Obsolete property is hidden`` () =
746+
assertCSharpInteropItem "ObsoleteProperty" """
747+
open FSharp.Compiler.Service.Tests
748+
ObsoleteMembersClass.{caret}
749+
"""
750+
751+
// https://github.com/dotnet/fsharp/issues/13512
752+
[<Fact>]
753+
let ``CSharp - Obsolete event is hidden`` () =
754+
assertCSharpInteropItem "ObsoleteEvent" """
755+
open FSharp.Compiler.Service.Tests
756+
ObsoleteMembersClass.{caret}
757+
"""
758+
759+
// https://github.com/dotnet/fsharp/issues/13512
760+
[<Fact>]
761+
let ``CSharp - Non-obsolete members are always shown`` () =
762+
let csharpAssembly = PathRelativeToTestAssembly "CSharp_Analysis.dll"
763+
let compilerOptions = [| $"-r:{csharpAssembly}" |]
764+
let source = """
765+
open FSharp.Compiler.Service.Tests
766+
ObsoleteMembersClass.{caret}
767+
"""
768+
[allowObsoleteOptions; disallowObsoleteOptions]
769+
|> List.iter (fun completionOptions ->
770+
let info = Checker.getCompletionInfoWithCompilerAndCompletionOptions compilerOptions completionOptions source
771+
assertItemsWithNames true ["NonObsoleteField"; "NonObsoleteMethod"; "NonObsoleteProperty"; "NonObsoleteEvent"] info
772+
)
773+
716774

717775
module PatternNameSuggestions =
718776
let private suggestPatternNames = { FSharpCodeCompletionOptions.Default with SuggestPatternNames = true }

tests/service/data/CSharp_Analysis/CSharpClass.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,25 @@ public class DummyClass
155155
{
156156
}
157157
}
158+
159+
/// <summary>
160+
/// Class with obsolete members for testing completion filtering (issue #13512).
161+
/// </summary>
162+
public class ObsoleteMembersClass
163+
{
164+
[Obsolete("Field is obsolete")] public static readonly int ObsoleteField = 1;
165+
166+
[Obsolete("Method is obsolete")]
167+
public static void ObsoleteMethod()
168+
{
169+
}
170+
171+
[Obsolete("Property is obsolete")] public static int ObsoleteProperty => 1;
172+
[Obsolete("Event is obsolete")] public static event EventHandler ObsoleteEvent;
173+
174+
public static readonly int NonObsoleteField = 2;
175+
public static void NonObsoleteMethod() { }
176+
public static int NonObsoleteProperty => 2;
177+
public static event EventHandler NonObsoleteEvent;
178+
}
158179
}

0 commit comments

Comments
 (0)