Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving ElseBranchHasWrongTypeTests over to NUnit #7104

Merged
merged 1 commit into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 176 additions & 0 deletions tests/fsharp/Compiler/ErrorMessages/ElseBranchHasWrongTypeTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
// 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 ``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
"""
FSharpErrorSeverity.Error
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
"""
FSharpErrorSeverity.Error
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)
"""
FSharpErrorSeverity.Error
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)
"""
FSharpErrorSeverity.Error
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
"""
FSharpErrorSeverity.Error
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"
"""
FSharpErrorSeverity.Error
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"
"""
FSharpErrorSeverity.Error
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"
"""
FSharpErrorSeverity.Error
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"
"""
[| FSharpErrorSeverity.Error, 1, (4, 19, 4, 22), "The 'if' expression needs to have type 'bool' to satisfy context type requirements. It currently has type 'string'."
FSharpErrorSeverity.Error, 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
if x = 1 then true
else
if x = 2 then "A"
else "B"
"""
[| FSharpErrorSeverity.Error, 1, (5, 19, 5, 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'."
FSharpErrorSeverity.Error, 1, (6, 10, 6, 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'."
FSharpErrorSeverity.Warning, 20, (3, 1, 6, 13), "The result of this expression has type 'bool' and is implicitly ignored. Consider using 'ignore' to discard this value explicitly, e.g. 'expr |> ignore', or 'let' to bind the result to a name, e.g. 'let result = expr'." |]
3 changes: 2 additions & 1 deletion tests/fsharp/FSharpSuite.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
Expand Down Expand Up @@ -32,6 +32,7 @@
<Compile Include="tests.fs" />
<Compile Include="Compiler\ILChecker.fs" />
<Compile Include="Compiler\CompilerAssert.fs" />
<Compile Include="Compiler\ErrorMessages\ElseBranchHasWrongTypeTests.fs" />
<Compile Include="Compiler\SourceTextTests.fs" />
<Compile Include="Compiler\Language\AnonRecordTests.fs" />
<Compile Include="Compiler\Language\SpanOptimizationTests.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