Skip to content

Commit

Permalink
move some error and warning tests to NUnit (#7244)
Browse files Browse the repository at this point in the history
* move some error and warning tests to NUnit

* CompilerAssert.ParseWithErrors now uses ParseFile instead of ParseAndCheckFileInProject

* merge conflicts
  • Loading branch information
smcl authored and cartermp committed Jul 17, 2019
1 parent 4cf7b5b commit 650805b
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 16 deletions.
19 changes: 19 additions & 0 deletions tests/fsharp/Compiler/CompilerAssert.fs
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,22 @@ module CompilerAssert =
Assert.AreEqual(expectedErrorMessage, errorMessage)
)

let ParseWithErrors (source: string) expectedParseErrors =
let parseResults = checker.ParseFile("test.fs", SourceText.ofString source, FSharpParsingOptions.Default) |> Async.RunSynchronously

Assert.True(parseResults.ParseHadErrors)

let errors =
parseResults.Errors
|> Array.distinctBy (fun e -> e.Severity, e.ErrorNumber, e.StartLineAlternate, e.StartColumn, e.EndLineAlternate, e.EndColumn, e.Message)

Assert.AreEqual(Array.length expectedParseErrors, errors.Length, sprintf "Type check errors: %A" parseResults.Errors)

Array.zip errors expectedParseErrors
|> Array.iter (fun (info, expectedError) ->
let (expectedServerity: FSharpErrorSeverity, expectedErrorNumber: int, expectedErrorRange: int * int * int * int, expectedErrorMsg: string) = expectedError
Assert.AreEqual(expectedServerity, info.Severity)
Assert.AreEqual(expectedErrorNumber, info.ErrorNumber, "expectedErrorNumber")
Assert.AreEqual(expectedErrorRange, (info.StartLineAlternate, info.StartColumn + 1, info.EndLineAlternate, info.EndColumn + 1), "expectedErrorRange")
Assert.AreEqual(expectedErrorMsg, info.Message, "expectedErrorMsg")
)
23 changes: 23 additions & 0 deletions tests/fsharp/Compiler/ErrorMessages/AssignmentErrorTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.

namespace FSharp.Compiler.UnitTests

open NUnit.Framework
open FSharp.Compiler.SourceCodeServices

[<TestFixture>]
module ``Errors assigning to mutable objects`` =

[<Test>]
let ``Assign to immutable error``() =
CompilerAssert.TypeCheckSingleError
"""
let x = 10
x <- 20
exit 0
"""
FSharpErrorSeverity.Error
27
(3, 1, 3, 8)
"This value is not mutable. Consider using the mutable keyword, e.g. 'let mutable x = expression'."
108 changes: 108 additions & 0 deletions tests/fsharp/Compiler/Warnings/AssignmentWarningTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.

namespace FSharp.Compiler.UnitTests

open NUnit.Framework
open FSharp.Compiler.SourceCodeServices

[<TestFixture>]
module ``Warnings assigning to mutable and immutable objects`` =

[<Test>]
let ``Unused compare with immutable when assignment might be intended``() =
CompilerAssert.TypeCheckSingleError
"""
let x = 10
let y = "hello"
let changeX() =
x = 20
y = "test"
exit 0
"""
FSharpErrorSeverity.Warning
20
(6, 5, 6, 11)
"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'."

[<Test>]
let ``Unused compare with mutable when assignment might be intended``() =
CompilerAssert.TypeCheckSingleError
"""
let mutable x = 10
let y = "hello"
let changeX() =
x = 20
y = "test"
exit 0
"""
FSharpErrorSeverity.Warning
20
(6, 5, 6, 11)
"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'."

[<Test>]
let ``Unused comparison of property in dotnet object when assignment might be intended``() =
CompilerAssert.TypeCheckSingleError
"""
open System
let z = new System.Timers.Timer()
let y = "hello"
let changeProperty() =
z.Enabled = true
y = "test"
exit 0
"""
FSharpErrorSeverity.Warning
20
(8, 5, 8, 21)
"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'."

[<Test>]
let ``Unused comparison of property when assignment might be intended ``() =
CompilerAssert.TypeCheckSingleError
"""
type MyClass(property1 : int) =
member val Property1 = property1
member val Property2 = "" with get, set
let x = MyClass(1)
let y = "hello"
let changeProperty() =
x.Property2 = "20"
y = "test"
exit 0
"""
FSharpErrorSeverity.Warning
20
(10, 5, 10, 23)
"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'."

[<Test>]
let ``Don't warn if assignment to property without setter ``() =
CompilerAssert.TypeCheckSingleError
"""
type MyClass(property1 : int) =
member val Property2 = "" with get
let x = MyClass(1)
let y = "hello"
let changeProperty() =
x.Property2 = "22"
y = "test"
exit 0
"""
FSharpErrorSeverity.Warning
20
(9, 5, 9, 23)
"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'."
2 changes: 2 additions & 0 deletions tests/fsharp/FSharpSuite.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
<Compile Include="Compiler\ErrorMessages\MissingElseBranch.fs" />
<Compile Include="Compiler\ErrorMessages\NameResolutionTests.fs" />
<Compile Include="Compiler\ErrorMessages\UpcastDowncastTests.fs" />
<Compile Include="Compiler\ErrorMessages\AssignmentErrorTests.fs" />
<Compile Include="Compiler\ErrorMessages\WarnExpressionTests.fs" />
<Compile Include="Compiler\SourceTextTests.fs" />
<Compile Include="Compiler\Language\AnonRecordTests.fs" />
<Compile Include="Compiler\Language\SpanOptimizationTests.fs" />
<Compile Include="Compiler\Language\SpanTests.fs" />
<Compile Include="Compiler\Language\StringConcatOptimizationTests.fs" />
<Compile Include="Compiler\Stress\LargeExprTests.fs" />
<Compile Include="Compiler\Warnings\AssignmentWarningTests.fs" />
<Content Include="packages.config" />
<None Include="app.config" />
<None Include="update.base.line.with.actuals.fsx" />
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion tests/fsharpqa/Source/ErrorMessages/NameResolution/env.lst

This file was deleted.

7 changes: 0 additions & 7 deletions tests/fsharpqa/Source/Warnings/AssignmentOnImmutable.fs

This file was deleted.

1 change: 0 additions & 1 deletion tests/fsharpqa/Source/Warnings/env.lst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
SOURCE=MatchingMethodWithSameNameIsNotAbstract.fs # MatchingMethodWithSameNameIsNotAbstract.fs
SOURCE=NoMatchingAbstractMethodWithSameName.fs # NoMatchingAbstractMethodWithSameName.fs
SOURCE=MissingExpressionAfterLet.fs # MissingExpressionAfterLet.fs
SOURCE=AssignmentOnImmutable.fs # AssignmentOnImmutable.fs
SOURCE=SuggestFieldsInCtor.fs # SuggestFieldsInCtor.fs
SOURCE=FieldSuggestion.fs # FieldSuggestion.fs
SOURCE=SuggestToUseIndexer.fs # SuggestToUseIndexer.fs
Expand Down
1 change: 0 additions & 1 deletion tests/fsharpqa/Source/test.lst
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ Misc01 Libraries\Core\Operators
Misc01 Libraries\Core\Reflection
Misc01 Libraries\Core\Unchecked
Misc01 Warnings
Misc01 ErrorMessages\NameResolution
Misc01 ErrorMessages\UnitGenericAbstractType
Misc01 ErrorMessages\ConfusingTypeName

Expand Down

0 comments on commit 650805b

Please sign in to comment.