From d504546dc52f602192d349d4cd7d6404f13681e7 Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Tue, 21 Jun 2022 07:34:18 +0200 Subject: [PATCH] Unit tests for Calling extension methods on obsolete types and modules (#13297) * Unit tests for Calling extension methods on obsolete types and modules * Remove Unexpected infix operator in definition * Uopdate test to use withDiagnostics for more exhaustive error message checking * Update tests/FSharp.Compiler.ComponentTests/Language/AttributeCheckingTests.fs Co-authored-by: Petr Semkin * Update tests/FSharp.Compiler.ComponentTests/Language/AttributeCheckingTests.fs Co-authored-by: Petr Semkin Co-authored-by: Petr Semkin --- .../Language/AttributeCheckingTests.fs | 332 ++++++++++++++++-- 1 file changed, 301 insertions(+), 31 deletions(-) diff --git a/tests/FSharp.Compiler.ComponentTests/Language/AttributeCheckingTests.fs b/tests/FSharp.Compiler.ComponentTests/Language/AttributeCheckingTests.fs index 26f23eb1b..0896a2a67 100644 --- a/tests/FSharp.Compiler.ComponentTests/Language/AttributeCheckingTests.fs +++ b/tests/FSharp.Compiler.ComponentTests/Language/AttributeCheckingTests.fs @@ -48,7 +48,6 @@ type C() = Fsx """ open System - type C() = [] @@ -75,8 +74,9 @@ let c = C() |> ignoreWarnings |> compile |> shouldFail - |> withErrorCode 101 - |> withErrorMessage "This construct is deprecated. Use B instead" + |> withDiagnostics [ + (Error 101, Line 9, Col 9, Line 9, Col 10, "This construct is deprecated. Use B instead") + ] [] let ``Obsolete attribute is taken into account when used on a member and invoking the member`` () = @@ -93,8 +93,9 @@ c.Update() |> ignoreWarnings |> compile |> shouldFail - |> withErrorCode 101 - |> withErrorMessage "This construct is deprecated. Use B instead" + |> withDiagnostics [ + (Error 101, Line 9, Col 1, Line 9, Col 9, "This construct is deprecated. Use B instead") + ] [] let ``Obsolete attribute is taken into account when used on type and invoking the member`` () = @@ -111,9 +112,10 @@ c.Update() |> ignoreWarnings |> compile |> shouldFail - |> withErrorCodes [ 101; 101] - |> withErrorMessages [ "This construct is deprecated. Use B instead"; "This construct is deprecated. Use B instead"] - + |> withDiagnostics [ + (Error 101, Line 8, Col 9, Line 8, Col 10, "This construct is deprecated. Use B instead"); + (Error 101, Line 9, Col 1, Line 9, Col 9, "This construct is deprecated. Use B instead") + ] [] let ``Obsolete attribute is taken into account when used on struct type and invoking the member`` () = @@ -127,14 +129,14 @@ type C = let c = C() c.Update() - """ |> ignoreWarnings |> compile |> shouldFail - |> withErrorCodes [ 101; 101] - |> withErrorMessages [ "This construct is deprecated. Use B instead"; "This construct is deprecated. Use B instead"] - + |> withDiagnostics [ + (Error 101, Line 9, Col 9, Line 9, Col 10, "This construct is deprecated. Use B instead"); + (Error 101, Line 10, Col 1, Line 10, Col 9, "This construct is deprecated. Use B instead") + ] [] let ``Obsolete attribute is taken into account when used on struct type and instantiate the type`` () = @@ -151,15 +153,15 @@ let c = C() |> ignoreWarnings |> compile |> shouldFail - |> withErrorCode 101 - |> withErrorMessage "This construct is deprecated. Use B instead" + |> withDiagnostics [ + (Error 101, Line 9, Col 9, Line 9, Col 10, "This construct is deprecated. Use B instead") + ] [] let ``Obsolete attribute is taken into account when used on a struct member and invoking the member`` () = Fsx """ open System - [] type C = [] @@ -171,16 +173,15 @@ c.Update() |> ignoreWarnings |> compile |> shouldFail - |> withErrorCode 101 - |> withErrorMessage "This construct is deprecated. Use B instead" - + |> withDiagnostics [ + (Error 101, Line 10, Col 1, Line 10, Col 9, "This construct is deprecated. Use B instead") + ] [] let ``Obsolete attribute is taken into account when used on a record property`` () = Fsx """ open System - type C = { [] X: int } @@ -189,16 +190,15 @@ let c = { X = 0 } |> ignoreWarnings |> compile |> shouldFail - |> withErrorCode 101 - |> withErrorMessage "This construct is deprecated. Use B instead" - + |> withDiagnostics [ + (Error 101, Line 7, Col 9, Line 7, Col 18, "This construct is deprecated. Use B instead") + ] [] let ``Obsolete attribute is taken into account when used on a record and member invocation`` () = Fsx """ open System - [] type C = { X : int } @@ -210,15 +210,15 @@ C.Update() |> ignoreWarnings |> compile |> shouldFail - |> withErrorCode 101 - |> withErrorMessage "This construct is deprecated. Use B instead" + |> withDiagnostics [ + (Error 101, Line 10, Col 1, Line 10, Col 2, "This construct is deprecated. Use B instead") + ] [] let ``Obsolete attribute is taken into account when used on a record member and method invocation`` () = Fsx """ open System - type C = { X : int } [] @@ -229,8 +229,9 @@ C.Update() |> ignoreWarnings |> compile |> shouldFail - |> withErrorCode 101 - |> withErrorMessage "This construct is deprecated. Use B instead" + |> withDiagnostics [ + (Error 101, Line 9, Col 1, Line 9, Col 9, "This construct is deprecated. Use B instead") + ] [] let ``Obsolete attribute is taken into account when used on an enum and invocation`` () = @@ -247,8 +248,9 @@ let c = Color.Red |> ignoreWarnings |> compile |> shouldFail - |> withErrorCode 101 - |> withErrorMessage "This construct is deprecated. Use B instead" + |> withDiagnostics [ + (Error 101, Line 9, Col 9, Line 9, Col 14, "This construct is deprecated. Use B instead") + ] [] let ``Obsolete attribute is taken into account when used on an enum entry and invocation`` () = @@ -263,4 +265,272 @@ let c = Color.Red """ |> ignoreWarnings |> compile - |> shouldSucceed \ No newline at end of file + |> shouldSucceed + + [] + let ``Obsolete attribute is taken into account when used on an type and use extension method`` () = + Fsx """ + +open System +open System.Runtime.CompilerServices + +[] +type Button = { Text : string } + +[] +type ButtonExtensions = + + [] + static member inline text(this: Button, text: string) = + { this with Text = text } + """ + |> ignoreWarnings + |> compile + |> shouldFail + |> withDiagnostics [ + (Error 101, Line 13, Col 37, Line 13, Col 43, "This construct is deprecated. Use B instead") + ] + + [] + let ``Obsolete attribute is taken into account when used on an type property and use extension method`` () = + Fsx """ + +open System +open System.Runtime.CompilerServices + +type Button = { [] Text : string } + +[] +type ButtonExtensions = + + [] + static member inline text(this: Button, text: string) = + { this with Text = text } + """ + |> ignoreWarnings + |> compile + |> shouldFail + |> withDiagnostics [ + (Error 101, Line 13, Col 9, Line 13, Col 34, "This construct is deprecated. Use B instead") + ] + + [] + let ``Obsolete attribute is taken into account when used on an type and property and use extension method`` () = + Fsx """ +open System +open System.Runtime.CompilerServices + +[] +type Button = { [] Text : string } + +[] +type ButtonExtensions = + + [] + static member inline text(this: Button, text: string) = + { this with Text = text } + """ + |> ignoreWarnings + |> compile + |> shouldFail + |> withDiagnostics [ + (Error 101, Line 12, Col 37, Line 12, Col 43, "This construct is deprecated. Use B instead"); + (Error 101, Line 13, Col 9, Line 13, Col 34, "This construct is deprecated. Use B instead") + ] + + [] + let ``Obsolete attribute is taken into account when used on an type property and set via module`` () = + Fsx """ +open System +open System.Runtime.CompilerServices + +type Button = { [] Text : string } + +module Button = + + let set text = { Text = text } + """ + |> ignoreWarnings + |> compile + |> shouldFail + |> withDiagnostics [ + (Error 101, Line 9, Col 20, Line 9, Col 36, "This construct is deprecated. Use B instead") + ] + + + [] + let ``Obsolete attribute is taken into account when used on an type and set property via module`` () = + Fsx """ +open System +open System.Runtime.CompilerServices + + [] +type Button = { [] Text : string } + +module Button = + + let set text = { Text = text } + """ + |> ignoreWarnings + |> compile + |> shouldFail + |> withDiagnostics [ + (Error 101, Line 10, Col 20, Line 10, Col 36, "This construct is deprecated. Use B instead") + ] + + [] + let ``Obsolete attribute is taken into account when used on an type property and set property via module using an extension method`` () = + Fsx """ +open System +open System.Runtime.CompilerServices + +type Button = { [] Text : string } + +module Button = + + let set text = { Text = text } +[] +type ButtonExtensions = + + [] + static member inline text(this: Button, text: string) = + Button.set text + """ + |> ignoreWarnings + |> compile + |> shouldFail + |> withDiagnostics [ + (Error 101, Line 9, Col 20, Line 9, Col 36, "This construct is deprecated. Use B instead") + ] + + [] + let ``Obsolete attribute is taken into account when used on an module and set property via module using an extension method`` () = + Fsx """ +open System +open System.Runtime.CompilerServices + +type Button = { Text : string } + +[] +module Button = + + let set text = { Text = text } + +[] +type ButtonExtensions = + + [] + static member inline text(this: Button, text: string) = + Button.set text + """ + |> ignoreWarnings + |> compile + |> shouldFail + |> withDiagnostics [ + (Error 101, Line 17, Col 9, Line 17, Col 15, "This construct is deprecated. Use B instead") + ] + + [] + let ``Obsolete attribute is taken into account when used on an moudle and function and set property via module using an extesnion method`` () = + Fsx """ +open System +open System.Runtime.CompilerServices + +type Button = { Text : string } + +[] +module Button = + + [] + let set text = { Text = text } + +[] +type ButtonExtensions = + + [] + static member inline text(this: Button, text: string) = + Button.set text + """ + |> ignoreWarnings + |> compile + |> shouldFail + |> withDiagnostics [ + (Error 101, Line 18, Col 9, Line 18, Col 15, "This construct is deprecated. Use B instead") + ] + + [] + let ``Obsolete attribute is taken into account when used on an moudle function and set property via module using an extesnion method`` () = + Fsx """ +open System +open System.Runtime.CompilerServices + +type Button = { Text : string } + +module Button = + + [] + let set text = { Text = text } + +[] +type ButtonExtensions = + + [] + static member inline text(this: Button, text: string) = + Button.set text + """ + |> ignoreWarnings + |> compile + |> shouldFail + |> withDiagnostics [ + (Error 101, Line 17, Col 9, Line 17, Col 19, "This construct is deprecated. Use B instead") + ] + + + [] + let ``Obsolete attribute is taken into account when used on an type extensions and used on an instance`` () = + Fsx """ +open System +open System.Runtime.CompilerServices + +type Button = { Text : string } + +[] +[] +type ButtonExtensions = + + [] + static member inline text(this: Button, text: string) = + { this with Text = text } + +let b = { Text = "Hello" } +b.text("Hello 2") |> ignore + """ + |> ignoreWarnings + |> compile + |> shouldSucceed + + [] + let ``Obsolete attribute is taken into account when used on an type extensions static function and used on an instance`` () = + Fsx """ +open System +open System.Runtime.CompilerServices + +type Button = { Text : string } + +[] +type ButtonExtensions = + + [] + [] + static member inline text(this: Button, text: string) = + { this with Text = text } + +let b = { Text = "Hello" } +b.text("Hello 2") |> ignore + """ + |> ignoreWarnings + |> compile + |> shouldFail + |> withDiagnostics [ + (Error 101, Line 16, Col 1, Line 16, Col 7, "This construct is deprecated. Use B instead") + ] \ No newline at end of file -- GitLab