Imported Upstream version 5.20.0.180

Former-commit-id: ff953ca879339fe1e1211f7220f563e1342e66cb
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2019-02-04 20:11:37 +00:00
parent 0e2d47d1c8
commit 0510252385
3360 changed files with 83827 additions and 39243 deletions

View File

@@ -195,7 +195,7 @@ namespace MonoTests.System.Reflection
// note: only available in default appdomain
// http://weblogs.asp.net/asanto/archive/2003/09/08/26710.aspx
// Not sure we should emulate this behavior.
#if __WATCHOS__
#if MONOTOUCH_WATCH
Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
#elif !MONODROID

View File

@@ -29,20 +29,60 @@
using NUnit.Framework;
using System;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace MonoTests.System.Reflection
{
enum Levels { one, two, three }
class Attr : Attribute {
public Attr (byte[] arr) {
}
}
[AttributeUsage (AttributeTargets.Method)]
class TestAttrWithObjectCtorParam : Attribute {
object o;
public TestAttrWithObjectCtorParam (object o) => this.o = o;
}
[AttributeUsage (AttributeTargets.Method)]
class TestAttrWithEnumCtorParam : Attribute {
Levels level;
public TestAttrWithEnumCtorParam (Levels level) => this.level = level;
}
[AttributeUsage (AttributeTargets.Method)]
class TestAttrWithObjectArrayCtorParam : Attribute {
object[] o;
public TestAttrWithObjectArrayCtorParam (object[] o) => this.o = o;
}
[AttributeUsage (AttributeTargets.Method)]
class TestAttrWithObjectArrayCtorParamAndParamsKeyword : Attribute {
object[] o;
public TestAttrWithObjectArrayCtorParamAndParamsKeyword (params object[] o) => this.o = o;
}
[AttributeUsage (AttributeTargets.Method)]
class TestAttrWithObjectArrayCtorParams : Attribute {
object[] o1;
object[] o2;
public TestAttrWithObjectArrayCtorParams (object[] o1, params object[] o2) {
this.o1 = o1;
this.o2 = o2;
}
}
[TestFixture]
public class CustomAttributeDataTest
{
[DllImport ("libc")]
public static extern void pinvoke ();
[MarshalAs (UnmanagedType.LPStr)]
[NonSerialized]
public string fieldDecoratedWithPseudoCustomAttributes = "test";
@@ -51,6 +91,31 @@ namespace MonoTests.System.Reflection
public void MethodWithAttr () {
}
[TestAttrWithObjectCtorParam (Levels.two)]
public void MethodDecoratedWithAttribute1 ()
{
}
[TestAttrWithEnumCtorParam (Levels.two)]
public void MethodDecoratedWithAttribute2 ()
{
}
[TestAttrWithObjectArrayCtorParam (new object[] { Levels.one, Levels.two})]
public void MethodDecoratedWithAttribute3 ()
{
}
[TestAttrWithObjectArrayCtorParamAndParamsKeyword (Levels.one, Levels.two)]
public void MethodDecoratedWithAttribute4 ()
{
}
[TestAttrWithObjectArrayCtorParams (new object[] { Levels.one, Levels.two}, Levels.three, Levels.two)]
public void MethodDecoratedWithAttribute5 ()
{
}
public void MethodWithParamDecoratedWithPseudoCustomAttributes ([Optional, In, Out, MarshalAs (UnmanagedType.LPStr)] String s)
{
}
@@ -62,7 +127,6 @@ namespace MonoTests.System.Reflection
}
[Test]
[Category ("MobileNotWorking")] // #10263
public void Arrays () {
IList<CustomAttributeData> cdata = CustomAttributeData.GetCustomAttributes (typeof (CustomAttributeDataTest).GetMethod ("MethodWithAttr"));
Assert.AreEqual (1, cdata.Count);
@@ -75,6 +139,78 @@ namespace MonoTests.System.Reflection
Assert.AreEqual (typeof (byte), arr [1].ArgumentType);
Assert.AreEqual (2, arr [1].Value);
}
[Test]
// https://github.com/mono/mono/issues/10951
public void ObjectArrays ()
{
CheckObjectArrayParam (nameof (MethodDecoratedWithAttribute3));
CheckObjectArrayParam (nameof (MethodDecoratedWithAttribute4));
}
private void CheckObjectArrayParam (string methodName)
{
IList<CustomAttributeData> cdata = CustomAttributeData.GetCustomAttributes (typeof (CustomAttributeDataTest).GetMethod (methodName));
Assert.AreEqual (1, cdata.Count, $"{methodName}#0");
CustomAttributeTypedArgument arg = cdata [0].ConstructorArguments [0];
Assert.IsTrue (typeof (IList<CustomAttributeTypedArgument>).IsAssignableFrom (arg.Value.GetType ()), $"{methodName}#1");
IList<CustomAttributeTypedArgument> arr = (IList<CustomAttributeTypedArgument>)arg.Value;
Assert.AreEqual (2, arr.Count, $"{methodName}#2");
Assert.AreEqual (typeof (Levels), arr [0].ArgumentType, $"{methodName}#3");
Assert.IsTrue (arr [0].ArgumentType.GetTypeInfo().IsEnum, $"{methodName}#4");
Assert.AreEqual (0, arr [0].Value, $"{methodName}#5");
Assert.AreEqual (typeof (int), arr [0].Value.GetType (), $"{methodName}#6");
Assert.AreEqual (typeof (Levels), arr [1].ArgumentType, $"{methodName}#7");
Assert.IsTrue (arr [1].ArgumentType.GetTypeInfo().IsEnum, $"{methodName}#8");
Assert.AreEqual (1, arr [1].Value, $"{methodName}#9");
Assert.AreEqual (typeof (int), arr [1].Value.GetType (), $"{methodName}#10");
}
[Test]
// https://github.com/mono/mono/issues/10951
public void CheckObjectArrayParams ()
{
string methodName = nameof (MethodDecoratedWithAttribute5);
IList<CustomAttributeData> cdata = CustomAttributeData.GetCustomAttributes (typeof (CustomAttributeDataTest).GetMethod (methodName));
Assert.AreEqual (1, cdata.Count, $"{methodName}#0");
Assert.AreEqual (2, cdata [0].ConstructorArguments.Count, $"{methodName}#00");
CustomAttributeTypedArgument arg = cdata [0].ConstructorArguments [0];
Assert.IsTrue (typeof (IList<CustomAttributeTypedArgument>).IsAssignableFrom (arg.Value.GetType ()), $"{methodName}#1");
IList<CustomAttributeTypedArgument> arr = (IList<CustomAttributeTypedArgument>)arg.Value;
Assert.AreEqual (2, arr.Count, $"{methodName}#2");
Assert.AreEqual (typeof (Levels), arr [0].ArgumentType, $"{methodName}#3");
Assert.IsTrue (arr [0].ArgumentType.GetTypeInfo().IsEnum, $"{methodName}#4");
Assert.AreEqual (0, arr [0].Value, $"{methodName}#5");
Assert.AreEqual (typeof (int), arr [0].Value.GetType (), $"{methodName}#6");
Assert.AreEqual (typeof (Levels), arr [1].ArgumentType, $"{methodName}#7");
Assert.IsTrue (arr [1].ArgumentType.GetTypeInfo().IsEnum, $"{methodName}#8");
Assert.AreEqual (1, arr [1].Value, $"{methodName}#9");
Assert.AreEqual (typeof (int), arr [1].Value.GetType (), $"{methodName}#10");
arg = cdata [0].ConstructorArguments [1];
Assert.IsTrue (typeof (IList<CustomAttributeTypedArgument>).IsAssignableFrom (arg.Value.GetType ()), $"{methodName}#11");
arr = (IList<CustomAttributeTypedArgument>)arg.Value;
Assert.AreEqual (2, arr.Count, $"{methodName}#12");
Assert.AreEqual (typeof (Levels), arr [0].ArgumentType, $"{methodName}#13");
Assert.IsTrue (arr [0].ArgumentType.GetTypeInfo().IsEnum, $"{methodName}#14");
Assert.AreEqual (2, arr [0].Value, $"{methodName}#15");
Assert.AreEqual (typeof (int), arr [0].Value.GetType (), $"{methodName}#16");
Assert.AreEqual (typeof (Levels), arr [1].ArgumentType, $"{methodName}#17");
Assert.IsTrue (arr [1].ArgumentType.GetTypeInfo().IsEnum, $"{methodName}#18");
Assert.AreEqual (1, arr [1].Value, $"{methodName}#19");
Assert.AreEqual (typeof (int), arr [1].Value.GetType (), $"{methodName}#20");
}
[Test]
public void ParameterIncludesPseudoCustomAttributesData ()
@@ -86,19 +222,19 @@ namespace MonoTests.System.Reflection
Assert.AreEqual (4, customAttributesData.Count);
var inAttributeData = customAttributesData [0];
var optionalAttributeData = customAttributesData [1];
var outAttributeData = customAttributesData [2];
var outAttributeData = customAttributesData [1];
var optionalAttributeData = customAttributesData [2];
var marshalAsAttributeData = customAttributesData [3];
var marshalAsAttributeCtorArg = marshalAsAttributeData.ConstructorArguments [0];
Assert.AreEqual (typeof (InAttribute), inAttributeData.AttributeType);
Assert.AreEqual (typeof (OptionalAttribute), optionalAttributeData.AttributeType);
Assert.AreEqual (typeof (OutAttribute), outAttributeData.AttributeType);
Assert.AreEqual (typeof (OptionalAttribute), optionalAttributeData.AttributeType);
Assert.AreEqual (typeof (MarshalAsAttribute), marshalAsAttributeData.AttributeType);
Assert.AreEqual (typeof (UnmanagedType), marshalAsAttributeCtorArg.ArgumentType);
Assert.AreEqual (UnmanagedType.LPStr, marshalAsAttributeCtorArg.Value);
Assert.AreEqual ((int)UnmanagedType.LPStr, marshalAsAttributeCtorArg.Value);
}
[Test]
@@ -116,7 +252,7 @@ namespace MonoTests.System.Reflection
Assert.AreEqual (typeof (NonSerializedAttribute), nonSerializedAttributeData.AttributeType);
Assert.AreEqual (typeof (MarshalAsAttribute), marshalAsAttributeData.AttributeType);
Assert.AreEqual (typeof (UnmanagedType), marshalAsAttributeDataCtorArg.ArgumentType);
Assert.AreEqual (UnmanagedType.LPStr, marshalAsAttributeDataCtorArg.Value);
Assert.AreEqual ((int)UnmanagedType.LPStr, marshalAsAttributeDataCtorArg.Value);
}
[Test]
@@ -131,7 +267,52 @@ namespace MonoTests.System.Reflection
Assert.AreEqual (1, customAttributesData.Count);
Assert.AreEqual (typeof (MarshalAsAttribute), marshalAsAttributeData.AttributeType);
Assert.AreEqual (typeof (UnmanagedType), ctorArg.ArgumentType);
Assert.AreEqual (UnmanagedType.LPStr, ctorArg.Value);
Assert.AreEqual ((int)UnmanagedType.LPStr, ctorArg.Value);
}
[Test]
// https://github.com/mono/mono/issues/10544
public void MethodIncludesDllImportAttributeData ()
{
var mi = typeof (CustomAttributeDataTest).FindMembers (MemberTypes.Method, BindingFlags.Static | BindingFlags.Public, (m, criteria) => m.Name == "pinvoke", null);
var data = ((MethodInfo)(mi[0])).CustomAttributes;
Assert.AreEqual (2, data.Count ());
Assert.AreEqual (typeof (PreserveSigAttribute), data.First ().AttributeType);
var dllImportAttributeData = data.Last ();
var ctorArg = dllImportAttributeData.ConstructorArguments [0];
Assert.AreEqual (typeof (DllImportAttribute), dllImportAttributeData.AttributeType);
Assert.AreEqual ("libc", ctorArg.Value);
}
[Test]
// https://github.com/mono/mono/issues/10555
public void CustomAttributeCtor_TakesEnumArg ()
{
var method = GetMethod (nameof (MethodDecoratedWithAttribute1));
var data = method.CustomAttributes;
var ctorArg = data.First ().ConstructorArguments [0];
Assert.AreEqual (typeof (Levels), ctorArg.ArgumentType);
Assert.AreEqual (1, ctorArg.Value);
Assert.AreEqual (typeof (int), ctorArg.Value.GetType ());
method = GetMethod (nameof (MethodDecoratedWithAttribute2));
data = method.CustomAttributes;
ctorArg = data.First ().ConstructorArguments [0];
Assert.AreEqual (typeof (Levels), ctorArg.ArgumentType);
Assert.AreEqual (1, ctorArg.Value);
Assert.AreEqual (typeof (int), ctorArg.Value.GetType ());
}
private MethodInfo GetMethod (string methodName)
{
var mi = typeof (CustomAttributeDataTest).FindMembers (MemberTypes.Method, BindingFlags.Instance | BindingFlags.Public, (m, criteria) => m.Name == methodName, null);
return (MethodInfo)(mi [0]);
}
}
}

