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

@@ -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]);
}
}
}