You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/DelegateTypes/DelegateDefinition.fs
+80-1Lines changed: 80 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -66,4 +66,83 @@ let a = A f
66
66
a.Invoke(5)"""
67
67
|> compileExeAndRun
68
68
|> shouldSucceed
69
-
|> verifyOutput "line: 5"
69
+
|> verifyOutput "line: 5"
70
+
71
+
[<Fact>]
72
+
let``Delegate with OptionalArgument and CallerFilePath`` ()=
73
+
FSharp """open System.Runtime.CompilerServices
74
+
open System.Runtime.InteropServices
75
+
type TestDelegate = delegate of [<OptionalArgument; CallerFilePath>] path: string option -> unit
76
+
let f = fun (path: string option) ->
77
+
match path with
78
+
| Some p -> if p.Contains("test") then printfn "SUCCESS" else printfn "FAIL: %s" p
79
+
| None -> printfn "FAIL: None"
80
+
let d = TestDelegate f
81
+
d.Invoke()"""
82
+
|> compileExeAndRun
83
+
|> shouldSucceed
84
+
|> verifyOutput "SUCCESS"
85
+
86
+
[<Fact>]
87
+
let``Delegate with OptionalArgument and CallerLineNumber`` ()=
88
+
FSharp """open System.Runtime.CompilerServices
89
+
open System.Runtime.InteropServices
90
+
type TestDelegate = delegate of [<OptionalArgument; CallerLineNumber>] line: int option -> unit
91
+
let f = fun (line: int option) ->
92
+
match line with
93
+
| Some l -> if l > 0 then printfn "SUCCESS: line %d" l else printfn "FAIL"
94
+
| None -> printfn "FAIL: None"
95
+
let d = TestDelegate f
96
+
d.Invoke()"""
97
+
|> compileExeAndRun
98
+
|> shouldSucceed
99
+
100
+
[<Fact>]
101
+
let``Delegate with OptionalArgument and CallerMemberName`` ()=
102
+
FSharp """open System.Runtime.CompilerServices
103
+
open System.Runtime.InteropServices
104
+
type TestDelegate = delegate of [<OptionalArgument; CallerMemberName>] memberName: string option -> unit
105
+
let f = fun (memberName: string option) ->
106
+
match memberName with
107
+
| Some m -> printfn "member: %s" m
108
+
| None -> printfn "FAIL"
109
+
let d = TestDelegate f
110
+
d.Invoke()"""
111
+
|> compileExeAndRun
112
+
|> shouldSucceed
113
+
114
+
[<Fact>]
115
+
let``Delegate with CallerFilePath without optional should fail`` ()=
116
+
FSharp """namespace Test
117
+
open System.Runtime.CompilerServices
118
+
type TestDelegate = delegate of [<CallerFilePath>] path: string -> unit"""
119
+
|> compile
120
+
|> shouldFail
121
+
|> withDiagnostics [
122
+
(Error 1247, Line 3, Col 41, Line 3, Col 45,"'CallerFilePath' can only be applied to optional arguments")
123
+
]
124
+
125
+
[<Fact>]
126
+
let``Delegate with CallerFilePath on wrong type should fail`` ()=
127
+
FSharp """namespace Test
128
+
open System.Runtime.CompilerServices
129
+
open System.Runtime.InteropServices
130
+
type TestDelegate = delegate of [<OptionalArgument; CallerFilePath>] x: int option -> unit"""
131
+
|> compile
132
+
|> shouldFail
133
+
|> withDiagnostics [
134
+
(Error 1246, Line 4, Col 69, Line 4, Col 70,"'CallerFilePath' must be applied to an argument of type 'string', but has been applied to an argument of type 'int'")
135
+
]
136
+
137
+
[<Fact>]
138
+
let``Delegate with CallerLineNumber on wrong type should fail`` ()=
139
+
FSharp """namespace Test
140
+
open System.Runtime.CompilerServices
141
+
open System.Runtime.InteropServices
142
+
type TestDelegate = delegate of [<OptionalArgument; CallerLineNumber>] x: string option -> unit"""
143
+
|> compile
144
+
|> shouldFail
145
+
|> withDiagnostics [
146
+
(Error 1246, Line 4, Col 73, Line 4, Col 74,"'CallerLineNumber' must be applied to an argument of type 'int', but has been applied to an argument of type 'string'")
0 commit comments