|
| 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 NUnit.Framework |
| 7 | + |
| 8 | + |
| 9 | +(* |
| 10 | + Tests in this file evaluate whether the language supports accessing functions on static classes using open |
| 11 | + The feature was added in FSharp4.7, the test cases ensure that the original errors are reproduced when the langversion:4.6 is specified |
| 12 | +*) |
| 13 | + |
| 14 | +[<TestFixture>] |
| 15 | +module OpenStaticClassesTests = |
| 16 | + |
| 17 | + let baseModule = """ |
| 18 | +module Core_OpenStaticClasses |
| 19 | +
|
| 20 | +[<AbstractClass; Sealed>] |
| 21 | +type MyMath() = |
| 22 | + static member Min(a: double, b: double) = System.Math.Min(a, b) |
| 23 | + static member Min(a: int, b: int) = System.Math.Min(a, b) |
| 24 | +
|
| 25 | +[<AbstractClass; Sealed; AutoOpen>] |
| 26 | +type AutoOpenMyMath() = |
| 27 | + static member AutoMin(a: double, b: double) = System.Math.Min(a, b) |
| 28 | + static member AutoMin(a: int, b: int) = System.Math.Min(a, b) |
| 29 | +
|
| 30 | +[<AbstractClass; Sealed; RequireQualifiedAccess>] |
| 31 | +type NotAllowedToOpen() = |
| 32 | + static member QualifiedMin(a: double, b: double) = System.Math.Min(a, b) |
| 33 | + static member QualifiedMin(a: int, b: int) = System.Math.Min(a, b) |
| 34 | +
|
| 35 | +""" |
| 36 | + |
| 37 | + [<Test>] |
| 38 | + let ``OpenStaticClassesTests - OpenSystemMathOnce - langversion:v4.6`` () = |
| 39 | + CompilerAssert.TypeCheckWithErrorsAndOptions |
| 40 | + [| "--langversion:4.6" |] |
| 41 | + (baseModule + """ |
| 42 | +module OpenSystemMathOnce = |
| 43 | +
|
| 44 | + open System.Math |
| 45 | + let x = Min(1.0, 2.0)""") |
| 46 | + [| |
| 47 | + (FSharpErrorSeverity.Error, 39, (22,28,22,32), "The namespace 'Math' is not defined."); |
| 48 | + (FSharpErrorSeverity.Error, 39, (23,24,23,27), "The value or constructor 'Min' is not defined.") |
| 49 | + |] |
| 50 | + |
| 51 | + [<Test>] |
| 52 | + let ``OpenStaticClassesTests - OpenSystemMathOnce - langversion:v4.7`` () = |
| 53 | + CompilerAssert.TypeCheckWithErrorsAndOptions |
| 54 | + [| "--langversion:4.7" |] |
| 55 | + (baseModule + """ |
| 56 | +module OpenSystemMathOnce = |
| 57 | +
|
| 58 | + open System.Math |
| 59 | + let x = Min(1.0, 2.0)""") |
| 60 | + [| |] |
| 61 | + |
| 62 | + [<Test>] |
| 63 | + let ``OpenStaticClassesTests - OpenSystemMathTwice - langversion:v4.6`` () = |
| 64 | + CompilerAssert.TypeCheckWithErrorsAndOptions |
| 65 | + [| "--langversion:4.6" |] |
| 66 | + (baseModule + """ |
| 67 | +module OpenSystemMathTwice = |
| 68 | +
|
| 69 | + open System.Math |
| 70 | + let x = Min(1.0, 2.0) |
| 71 | +
|
| 72 | + open System.Math |
| 73 | + let x2 = Min(2.0, 1.0)""") |
| 74 | + [| |
| 75 | + (FSharpErrorSeverity.Error, 39, (22,17,22,21), "The namespace 'Math' is not defined."); |
| 76 | + (FSharpErrorSeverity.Error, 39, (23,13,23,16), "The value or constructor 'Min' is not defined.") |
| 77 | + (FSharpErrorSeverity.Error, 39, (25,17,25,21), "The namespace 'Math' is not defined."); |
| 78 | + (FSharpErrorSeverity.Error, 39, (26,14,26,17), "The value or constructor 'Min' is not defined.") |
| 79 | + |] |
| 80 | + |
| 81 | + [<Test>] |
| 82 | + let ``OpenStaticClassesTests - OpenSystemMathTwice - langversion:v4.7`` () = |
| 83 | + CompilerAssert.TypeCheckWithErrorsAndOptions |
| 84 | + [| "--langversion:4.7" |] |
| 85 | + (baseModule + """ |
| 86 | +module OpenSystemMathOnce = |
| 87 | +
|
| 88 | + open System.Math |
| 89 | + let x = Min(1.0, 2.0)""") |
| 90 | + [| |] |
| 91 | + |
| 92 | + [<Test>] |
| 93 | + let ``OpenStaticClassesTests - OpenMyMathOnce - langversion:v4.6`` () = |
| 94 | + CompilerAssert.TypeCheckWithErrorsAndOptions |
| 95 | + [| "--langversion:4.6" |] |
| 96 | + (baseModule + """ |
| 97 | +module OpenMyMathOnce = |
| 98 | +
|
| 99 | + open MyMath |
| 100 | + let x = Min(1.0, 2.0) |
| 101 | + let x2 = Min(1, 2)""") |
| 102 | + [| |
| 103 | + (FSharpErrorSeverity.Error, 39, (22,10,22,16), "The namespace or module 'MyMath' is not defined."); |
| 104 | + (FSharpErrorSeverity.Error, 39, (23,13,23,16), "The value or constructor 'Min' is not defined.") |
| 105 | + (FSharpErrorSeverity.Error, 39, (24,14,24,17), "The value or constructor 'Min' is not defined.") |
| 106 | + |] |
| 107 | + |
| 108 | + [<Test>] |
| 109 | + let ``OpenStaticClassesTests - OpenMyMathOnce - langversion:v4.7`` () = |
| 110 | + CompilerAssert.TypeCheckWithErrorsAndOptions |
| 111 | + [| "--langversion:4.7" |] |
| 112 | + (baseModule + """ |
| 113 | +module OpenMyMathOnce = |
| 114 | +
|
| 115 | + open MyMath |
| 116 | + let x = Min(1.0, 2.0) |
| 117 | + let x2 = Min(1, 2)""") |
| 118 | + [| |] |
| 119 | + |
| 120 | + [<Test>] |
| 121 | + let ``OpenStaticClassesTests - DontOpenAutoMath - langversion:v4.6`` () = |
| 122 | + CompilerAssert.TypeCheckWithErrorsAndOptions |
| 123 | + [| "--langversion:4.6" |] |
| 124 | + (baseModule + """ |
| 125 | +module DontOpenAutoMath = |
| 126 | +
|
| 127 | + let x = AutoMin(1.0, 2.0) |
| 128 | + let x2 = AutoMin(1, 2)""") |
| 129 | + [| |
| 130 | + (FSharpErrorSeverity.Error, 39, (22,13,22,20), "The value or constructor 'AutoMin' is not defined."); |
| 131 | + (FSharpErrorSeverity.Error, 39, (23,14,23,21), "The value or constructor 'AutoMin' is not defined.") |
| 132 | + |] |
| 133 | + |
| 134 | + [<Test>] |
| 135 | + let ``OpenStaticClassesTests - DontOpenAutoMath - langversion:v4.7`` () = |
| 136 | + CompilerAssert.TypeCheckWithErrorsAndOptions |
| 137 | + [| "--langversion:4.7" |] |
| 138 | + (baseModule + """ |
| 139 | +module DontOpenAutoMath = |
| 140 | +
|
| 141 | + let x = AutoMin(1.0, 2.0) |
| 142 | + let x2 = AutoMin(1, 2)""") |
| 143 | + [| |] |
| 144 | + |
| 145 | + [<Test>] |
| 146 | + let ``OpenStaticClassesTests - OpenAutoMath - langversion:v4.6`` () = |
| 147 | + CompilerAssert.TypeCheckWithErrorsAndOptions |
| 148 | + [| "--langversion:4.6" |] |
| 149 | + (baseModule + """ |
| 150 | +module OpenAutoMath = |
| 151 | + open AutoOpenMyMath |
| 152 | + //open NotAllowedToOpen |
| 153 | +
|
| 154 | + let x = AutoMin(1.0, 2.0) |
| 155 | + let x2 = AutoMin(1, 2)""") |
| 156 | + [| |
| 157 | + (FSharpErrorSeverity.Error, 39, (21,10,21,24), "The namespace or module 'AutoOpenMyMath' is not defined."); |
| 158 | + (FSharpErrorSeverity.Error, 39, (24,13,24,20), "The value or constructor 'AutoMin' is not defined.") |
| 159 | + (FSharpErrorSeverity.Error, 39, (25,14,25,21), "The value or constructor 'AutoMin' is not defined.") |
| 160 | + |] |
| 161 | + |
| 162 | + [<Test>] |
| 163 | + let ``OpenStaticClassesTests - OpenAutoMath - langversion:v4.7`` () = |
| 164 | + CompilerAssert.TypeCheckWithErrorsAndOptions |
| 165 | + [| "--langversion:4.7" |] |
| 166 | + (baseModule + """ |
| 167 | +module OpenAutoMath = |
| 168 | + open AutoOpenMyMath |
| 169 | + //open NotAllowedToOpen |
| 170 | +
|
| 171 | + let x = AutoMin(1.0, 2.0) |
| 172 | + let x2 = AutoMin(1, 2)""") |
| 173 | + [| |] |
0 commit comments