update .sln too.
[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 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
8 //
9 //
10
11 using System;
12 using System.Threading;
13 using System.Reflection;
14 using System.Runtime.InteropServices;
15 using System.Runtime.CompilerServices;
16 using System.Collections.Generic;
17
18 using NUnit.Framework;
19
20 namespace MonoTests.System.Reflection
21 {
22         public class Marshal1 : ICustomMarshaler
23         {
24                 public static ICustomMarshaler GetInstance (string s)
25                 {
26                         return new Marshal1 ();
27                 }
28
29                 public void CleanUpManagedData (object managedObj)
30                 {
31                 }
32
33                 public void CleanUpNativeData (IntPtr pNativeData)
34                 {
35                 }
36
37                 public int GetNativeDataSize ()
38                 {
39                         return 4;
40                 }
41
42                 public IntPtr MarshalManagedToNative (object managedObj)
43                 {
44                         return IntPtr.Zero;
45                 }
46
47                 public object MarshalNativeToManaged (IntPtr pNativeData)
48                 {
49                         return null;
50                 }
51         }
52
53         [TestFixture]
54         public class ParameterInfoTest
55         {
56                 [Test]
57                 public void IsDefined_AttributeType_Null ()
58                 {
59                         MethodInfo mi = typeof (object).GetMethod ("Equals", 
60                                 new Type [1] { typeof (object) });
61                         ParameterInfo pi = mi.GetParameters () [0];
62
63                         try {
64                                 pi.IsDefined ((Type) null, false);
65                                 Assert.Fail ("#1");
66                         } catch (ArgumentNullException ex) {
67                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
68                                 Assert.IsNull (ex.InnerException, "#3");
69                                 Assert.IsNotNull (ex.Message, "#4");
70                                 Assert.IsNotNull (ex.ParamName, "#5");
71                                 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
72                         }
73                 }
74
75 #if NET_2_0 && !NET_2_1
76                 public enum ParamEnum {
77                         None = 0,
78                         Foo = 1,
79                         Bar = 2
80                 };
81
82                 public static void paramMethod (int i, [In] int j, [Out] int k, [Optional] int l, [In,Out] int m, ParamEnum n = ParamEnum.Foo)
83                 {
84                 }
85
86 #if !TARGET_JVM // No support for extern methods in TARGET_JVM
87                 [DllImport ("foo")]
88                 public extern static void marshalAsMethod (
89                         [MarshalAs(UnmanagedType.Bool)]int p0, 
90                         [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] string [] p1,
91                         [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Marshal1), MarshalCookie = "5")] object p2);
92 #endif
93                 [Test]
94                 public void DefaultValueEnum () {
95                         ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
96
97                         Assert.AreEqual (typeof (ParamEnum), info [5].DefaultValue.GetType (), "#1");
98                         Assert.AreEqual (ParamEnum.Foo, info [5].DefaultValue, "#2");
99                 }
100
101                 public static void Sample2 ([DecimalConstantAttribute(2,2,2,2,2)] decimal a, [DateTimeConstantAttribute(123456)] DateTime b) {}
102
103                 [Test]
104                 public void DefaultValuesFromCustomAttr () {
105                         ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("Sample2").GetParameters ();
106
107                         Assert.AreEqual (typeof (Decimal), info [0].DefaultValue.GetType (), "#1");
108                         Assert.AreEqual (typeof (DateTime), info [1].DefaultValue.GetType (), "#2");
109                 }
110
111                 [Test] // bug #339013
112                 public void TestDefaultValues ()
113                 {
114                         ParameterInfo [] pi = typeof (ParameterInfoTest).GetMethod ("Sample").GetParameters ();
115
116                         Assert.AreEqual (pi [0].DefaultValue.GetType (), typeof (DBNull), "#1");
117                         Assert.AreEqual (pi [1].DefaultValue.GetType (), typeof (Missing), "#2");
118                 }
119
120                 public void Sample (int a, [Optional] int b)
121                 {
122                 }
123
124                 [Test]
125                 public void PseudoCustomAttributes () {
126                         ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
127                         Assert.AreEqual (0, info[0].GetCustomAttributes (true).Length, "#A1");
128                         Assert.AreEqual (1, info[1].GetCustomAttributes (typeof (InAttribute), true).Length, "#A2");
129                         Assert.AreEqual (1, info[2].GetCustomAttributes (typeof (OutAttribute), true).Length, "#A3");
130                         Assert.AreEqual (1, info[3].GetCustomAttributes (typeof (OptionalAttribute), true).Length, "#A4");
131                         Assert.AreEqual (2, info[4].GetCustomAttributes (true).Length, "#A5");
132
133 #if !TARGET_JVM // No support for extern methods in TARGET_JVM
134                         ParameterInfo[] pi = typeof (ParameterInfoTest).GetMethod ("marshalAsMethod").GetParameters ();
135                         MarshalAsAttribute attr;
136
137                         attr = (MarshalAsAttribute)(pi [0].GetCustomAttributes (true) [0]);
138                         Assert.AreEqual (UnmanagedType.Bool, attr.Value, "#B");
139
140                         attr = (MarshalAsAttribute)(pi [1].GetCustomAttributes (true) [0]);
141                         Assert.AreEqual (UnmanagedType.LPArray, attr.Value, "#C1");
142                         Assert.AreEqual (UnmanagedType.LPStr, attr.ArraySubType, "#C2");
143
144                         attr = (MarshalAsAttribute)(pi [2].GetCustomAttributes (true) [0]);
145                         Assert.AreEqual (UnmanagedType.CustomMarshaler, attr.Value, "#D1");
146                         Assert.AreEqual ("5", attr.MarshalCookie, "#D2");
147                         Assert.AreEqual (typeof (Marshal1), Type.GetType (attr.MarshalType), "#D3");
148 #endif
149                 }
150
151                 [Test] // bug #342536
152                 public void Generics_Name ()
153                 {
154                         MethodInfo mi;
155                         Type type;
156                         ParameterInfo [] info;
157                 
158                         type = typeof (BaseType<string>);
159
160                         mi = type.GetMethod ("GetItems");
161                         Assert.IsNotNull (mi, "#A1");
162                         info = mi.GetParameters ();
163                         Assert.AreEqual (1, info.Length, "#A2");
164                         Assert.AreEqual ("count", info [0].Name, "#A3");
165
166                         mi = type.GetMethod ("Add");
167                         Assert.IsNotNull (mi, "#B1");
168                         info = mi.GetParameters ();
169                         Assert.AreEqual (2, info.Length, "#B2");
170                         Assert.AreEqual ("item", info [0].Name, "#B3");
171                         Assert.AreEqual ("index", info [1].Name, "#B4");
172
173                         mi = type.GetMethod ("Create");
174                         Assert.IsNotNull (mi, "#C1");
175                         info = mi.GetParameters ();
176                         Assert.AreEqual (2, info.Length, "#C2");
177                         Assert.AreEqual ("x", info [0].Name, "#C3");
178                         Assert.AreEqual ("item", info [1].Name, "#C4");
179                 }
180
181                 public class BaseType <T>
182                 {
183                         public void GetItems (int count)
184                         {
185                         }
186
187                         public void Add (T item, int index)
188                         {
189                         }
190
191                         public V Create <V> (int x, T item)
192                         {
193                                 return default (V);
194                         }
195                 }
196 #endif
197
198                 [Test]
199                 public void Member () {
200                         ParameterInfo parm = typeof (Derived).GetMethod ("SomeMethod").GetParameters()[0];
201                         Assert.AreEqual (typeof (Derived), parm.Member.ReflectedType);
202                         Assert.AreEqual (typeof (Base), parm.Member.DeclaringType);
203                 }
204
205                 [Test]
206                 public void ArrayMethodParameters ()
207                 {
208                         var matrix_int_get = typeof (int[,,]).GetMethod ("Get");
209                         var parameters = matrix_int_get.GetParameters ();
210
211                         Assert.AreEqual (3, parameters.Length);
212                         Assert.AreEqual (0, parameters [0].GetCustomAttributes (false).Length);
213                         Assert.AreEqual (0, parameters [1].GetCustomAttributes (false).Length);
214                         Assert.AreEqual (0, parameters [2].GetCustomAttributes (false).Length);
215                 }
216
217                 class Base
218                 {
219                         public void SomeMethod( int x )
220                         {
221                         }
222                 }
223
224                 class Derived : Base
225                 {
226                 }
227
228 #if NET_4_0
229                 public static void TestC (decimal u = decimal.MaxValue) {
230                 }
231
232                 [Test]
233                 public void DefaultValueDecimal () {
234                         var info = typeof (ParameterInfoTest).GetMethod ("TestC").GetParameters ();
235                         Assert.AreEqual (decimal.MaxValue, info [0].DefaultValue);
236                 }
237
238                 class MyParameterInfo2 : ParameterInfo
239                 {
240                         public ParameterAttributes MyAttrsImpl;
241
242                         public override ParameterAttributes Attributes {
243                                 get {return MyAttrsImpl;}
244                         }
245
246                         public IList<CustomAttributeData> myList = new List<CustomAttributeData> ();
247
248                         public override IList<CustomAttributeData> GetCustomAttributesData () {
249                                 return myList;
250                         }
251                 }
252
253                 class MyParameterInfo : ParameterInfo
254                 {
255                         public void SetClassImpl (Type t)
256                         {
257                                 ClassImpl = t;
258                         }
259
260                         public void SetDefaultValueImpl (object o)
261                         {
262                                 DefaultValueImpl = o;
263                         }
264
265                         public void SetMemberImpl (MemberInfo o)
266                         {
267                                 MemberImpl = o;
268                         }
269
270                         public void SetNameImpl (string s)
271                         {
272                                 NameImpl = s;
273                         }
274
275                         public void SetPositionImpl (int i)
276                         {
277                                 PositionImpl = i;
278                         }
279
280                         public void SetAttrsImpl (ParameterAttributes a)
281                         {
282                                 AttrsImpl = a;
283                         }
284
285                         public void TestMethod (int a) {}
286                         public int this[int x, int y] {
287                                 get { return 0; }
288                                 set { }
289                         }
290
291                 }
292
293                 [Test]
294                 public void SubClassWithNoOverrides ()
295                 {
296                         var p = new MyParameterInfo ();
297                         Assert.IsFalse (p.IsDefined (typeof (FlagsAttribute), false), "#1");
298                         Assert.AreEqual (0, p.GetCustomAttributes (false).Length, "#2");
299                         Assert.AreEqual (0, p.GetCustomAttributes (typeof (FlagsAttribute), false).Length, "#3");
300                         Assert.AreEqual (0, p.GetOptionalCustomModifiers ().Length, "#4");
301                         Assert.AreEqual (0, p.GetRequiredCustomModifiers ().Length, "#5");
302 #if NET_4_5
303                         try {
304                                 var ign = p.HasDefaultValue;
305                                 Assert.Fail ("#6");
306                         } catch (NotImplementedException) {
307                         }
308 #endif
309                         Assert.IsFalse (p.IsIn, "#7");
310                         Assert.IsFalse (p.IsLcid, "#8");
311                         Assert.IsFalse (p.IsOptional, "#9");
312                         Assert.IsFalse (p.IsOut, "#10");
313                         Assert.IsFalse (p.IsRetval, "#10");
314 #if NET_4_5
315                         try {
316                                 var ign = p.CustomAttributes;
317                                 Assert.Fail ("#11");
318                         } catch (NotImplementedException) {
319                         }
320 #endif
321                         try {
322                                 p.GetCustomAttributesData ();
323                                 Assert.Fail ("#12");
324                         } catch (NotImplementedException) {
325                         }
326
327                         Assert.AreEqual (0x8000000, p.MetadataToken, "#13");
328                         Assert.AreEqual (0, p.Position, "#14");
329                         try {
330                                 var ign = p.DefaultValue;
331                                 Assert.Fail ("#15");
332                         } catch (NotImplementedException) {
333                         }
334                         try {
335                                 var ign = p.RawDefaultValue;
336                                 Assert.Fail ("#16");
337                         } catch (NotImplementedException) {
338                         }
339                         Assert.IsNull (p.Member, "#17");
340                         Assert.AreEqual (ParameterAttributes.None, p.Attributes, "#18");
341                         Assert.IsNull (p.Name, "#19");
342                         Assert.IsNull (p.ParameterType, "#20");
343                 }
344
345                 [Test]
346                 public void SubClassWithValuesSet ()
347                 {
348                         var p = new MyParameterInfo ();
349                         p.SetClassImpl (typeof (Decimal));
350                         Assert.AreEqual (typeof (Decimal), p.ParameterType, "#1");
351                         p.SetClassImpl (null);
352
353                         p.SetDefaultValueImpl ("foo");
354                         try {
355                                 var ign = p.DefaultValue;
356                                 Assert.Fail ("#2");
357                         } catch (NotImplementedException) {
358                         }
359                         p.SetDefaultValueImpl (null);
360
361                         var obj = typeof (object);
362                         p.SetMemberImpl (obj);
363                         Assert.AreEqual (obj, p.Member, "#3");
364                         Assert.AreEqual (0x8000000, p.MetadataToken, "#4");
365                         p.SetMemberImpl (null);
366
367                         var method = typeof (MyParameterInfo).GetMethod ("TestMethod");
368                         p.SetMemberImpl (method);
369                         Assert.IsNotNull (method, "#5");
370                         Assert.AreEqual (method, p.Member, "#6");
371                         Assert.AreEqual (0x8000000, p.MetadataToken, "#7");
372                         p.SetMemberImpl (null);
373
374                         var property = typeof (MyParameterInfo).GetProperty ("Item");
375                         p.SetMemberImpl (property);
376                         Assert.IsNotNull (property, "#8");
377                         Assert.AreEqual (property, p.Member, "#9");
378                         Assert.AreEqual (0x8000000, p.MetadataToken, "#10");
379                         p.SetMemberImpl (null);
380
381                         p.SetNameImpl ("foo");
382                         Assert.AreEqual ("foo", p.Name, "#11");
383                         p.SetNameImpl (null);
384
385                         p.SetPositionImpl (99);
386                         Assert.AreEqual (p.Position, 99, "#12");
387                         Assert.AreEqual (p.MetadataToken, 0x8000000, "#13");
388                         p.SetPositionImpl (0);
389
390                         Assert.IsFalse (p.IsIn, "#14");
391                         p.SetAttrsImpl (ParameterAttributes.In);
392                         Assert.IsTrue (p.IsIn, "#15");
393                 }
394
395                 [Test]
396                 public void SubClassWithOverrides()
397                 {
398                         var p2 = new MyParameterInfo2 ();
399                         Assert.IsFalse (p2.IsIn, "#1");
400                         p2.MyAttrsImpl = ParameterAttributes.In;
401                         Assert.IsTrue (p2.IsIn, "#2");
402 #if NET_4_5
403                         Assert.AreEqual (p2.myList, p2.CustomAttributes, "#3");
404 #endif
405                 }
406 #endif
407         }
408 }