|
| 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 | + """ |
0 commit comments