Merge pull request #1624 from esdrubal/getprocesstimes
[mono.git] / mcs / class / corlib / Test / System.Reflection / ParameterInfoTest.cs
index 59adc02f9db01622b6a3db5132efbc8d6fa6f009..423c7c0670d9d915f1a031208f0cc87b21740af1 100644 (file)
 using System;
 using System.Threading;
 using System.Reflection;
-#if !TARGET_JVM
-using System.Reflection.Emit;
-#endif // TARGET_JVM
 using System.Runtime.InteropServices;
 using System.Runtime.CompilerServices;
+using System.Collections.Generic;
+using System.Linq;
 
 using NUnit.Framework;
 
@@ -74,24 +73,22 @@ namespace MonoTests.System.Reflection
                        }
                }
 
-#if NET_2_0 && !NET_2_1
+#if !NET_2_1
                public enum ParamEnum {
                        None = 0,
                        Foo = 1,
                        Bar = 2
                };
 
-               public static void paramMethod (int i, [In] int j, [Out] int k, [Optional] int l, [In,Out] int m, [DefaultParameterValue (ParamEnum.Foo)] ParamEnum n)
+               public static void paramMethod (int i, [In] int j, [Out] int k, [Optional] int l, [In,Out] int m, ParamEnum n = ParamEnum.Foo)
                {
                }
 
-#if !TARGET_JVM // No support for extern methods in TARGET_JVM
                [DllImport ("foo")]
                public extern static void marshalAsMethod (
                        [MarshalAs(UnmanagedType.Bool)]int p0, 
                        [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] string [] p1,
                        [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Marshal1), MarshalCookie = "5")] object p2);
-#endif
                [Test]
                public void DefaultValueEnum () {
                        ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
@@ -100,6 +97,15 @@ namespace MonoTests.System.Reflection
                        Assert.AreEqual (ParamEnum.Foo, info [5].DefaultValue, "#2");
                }
 
+#if NET_4_5
+               [Test]
+               public void HasDefaultValueEnum () {
+                       ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
+
+                       Assert.IsTrue (info [5].HasDefaultValue);
+               }
+#endif
+
                public static void Sample2 ([DecimalConstantAttribute(2,2,2,2,2)] decimal a, [DateTimeConstantAttribute(123456)] DateTime b) {}
 
                [Test]
@@ -119,7 +125,19 @@ namespace MonoTests.System.Reflection
                        Assert.AreEqual (pi [1].DefaultValue.GetType (), typeof (Missing), "#2");
                }
 
-               public void Sample (int a, [Optional] int b)
+#if NET_4_5
+               [Test]
+               public void TestHasDefaultValues ()
+               {
+                       ParameterInfo [] pi = typeof (ParameterInfoTest).GetMethod ("Sample").GetParameters ();
+
+                       Assert.IsFalse (pi [0].HasDefaultValue, "#1");
+                       Assert.IsFalse (pi [1].HasDefaultValue, "#2");
+                       Assert.IsTrue (pi [2].HasDefaultValue, "#3");
+               }
+#endif
+
+               public void Sample (int a, [Optional] int b, object c = null)
                {
                }
 
@@ -132,7 +150,6 @@ namespace MonoTests.System.Reflection
                        Assert.AreEqual (1, info[3].GetCustomAttributes (typeof (OptionalAttribute), true).Length, "#A4");
                        Assert.AreEqual (2, info[4].GetCustomAttributes (true).Length, "#A5");
 
-#if !TARGET_JVM // No support for extern methods in TARGET_JVM
                        ParameterInfo[] pi = typeof (ParameterInfoTest).GetMethod ("marshalAsMethod").GetParameters ();
                        MarshalAsAttribute attr;
 
@@ -147,7 +164,6 @@ namespace MonoTests.System.Reflection
                        Assert.AreEqual (UnmanagedType.CustomMarshaler, attr.Value, "#D1");
                        Assert.AreEqual ("5", attr.MarshalCookie, "#D2");
                        Assert.AreEqual (typeof (Marshal1), Type.GetType (attr.MarshalType), "#D3");
