-
Notifications
You must be signed in to change notification settings - Fork 789
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
222 additions
and
127 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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