From 7c8749eeada349b3c6e2ea806a5538e7c57a9077 Mon Sep 17 00:00:00 2001 From: Vladimir Sadov Date: Sat, 19 Oct 2019 18:01:33 -0700 Subject: [PATCH] A few more test scenarios for casting of nullables and open generic arrays. (dotnet/corefx#41918) * A few more test scenarios for casting of nullables and open generic arrays. Commit migrated from https://github.com/dotnet/corefx/commit/03dd803990dfb8f05db8d0ec1bc34268386c1d5b --- .../System.Reflection/tests/TypeInfoTests.cs | 55 ++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/libraries/System.Reflection/tests/TypeInfoTests.cs b/src/libraries/System.Reflection/tests/TypeInfoTests.cs index 76989f6..b1d9326 100644 --- a/src/libraries/System.Reflection/tests/TypeInfoTests.cs +++ b/src/libraries/System.Reflection/tests/TypeInfoTests.cs @@ -574,6 +574,12 @@ namespace System.Reflection.Tests Assert.Equal(expected, type.GetTypeInfo().IsAssignableFrom(c?.GetTypeInfo())); } + class G where T : U + { + } + + static volatile object s_boxedInt32; + [Fact] public void IsAssignableFromNullable() { @@ -599,6 +605,55 @@ namespace System.Reflection.Tests // Nullable is not assignable from T Assert.False(nubOfT.IsAssignableFrom(T)); Assert.False(T.IsAssignableFrom(nubOfT)); + + // illegal type construction due to T->T? + Assert.Throws(() => typeof(G<,>).MakeGenericType(typeof(int), typeof(int?))); + + // Test trivial object casts + s_boxedInt32 = (object)1234; + Assert.True((s_boxedInt32 is int?) && (int?)s_boxedInt32 == 1234); + + // test construction again to catch caching issues + Assert.Throws(() => typeof(G<,>).MakeGenericType(typeof(int), typeof(int?))); + } + + interface IFace + { + } + + class G where T : class, IFace + { + //void OpenGenericArrays() + //{ + // // this is valid, reflection checks below should agree + // IFace[] arr2 = default(T[]); + // IEnumerable ie = default(T[]); + //} + } + + class GG where T : class, U + { + //void OpenGenericArrays() + //{ + // // this is valid, reflection checks below should agree + // U[] arr2 = default(T[]); + // IEnumerable ie = default(T[]); + //} + } + + [Fact] + public void OpenGenericArrays() + { + Type a = typeof(G<>).GetGenericArguments()[0].MakeArrayType(); + Assert.True(typeof(IFace[]).IsAssignableFrom(a)); + Assert.True(typeof(IEnumerable).IsAssignableFrom(a)); + + Type a1 = typeof(GG<,>).GetGenericArguments()[0].MakeArrayType(); + Type a2 = typeof(GG<,>).GetGenericArguments()[1].MakeArrayType(); + Assert.True(a2.IsAssignableFrom(a1)); + + Type ie = typeof(IEnumerable<>).MakeGenericType(typeof(GG<,>).GetGenericArguments()[1]); + Assert.True(ie.IsAssignableFrom(a1)); } public static IEnumerable IsEquivilentTo_TestData() -- 2.7.4