Merge branch 'master' of github.com:mono/mono
[mono.git] / mcs / class / corlib / Test / System.Reflection / ParameterInfoTest.cs
1 //
2 // ParameterInfoTest - NUnit Test Cases for the ParameterInfo class
3 //
4 // Zoltan Varga (vargaz@freemail.hu)
5 //
6 // (C) Ximian, Inc.  http://www.ximian.com
7 //
8 //
9
10 using System;
11 using System.Threading;
12 using System.Reflection;
13 #if !TARGET_JVM
14 using System.Reflection.Emit;
15 #endif // TARGET_JVM
16 using System.Runtime.InteropServices;
17 using System.Runtime.CompilerServices;
18
19 using NUnit.Framework;
20
21 namespace MonoTests.System.Reflection
22 {
23         public class Marshal1 : ICustomMarshaler
24         {
25                 public static ICustomMarshaler GetInstance (string s)
26                 {
27                         return new Marshal1 ();
28                 }
29
30                 public void CleanUpManagedData (object managedObj)
31                 {
32                 }
33
34                 public void CleanUpNativeData (IntPtr pNativeData)
35                 {
36                 }
37
38                 public int GetNativeDataSize ()
39                 {
40                         return 4;
41                 }
42
43                 public IntPtr MarshalManagedToNative (object managedObj)
44                 {
45                         return IntPtr.Zero;
46                 }
47
48                 public object MarshalNativeToManaged (IntPtr pNativeData)
49                 {
50                         return null;
51                 }
52         }
53
54         [TestFixture]
55         public class ParameterInfoTest
56         {
57                 [Test]
58                 public void IsDefined_AttributeType_Null ()
59                 {
60                         MethodInfo mi = typeof (object).GetMethod ("Equals", 
61                                 new Type [1] { typeof (object) });
62                         ParameterInfo pi = mi.GetParameters () [0];
63
64                         try {
65                                 pi.IsDefined ((Type) null, false);
66                                 Assert.Fail ("#1");
67                         } catch (ArgumentNullException ex) {
68                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
69                                 Assert.IsNull (ex.InnerException, "#3");
70                                 Assert.IsNotNull (ex.Message, "#4");
71                                 Assert.IsNotNull (ex.ParamName, "#5");
72                                 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
73                         }
74                 }
75
76 #if NET_2_0
77                 public enum ParamEnum {
78                         None = 0,
79                         Foo = 1,
80                         Bar = 2
81                 };
82
83                 public static void paramMethod (int i, [In] int j, [Out] int k, [Optional] int l, [In,Out] int m, [DefaultParameterValue (ParamEnum.Foo)] ParamEnum n)
84                 {
85                 }
86
87 #if !TARGET_JVM // No support for extern methods in TARGET_JVM
88                 [DllImport ("foo")]
89                 public extern static void marshalAsMethod (
90                         [MarshalAs(UnmanagedType.Bool)]int p0, 
91                         [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] string [] p1,
92                         [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Marshal1), MarshalCookie = "5")] object p2);
93 #endif
94                 [Test]
95                 public void DefaultValueEnum () {
96                         ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
97
98                         Assert.AreEqual (typeof (ParamEnum), info [5].DefaultValue.GetType (), "#1");
99                         Assert.AreEqual (ParamEnum.Foo, info [5].DefaultValue, "#2");
100                 }
101
102                 public static void Sample2 ([DecimalConstantAttribute(2,2,2,2,2)] decimal a, [DateTimeConstantAttribute(123456)] DateTime b) {}
103
104                 [Test]
105                 public void DefaultValuesFromCustomAttr () {
106                         ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("Sample2").GetParameters ();
107
108                         Assert.AreEqual (typeof (Decimal), info [0].DefaultValue.GetType (), "#1");
109                         Assert.AreEqual (typeof (DateTime), info [1].DefaultValue.GetType (), "#2");
110                 }
111
112                 [Test] // bug #339013
113                 public void TestDefaultValues ()
114                 {
115                         ParameterInfo [] pi = typeof (ParameterInfoTest).GetMethod ("Sample").GetParameters ();
116
117                         Assert.AreEqual (pi [0].DefaultValue.GetType (), typeof (DBNull), "#1");
118                         Assert.AreEqual (pi [1].DefaultValue.GetType (), typeof (Missing), "#2");
119                 }
120
121                 public void Sample (int a, [Optional] int b)
122                 {
123                 }
124
125                 [Test]
126                 public void PseudoCustomAttributes () {
127                         ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
128                         Assert.AreEqual (0, info[0].GetCustomAttributes (true).Length, "#A1");
129                         Assert.AreEqual (1, info[1].GetCustomAttributes (typeof (InAttribute), true).Length, "#A2");
130                         Assert.AreEqual (1, info[2].GetCustomAttributes (typeof (OutAttribute), true).Length, "#A3");
131                         Assert.AreEqual (1, info[3].GetCustomAttributes (typeof (OptionalAttribute), true).Length, "#A4");
132                         Assert.AreEqual (2, info[4].GetCustomAttributes (true).Length, "#A5");
133
134 #if !TARGET_JVM // No support for extern methods in TARGET_JVM
135                         ParameterInfo[] pi = typeof (ParameterInfoTest).GetMethod ("marshalAsMethod").GetParameters ();
136                         MarshalAsAttribute attr;
137
138                         attr = (MarshalAsAttribute)(pi [0].GetCustomAttributes (true) [0]);
139                         Assert.AreEqual (UnmanagedType.Bool, attr.Value, "#B");
140
141                         attr = (MarshalAsAttribute)(pi [1].GetCustomAttributes (true) [0]);
142                         Assert.AreEqual (UnmanagedType.LPArray, attr.Value, "#C1");
143                         Assert.AreEqual (UnmanagedType.LPStr, attr.ArraySubType, "#C2");
144
145                         attr = (MarshalAsAttribute)(pi [2].GetCustomAttributes (true) [0]);
146                         Assert.AreEqual (UnmanagedType.CustomMarshaler, attr.Value, "#D1");
147                         Assert.AreEqual ("5", attr.MarshalCookie, "#D2");
148                         Assert.AreEqual (typeof (Marshal1), Type.GetType (attr.MarshalType), "#D3");
149 #endif
150                 }
151
152                 [Test] // bug #342536
153                 public void Generics_Name ()
154                 {
155                         MethodInfo mi;
156                         Type type;
157                         ParameterInfo [] info;
158                 
159                         type = typeof (BaseType<string>);
160
161                         mi = type.GetMethod ("GetItems");
162                         Assert.IsNotNull (mi, "#A1");
163                         info = mi.GetParameters ();
164                         Assert.AreEqual (1, info.Length, "#A2");
165                         Assert.AreEqual ("count", info [0].Name, "#A3");
166
167                         mi = type.GetMethod ("Add");
168                         Assert.IsNotNull (mi, "#B1");
169                         info = mi.GetParameters ();
170                         Assert.AreEqual (2, info.Length, "#B2");
171                         Assert.AreEqual ("item", info [0].Name, "#B3");
172                         Assert.AreEqual ("index", info [1].Name, "#B4");
173
174                         mi = type.GetMethod ("Create");
175                         Assert.IsNotNull (mi, "#C1");
176                         info = mi.GetParameters ();
177                         Assert.AreEqual (2, info.Length, "#C2");
178                         Assert.AreEqual ("x", info [0].Name, "#C3");
179                         Assert.AreEqual ("item", info [1].Name, "#C4");
180                 }
181
182                 public class BaseType <T>
183                 {
184                         public void GetItems (int count)
185                         {
186                         }
187
188                         public void Add (T item, int index)
189                         {
190                         }
191
192                         public V Create <V> (int x, T item)
193                         {
194                                 return default (V);
195                         }
196                 }
197 #endif
198
199                 [Test]
200                 public void Member () {
201                         ParameterInfo parm = typeof (Derived).GetMethod ("SomeMethod").GetParameters()[0];
202                         Assert.AreEqual (typeof (Derived), parm.Member.ReflectedType);
203                         Assert.AreEqual (typeof (Base), parm.Member.DeclaringType);
204                 }
205
206                 [Test]
207                 public void ArrayMethodParameters ()
208                 {
209                         var matrix_int_get = typeof (int[,,]).GetMethod ("Get");
210                         var parameters = matrix_int_get.GetParameters ();
211
212                         Assert.AreEqual (3, parameters.Length);
213                         Assert.AreEqual (0, parameters [0].GetCustomAttributes (false).Length);
214                         Assert.AreEqual (0, parameters [1].GetCustomAttributes (false).Length);
215                         Assert.AreEqual (0, parameters [2].GetCustomAttributes (false).Length);
216                 }
217
218                 class Base
219                 {
220                         public void SomeMethod( int x )
221                         {
222                         }
223                 }
224
225                 class Derived : Base
226                 {
227                 }
228
229 #if NET_4_0
230                 public static void TestC (decimal u = decimal.MaxValue) {
231                 }
232
233                 [Test]
234                 public void DefaultValueDecimal () {
235                         var info = typeof (ParameterInfoTest).GetMethod ("TestC").GetParameters ();
236                         Assert.AreEqual (decimal.MaxValue, info [0].DefaultValue);
237                 }
238 #endif
239         }
240 }