View File

@@ -123,6 +123,13 @@ namespace MonoTests.System.Reflection
Assert.AreEqual (type.Module, ev.Module);
}
[Test]
public void MetadataToken ()
{
EventInfo ev = typeof (TestClass).GetEvent ("pub");
Assert.IsTrue ((int)ev.MetadataToken > 0);
}
#pragma warning disable 67
public class PrivateEvent
{

View File

@@ -29,6 +29,7 @@
//
using System;
using System.Globalization;
using System.Threading;
using System.Reflection;
#if !MONOTOUCH && !FULL_AOT_RUNTIME
@@ -224,6 +225,14 @@ namespace MonoTests.System.Reflection
Assert.AreEqual (typeof (ObsoleteAttribute), attrs [0].GetType (), "#D10");
}
[Test]
public void MetadataToken ()
{
Type type = typeof (FieldInfoTest);
FieldInfo field = type.GetField ("i");
Assert.IsTrue ((int)field.MetadataToken > 0);
}
[Test] // GetFieldFromHandle (RuntimeFieldHandle)
public void GetFieldFromHandle1_Handle_Zero ()
{
@@ -1455,6 +1464,41 @@ namespace MonoTests.System.Reflection
public const FieldInfoTest object_field = null;
public int non_const_field;
class FieldInfoWrapper : FieldInfo
{
private FieldInfo fieldInfo;
public FieldInfoWrapper (FieldInfo fieldInfo)
{
this.fieldInfo = fieldInfo;
}
public override FieldAttributes Attributes => fieldInfo.Attributes;
public override Type DeclaringType => fieldInfo.DeclaringType;
public override RuntimeFieldHandle FieldHandle => fieldInfo.FieldHandle;
public override Type FieldType => fieldInfo.FieldType;
public override string Name => fieldInfo.Name;
public override Type ReflectedType => fieldInfo.ReflectedType;
public override object[] GetCustomAttributes (bool inherit) => fieldInfo.GetCustomAttributes (inherit);
public override object[] GetCustomAttributes (Type attributeType, bool inherit) => fieldInfo.GetCustomAttributes (attributeType, inherit);
public override object GetValue (object obj) => fieldInfo.GetValue (obj);
public override bool IsDefined (Type attributeType, bool inherit) => fieldInfo.IsDefined (attributeType, inherit);
public override void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture) =>
fieldInfo.SetValue (obj, value, invokeAttr, binder, culture);
}
[Test]
public void CustomFieldInfo ()
{
var fieldInfoWrapper = new FieldInfoWrapper (GetType ().GetField (nameof (non_const_field)));
MethodInfo method = typeof (FieldInfoWrapper).GetMethod ("GetFieldOffset", BindingFlags.NonPublic | BindingFlags.Instance);
Assert.IsNotNull (method);
Assert.IsTrue (method.IsVirtual);
var ex = Assert.Catch<Exception> (() => method.Invoke (fieldInfoWrapper, new object[] {}));
Assert.IsTrue (ex.InnerException is SystemException);
}
}
// We do not refernece the field, that is expected

