Skip to content

Commit 75cef56

Browse files
sergey-tihonKevinRansom
authored andcommitted
Moving ConstructorTests over to NUnit (#7236)
1 parent c4dba1f commit 75cef56

10 files changed

Lines changed: 116 additions & 80 deletions

File tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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 ``Constructor`` =
10+
11+
[<Test>]
12+
let ``Invalid Record``() =
13+
CompilerAssert.TypeCheckWithErrors
14+
"""
15+
type Record = {field1:int; field2:int}
16+
let doSomething (xs) = List.map (fun {field1=x} -> x) xs
17+
18+
doSomething {Record.field1=0; field2=0}
19+
"""
20+
[|
21+
FSharpErrorSeverity.Error, 1, (5, 13, 5, 40), "This expression was expected to have type\n 'Record list' \nbut here has type\n 'Record' "
22+
FSharpErrorSeverity.Warning, 20, (5, 1, 5, 40), "The result of this expression has type 'int list' 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'."
23+
|]
24+
25+
[<Test>]
26+
let ``Comma In Rec Ctor``() =
27+
CompilerAssert.TypeCheckWithErrors
28+
"""
29+
type Person = { Name : string; Age : int; City : string }
30+
let x = { Name = "Isaac", Age = 21, City = "London" }
31+
"""
32+
[|
33+
FSharpErrorSeverity.Error, 1, (3, 18, 3, 52), "This expression was expected to have type\n 'string' \nbut here has type\n ''a * 'b * 'c' \r\nA ';' is used to separate field values in records. Consider replacing ',' with ';'."
34+
FSharpErrorSeverity.Error, 764, (3, 9, 3, 54), "No assignment given for field 'Age' of type 'Test.Person'"
35+
|]
36+
37+
[<Test>]
38+
let ``Missing Comma In Ctor``() =
39+
CompilerAssert.TypeCheckWithErrors
40+
"""
41+
type Person() =
42+
member val Name = "" with get,set
43+
member val Age = 0 with get,set
44+
45+
let p =
46+
Person(Name = "Fred"
47+
Age = 18)
48+
"""
49+
[|
50+
FSharpErrorSeverity.Error, 39, (7, 12, 7, 16), "The value or constructor 'Name' is not defined."
51+
FSharpErrorSeverity.Warning, 20, (7, 12, 7, 25), "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'."
52+
FSharpErrorSeverity.Error, 39, (8, 12, 8, 15), "The value or constructor 'Age' is not defined."
53+
FSharpErrorSeverity.Error, 501, (7, 5, 8, 21), "The object constructor 'Person' takes 0 argument(s) but is here given 1. The required signature is 'new : unit -> Person'. If some of the arguments are meant to assign values to properties, consider separating those arguments with a comma (',')."
54+
|]
55+
56+
[<Test>]
57+
let ``Missing Ctor Value``() =
58+
CompilerAssert.TypeCheckSingleError
59+
"""
60+
type Person(x:int) =
61+
member val Name = "" with get,set
62+
member val Age = x with get,set
63+
64+
let p =
65+
Person(Name = "Fred",
66+
Age = 18)
67+
"""
68+
FSharpErrorSeverity.Error
69+
496
70+
(7, 5, 8, 21)
71+
"The member or object constructor 'Person' requires 1 argument(s). The required signature is 'new : x:int -> Person'."
72+
73+
[<Test>]
74+
let ``Extra Argument In Ctor``() =
75+
CompilerAssert.TypeCheckSingleError
76+
"""
77+
type Person() =
78+
member val Name = "" with get,set
79+
member val Age = 0 with get,set
80+
81+
let p =
82+
Person(1)
83+
"""
84+
FSharpErrorSeverity.Error
85+
501
86+
(7, 5, 7, 14)
87+
"The object constructor 'Person' takes 0 argument(s) but is here given 1. The required signature is 'new : unit -> Person'."
88+
89+
[<Test>]
90+
let ``Extra Argument In Ctor2``() =
91+
CompilerAssert.TypeCheckSingleError
92+
"""
93+
type Person() =
94+
member val Name = "" with get,set
95+
member val Age = 0 with get,set
96+
97+
let b = 1
98+
99+
let p =
100+
Person(1=b)
101+
"""
102+
FSharpErrorSeverity.Error
103+
501
104+
(9, 5, 9, 16)
105+
"The object constructor 'Person' takes 0 argument(s) but is here given 1. The required signature is 'new : unit -> Person'."
106+
107+
[<Test>]
108+
let ``Valid Comma In Rec Ctor``() =
109+
CompilerAssert.Pass
110+
"""
111+
type Person = { Name : string * bool * bool }
112+
let Age = 22
113+
let City = "London"
114+
let x = { Name = "Isaac", Age = 21, City = "London" }
115+
"""

tests/fsharp/FSharpSuite.Tests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<Compile Include="tests.fs" />
3333
<Compile Include="Compiler\ILChecker.fs" />
3434
<Compile Include="Compiler\CompilerAssert.fs" />
35+
<Compile Include="Compiler\ErrorMessages\ConstructorTests.fs" />
3536
<Compile Include="Compiler\ErrorMessages\AccessOfTypeAbbreviationTests.fs" />
3637
<Compile Include="Compiler\ErrorMessages\ElseBranchHasWrongTypeTests.fs" />
3738
<Compile Include="Compiler\ErrorMessages\MissingElseBranch.fs" />

tests/fsharpqa/Source/Warnings/CommaInRecCtor.fs

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

tests/fsharpqa/Source/Warnings/ExtraArgumentInCtor.fs

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

tests/fsharpqa/Source/Warnings/ExtraArgumentInCtor2.fs

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

tests/fsharpqa/Source/Warnings/InvalidRecord.fs

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

tests/fsharpqa/Source/Warnings/MissingCommaInCtor.fs

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

tests/fsharpqa/Source/Warnings/MissingCtorValue.fs

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

tests/fsharpqa/Source/Warnings/ValidCommaInRecCtor.fs

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

tests/fsharpqa/Source/Warnings/env.lst

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
22
SOURCE=ReturnInsteadOfReturnBang.fs # ReturnInsteadOfReturnBang.fs
33
SOURCE=YieldInsteadOfYieldBang.fs # YieldInsteadOfYieldBang.fs
44
SOURCE=TupleInAbstractMethod.fs # TupleInAbstractMethod.fs
5-
SOURCE=InvalidRecord.fs # InvalidRecord.fs
6-
SOURCE=CommaInRecCtor.fs # CommaInRecCtor.fs
7-
SOURCE=MissingCommaInCtor.fs # MissingCommaInCtor.fs
8-
SOURCE=MissingCtorValue.fs # MissingCtorValue.fs
9-
SOURCE=ExtraArgumentInCtor.fs # ExtraArgumentInCtor.fs
10-
SOURCE=ExtraArgumentInCtor2.fs # ExtraArgumentInCtor2.fs
11-
SOURCE=ValidCommaInRecCtor.fs # ValidCommaInRecCtor.fs
125
SOURCE=FS0988AtEndOfFile.fs # FS0988AtEndOfFile.fs
136
SOURCE=WrongArity.fs # WrongArity.fs
147
SOURCE=OverrideErrors.fs # OverrideErrors.fs

0 commit comments

Comments
 (0)