Merge pull request #5560 from kumpera/wasm-work-p3
[mono.git] / mcs / class / corlib / Test / System / TypeTest.cs
index 72300155135e4f7b93d04f914b52d093a997ac10..72c2cb41923870747b317d5a06907fe1da14446c 100644 (file)
@@ -296,6 +296,14 @@ namespace MonoTests.System
                {
                }
 
+               private void GenericMethod2<A, B, C, D> ()
+                       where C : Duper
+                       where A : B, IFace
+                       where B : C
+                       where D : Baz<object>
+               {
+               }
+
                public class Nested
                {
 
@@ -369,6 +377,54 @@ namespace MonoTests.System
                        // Tests for parameters with generic constraints
                        mi = typeof (TypeTest).GetMethod ("GenericMethod", BindingFlags.Instance|BindingFlags.NonPublic);
                        Assert.IsTrue (typeof (IFace).IsAssignableFrom (mi.GetParameters ()[1].ParameterType));
+
+                       // Transitivity of IsAssignableFrom for type parameters
+                       mi = typeof (TypeTest).GetMethod ("GenericMethod2", BindingFlags.Instance|BindingFlags.NonPublic);
+                       var gparams = mi.GetGenericArguments ();
+                       // B : Duper since B : C and C : Duper
+                       Assert.IsTrue (typeof (Duper).IsAssignableFrom (gparams[1]), "#36");
+                       // A : Duper since A : B and B : Duper
+                       Assert.IsTrue (typeof (Duper).IsAssignableFrom (gparams[0]), "#37a");
+                       // A : IFace since A : IFace
+                       Assert.IsTrue (typeof (IFace).IsAssignableFrom (gparams[0]), "#37b");
+                       // B : Super since B : Duper and Duper : Super
+                       Assert.IsTrue (typeof (Super).IsAssignableFrom (gparams[1]), "#38");
+                       // A : Super since A : B and B : Super
+                       Assert.IsTrue (typeof (Super).IsAssignableFrom (gparams[0]), "#39");
+                       // D : IBar<object> since D : Baz<object> and Baz<object> : IBar<object>
+                       Assert.IsTrue (typeof (IBar<object>).IsAssignableFrom (gparams [3]), "#40");
+                       // A not assignable from B since A : B
+                       Assert.IsFalse (gparams[0].IsAssignableFrom (gparams [1]), "#41");
+                       Assert.IsFalse (gparams[0].IsAssignableFrom (gparams [2]), "#42");
+
+                       // A is not assignable from Array and Delegate and vice versa
+                       Assert.IsFalse (gparams[0].IsAssignableFrom (typeof (Array)), "#43");
+                       Assert.IsFalse (gparams[0].IsAssignableFrom (typeof (Delegate)), "#44");
+                       Assert.IsFalse (typeof (Array).IsAssignableFrom (gparams[0]), "#45");
+                       Assert.IsFalse (typeof (Delegate).IsAssignableFrom (gparams[0]), "#46");
+
+               }
+
+               [Test]
+               public void GenericParameterBaseType ()
+               {
+                       var mi = typeof (TypeTest).GetMethod ("GenericMethod2", BindingFlags.Instance|BindingFlags.NonPublic);
+                       var gparams = mi.GetGenericArguments ();
+
+                       // From the .NET documentation: BaseType property of a
+                       // gparam is "object" if its only constraints are other
+                       // gparams or interfaces, otherwise if it has a class
+                       // constraint that class is the BaseType.
+
+                       // A : B where B is a gparam, and A : IFace which is an
+                       // interface, so A.BaseType is object
+                       Assert.AreEqual (typeof (object), gparams[0].BaseType, "#1");
+                       // B : C where C is a gparam, so B.BaseType is object
+                       Assert.AreEqual (typeof (object), gparams[1].BaseType, "#2");
+                       // C : Duper where Duper is a class, so A.BaseType is Duper
+                       Assert.AreEqual (typeof (Duper), gparams[2].BaseType, "#3");
+                       // D : Baz<object>
+                       Assert.AreEqual (typeof (Baz<object>), gparams[3].BaseType, "#4");
                }
 
                [Test]
