Skip to content

Commit

Permalink
Moving ElseBranchHasWrongTypeTests over to NUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed Jul 8, 2019
1 parent bc334c5 commit 62d0a97
Show file tree
Hide file tree
Showing 14 changed files with 176 additions and 130 deletions.
12 changes: 8 additions & 4 deletions tests/fsharp/Compiler/CompilerAssert.fs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module CompilerAssert =
Assert.IsEmpty(typeCheckResults.Errors, sprintf "Type Check errors: %A" typeCheckResults.Errors)


let TypeCheckSingleError (source: string) (expectedErrorNumber: int) (expectedErrorRange: int * int * int * int) (expectedErrorMsg: string) =
let TypeCheckWithErrors (source: string) expectedTypeErrors =
lock lockObj <| fun () ->
let parseResults, fileAnswer = checker.ParseAndCheckFileInProject("test.fs", 0, SourceText.ofString source, defaultProjectOptions) |> Async.RunSynchronously

Expand All @@ -76,16 +76,20 @@ module CompilerAssert =
let errors =
typeCheckResults.Errors
|> Array.distinctBy (fun e -> e.Severity, e.ErrorNumber, e.StartLineAlternate, e.StartColumn, e.EndLineAlternate, e.EndColumn, e.Message)

Assert.AreEqual(Array.length expectedTypeErrors, errors.Length, sprintf "Expected one type check error: %A" typeCheckResults.Errors)