View File

@@ -317,6 +317,13 @@ public class ModuleTest
Assert.AreEqual (method, res, "#1");
}
[Test]
public void ResolveInvalidMember () // https://github.com/mono/mono/issues/9604
{
Module m = typeof (ModuleTest).Module;
Assert.Throws<ArgumentOutOfRangeException> (() => m.ResolveMember(0x0A00F000));
}
[Test]
public void FindTypes ()
{

View File

@@ -507,6 +507,26 @@ namespace MonoTests.System.Reflection
Assert.AreEqual (expected, actual, "#1");
}
public class Dummy {
public void M1 (decimal? arg = 12.345M) { }
public void M2 ([Optional, DecimalConstant (1, 2, 3, 4, 5)] decimal? arg) { }
public void M3 (decimal? arg = null) { }
public void M4 ([Optional, DateTimeConstant (1L)] DateTime? arg) { }
public void M5 (DateTime? arg = null) { }
}
[Test]
// https://github.com/mono/mono/issues/11303
public void RawDefaultValue_Nullable ()
{
var type = typeof (Dummy);
Assert.AreEqual (12.345M, type.GetMethod("M1").GetParameters () [0].RawDefaultValue);
Assert.AreEqual (new DecimalConstantAttribute (1, 2, 3, 4, 5).Value, type.GetMethod ("M2").GetParameters () [0].RawDefaultValue);
Assert.AreEqual (null, type.GetMethod ("M3").GetParameters () [0].RawDefaultValue);
Assert.AreEqual (new DateTime (1), type.GetMethod ("M4").GetParameters () [0].RawDefaultValue);
Assert.AreEqual (null, type.GetMethod ("M5").GetParameters () [0].RawDefaultValue);
}
[Test]
public void ReturnParameter_IsDefined_False ()
{

View File

@@ -560,5 +560,12 @@ namespace MonoTests.System.Reflection
Assert.AreEqual ("param", defaultParam.Name, "#1");
Assert.AreEqual ("test", defaultParam.DefaultValue, "#2");
}
[Test]
public void MetadataToken ()
{
PropertyInfo property = typeof (Base).GetProperty ("P");
Assert.IsTrue ((int)property.MetadataToken > 0);
}
}
}