-#endif
                }
 
                [Test] // bug #342536
@@ -236,6 +252,205 @@ namespace MonoTests.System.Reflection
                        var info = typeof (ParameterInfoTest).GetMethod ("TestC").GetParameters ();
                        Assert.AreEqual (decimal.MaxValue, info [0].DefaultValue);
                }
+
+#if NET_4_5
+               [Test]
+               public void HasDefaultValueDecimal () {
+                       var info = typeof (ParameterInfoTest).GetMethod ("TestC").GetParameters ();
+                       Assert.IsTrue (info [0].HasDefaultValue);
+               }
+#endif
+
+               class TestAttribute : Attribute
+               {
+               }
+
+               public static int TestCustomAttribute_Method ([Test] string arg)
+               {
+                       return arg.Length;
+               }
+
+               [Test]
+               public void TestCustomAttribute ()
+               {
+                       var metInfo = GetType ().GetMethod ("TestCustomAttribute_Method", new Type[] { typeof(string) });
+                       var paramInfos = metInfo.GetParameters ();
+                       var argParamInfo = paramInfos[0];
+
+                       var custAttrs = argParamInfo.GetCustomAttributes ();
+                       Assert.AreEqual (1, custAttrs.Count ());
+               }
+
+               class MyParameterInfo2 : ParameterInfo
+               {
+                       public ParameterAttributes MyAttrsImpl;
+
+                       public override ParameterAttributes Attributes {
+                               get {return MyAttrsImpl;}
+                       }
+
+                       public IList<CustomAttributeData> myList = new List<CustomAttributeData> ();
+
+                       public override IList<CustomAttributeData> GetCustomAttributesData () {
+                               return myList;
+                       }
+               }
+
+               class MyParameterInfo : ParameterInfo
+               {
+                       public void SetClassImpl (Type t)
+                       {
+                               ClassImpl = t;
+                       }
+
+                       public void SetDefaultValueImpl (object o)
+                       {
+                               DefaultValueImpl = o;
+                       }
+
+                       public void SetMemberImpl (MemberInfo o)
+                       {
+                               MemberImpl = o;
+                       }
+
+                       public void SetNameImpl (string s)
+                       {
+                               NameImpl = s;
+                       }
+
+                       public void SetPositionImpl (int i)
+                       {
+                               PositionImpl = i;
+                       }
+
+                       public void SetAttrsImpl (ParameterAttributes a)
+                       {
+                               AttrsImpl = a;
+                       }
+
+                       public void TestMethod (int a) {}
+                       public int this[int x, int y] {
+                               get { return 0; }
+                               set { }
+                       }
+
+               }
+
+               [Test]
+               public void SubClassWithNoOverrides ()
+               {
+                       var p = new MyParameterInfo ();
+                       Assert.IsFalse (p.IsDefined (typeof (FlagsAttribute), false), "#1");
+                       Assert.AreEqual (0, p.GetCustomAttributes (false).Length, "#2");
+                       Assert.AreEqual (0, p.GetCustomAttributes (typeof (FlagsAttribute), false).Length, "#3");
+                       Assert.AreEqual (0, p.GetOptionalCustomModifiers ().Length, "#4");
+                       Assert.AreEqual (0, p.GetRequiredCustomModifiers ().Length, "#5");
+#if NET_4_5
+                       try {
+                               var ign = p.HasDefaultValue;
+                               Assert.Fail ("#6");
+                       } catch (NotImplementedException) {
+                       }
+#endif
+                       Assert.IsFalse (p.IsIn, "#7");
+#if FEATURE_USE_LCID
+                       Assert.IsFalse (p.IsLcid, "#8");
+#endif
+                       Assert.IsFalse (p.IsOptional, "#9");
+                       Assert.IsFalse (p.IsOut, "#10");
+                       Assert.IsFalse (p.IsRetval, "#10");
+#if NET_4_5
+                       try {
+                               var ign = p.CustomAttributes;
+                               Assert.Fail ("#11");
+                       } catch (NotImplementedException) {
+                       }
+#endif
+                       try {
+                               p.GetCustomAttributesData ();
+                               Assert.Fail ("#12");
+                       } catch (NotImplementedException) {
+                       }
+
+                       Assert.AreEqual (0x8000000, p.MetadataToken, "#13");
+                       Assert.AreEqual (0, p.Position, "#14");
+                       try {
+                               var ign = p.DefaultValue;
+                               Assert.Fail ("#15");
+                       } catch (NotImplementedException) {
+                       }
+                       try {
+                               var ign = p.RawDefaultValue;
+                               Assert.Fail ("#16");
+                       } catch (NotImplementedException) {
+                       }
+                       Assert.IsNull (p.Member, "#17");
+                       Assert.AreEqual (ParameterAttributes.None, p.Attributes, "#18");
+                       Assert.IsNull (p.Name, "#19");
+                       Assert.IsNull (p.ParameterType, "#20");
+               }
+
+               [Test]
+               public void SubClassWithValuesSet ()
+               {
+                       var p = new MyParameterInfo ();
+                       p.SetClassImpl (typeof (Decimal));
+                       Assert.AreEqual (typeof (Decimal), p.ParameterType, "#1");
+                       p.SetClassImpl (null);
+
+                       p.SetDefaultValueImpl ("foo");
+                       try {
+                               var ign = p.DefaultValue;
+                               Assert.Fail ("#2");
+                       } catch (NotImplementedException) {
+                       }
+                       p.SetDefaultValueImpl (null);
+
+                       var obj = typeof (object);
+                       p.SetMemberImpl (obj);
+                       Assert.AreEqual (obj, p.Member, "#3");
+                       Assert.AreEqual (0x8000000, p.MetadataToken, "#4");
+                       p.SetMemberImpl (null);
+
+                       var method = typeof (MyParameterInfo).GetMethod ("TestMethod");
+                       p.SetMemberImpl (method);
+                       Assert.IsNotNull (method, "#5");
+                       Assert.AreEqual (method, p.Member, "#6");
+                       Assert.AreEqual (0x8000000, p.MetadataToken, "#7");
+                       p.SetMemberImpl (null);
+
+                       var property = typeof (MyParameterInfo).GetProperty ("Item");
+                       p.SetMemberImpl (property);
+                       Assert.IsNotNull (property, "#8");
+                       Assert.AreEqual (property, p.Member, "#9");
+                       Assert.AreEqual (0x8000000, p.MetadataToken, "#10");
+                       p.SetMemberImpl (null);
+
+                       p.SetNameImpl ("foo");
+                       Assert.AreEqual ("foo", p.Name, "#11");
+                       p.SetNameImpl (null);
+
+                       p.SetPositionImpl (99);
+                       Assert.AreEqual (p.Position, 99, "#12");
+                       Assert.AreEqual (p.MetadataToken, 0x8000000, "#13");
+                       p.SetPositionImpl (0);
+
+                       Assert.IsFalse (p.IsIn, "#14");
+                       p.SetAttrsImpl (ParameterAttributes.In);
+                       Assert.IsTrue (p.IsIn, "#15");
+               }
+
+               [Test]
+               public void SubClassWithOverrides()
+               {
+                       var p2 = new MyParameterInfo2 ();
+                       Assert.IsFalse (p2.IsIn, "#1");
+                       p2.MyAttrsImpl = ParameterAttributes.In;
+                       Assert.IsTrue (p2.IsIn, "#2");
+#if NET_4_5
+                       Assert.AreEqual (p2.myList, p2.CustomAttributes, "#3");
+#endif
+               }
 #endif
        }
 }