From 246e82fc8460edd7af36626e1ef39bed6d4aa7fc Mon Sep 17 00:00:00 2001 From: Will Smith Date: Sun, 28 Jul 2019 23:37:39 -0700 Subject: [PATCH] Added more tests --- .../DefaultInterfaceMethodTests.fs | 456 ++++++++++++++++++ 1 file changed, 456 insertions(+) diff --git a/tests/fsharp/Compiler/Language/DefaultInterfaceMethods/DefaultInterfaceMethodTests.fs b/tests/fsharp/Compiler/Language/DefaultInterfaceMethods/DefaultInterfaceMethodTests.fs index 42ececf01c0..239167856b4 100644 --- a/tests/fsharp/Compiler/Language/DefaultInterfaceMethods/DefaultInterfaceMethodTests.fs +++ b/tests/fsharp/Compiler/Language/DefaultInterfaceMethods/DefaultInterfaceMethodTests.fs @@ -723,6 +723,186 @@ let main _ = let c = CompilationUtil.CreateCSharpCompilation (csharpSource, RoslynLanguageVersion.CSharp8, TargetFramework.NetCoreApp30) CompilerAssert.CompileExeAndRun (fsharpSource, c, "Explicit-Explicit", fsharpLanguageVersion = "4.6") + [] + let ``C# with overloading and generics - Runs`` () = + let csharpSource = + """ +using System; + +namespace CSharpTest +{ + public interface IA + { + void M(U x, T y) + { + Console.Write("M(U, T)"); + } + + void M(T x, U y); + + void M(T x); + + void M(U x); + + void M(T x, string text); + } + + public interface IB : IA + { + void IA.M(T x) + { + Console.Write("InIB"); + } + } +} + """ + + let fsharpSource = + """ +open CSharpTest + +open System + +type Test () = + + interface IA with + + member __.M(_x: int) = Console.Write("InTest") + + member __.M<'Item> (x: int, y: 'Item) = + Console.Write(string x) + Console.Write(y.ToString ()) + + member __.M<'TTT> (x: 'TTT) = + Console.Write(x.ToString ()) + + member __.M (x: int, text: string) = + Console.Write("ABC") + Console.Write(string x) + Console.Write(text) + + member __.M<'U> (_x: 'U, _y: int) = () + +type Test2 () = + inherit Test () + + interface IB with + + member __.M(_x: int) = Console.Write("InTest2") + +[] +let main _ = + let test = Test () :> IA + let test2 = Test2 () :> IA + + test.M 1 + test2.M 1 + + test.M (123, 456s) + test2.M (789, 111s) + + test.M "STRING" + test2.M "-STRING" + + test.M (222, "FSharp") + test2.M (333, "CSharp") + + test.M (obj (), 1) + test2.M (obj (), 1) + + 0 + """ + + let c = CompilationUtil.CreateCSharpCompilation (csharpSource, RoslynLanguageVersion.CSharp8, TargetFramework.NetCoreApp30) + CompilerAssert.CompileExeAndRun (fsharpSource, c, "InTestInTest2123456789111STRING-STRINGABC222FSharpABC333CSharp", fsharpLanguageVersion = "4.6") + + + [] + let ``C# with overloading and generics - Errors with lang version`` () = + let csharpSource = + """ +using System; + +namespace CSharpTest +{ + public interface IA + { + void M(U x, T y) + { + Console.Write("M(U, T)"); + } + + void M(T x, U y); + + void M(T x); + + void M(U x); + + void M(T x, string text); + } + + public interface IB : IA + { + void IA.M(T x) + { + Console.Write("InIB"); + } + } +} + """ + + let fsharpSource = + """ +namespace FSharpTest + +open CSharpTest + +open System + +type Test () = + + interface IA with + + member __.M(_x: int) = Console.Write("InTest") + + member __.M<'Item> (x: int, y: 'Item) = + Console.Write(string x) + Console.Write(y.ToString ()) + + member __.M<'TTT> (x: 'TTT) = + Console.Write(x.ToString ()) + + member __.M (x: int, text: string) = + Console.Write("ABC") + Console.Write(string x) + Console.Write(text) + +type Test2 () = + inherit Test () + + interface IB + """ + + let c = CompilationUtil.CreateCSharpCompilation (csharpSource, RoslynLanguageVersion.CSharp8, TargetFramework.NetCoreApp30) + CompilerAssert.HasTypeCheckErrors (fsharpSource, c, [ + { + Number = 3302 + StartLine = 10 + StartColumn = 14 + EndLine = 10 + EndColumn = 21 + Message = "Feature 'default interface method consumption' is not available in F# 4.6. Please use language version 4.7 or greater." + } + { + Number = 358 + StartLine = 10 + StartColumn = 14 + EndLine = 10 + EndColumn = 21 + Message = "The override for 'M<'U> : 'U * int -> unit' was ambiguous" + } + ], fsharpLanguageVersion = "4.6") + #else [] @@ -3957,6 +4137,282 @@ type Test () = } ]) + [] + let ``C# with overloading and generics - Runs`` () = + let csharpSource = + """ +using System; + +namespace CSharpTest +{ + public interface IA + { + void M(U x, T y) + { + Console.Write("M(U, T)"); + } + + void M(T x, U y); + + void M(T x); + + void M(U x); + + void M(T x, string text); + } + + public interface IB : IA + { + void IA.M(T x) + { + Console.Write("InIB"); + } + } +} + """ + + let fsharpSource = + """ +open CSharpTest + +open System + +type Test () = + + interface IA with + + member __.M(_x: int) = Console.Write("InTest") + + member __.M<'Item> (x: int, y: 'Item) = + Console.Write(string x) + Console.Write(y.ToString ()) + + member __.M<'TTT> (x: 'TTT) = + Console.Write(x.ToString ()) + + member __.M (x: int, text: string) = + Console.Write("ABC") + Console.Write(string x) + Console.Write(text) + +type Test2 () = + inherit Test () + + interface IB + +[] +let main _ = + let test = Test () :> IA + let test2 = Test2 () :> IB + + test.M 1 + test2.M 1 + + test.M (123, 456s) + test2.M (789, 111s) + + test.M "STRING" + test2.M "-STRING" + + test.M (222, "FSharp") + test2.M (333, "CSharp") + + test.M (obj (), 1) + test2.M (obj (), 1) + + 0 + """ + + let c = CompilationUtil.CreateCSharpCompilation (csharpSource, RoslynLanguageVersion.CSharp8, TargetFramework.NetCoreApp30) + CompilerAssert.CompileExeAndRun (fsharpSource, c, "InTestInTest123456789111STRING-STRINGABC222FSharpABC333CSharpM(U, T)M(U, T)") + + [] + let ``C# diamond inheritance with overloading and generics and properties - Runs`` () = + let csharpSource = + """ +using System; + +namespace CSharpTest +{ + public interface IA + { + T Prop1 { get { return default(T); } } + + T Prop2 { set; } + + T Prop3 { get { return default(T); } set { } } + + void M(T x); + + void M(int x) + { + } + } + + public interface IB : IA + { + void IA.M(T x) + { + Console.Write("InIB"); + } + + T IA.Prop2 + { + set + { + Console.Write("IB.Prop2Set"); + } + } + } + + public interface IC : IA + { + void IA.M(string x) + { + Console.Write("InIC"); + } + + string IA.Prop2 + { + set + { + Console.Write("IC.Prop2Set"); + } + } + + void M_C() + { + } + } +} + """ + + let fsharpSource = + """ +open CSharpTest + +open System + +type Test () = + + interface IB + interface IC + interface IA with + + member __.M(_x: string) = Console.Write("Test.String") + + member __.Prop2 with set _ = Console.Write("Test.Prop2") + +[] +let main _ = + let test = Test () :> IC + test.M("") + Console.Write("-") + test.Prop2 <- "" + 0 + """ + + let c = CompilationUtil.CreateCSharpCompilation (csharpSource, RoslynLanguageVersion.CSharp8, TargetFramework.NetCoreApp30) + CompilerAssert.CompileExeAndRun (fsharpSource, c, "Test.String-Test.Prop2") + + [] + let ``C# diamond inheritance with overloading and generics and properties - Errors with no specific implementation`` () = + let csharpSource = + """ +using System; + +namespace CSharpTest +{ + public interface IA + { + T Prop1 { get { return default(T); } } + + T Prop2 { set; } + + T Prop3 { get { return default(T); } set { } } + + void M(T x); + + void M(int x) + { + } + } + + public interface IB : IA + { + void IA.M(T x) + { + Console.Write("InIB"); + } + + T IA.Prop2 + { + set + { + Console.Write("IB.Prop2Set"); + } + } + } + + public interface IC : IA + { + void IA.M(string x) + { + Console.Write("InIC"); + } + + string IA.Prop2 + { + set + { + Console.Write("IC.Prop2Set"); + } + } + + void M_C() + { + } + } +} + """ + + let fsharpSource = + """ +namespace FSharpTest + +open CSharpTest + +open System + +type Test () = + + interface IB + interface IC + interface IA with + + member __.M(_x: string) = Console.Write("Test.String") + """ + + let c = CompilationUtil.CreateCSharpCompilation (csharpSource, RoslynLanguageVersion.CSharp8, TargetFramework.NetCoreApp30) + CompilerAssert.HasTypeCheckErrors (fsharpSource, c, [ + { + Number = 3304 + StartLine = 12 + StartColumn = 14 + EndLine = 12 + EndColumn = 24 + Message = "Interface member 'IA.set_Prop2(value: string) : unit' does not have a most specific implementation." + } + { + Number = 366 + StartLine = 12 + StartColumn = 14 + EndLine = 12 + EndColumn = 24 + Message = "No implementation was given for 'IA.set_Prop2(value: string) : unit'. Note that all interface members must be implemented and listed under an appropriate 'interface' declaration, e.g. 'interface ... with member ...'." + } + ]) + [] let ``C# simple diamond inheritance using object expression - Errors with no specific implementation`` () = let csharpSource =