@@ -2261,11 +2317,14 @@ namespace MonoTests.System
 
                [Test]
                public void GetGenericMethodDefinitionOverInflatedMethodOnGTD () {
+                       var s = new List<int> () { 1, 2, 3 }.ConvertAll ( i => i.ToString () );
+                       Assert.AreEqual (3, s.Count);
                        var l = typeof (List<>);
                        var m = l.GetMethod ("ConvertAll");
                        var infl = m.MakeGenericMethod (typeof (int));
                        var res = m.GetGenericMethodDefinition ();
                        Assert.AreEqual (m, res, "#1");
+                       Assert.AreEqual (1, infl.GetGenericArguments().Length, "#2");
                }
 
                [Test]
@@ -3271,6 +3330,38 @@ namespace MonoTests.System
                        Assert.AreSame (expectedType, r, "#2");
                }
 
+               public class BConstrained<Y> where Y : BConstrained<Y> {
+               }
+
+               public class AConstrained<X> : BConstrained<AConstrained<X>> {
+               }
+
+               [Test] // Bug https://bugzilla.xamarin.com/show_bug.cgi?id=54485
+               public void MakeGenericType_GTD_Constraint ()
+               {
+                       // This is pretty weird, but match .NET behavior (note
+                       // that typeof(BConstrained<AConstrained<>>) is a
+                       // compile-time error with roslyn, but it's apparently
+                       // an ok thing to make with reflection.
+                       var tb = typeof (BConstrained<>);
+                       var ta = typeof (AConstrained<>);
+                       var result = tb.MakeGenericType (ta);
+                       Assert.IsNotNull (result, "#1");
+                       // lock down the answer to match what .NET makes
+                       Assert.IsTrue (result.IsGenericType, "#2");
+                       Assert.AreEqual (tb, result.GetGenericTypeDefinition (), "#3");
+                       var bargs = result.GetGenericArguments ();
+                       Assert.AreEqual (1, bargs.Length, "#4");
+                       var arg = bargs [0];
+                       Assert.IsTrue (arg.IsGenericType, "#5");
+                       // N.B. evidently AConstrained`1 and AConstrained`1<!0> are the same type
+                       Assert.IsTrue (arg.IsGenericTypeDefinition, "#6");
+                       Assert.AreEqual (ta, arg.GetGenericTypeDefinition (), "#7");
+                       var aargs = arg.GetGenericArguments ();
+                       Assert.AreEqual (1, aargs.Length, "#8");
+                       Assert.AreEqual (ta.GetGenericArguments () [0], aargs [0], "#9");
+               }
+
        [Test]
        public void EqualsUserType () {
                UserType2 t1 = new UserType2(null);
@@ -3584,6 +3675,38 @@ namespace MonoTests.System
                        public int field;
                }
 
+               [Test]
+               public void IsAssignableFromGenericArgumentsWithConstraints ()
+               {
+                       // Regression test for #58809
+
+                       // Generic Parameters of a gtd should have their
+                       // constraints respected even when those constraints
+                       // are other generic parameters themselves.
+
+                       var ps = typeof (GenericWithParamConstraints<,,>).GetGenericArguments ();
+
+                       var a = ps[0];
+                       var b = ps[1];
+                       var c = ps[2];
+
+                       // Foo<C>
+                       var fooOfC = typeof (Foo<>).MakeGenericType (c);
+
+                       // constraint B : Foo <C>
+                       Assert.IsTrue (fooOfC.IsAssignableFrom (b), "#1");
+
+                       // constraint A : B
+                       Assert.IsTrue (b.IsAssignableFrom (a), "#2");
+
+                       // A : Foo<C> since A : B and B : Foo<C>
+                       Assert.IsTrue (fooOfC.IsAssignableFrom (a), "#3");
+               }
+
+               class GenericWithParamConstraints<A, B, C> where B : Foo<C> where A : B
+               {
+               }
+
                [Test] // Bug #612780
                public void CannotMakeDerivedTypesFromTypedByRef ()
                {
@@ -4305,7 +4428,7 @@ namespace MonoTests.System
                        var nm = new AssemblyName ("asm");
                        var ab = AssemblyBuilder.DefineDynamicAssembly (nm,
                                                                        AssemblyBuilderAccess.Run);
-                       var mb = ab.DefineDynamicModule("m", true);
+                       var mb = ab.DefineDynamicModule("m", false);
                        var tb = mb.DefineType ("NameSpace,+*&[]\\.Type,+*&[]\\",
                                                TypeAttributes.Class | TypeAttributes.Public);