Assert.AreEqual(1, errors.Length, sprintf "Expected one type check error: %A" typeCheckResults.Errors)
errors
|> Array.iter (fun info ->
Array.zip errors expectedTypeErrors
|> Array.iter (fun (info,(expectedErrorNumber: int, expectedErrorRange: int * int * int * int, expectedErrorMsg: string)) ->
Assert.AreEqual(FSharpErrorSeverity.Error, 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")
)

let TypeCheckSingleError (source: string) (expectedErrorNumber: int) (expectedErrorRange: int * int * int * int) (expectedErrorMsg: string) =
TypeCheckWithErrors (source: string) [| expectedErrorNumber, expectedErrorRange, expectedErrorMsg |]

let RunScript (source: string) (expectedErrorMessages: string list) =
lock lockObj <| fun () ->
// Intialize output and input streams
Expand Down
167 changes: 167 additions & 0 deletions tests/fsharp/Compiler/ErrorMessages/ElseBranchHasWrongTypeTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.

namespace FSharp.Compiler.UnitTests

open NUnit.Framework

[<TestFixture>]
module ``Else branch has wrong type`` =

[<Test>]
let ``Else branch is int while if branch is string``() =
CompilerAssert.TypeCheckSingleError
"""
let test = 100
let y =
if test > 10 then "test"
else 123
"""
1
(5, 10, 5, 13)
"All branches of an 'if' expression must return values of the same type as the first branch, which here is 'string'. This branch returns a value of type 'int'."

[<Test>]
let ``Else branch is a function that returns int while if branch is string``() =
CompilerAssert.TypeCheckSingleError
"""
let test = 100
let f x = test
let y =
if test > 10 then "test"
else f 10
"""
1
(6, 10, 6, 14)
"All branches of an 'if' expression must return values of the same type as the first branch, which here is 'string'. This branch returns a value of type 'int'."


[<Test>]
let ``Else branch is a sequence of expressions that returns int while if branch is string``() =
CompilerAssert.TypeCheckSingleError
"""
let f x = x + 4
let y =
if true then
""
else
"" |> ignore
(f 5)
"""
1
(9, 10, 9, 13)
"All branches of an 'if' expression must return values of the same type as the first branch, which here is 'string'. This branch returns a value of type 'int'."


[<Test>]
let ``Else branch is a longer sequence of expressions that returns int while if branch is string``() =
CompilerAssert.TypeCheckSingleError
"""
let f x = x + 4
let y =
if true then
""
else
"" |> ignore
let z = f 4
let a = 3 * z
(f a)
"""
1
(11, 10, 11, 13)
"All branches of an 'if' expression must return values of the same type as the first branch, which here is 'string'. This branch returns a value of type 'int'."


[<Test>]
let ``Else branch context doesn't propagate into function application``() =
CompilerAssert.TypeCheckSingleError
"""
let test = 100
let f x : string = x
let y =
if test > 10 then "test"
else
f 123
"""
1
(7, 11, 7, 14)
"This expression was expected to have type\n 'string' \nbut here has type\n 'int' "

[<Test>]
let ``Else branch context doesn't propagate into function application even if not last expr``() =
CompilerAssert.TypeCheckSingleError
"""
let test = 100
let f x = printfn "%s" x
let y =
if test > 10 then "test"
else
f 123
"test"
"""
1
(7, 11, 7, 14)
"This expression was expected to have type\n 'string' \nbut here has type\n 'int' "

[<Test>]
let ``Else branch context doesn't propagate into for loop``() =
CompilerAssert.TypeCheckSingleError
"""
let test = 100
let list = [1..10]
let y =
if test > 10 then "test"
else
for (x:string) in list do
printfn "%s" x
"test"
"""
1
(7, 14, 7, 22)
"This expression was expected to have type\n 'int' \nbut here has type\n 'string' "

[<Test>]
let ``Else branch context doesn't propagate to lines before last line``() =
CompilerAssert.TypeCheckSingleError
"""
let test = 100
let list = [1..10]
let y =
if test > 10 then "test"
else
printfn "%s" 1
"test"
"""
1
(7, 22, 7, 23)
"This expression was expected to have type\n 'string' \nbut here has type\n 'int' "

[<Test>]
let ``Else branch should not have wrong context type``() =
CompilerAssert.TypeCheckWithErrors
"""
let x = 1
let y : bool =
if x = 2 then "A"
else "B"
"""
[| 1, (4, 19, 4, 22), "The 'if' expression needs to have type 'bool' to satisfy context type requirements. It currently has type 'string'."
1, (5, 10, 5, 13), "All branches of an 'if' expression must return values of the same type as the first branch, which here is 'bool'. This branch returns a value of type 'string'." |]


[<Test>]
let ``Else branch has wrong type in nested if``() =
CompilerAssert.TypeCheckWithErrors
"""
let x = 1
let f =
if x = 1 then true
else
if x = 2 then "A"
else "B"
"""
[| 1, (6, 23, 6, 26), "All branches of an 'if' expression must return values of the same type as the first branch, which here is 'bool'. This branch returns a value of type 'string'."
1, (7, 14, 7, 17), "All branches of an 'if' expression must return values of the same type as the first branch, which here is 'bool'. This branch returns a value of type 'string'." |]
1 change: 1 addition & 0 deletions tests/fsharp/FSharpSuite.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<Compile Include="TypeProviderTests.fs" />
<Compile Include="tests.fs" />
<Compile Include="Compiler\CompilerAssert.fs" />
<Compile Include="Compiler\ErrorMessages\ElseBranchHasWrongTypeTests.fs" />
<Compile Include="Compiler\ILHelpers.fs" />
<Compile Include="Compiler\SourceTextTests.fs" />
<Compile Include="Compiler\Language\AnonRecordTests.fs" />
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

9 changes: 0 additions & 9 deletions tests/fsharpqa/Source/Warnings/ElseBranchHasWrongType.fs

This file was deleted.

10 changes: 0 additions & 10 deletions tests/fsharpqa/Source/Warnings/ElseBranchHasWrongType2.fs

This file was deleted.

13 changes: 0 additions & 13 deletions tests/fsharpqa/Source/Warnings/ElseBranchHasWrongType3.fs

This file was deleted.

15 changes: 0 additions & 15 deletions tests/fsharpqa/Source/Warnings/ElseBranchHasWrongType4.fs

This file was deleted.

10 changes: 0 additions & 10 deletions tests/fsharpqa/Source/Warnings/NestedElseBranchHasWrongType.fs

This file was deleted.

10 changes: 0 additions & 10 deletions tests/fsharpqa/Source/Warnings/env.lst
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@
SOURCE=SuggestDoubleBacktickIdentifiers.fs SCFLAGS="--vserrors" # SuggestDoubleBacktickIdentifiers.fs
SOURCE=SuggestDoubleBacktickUnions.fs SCFLAGS="--vserrors" # SuggestDoubleBacktickUnions.fs
SOURCE=GuardHasWrongType.fs # GuardHasWrongType.fs
SOURCE=ElseBranchHasWrongType.fs # ElseBranchHasWrongType.fs
SOURCE=ElseBranchHasWrongType2.fs # ElseBranchHasWrongType2.fs
SOURCE=ElseBranchHasWrongType3.fs # ElseBranchHasWrongType3.fs
SOURCE=ElseBranchHasWrongType4.fs # ElseBranchHasWrongType4.fs
SOURCE=NestedElseBranchHasWrongType.fs # NestedElseBranchHasWrongType.fs
SOURCE=ElseBranchHasWrongContextType.fs # ElseBranchHasWrongContextType.fs
SOURCE=ElseBranchContextDoesntPropagateInAppl.fs # ElseBranchContextDoesntPropagateInAppl.fs
SOURCE=ElseBranchContextDoesntPropagateInAppl2.fs # ElseBranchContextDoesntPropagateInAppl2.fs
SOURCE=ElseBranchContextDoesntPropagateInForLoop.fs # ElseBranchContextDoesntPropagateInForLoop.fs
SOURCE=ElseBranchContextDoesntPropagateToLinesBeforeLastLine.fs # ElseBranchContextDoesntPropagateToLinesBeforeLastLine.fs
SOURCE=MatchingMethodWithSameNameIsNotAbstract.fs # MatchingMethodWithSameNameIsNotAbstract.fs
SOURCE=NoMatchingAbstractMethodWithSameName.fs # NoMatchingAbstractMethodWithSameName.fs
SOURCE=MissingExpressionAfterLet.fs # MissingExpressionAfterLet.fs
Expand Down

0 comments on commit 62d0a97

Please sign in to comment.