Skip to content

Commit f540da5

Browse files
T-GroCopilot
andauthored
Add regression test: #13519, C# optional parameters from F# (#19473)
* Add regression test for #13519: C# optional parameters from F# Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Add ParamArray + optional params regression test for #13519 Addresses reviewer feedback: the original issue was specifically about the intersection of omitted optional arguments and ParamArray arguments, as identified by Don Syme. This adds a test covering that exact scenario. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 3ee2a3e commit f540da5

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

tests/FSharp.Compiler.ComponentTests/Interop/SimpleInteropTests.fs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,112 @@ Holder.Label <- "nope"
114114
fsLib
115115
|> compile
116116
|> shouldFail
117+
118+
// https://github.com/dotnet/fsharp/issues/13519
119+
[<FSharp.Test.FactForNETCOREAPP>]
120+
let ``Issue 13519 - C# method with optional parameters can be called from F# without specifying defaults`` () =
121+
let csLib =
122+
CSharp
123+
"""
124+
using System;
125+
126+
namespace CSharpLib
127+
{
128+
public class MyClass
129+
{
130+
public string DoSomething(string required, string optional1 = "default1", string optional2 = "default2")
131+
{
132+
return required + "|" + optional1 + "|" + optional2;
133+
}
134+
}
135+
}
136+
"""
137+
|> withName "CSharpOptionalParams"
138+
139+
let fsApp =
140+
FSharp
141+
"""
142+
module TestApp
143+
144+
open CSharpLib
145+
146+
[<EntryPoint>]
147+
let main _ =
148+
let c = MyClass()
149+
let result = c.DoSomething("hello")
150+
if result <> "hello|default1|default2" then
151+
failwithf "Expected 'hello|default1|default2' but got '%s'" result
152+
let result2 = c.DoSomething("hello", "custom1")
153+
if result2 <> "hello|custom1|default2" then
154+
failwithf "Expected 'hello|custom1|default2' but got '%s'" result2
155+
0
156+
"""
157+
|> withReferences [ csLib ]
158+
159+
fsApp
160+
|> asExe
161+
|> compileExeAndRun
162+
|> shouldSucceed
163+
164+
// https://github.com/dotnet/fsharp/issues/13519
165+
// Specifically testing the intersection of optional parameters and ParamArray
166+
// as identified by Don Syme: https://github.com/dotnet/fsharp/issues/13519#issuecomment-1253808416
167+
[<FSharp.Test.FactForNETCOREAPP>]
168+
let ``Issue 13519 - C# method with optional parameters and ParamArray can be called from F# without specifying defaults`` () =
169+
let csLib =
170+
CSharp
171+
"""
172+
using System;
173+
174+
namespace CSharpParamArrayLib
175+
{
176+
public class Assertions
177+
{
178+
public string BeEquivalentTo(string expected, string because = "", params object[] becauseArgs)
179+
{
180+
string formatted = because;
181+
if (becauseArgs != null && becauseArgs.Length > 0)
182+
{
183+
formatted = string.Format(because, becauseArgs);
184+
}
185+
return expected + "|" + formatted + "|" + becauseArgs.Length;
186+
}
187+
}
188+
}
189+
"""
190+
|> withName "CSharpParamArrayLib"
191+
192+
let fsApp =
193+
FSharp
194+
"""
195+
module TestParamArrayApp
196+
197+
open CSharpParamArrayLib
198+
199+
[<EntryPoint>]
200+
let main _ =
201+
let a = Assertions()
202+
203+
// Call with only the required argument - omitting both optional and params
204+
let result1 = a.BeEquivalentTo("test")
205+
if result1 <> "test||0" then
206+
failwithf "Test 1 failed: Expected 'test||0' but got '%s'" result1
207+
208+
// Call with required + optional, omitting params
209+
let result2 = a.BeEquivalentTo("test", "because {0}")
210+
if result2 <> "test|because {0}|0" then
211+
failwithf "Test 2 failed: Expected 'test|because {0}|0' but got '%s'" result2
212+
213+
// Call with all arguments including params values
214+
let result3 = a.BeEquivalentTo("test", "because {0}", "reason")
215+
if result3 <> "test|because reason|1" then
216+
failwithf "Test 3 failed: Expected 'test|because reason|1' but got '%s'" result3
217+
218+
0
219+
"""
220+
|> withReferences [ csLib ]
221+
222+
fsApp
223+
|> asExe
224+
|> compileExeAndRun
225+
|> shouldSucceed

0 commit comments

Comments
 (0)