-
Notifications
You must be signed in to change notification settings - Fork 826
C# params interop tests #17495
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
Merged
Merged
C# params interop tests #17495
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
tests/FSharp.Compiler.ComponentTests/Interop/ParamArray.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. | ||
|
||
namespace Interop | ||
|
||
open Xunit | ||
open FSharp.Test | ||
open FSharp.Test.Compiler | ||
|
||
module ParamArray = | ||
|
||
[<Fact>] | ||
let ``C# 13 params enhancements`` () = | ||
let csharp = | ||
CSharp """ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace CSharpAssembly; | ||
|
||
public class CS13ParamArray | ||
{ | ||
public static void WriteNames(params string[] names) | ||
=> Console.WriteLine("First: " + string.Join(" + ", names)); | ||
|
||
public static void WriteNames(params List<string> names) | ||
=> Console.WriteLine("Second: " + string.Join(" + ", names)); | ||
|
||
public static void WriteNames(params IEnumerable<string> names) | ||
=> Console.WriteLine("Third: " + string.Join(" + ", names)); | ||
}""" | ||
|> withCSharpLanguageVersionPreview | ||
|
||
FSharp """ | ||
open System.Collections.Generic; | ||
open CSharpAssembly | ||
|
||
CS13ParamArray.WriteNames("Petr", "Jana") | ||
CS13ParamArray.WriteNames(List["Petr"; "Jana"]) | ||
CS13ParamArray.WriteNames(["Petr"; "Jana"]) | ||
""" | ||
|> withReferences [csharp] | ||
|> compileExeAndRun | ||
|> shouldSucceed | ||
|> withStdOutContainsAllInOrder [ | ||
"First: Petr + Jana" | ||
"Second: Petr + Jana" | ||
"Third: Petr + Jana" | ||
] | ||
|
||
[<FactForNETCOREAPP>] | ||
let ``C# 13 params enhancements - ReadOnlySpan`` () = | ||
let csharp = | ||
CSharp """ | ||
using System; | ||
|
||
namespace CSharpAssembly; | ||
|
||
public class CS13ParamArray | ||
{ | ||
public static void WriteNames(params ReadOnlySpan<string> names) | ||
=> Console.WriteLine(string.Join(" + ", names)); | ||
}""" | ||
|> withCSharpLanguageVersionPreview | ||
|
||
FSharp """ | ||
open System | ||
open CSharpAssembly | ||
|
||
CS13ParamArray.WriteNames(ReadOnlySpan([|"Petr"; "Jana"|])) | ||
""" | ||
|> withReferences [csharp] | ||
|> compileExeAndRun | ||
|> shouldSucceed | ||
|> withStdOutContainsAllInOrder [ "Petr + Jana" ] | ||
|
||
[<Fact>] | ||
let ``C# 13 params enhancements - error when no matching overload is available`` () = | ||
let csharp = | ||
CSharp """ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace CSharpAssembly; | ||
|
||
public class CS13ParamArray | ||
{ | ||
public static void WriteNames(params List<string> names) | ||
=> Console.WriteLine("Second: " + string.Join(" + ", names)); | ||
|
||
public static void WriteNames(params IEnumerable<string> names) | ||
=> Console.WriteLine("Third: " + string.Join(" + ", names)); | ||
}""" | ||
|> withCSharpLanguageVersionPreview | ||
|
||
FSharp """ | ||
open CSharpAssembly | ||
|
||
CS13ParamArray.WriteNames("Petr", "Jana") | ||
""" | ||
|> withReferences [csharp] | ||
|> asExe | ||
|> compile | ||
|> shouldFail | ||
|> withDiagnostics [ | ||
(Error 503, Line 4, Col 1, Line 4, Col 42, | ||
"A member or object constructor 'WriteNames' taking 2 arguments is not accessible from this code location. All accessible versions of method 'WriteNames' take 1 arguments.") | ||
] |
110 changes: 110 additions & 0 deletions
110
tests/FSharp.Compiler.ComponentTests/Interop/ParamArrayMigrated.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. | ||
|
||
namespace Interop | ||
|
||
open Xunit | ||
open FSharp.Test.Compiler | ||
|
||
module ParamArrayMigrated = | ||
|
||
let csharp = | ||
CSharp """ | ||
using System; | ||
|
||
namespace CSharpAssembly | ||
{ | ||
[AttributeUsage(AttributeTargets.All)] | ||
public class AttributeWithParamArray : Attribute | ||
{ | ||
public object[] Parameters; | ||
|
||
public AttributeWithParamArray(params object[] x) | ||
{ | ||
|
||
Parameters = x; | ||
} | ||
} | ||
|
||
public class CSParamArray | ||
{ | ||
public static int Method(params int[] allArgs) | ||
{ | ||
int total = 0; | ||
foreach (int i in allArgs) | ||
total += i; | ||
|
||
return total; | ||
} | ||
|
||
public static int Method<T>(params T[] args) | ||
{ | ||
return args.Length; | ||
} | ||
} | ||
}""" | ||
|
||
[<Fact>] | ||
let ``Valid params call`` () = | ||
FSharp """ | ||
open System | ||
open CSharpAssembly | ||
|
||
// Apply the attribute | ||
[<AttributeWithParamArray([| (0 :> obj) |])>] | ||
type Foo() = | ||
[<AttributeWithParamArray([| ("foo" :> obj); ("bar" :> obj) |])>] | ||
override this.ToString() = "Stuff" | ||
|
||
let callCSGenericMethod (a: 't[]) = CSParamArray.Method(a) | ||
|
||
[<assembly:AttributeWithParamArray ([| |])>] | ||
do | ||
let getTestAttribute (t : Type) = | ||
let tyAttributes = t.GetCustomAttributes(false) | ||
let attrib = tyAttributes |> Array.find (fun attrib -> match attrib with :? AttributeWithParamArray -> true | _ -> false) | ||
(attrib :?> AttributeWithParamArray) | ||
|
||
let tyFoo = typeof<Foo> | ||
let testAtt = getTestAttribute tyFoo | ||
if testAtt.Parameters <> [| (0 :> obj) |] then | ||
failwith "Attribute parameters not as expected" | ||
|
||
let directCallWorks = | ||
CSParamArray.Method(9, 8, 7) + CSParamArray.Method(1, 2) + CSParamArray.Method() = (9 + 8 + 7) + (1 + 2) | ||
if not directCallWorks then | ||
failwith "Calling C# param array method gave unexpected result" | ||
|
||
let callParamArray (x : int array) = CSParamArray.Method(x) | ||
let asArrayCallWorks = (callParamArray [| 9; 8; 7 |]) = (9 + 8 + 7) | ||
if not asArrayCallWorks then | ||
failwith "Calling C# param array method, passing args as an array, gave unexpected result" | ||
|
||
if callCSGenericMethod [|"1";"2";"3"|] <> 3 then | ||
failwith "Calling C# generic param array method gave unexpected result" | ||
|
||
if CSParamArray.Method("1", "2", "3") <> CSParamArray.Method([|"1"; "2"; "3"|]) then | ||
failwith "Calling C# generic param array in normal and expanded method gave unexpected result" | ||
""" | ||
|> withReferences [csharp] | ||
|> compileExeAndRun | ||
|> shouldSucceed | ||
|
||
[<Fact>] | ||
let ``Invalid params call`` () = | ||
FSharp """ | ||
open CSharpAssembly | ||
|
||
[<AttributeWithParamArray([|upcast 0|])>] | ||
type Foo() = | ||
override this.ToString() = "Stuff" | ||
""" | ||
|> withReferences [csharp] | ||
|> asExe | ||
|> compile | ||
|> shouldFail | ||
|> withDiagnostics [ | ||
(Error 13, Line 4, Col 29, Line 4, Col 37, | ||
"The static coercion from type\n int \nto \n 'a \n involves an indeterminate type based on information prior to this program point. Static coercions are not allowed on some types. Further type annotations are needed.") | ||
(Error 267, Line 4, Col 29, Line 4, Col 37, | ||
"This is not a valid constant expression or custom attribute value") | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.