-
Notifications
You must be signed in to change notification settings - Fork 790
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving ElseBranchHasWrongTypeTests over to NUnit
- Loading branch information
Showing
14 changed files
with
176 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
tests/fsharp/Compiler/ErrorMessages/ElseBranchHasWrongTypeTests.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, 19, 6, 22), "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, 10, 7, 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'." |] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 0 additions & 11 deletions
11
tests/fsharpqa/Source/Warnings/ElseBranchContextDoesntPropagateInAppl.fs
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
tests/fsharpqa/Source/Warnings/ElseBranchContextDoesntPropagateInAppl2.fs
This file was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
tests/fsharpqa/Source/Warnings/ElseBranchContextDoesntPropagateInForLoop.fs
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
tests/fsharpqa/Source/Warnings/ElseBranchContextDoesntPropagateToLinesBeforeLastLine.fs
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
tests/fsharpqa/Source/Warnings/ElseBranchHasWrongContextType.fs
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
tests/fsharpqa/Source/Warnings/NestedElseBranchHasWrongType.fs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters