diff --git a/Tests/NFUnitTestSystemLibReflection/UnitTestGenericArgumentTests.cs b/Tests/NFUnitTestSystemLibReflection/UnitTestGenericArgumentTests.cs index 3d77eb52..dfe0099f 100644 --- a/Tests/NFUnitTestSystemLibReflection/UnitTestGenericArgumentTests.cs +++ b/Tests/NFUnitTestSystemLibReflection/UnitTestGenericArgumentTests.cs @@ -99,7 +99,8 @@ public void CastClass_ArrayToGenericInterface_ComparesElementType() Assert.IsNotNull((IEnumerable)value); Assert.ThrowsException(typeof(InvalidCastException), () => { _ = (IEnumerable)value; }); - Assert.ThrowsException(typeof(InvalidCastException), () => { _ = (IEnumerable)value; }); + Assert.IsNotNull((IEnumerable)value); + Assert.ThrowsException(typeof(InvalidCastException), () => { _ = (IEnumerable)value; }); } [TestMethod] diff --git a/Tests/NFUnitTestSystemLibReflection2/NFUnitTestSystemLibReflection2.nfproj b/Tests/NFUnitTestSystemLibReflection2/NFUnitTestSystemLibReflection2.nfproj index cd5c0a71..5502bd7c 100644 --- a/Tests/NFUnitTestSystemLibReflection2/NFUnitTestSystemLibReflection2.nfproj +++ b/Tests/NFUnitTestSystemLibReflection2/NFUnitTestSystemLibReflection2.nfproj @@ -29,6 +29,7 @@ + diff --git a/Tests/NFUnitTestSystemLibReflection2/UnitTestGenericParameterConstraintTests.cs b/Tests/NFUnitTestSystemLibReflection2/UnitTestGenericParameterConstraintTests.cs index c5909850..a6bf37fb 100644 --- a/Tests/NFUnitTestSystemLibReflection2/UnitTestGenericParameterConstraintTests.cs +++ b/Tests/NFUnitTestSystemLibReflection2/UnitTestGenericParameterConstraintTests.cs @@ -384,13 +384,13 @@ public void EnumConstraint_IsNotSupported() } [TestMethod] - public void VariantInterface_NeedsVariance_IsNotSupported() + public void VariantInterface_UsesVariance() { Type t = Arg(typeof(GcVariant<>)); Assert.IsTrue(typeof(IGcOut).IsAssignableFrom(t)); - Assert.IsTrue(ThrowsNotSupported(() => (object)typeof(IGcOut).IsAssignableFrom(t))); - Assert.IsTrue(ThrowsNotSupported(() => (object)typeof(IGcOut).IsAssignableFrom(t))); + Assert.IsFalse(typeof(IGcOut).IsAssignableFrom(t)); + Assert.IsTrue(typeof(IGcOut).IsAssignableFrom(t)); } [TestMethod] diff --git a/Tests/NFUnitTestSystemLibReflection2/UnitTestGenericTypeIdentityTests.cs b/Tests/NFUnitTestSystemLibReflection2/UnitTestGenericTypeIdentityTests.cs index 7241e2b6..83a2954f 100644 --- a/Tests/NFUnitTestSystemLibReflection2/UnitTestGenericTypeIdentityTests.cs +++ b/Tests/NFUnitTestSystemLibReflection2/UnitTestGenericTypeIdentityTests.cs @@ -479,11 +479,11 @@ public void Assignable_ArraysAsInterfaces() } [TestMethod] - public void Assignable_VarianceUnsupported() + public void Assignable_Variance() { - Assert.IsTrue(Throws(() => typeof(IEnumerable).IsAssignableFrom(typeof(IdEnumerable)))); - Assert.IsTrue(Throws(() => typeof(IEnumerable).IsAssignableFrom(typeof(IEnumerable)))); - Assert.IsTrue(Throws(() => typeof(Func).IsAssignableFrom(typeof(Func)))); + Assert.IsTrue(typeof(IEnumerable).IsAssignableFrom(typeof(IdEnumerable))); + Assert.IsTrue(typeof(IEnumerable).IsAssignableFrom(typeof(IEnumerable))); + Assert.IsTrue(typeof(Func).IsAssignableFrom(typeof(Func))); // the same arguments are exact, and so is an unrelated definition Assert.IsTrue(typeof(IEnumerable).IsAssignableFrom(typeof(IdEnumerable))); @@ -497,15 +497,15 @@ public void Assignable_ExactMatchWinsOverVariance() // both interfaces are implemented, the exact one is found whatever the order of the table Assert.IsTrue(typeof(IEnumerable).IsAssignableFrom(typeof(IdBothEnumerables))); Assert.IsTrue(typeof(IEnumerable).IsAssignableFrom(typeof(IdBothEnumerables))); - Assert.IsTrue(Throws(() => typeof(IEnumerable>).IsAssignableFrom(typeof(IdBothEnumerables)))); + Assert.IsFalse(typeof(IEnumerable>).IsAssignableFrom(typeof(IdBothEnumerables))); } [TestMethod] - public void Assignable_IntegralArraysUnsupported() + public void Assignable_IntegralArrays() { - Assert.IsTrue(Throws(() => typeof(int[]).IsAssignableFrom(typeof(uint[])))); - Assert.IsTrue(Throws(() => typeof(IdEnumB[]).IsAssignableFrom(typeof(int[])))); - Assert.IsTrue(Throws(() => typeof(int[][]).IsAssignableFrom(typeof(uint[][])))); + Assert.IsTrue(typeof(int[]).IsAssignableFrom(typeof(uint[]))); + Assert.IsTrue(typeof(IdEnumB[]).IsAssignableFrom(typeof(int[]))); + Assert.IsTrue(typeof(int[][]).IsAssignableFrom(typeof(uint[][]))); } #endregion diff --git a/Tests/NFUnitTestSystemLibReflection2/UnitTestGenericVarianceTests.cs b/Tests/NFUnitTestSystemLibReflection2/UnitTestGenericVarianceTests.cs new file mode 100644 index 00000000..ee6b67a4 --- /dev/null +++ b/Tests/NFUnitTestSystemLibReflection2/UnitTestGenericVarianceTests.cs @@ -0,0 +1,430 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Collections; +using System.Reflection; +using nanoFramework.TestFramework; + +namespace NFUnitTestSystemLib +{ + public interface IVarOut + { + } + + public interface IVarIn + { + } + + public interface IVarInv + { + } + + public interface IVarBoth + { + } + + public interface IVarOutBase : IVarOut + { + } + + public class VarBase + { + } + + public class VarDerived : VarBase + { + } + + public class VarOther + { + } + + public struct VarStruct + { + } + + public enum VarEnumI : int + { + A, + } + + public enum VarEnumJ : int + { + A, + } + + public enum VarEnumU : uint + { + A, + } + + public enum VarEnumB : byte + { + A, + } + + public class VarCOut : IVarOut + { + } + + public class VarCIn : IVarIn + { + } + + public class VarCInv : IVarInv + { + } + + public class VarCBoth : IVarBoth + { + } + + public class VarCOutBase : IVarOutBase + { + } + + public class VarCDerived : VarCOut + { + } + + public class VarCTwo : IVarOut, IVarOut + { + } + + public class VarCMulti : IVarOut, IVarIn + { + } + + public class VarGBase + { + } + + public class VarGDerived : VarGBase + { + } + + public class VarCmp : IComparable + { + public int CompareTo(VarBase other) + { + return 0; + } + } + + public class VarTake + { + public static int Count; + + public IVarOut Field; + + public void Take(IVarOut value) + { + Count++; + } + } + + // Variance of generic interfaces and delegates on reference-type arguments, and the array rules of ECMA-335. + // Every expected value was printed by .NET 10. + [TestClass] + public class UnitTestGenericVarianceTests + { + [TestMethod] + public void Variance_Covariant() + { + object derived = new VarCOut(); + object @base = new VarCOut(); + + Assert.IsTrue(derived is IVarOut); + Assert.IsTrue(derived is IVarOut); + Assert.IsFalse(@base is IVarOut); + Assert.IsFalse(derived is IVarOut); + Assert.IsNotNull((IVarOut)derived); + Assert.IsNull(@base as IVarOut); + Assert.ThrowsException(typeof(InvalidCastException), () => { IVarOut d = (IVarOut)@base; }); + } + + [TestMethod] + public void Variance_Contravariant() + { + object @base = new VarCIn(); + object derived = new VarCIn(); + + Assert.IsTrue(@base is IVarIn); + Assert.IsFalse(derived is IVarIn); + Assert.IsNull(derived as IVarIn); + } + + [TestMethod] + public void Variance_Invariant() + { + object derived = new VarCInv(); + + Assert.IsFalse(derived is IVarInv); + Assert.IsTrue(derived is IVarInv); + Assert.IsFalse(new VarGDerived() is VarGBase); + } + + [TestMethod] + public void Variance_ValueTypeArguments() + { + Assert.IsFalse((object)new VarCOut() is IVarOut); + Assert.IsFalse((object)new VarCOut() is IVarOut); + Assert.IsFalse((object)new VarCOut() is IVarOut); + Assert.IsFalse((object)new VarCOut() is IVarOut); + Assert.IsFalse((object)new VarCOut() is IVarOut); + Assert.IsTrue((object)new VarCOut() is IVarOut); + } + + [TestMethod] + public void Variance_ArrayArguments() + { + Assert.IsTrue((object)new VarCOut() is IVarOut); + Assert.IsTrue((object)new VarCOut() is IVarOut); + Assert.IsTrue((object)new VarCOut() is IVarOut); + Assert.IsTrue((object)new VarCOut() is IVarOut); + Assert.IsFalse((object)new VarCOut() is IVarOut); + Assert.IsTrue((object)new VarCOut() is IVarOut); + } + + [TestMethod] + public void Variance_Nested() + { + Assert.IsTrue((object)new VarCOut>() is IVarOut>); + Assert.IsFalse((object)new VarCOut>() is IVarOut>); + Assert.IsTrue((object)new VarCOut>() is IVarOut>); + Assert.IsFalse((object)new VarCOut>() is IVarOut>); + Assert.IsTrue((object)new VarCIn>() is IVarIn>); + Assert.IsFalse((object)new VarCOut>() is IVarOut>); + } + + [TestMethod] + public void Variance_BaseAndInterfaceChains() + { + Assert.IsTrue((object)new VarCOutBase() is IVarOut); + Assert.IsTrue((object)new VarCOutBase() is IVarOutBase); + Assert.IsTrue((object)new VarCDerived() is IVarOut); + Assert.IsFalse((object)new VarCDerived() is IVarOut); + Assert.IsTrue((object)new VarCTwo() is IVarOut); + Assert.IsTrue((object)new VarCTwo() is IVarOut); + Assert.IsTrue((object)new VarCMulti() is IVarIn); + Assert.IsFalse((object)new VarCMulti() is IVarIn); + } + + [TestMethod] + public void Variance_Both() + { + Assert.IsTrue((object)new VarCBoth() is IVarBoth); + Assert.IsFalse((object)new VarCBoth() is IVarBoth); + } + + [TestMethod] + public void Variance_Comparable() + { + Assert.IsTrue((object)new VarCmp() is IComparable); + Assert.IsTrue((object)new VarCmp() is IComparable); + Assert.IsFalse((object)new VarCmp() is IComparable); + } + + [TestMethod] + public void Variance_Enumerable() + { + Assert.IsTrue((object)new VarDerived[0] is System.Collections.Generic.IEnumerable); + } + + [TestMethod] + public void Variance_Store() + { + IVarOut[] items = new IVarOut[2]; + + items[0] = new VarCOut(); + items[1] = new VarCOut(); + + Assert.IsNotNull(items[0]); + Assert.ThrowsException( + typeof(Exception), + () => + { + object[] objects = new IVarOut[1]; + objects[0] = new VarCOut(); + }); + } + + [TestMethod] + public void Variance_ArrayCopy() + { + IVarOut[] source = new IVarOut[1] { new VarCOut() }; + IVarOut[] target = new IVarOut[1]; + + Array.Copy(source, target, 1); + + Assert.IsNotNull(target[0]); + + IVarOut[] wide = new IVarOut[1] { new VarCOut() }; + IVarOut[] narrow = new IVarOut[1]; + + Assert.ThrowsException(typeof(InvalidCastException), () => Array.Copy(wide, narrow, 1)); + } + + [TestMethod] + public void Variance_ArrayCovariantCast() + { + object derived = new IVarOut[1]; + + Assert.IsTrue(derived is IVarOut[]); + Assert.IsTrue(derived is object[]); + Assert.IsFalse((object)new IVarOut[1] is IVarOut[]); + Assert.IsTrue((object)new VarCOut[1] is IVarOut[]); + } + + [TestMethod] + public void Variance_Invoke() + { + MethodInfo take = typeof(VarTake).GetMethod("Take"); + VarTake target = new VarTake(); + int before = VarTake.Count; + + take.Invoke(target, new object[] { new VarCOut() }); + take.Invoke(target, new object[] { new VarCOut() }); + + Assert.AreEqual(before + 2, VarTake.Count); + Assert.ThrowsException(typeof(InvalidCastException), () => take.Invoke(target, new object[] { new VarCOut() })); + Assert.ThrowsException(typeof(InvalidCastException), () => take.Invoke(target, new object[] { new VarCOut() })); + } + + [TestMethod] + public void Variance_SetValue() + { + FieldInfo field = typeof(VarTake).GetField("Field"); + VarTake target = new VarTake(); + + field.SetValue(target, new VarCOut()); + + Assert.IsNotNull(target.Field); + Assert.ThrowsException(typeof(Exception), () => field.SetValue(target, new VarCOut())); + } + + [TestMethod] + public void Variance_IsInstanceOfType() + { + Assert.IsTrue(typeof(IVarOut).IsInstanceOfType(new VarCOut())); + Assert.IsFalse(typeof(IVarOut).IsInstanceOfType(new VarCOut())); + Assert.IsTrue(typeof(IVarOut[]).IsInstanceOfType(new IVarOut[0])); + } + + [TestMethod] + public void Variance_IsAssignableFrom() + { + Assert.IsTrue(typeof(IVarOut).IsAssignableFrom(typeof(IVarOut))); + Assert.IsFalse(typeof(IVarOut).IsAssignableFrom(typeof(IVarOut))); + Assert.IsTrue(typeof(IVarOut).IsAssignableFrom(typeof(VarCOut))); + Assert.IsTrue(typeof(IVarOut).IsAssignableFrom(typeof(VarCDerived))); + Assert.IsTrue(typeof(IVarIn).IsAssignableFrom(typeof(IVarIn))); + Assert.IsFalse(typeof(IVarInv).IsAssignableFrom(typeof(IVarInv))); + Assert.IsFalse(typeof(VarGBase).IsAssignableFrom(typeof(VarGDerived))); + Assert.IsTrue(typeof(Func).IsAssignableFrom(typeof(Func))); + Assert.IsFalse(typeof(Func).IsAssignableFrom(typeof(Func))); + Assert.IsTrue(typeof(Action).IsAssignableFrom(typeof(Action))); + Assert.IsFalse(typeof(Action).IsAssignableFrom(typeof(Action))); + Assert.IsFalse(typeof(Func).IsAssignableFrom(typeof(Func))); + Assert.IsTrue(typeof(Func).IsAssignableFrom(typeof(Func))); + } + + [TestMethod] + public void Array_IntegralSameSize() + { + Assert.IsTrue((object)new int[1] is uint[]); + Assert.IsTrue((object)new uint[1] is int[]); + Assert.IsTrue((object)new byte[1] is sbyte[]); + Assert.IsTrue((object)new long[1] is ulong[]); + Assert.IsTrue((object)new VarEnumI[1] is int[]); + Assert.IsTrue((object)new VarEnumI[1] is uint[]); + Assert.IsTrue((object)new VarEnumI[1] is VarEnumJ[]); + Assert.IsTrue((object)new int[1] is VarEnumI[]); + Assert.IsTrue((object)new VarEnumU[1] is int[]); + Assert.IsTrue((object)new VarEnumB[1] is sbyte[]); + Assert.IsTrue((object)new int[1][] is uint[][]); + } + + [TestMethod] + public void Array_IntegralDifferentSize() + { + Assert.IsFalse((object)new int[1] is long[]); + Assert.IsFalse((object)new char[1] is short[]); + Assert.IsFalse((object)new bool[1] is byte[]); + Assert.IsFalse((object)new float[1] is int[]); + Assert.IsFalse((object)new int[1] is object[]); + Assert.IsFalse((object)new int[1] is ValueType[]); + Assert.IsFalse((object)new VarEnumI[1] is Enum[]); + Assert.IsFalse((object)new VarStruct[1] is ValueType[]); + } + + [TestMethod] + public void Array_Depth() + { + Assert.IsTrue((object)new string[1][] is object[]); + Assert.IsTrue((object)new string[1][] is object[][]); + Assert.IsTrue((object)new string[1][][] is object[][]); + Assert.IsTrue((object)new int[1][] is object[]); + Assert.IsTrue((object)new int[1][] is Array[]); + Assert.IsTrue((object)new VarDerived[1][] is VarBase[][]); + } + + [TestMethod] + public void Array_CollectionViews() + { + object derived = new VarDerived[1]; + object integers = new int[1]; + + Assert.IsTrue(derived is System.Collections.Generic.IEnumerable); + Assert.IsTrue(integers is System.Collections.Generic.IEnumerable); + Assert.IsFalse(integers is System.Collections.Generic.IEnumerable); + Assert.IsFalse(integers is System.Collections.Generic.IEnumerable); + Assert.IsTrue(integers is System.Collections.Generic.IEnumerable); + Assert.IsTrue((object)new VarDerived[1][] is System.Collections.Generic.IEnumerable); + Assert.IsTrue((object)new VarCOut[1] is System.Collections.Generic.IEnumerable>); + Assert.IsFalse((object)new VarDerived[1] is System.Collections.Generic.IEnumerable>); + } + + [TestMethod] + public void Array_CopyIntegral() + { + int[] source = new int[2] { -1, 5 }; + uint[] target = new uint[2]; + + Array.Copy(source, target, 2); + + Assert.AreEqual(4294967295u, target[0]); + Assert.AreEqual(5u, target[1]); + } + + [TestMethod] + public void Array_IsAssignableFrom() + { + Assert.IsTrue(typeof(uint[]).IsAssignableFrom(typeof(int[]))); + Assert.IsTrue(typeof(int[]).IsAssignableFrom(typeof(VarEnumI[]))); + Assert.IsTrue(typeof(VarEnumJ[]).IsAssignableFrom(typeof(VarEnumI[]))); + Assert.IsFalse(typeof(long[]).IsAssignableFrom(typeof(int[]))); + Assert.IsFalse(typeof(short[]).IsAssignableFrom(typeof(char[]))); + Assert.IsFalse(typeof(object[]).IsAssignableFrom(typeof(int[]))); + Assert.IsTrue(typeof(VarBase[][]).IsAssignableFrom(typeof(VarDerived[][]))); + Assert.IsTrue(typeof(object[]).IsAssignableFrom(typeof(string[][]))); + Assert.IsTrue(typeof(System.Collections.Generic.IEnumerable).IsAssignableFrom(typeof(int[]))); + Assert.IsFalse(typeof(System.Collections.Generic.IEnumerable).IsAssignableFrom(typeof(int[]))); + Assert.IsTrue(typeof(IVarOut[]).IsAssignableFrom(typeof(VarCOut[]))); + } + + [TestMethod] + public void Unsupported_ObjectsWithoutClosedType() + { + // a delegate object carries no closed type: the cast is accepted by erasure + object action = new Action(VarAction); + + Assert.IsTrue(action is Action); + } + + private static void VarAction(string value) + { + } + } +}