|
| 1 | +// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. |
| 2 | + |
| 3 | +namespace FSharp.Compiler.UnitTests |
| 4 | + |
| 5 | +open FSharp.Compiler.SourceCodeServices |
| 6 | +open FSharp.Reflection |
| 7 | +open FSharp.Test |
| 8 | +open FSharp.Test.Utilities |
| 9 | +open FSharp.Test.Utilities.Utilities |
| 10 | +open NUnit.Framework |
| 11 | + |
| 12 | +[<TestFixture>] |
| 13 | +module ILMemberAccessTests = |
| 14 | + |
| 15 | + let csharpBaseClass = """ |
| 16 | +namespace ExhaustiveCombinations |
| 17 | +{ |
| 18 | + public class CSharpBaseClass |
| 19 | + { |
| 20 | + public string GetPublicSetInternal { get; internal set; } |
| 21 | + public string GetPublicSetProtected { get; protected set; } |
| 22 | + public string GetPublicSetPrivateProtected { get; private protected set; } |
| 23 | + public string GetPublicSetProtectedInternal { get; protected internal set; } |
| 24 | + public string GetPublicSetPrivate { get; private set; } |
| 25 | +
|
| 26 | + public string SetPublicGetInternal { internal get; set; } |
| 27 | + public string SetPublicGetProtected { protected get; set; } |
| 28 | + public string SetPublicGetPrivateProtected { private protected get; set; } |
| 29 | + public string SetPublicGetProtectedInternal { protected internal get; set; } |
| 30 | + public string SetPublicGetPrivate { private get; set; } |
| 31 | + } |
| 32 | +} |
| 33 | +""" |
| 34 | + |
| 35 | + let fsharpBaseClass = """ |
| 36 | +namespace ExhaustiveCombinations |
| 37 | +
|
| 38 | +open System |
| 39 | +
|
| 40 | +type FSharpBaseClass () = |
| 41 | +
|
| 42 | + member this.GetPublicSetInternal with public get() = "" and internal set (parameter:string) = ignore parameter |
| 43 | + member this.GetPublicSetPrivate with public get() = "" and private set (parameter:string) = ignore parameter |
| 44 | + member this.SetPublicGetInternal with internal get() = "" and public set (parameter:string) = ignore parameter |
| 45 | + member this.SetPublicGetPrivate with private get() = "" and public set (parameter:string) = ignore parameter |
| 46 | +
|
| 47 | +""" |
| 48 | + |
| 49 | + |
| 50 | + [<Test>] |
| 51 | + let ``VerifyVisibility of Properties C# class F# derived class -- AccessPublicStuff`` () = |
| 52 | + |
| 53 | + let fsharpSource = |
| 54 | + fsharpBaseClass + """ |
| 55 | +open System |
| 56 | +open ExhaustiveCombinations |
| 57 | +
|
| 58 | +type MyFSharpClass () = |
| 59 | + inherit CSharpBaseClass() |
| 60 | +
|
| 61 | + member this.AccessPublicStuff() = |
| 62 | +
|
| 63 | + this.GetPublicSetInternal <- "1" // Inaccessible |
| 64 | + let _ = this.GetPublicSetInternal // Accessible |
| 65 | +
|
| 66 | + this.GetPublicSetPrivateProtected <- "1" // Accessible |
| 67 | + let _ = this.GetPublicSetPrivateProtected // Accessible |
| 68 | +
|
| 69 | + this.GetPublicSetProtectedInternal <- "1" // Accessible |
| 70 | + let _ = this.GetPublicSetProtectedInternal // Accessible |
| 71 | +
|
| 72 | + this.GetPublicSetProtected <- "1" // Accessible |
| 73 | + let _ = this.GetPublicSetProtected // Accessible |
| 74 | +
|
| 75 | + this.GetPublicSetPrivate <- "1" // Inaccessible |
| 76 | + let _ = this.GetPublicSetPrivate // Accessible |
| 77 | + () |
| 78 | +""" |
| 79 | + |
| 80 | + let csCmpl = |
| 81 | + CompilationUtil.CreateCSharpCompilation(csharpBaseClass, CSharpLanguageVersion.CSharp8, TargetFramework.NetCoreApp30) |
| 82 | + |> CompilationReference.Create |
| 83 | + |
| 84 | + let fsCmpl = |
| 85 | + Compilation.Create(fsharpSource, Fsx, Exe, options = [|"--langversion:preview"|], cmplRefs = [csCmpl]) |
| 86 | + |
| 87 | + CompilerAssert.CompileWithErrors(fsCmpl, [| |
| 88 | + (FSharpErrorSeverity.Error, 491, (22, 9, 22, 41), |
| 89 | + "The member or object constructor 'GetPublicSetInternal' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions."); |
| 90 | + (FSharpErrorSeverity.Error, 491, (25, 9, 25, 49), |
| 91 | + "The member or object constructor 'GetPublicSetPrivateProtected' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions."); |
| 92 | + (FSharpErrorSeverity.Error, 491, (34, 9, 34, 40), |
| 93 | + "The member or object constructor 'GetPublicSetPrivate' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions.")|]) |
| 94 | + |
| 95 | + |
| 96 | + [<Test>] |
| 97 | + let ``VerifyVisibility of Properties C# class F# non-derived class -- AccessPublicStuff`` () = |
| 98 | + |
| 99 | + let fsharpSource = |
| 100 | + fsharpBaseClass + """ |
| 101 | +open System |
| 102 | +open ExhaustiveCombinations |
| 103 | +
|
| 104 | +type MyFSharpClass () = |
| 105 | +
|
| 106 | + member _.AccessPublicStuff() = |
| 107 | + let bc = new CSharpBaseClass() |
| 108 | +
|
| 109 | + bc.GetPublicSetInternal <- "1" // Inaccessible |
| 110 | + let _ = bc.GetPublicSetInternal // Accessible |
| 111 | +
|
| 112 | + bc.GetPublicSetPrivateProtected <- "1" // Inaccessible |
| 113 | + let _ = bc.GetPublicSetPrivateProtected // Accessible |
| 114 | +
|
| 115 | + bc.GetPublicSetProtectedInternal <- "1" // Accessible |
| 116 | + let _ = bc.GetPublicSetProtectedInternal // Inaccessible |
| 117 | +
|
| 118 | + bc.GetPublicSetProtected <- "1" // Inaccessible |
| 119 | + let _ = bc.SetPublicGetProtected // Accessible |
| 120 | +
|
| 121 | + bc.GetPublicSetPrivate <- "1" // Inaccessible |
| 122 | + let _ = bc.GetPublicSetPrivate // Accessible |
| 123 | + () |
| 124 | +""" |
| 125 | + |
| 126 | + let csCmpl = |
| 127 | + CompilationUtil.CreateCSharpCompilation(csharpBaseClass, CSharpLanguageVersion.CSharp8, TargetFramework.NetCoreApp30) |
| 128 | + |> CompilationReference.Create |
| 129 | + |
| 130 | + let fsCmpl = |
| 131 | + Compilation.Create(fsharpSource, Fsx, Exe, options = [|"--langversion:preview"|], cmplRefs = [csCmpl]) |
| 132 | + |
| 133 | + CompilerAssert.CompileWithErrors(fsCmpl, [| |
| 134 | + (FSharpErrorSeverity.Error, 491, (22, 9, 22, 39), |
| 135 | + "The member or object constructor 'GetPublicSetInternal' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions."); |
| 136 | + (FSharpErrorSeverity.Error, 491, (25, 9, 25, 47), |
| 137 | + "The member or object constructor 'GetPublicSetPrivateProtected' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions."); |
| 138 | + (FSharpErrorSeverity.Error, 491, (28, 9, 28, 48), |
| 139 | + "The member or object constructor 'GetPublicSetProtectedInternal' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions."); |
| 140 | + (FSharpErrorSeverity.Error, 491, (31, 9, 31, 40), |
| 141 | + "The member or object constructor 'GetPublicSetProtected' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions."); |
| 142 | + (FSharpErrorSeverity.Error, 491, (32, 17, 32, 41), |
| 143 | + "The member or object constructor 'SetPublicGetProtected' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions."); |
| 144 | + (FSharpErrorSeverity.Error, 491, (34, 9, 34, 38), |
| 145 | + "The member or object constructor 'GetPublicSetPrivate' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions.")|]) |
| 146 | + |
| 147 | + |
| 148 | + [<Test>] |
| 149 | + let ``VerifyVisibility of Properties F# base F# derived class -- AccessPublicStuff`` () = |
| 150 | + |
| 151 | + let fsharpSource = |
| 152 | + fsharpBaseClass + """ |
| 153 | +open System |
| 154 | +open ExhaustiveCombinations |
| 155 | +
|
| 156 | +type MyFSharpClass () = |
| 157 | + inherit FSharpBaseClass() |
| 158 | +
|
| 159 | + member this.AccessPublicStuff() = |
| 160 | +
|
| 161 | + this.GetPublicSetInternal <- "1" // Inaccessible |
| 162 | + let _ = this.GetPublicSetInternal // Accessible |
| 163 | +
|
| 164 | + this.GetPublicSetPrivate <- "1" // Inaccessible |
| 165 | + let _ = this.GetPublicSetPrivate // Accessible |
| 166 | +
|
| 167 | + this.SetPublicGetInternal <- "1" // Accessible |
| 168 | + let _ = this.SetPublicGetInternal // Inaccessible |
| 169 | +
|
| 170 | + this.SetPublicGetPrivate <- "1" // Accessible |
| 171 | + let _ = this.SetPublicGetPrivate // accessible |
| 172 | +
|
| 173 | + () |
| 174 | +""" |
| 175 | + |
| 176 | + let csCmpl = |
| 177 | + CompilationUtil.CreateCSharpCompilation(csharpBaseClass, CSharpLanguageVersion.CSharp8, TargetFramework.NetCoreApp30) |
| 178 | + |> CompilationReference.Create |
| 179 | + |
| 180 | + let fsCmpl = |
| 181 | + Compilation.Create(fsharpSource, Fsx, Exe, options = [|"--langversion:preview"|], cmplRefs = [csCmpl]) |
| 182 | + |
| 183 | + CompilerAssert.CompileWithErrors(fsCmpl, [| |
| 184 | + (FSharpErrorSeverity.Error, 810, (25, 9, 25, 33), |
| 185 | + "Property 'GetPublicSetPrivate' cannot be set"); |
| 186 | + (FSharpErrorSeverity.Error, 807, (32, 17, 32, 41), |
| 187 | + "Property 'SetPublicGetPrivate' is not readable") |
| 188 | + |]) |
| 189 | + |
| 190 | + |
| 191 | + [<Test>] |
| 192 | + let ``VerifyVisibility of Properties F# class F# non-derived class -- AccessPublicStuff`` () = |
| 193 | + |
| 194 | + let fsharpSource = |
| 195 | + fsharpBaseClass + """ |
| 196 | +open System |
| 197 | +open ExhaustiveCombinations |
| 198 | +
|
| 199 | +type MyFSharpClass () = |
| 200 | +
|
| 201 | + member _.AccessPublicStuff() = |
| 202 | + let bc = new FSharpBaseClass() |
| 203 | +
|
| 204 | + bc.GetPublicSetInternal <- "1" // Inaccessible |
| 205 | + let _ = bc.GetPublicSetInternal // Accessible |
| 206 | +
|
| 207 | + bc.GetPublicSetPrivate <- "1" // Inaccessible |
| 208 | + let _ = bc.GetPublicSetPrivate // Accessible |
| 209 | + () |
| 210 | +""" |
| 211 | + |
| 212 | + let csCmpl = |
| 213 | + CompilationUtil.CreateCSharpCompilation(csharpBaseClass, CSharpLanguageVersion.CSharp8, TargetFramework.NetCoreApp30) |
| 214 | + |> CompilationReference.Create |
| 215 | + |
| 216 | + let fsCmpl = |
| 217 | + Compilation.Create(fsharpSource, Fsx, Exe, options = [|"--langversion:preview"|], cmplRefs = [csCmpl]) |
| 218 | + |
| 219 | + CompilerAssert.CompileWithErrors(fsCmpl, [| |
| 220 | + (FSharpErrorSeverity.Error, 810, (25, 9, 25, 31), |
| 221 | + "Property 'GetPublicSetPrivate' cannot be set")|]) |
| 222 | + |
| 223 | + |
| 224 | + |
0 commit comments