Skip to content

Commit 650805b

Browse files
smclcartermp
authored andcommitted
move some error and warning tests to NUnit (#7244)
* move some error and warning tests to NUnit * CompilerAssert.ParseWithErrors now uses ParseFile instead of ParseAndCheckFileInProject * merge conflicts
1 parent 4cf7b5b commit 650805b

9 files changed

Lines changed: 152 additions & 16 deletions

File tree

tests/fsharp/Compiler/CompilerAssert.fs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,22 @@ module CompilerAssert =
217217
Assert.AreEqual(expectedErrorMessage, errorMessage)
218218
)
219219

220+
let ParseWithErrors (source: string) expectedParseErrors =
221+
let parseResults = checker.ParseFile("test.fs", SourceText.ofString source, FSharpParsingOptions.Default) |> Async.RunSynchronously
222+
223+
Assert.True(parseResults.ParseHadErrors)
224+
225+
let errors =
226+
parseResults.Errors
227+
|> Array.distinctBy (fun e -> e.Severity, e.ErrorNumber, e.StartLineAlternate, e.StartColumn, e.EndLineAlternate, e.EndColumn, e.Message)
228+
229+
Assert.AreEqual(Array.length expectedParseErrors, errors.Length, sprintf "Type check errors: %A" parseResults.Errors)
230+
231+
Array.zip errors expectedParseErrors
232+
|> Array.iter (fun (info, expectedError) ->
233+
let (expectedServerity: FSharpErrorSeverity, expectedErrorNumber: int, expectedErrorRange: int * int * int * int, expectedErrorMsg: string) = expectedError
234+
Assert.AreEqual(expectedServerity, info.Severity)
235+
Assert.AreEqual(expectedErrorNumber, info.ErrorNumber, "expectedErrorNumber")
236+
Assert.AreEqual(expectedErrorRange, (info.StartLineAlternate, info.StartColumn + 1, info.EndLineAlternate, info.EndColumn + 1), "expectedErrorRange")
237+
Assert.AreEqual(expectedErrorMsg, info.Message, "expectedErrorMsg")
238+
)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 NUnit.Framework
6+
open FSharp.Compiler.SourceCodeServices
7+
8+
[<TestFixture>]
9+
module ``Errors assigning to mutable objects`` =
10+
11+
[<Test>]
12+
let ``Assign to immutable error``() =
13+
CompilerAssert.TypeCheckSingleError
14+
"""
15+
let x = 10
16+
x <- 20
17+
18+
exit 0
19+
"""
20+
FSharpErrorSeverity.Error
21+
27
22+
(3, 1, 3, 8)
23+
"This value is not mutable. Consider using the mutable keyword, e.g. 'let mutable x = expression'."
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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 NUnit.Framework
6+
open FSharp.Compiler.SourceCodeServices
7+
8+
[<TestFixture>]
9+
module ``Warnings assigning to mutable and immutable objects`` =
10+
11+
[<Test>]
12+
let ``Unused compare with immutable when assignment might be intended``() =
13+
CompilerAssert.TypeCheckSingleError
14+
"""
15+
let x = 10
16+
let y = "hello"
17+
18+
let changeX() =
19+
x = 20
20+
y = "test"
21+
22+
exit 0
23+
"""
24+
FSharpErrorSeverity.Warning
25+
20
26+
(6, 5, 6, 11)
27+
"The result of this equality expression has type 'bool' and is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to mutate a value, then mark the value 'mutable' and use the '<-' operator e.g. 'x <- expression'."
28+
29+
[<Test>]
30+
let ``Unused compare with mutable when assignment might be intended``() =
31+
CompilerAssert.TypeCheckSingleError
32+
"""
33+
let mutable x = 10
34+
let y = "hello"
35+
36+
let changeX() =
37+
x = 20
38+
y = "test"
39+
40+
exit 0
41+
"""
42+
FSharpErrorSeverity.Warning
43+
20
44+
(6, 5, 6, 11)
45+
"The result of this equality expression has type 'bool' and is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to mutate a value, then use the '<-' operator e.g. 'x <- expression'."
46+
47+
[<Test>]
48+
let ``Unused comparison of property in dotnet object when assignment might be intended``() =
49+
CompilerAssert.TypeCheckSingleError
50+
"""
51+
open System
52+
53+
let z = new System.Timers.Timer()
54+
let y = "hello"
55+
56+
let changeProperty() =
57+
z.Enabled = true
58+
y = "test"
59+
60+
exit 0
61+
"""
62+
FSharpErrorSeverity.Warning
63+
20
64+
(8, 5, 8, 21)
65+
"The result of this equality expression has type 'bool' and is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to set a value to a property, then use the '<-' operator e.g. 'z.Enabled <- expression'."
66+
67+
[<Test>]
68+
let ``Unused comparison of property when assignment might be intended ``() =
69+
CompilerAssert.TypeCheckSingleError
70+
"""
71+
type MyClass(property1 : int) =
72+
member val Property1 = property1
73+
member val Property2 = "" with get, set
74+
75+
let x = MyClass(1)
76+
let y = "hello"
77+
78+
let changeProperty() =
79+
x.Property2 = "20"
80+
y = "test"
81+
82+
exit 0
83+
"""
84+
FSharpErrorSeverity.Warning
85+
20
86+
(10, 5, 10, 23)
87+
"The result of this equality expression has type 'bool' and is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to set a value to a property, then use the '<-' operator e.g. 'x.Property2 <- expression'."
88+
89+
[<Test>]
90+
let ``Don't warn if assignment to property without setter ``() =
91+
CompilerAssert.TypeCheckSingleError
92+
"""
93+
type MyClass(property1 : int) =
94+
member val Property2 = "" with get
95+
96+
let x = MyClass(1)
97+
let y = "hello"
98+
99+
let changeProperty() =
100+
x.Property2 = "22"
101+
y = "test"
102+
103+
exit 0
104+
"""
105+
FSharpErrorSeverity.Warning
106+
20
107+
(9, 5, 9, 23)
108+
"The result of this equality expression has type 'bool' and is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'."

tests/fsharp/FSharpSuite.Tests.fsproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@
3838
<Compile Include="Compiler\ErrorMessages\MissingElseBranch.fs" />
3939
<Compile Include="Compiler\ErrorMessages\NameResolutionTests.fs" />
4040
<Compile Include="Compiler\ErrorMessages\UpcastDowncastTests.fs" />
41+
<Compile Include="Compiler\ErrorMessages\AssignmentErrorTests.fs" />
4142
<Compile Include="Compiler\ErrorMessages\WarnExpressionTests.fs" />
4243
<Compile Include="Compiler\SourceTextTests.fs" />
4344
<Compile Include="Compiler\Language\AnonRecordTests.fs" />
4445
<Compile Include="Compiler\Language\SpanOptimizationTests.fs" />
4546
<Compile Include="Compiler\Language\SpanTests.fs" />
4647
<Compile Include="Compiler\Language\StringConcatOptimizationTests.fs" />
4748
<Compile Include="Compiler\Stress\LargeExprTests.fs" />
49+
<Compile Include="Compiler\Warnings\AssignmentWarningTests.fs" />
4850
<Content Include="packages.config" />
4951
<None Include="app.config" />
5052
<None Include="update.base.line.with.actuals.fsx" />

tests/fsharpqa/Source/ErrorMessages/NameResolution/E_GlobalQualifierAfterDot.fs

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/fsharpqa/Source/ErrorMessages/NameResolution/env.lst

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/fsharpqa/Source/Warnings/AssignmentOnImmutable.fs

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/fsharpqa/Source/Warnings/env.lst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
SOURCE=MatchingMethodWithSameNameIsNotAbstract.fs # MatchingMethodWithSameNameIsNotAbstract.fs
3333
SOURCE=NoMatchingAbstractMethodWithSameName.fs # NoMatchingAbstractMethodWithSameName.fs
3434
SOURCE=MissingExpressionAfterLet.fs # MissingExpressionAfterLet.fs
35-
SOURCE=AssignmentOnImmutable.fs # AssignmentOnImmutable.fs
3635
SOURCE=SuggestFieldsInCtor.fs # SuggestFieldsInCtor.fs
3736
SOURCE=FieldSuggestion.fs # FieldSuggestion.fs
3837
SOURCE=SuggestToUseIndexer.fs # SuggestToUseIndexer.fs

tests/fsharpqa/Source/test.lst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ Misc01 Libraries\Core\Operators
264264
Misc01 Libraries\Core\Reflection
265265
Misc01 Libraries\Core\Unchecked
266266
Misc01 Warnings
267-
Misc01 ErrorMessages\NameResolution
268267
Misc01 ErrorMessages\UnitGenericAbstractType
269268
Misc01 ErrorMessages\ConfusingTypeName
270269

0 commit comments

Comments
 (0)