Merge pull request #1200 from akoeplinger/remove-jvm
[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                 [DllImport ("foo")]
87                 public extern static void marshalAsMethod (
88                         [MarshalAs(UnmanagedType.Bool)]int p0, 
89                         [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] string [] p1,
90                         [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Marshal1), MarshalCookie = "5")] object p2);
91                 [Test]
92                 public void DefaultValueEnum () {
93                         ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
94
95                         Assert.AreEqual (typeof (ParamEnum), info [5].DefaultValue.GetType (), "#1");
96                         Assert.AreEqual (ParamEnum.Foo, info [5].DefaultValue, "#2");
97                 }
98
99 #if NET_4_5
100                 [Test]
101                 public void HasDefaultValueEnum () {
102                         ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
103
104                         Assert.IsTrue (info [5].HasDefaultValue);
105                 }
106 #endif
107
108                 public static void Sample2 ([DecimalConstantAttribute(2,2,2,2,2)] decimal a, [DateTimeConstantAttribute(123456)] DateTime b) {}
109
110                 [Test]
111                 public void DefaultValuesFromCustomAttr () {
112                         ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("Sample2").GetParameters ();
113
114                         Assert.AreEqual (typeof (Decimal), info [0].DefaultValue.GetType (), "#1");
115                         Assert.AreEqual (typeof (DateTime), info [1].DefaultValue.GetType (), "#2");
116                 }
117
118                 [Test] // bug #339013
119                 public void TestDefaultValues ()
120                 {
121                         ParameterInfo [] pi = typeof (ParameterInfoTest).GetMethod ("Sample").GetParameters ();
122
123                         Assert.AreEqual (pi [0].DefaultValue.GetType (), typeof (DBNull), "#1");
124                         Assert.AreEqual (pi [1].DefaultValue.GetType (), typeof (Missing), "#2");
125                 }
126
127 #if NET_4_5
128                 [Test]
129                 public void TestHasDefaultValues ()
130                 {
131                         ParameterInfo [] pi = typeof (ParameterInfoTest).GetMethod ("Sample").GetParameters ();
132
133                         Assert.IsFalse (pi [0].HasDefaultValue, "#1");
134                         Assert.IsFalse (pi [1].HasDefaultValue, "#2");
135                         Assert.IsTrue (pi [2].HasDefaultValue, "#3");
136                 }
137 #endif
138
139                 public void Sample (int a, [Optional] int b, object c = null)
140                 {
141                 }
142
143                 [Test]
144                 public void PseudoCustomAttributes () {
145                         ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
146                         Assert.AreEqual (0, info[0].GetCustomAttributes (true).Length, "#A1");
147                         Assert.AreEqual (1, info[1].GetCustomAttributes (typeof (InAttribute), true).Length, "#A2");
148                         Assert.AreEqual (1, info[2].GetCustomAttributes (typeof (OutAttribute), true).Length, "#A3");
149                         Assert.AreEqual (1, info[3].GetCustomAttributes (typeof (OptionalAttribute), true).Length, "#A4");
150                         Assert.AreEqual (2, info[4].GetCustomAttributes (true).Length, "#A5");
151
152                         ParameterInfo[] pi = typeof (ParameterInfoTest).GetMethod ("marshalAsMethod").GetParameters ();
153                         MarshalAsAttribute attr;
154
155                         attr = (MarshalAsAttribute)(pi [0].GetCustomAttributes (true) [0]);
156                         Assert.AreEqual (UnmanagedType.Bool, attr.Value, "#B");
157
158                         attr = (MarshalAsAttribute)(pi [1].GetCustomAttributes (true) [0]);
159                         Assert.AreEqual (UnmanagedType.LPArray, attr.Value, "#C1");
160                         Assert.AreEqual (UnmanagedType.LPStr, attr.ArraySubType, "#C2");
161
162                         attr = (MarshalAsAttribute)(pi [2].GetCustomAttributes (true) [0]);
163                         Assert.AreEqual (UnmanagedType.CustomMarshaler, attr.Value, "#D1");
164                         Assert.AreEqual ("5", attr.MarshalCookie, "#D2");
165                         Assert.AreEqual (typeof (Marshal1), Type.GetType (attr.MarshalType), "#D3");
166                 }
167
168                 [Test] // bug #342536
169                 public void Generics_Name ()
170                 {
171                         MethodInfo mi;
172                         Type type;
173                         ParameterInfo [] info;
174                 
175                         type = typeof (BaseType<string>);
176
177                         mi = type.GetMethod ("GetItems");
178                         Assert.IsNotNull (mi, "#A1");
179                         info = mi.GetParameters ();
180                         Assert.AreEqual (1, info.Length, "#A2");
181                         Assert.AreEqual ("count", info [0].Name, "#A3");
182
183                         mi = type.GetMethod ("Add");
184                         Assert.IsNotNull (mi, "#B1");
185                         info = mi.GetParameters ();
186                         Assert.AreEqual (2, info.Length, "#B2");
187                         Assert.AreEqual ("item", info [0].Name, "#B3");
188                         Assert.AreEqual ("index", info [1].Name, "#B4");
189
190                         mi = type.GetMethod ("Create");
191                         Assert.IsNotNull (mi, "#C1");
192                         info = mi.GetParameters ();
193                         Assert.AreEqual (2, info.Length, "#C2");
194                         Assert.AreEqual ("x", info [0].Name, "#C3");
195                         Assert.AreEqual ("item", info [1].Name, "#C4");
196                 }
197
198                 public class BaseType <T>
199                 {
200                         public void GetItems (int count)
201                         {
202                         }
203
204                         public void Add (T item, int index)
205                         {
206                         }
207
208                         public V Create <V> (int x, T item)
209                         {
210                                 return default (V);
211                         }
212                 }
213 #endif
214
215                 [Test]
216                 public void Member () {
217                         ParameterInfo parm = typeof (Derived).GetMethod ("SomeMethod").GetParameters()[0];
218                         Assert.AreEqual (typeof (Derived), parm.Member.ReflectedType);
219                         Assert.AreEqual (typeof (Base), parm.Member.DeclaringType);
220                 }
221
222                 [Test]
223                 public void ArrayMethodParameters ()
224                 {
225                         var matrix_int_get = typeof (int[,,]).GetMethod ("Get");
226                         var parameters = matrix_int_get.GetParameters ();
227
228                         Assert.AreEqual (3, parameters.Length);
229                         Assert.AreEqual (0, parameters [0].GetCustomAttributes (false).Length);
230                         Assert.AreEqual (0, parameters [1].GetCustomAttributes (false).Length);
231                         Assert.AreEqual (0, parameters [2].GetCustomAttributes (false).Length);
232                 }
233
234                 class Base
235                 {
236                         public void SomeMethod( int x )
237                         {
238                         }
239                 }
240
241                 class Derived : Base
242                 {
243                 }
244
245 #if NET_4_0
246                 public static void TestC (decimal u = decimal.MaxValue) {
247                 }
248
249                 [Test]
250                 public void DefaultValueDecimal () {
251                         var info = typeof (ParameterInfoTest).GetMethod ("TestC").GetParameters ();
252                         Assert.AreEqual (decimal.MaxValue, info [0].DefaultValue);
253                 }
254
255 #if NET_4_5
256                 [Test]
257                 public void HasDefaultValueDecimal () {
258                         var info = typeof (ParameterInfoTest).GetMethod ("TestC").GetParameters ();
259                         Assert.IsTrue (info [0].HasDefaultValue);
260                 }
261 #endif
262
263                 class MyParameterInfo2 : ParameterInfo
264                 {
265                         public ParameterAttributes MyAttrsImpl;
266
267                         public override ParameterAttributes Attributes {
268                                 get {return MyAttrsImpl;}
269                         }
270
271                         public IList<CustomAttributeData> myList = new List<CustomAttributeData> ();
272
273                         public override IList<CustomAttributeData> GetCustomAttributesData () {
274                                 return myList;
275                         }
276                 }
277
278                 class MyParameterInfo : ParameterInfo
279                 {
280                         public void SetClassImpl (Type t)
281                         {
282                                 ClassImpl = t;
283                         }
284
285                         public void SetDefaultValueImpl (object o)
286                         {
287                                 DefaultValueImpl = o;
288                         }
289
290                         public void SetMemberImpl (MemberInfo o)
291                         {
292                                 MemberImpl = o;
293                         }
294
295                         public void SetNameImpl (string s)
296                         {
297                                 NameImpl = s;
298                         }
299
300                         public void SetPositionImpl (int i)
301                         {
302                                 PositionImpl = i;
303                         }
304
305                         public void SetAttrsImpl (ParameterAttributes a)
306                         {
307                                 AttrsImpl = a;
308                         }
309
310                         public void TestMethod (int a) {}
311                         public int this[int x, int y] {
312                                 get { return 0; }
313                                 set { }
314                         }
315
316                 }
317
318                 [Test]
319                 public void SubClassWithNoOverrides ()
320                 {
321                         var p = new MyParameterInfo ();
322                         Assert.IsFalse (p.IsDefined (typeof (FlagsAttribute), false), "#1");
323                         Assert.AreEqual (0, p.GetCustomAttributes (false).Length, "#2");
324                         Assert.AreEqual (0, p.GetCustomAttributes (typeof (FlagsAttribute), false).Length, "#3");
325                         Assert.AreEqual (0, p.GetOptionalCustomModifiers ().Length, "#4");
326                         Assert.AreEqual (0, p.GetRequiredCustomModifiers ().Length, "#5");
327 #if NET_4_5
328                         try {
329                                 var ign = p.HasDefaultValue;
330                                 Assert.Fail ("#6");
331                         } catch (NotImplementedException) {
332                         }
333 #endif
334                         Assert.IsFalse (p.IsIn, "#7");
335                         Assert.IsFalse (p.IsLcid, "#8");
336                         Assert.IsFalse (p.IsOptional, "#9");
337                         Assert.IsFalse (p.IsOut, "#10");
338                         Assert.IsFalse (p.IsRetval, "#10");
339 #if NET_4_5
340                         try {
341                                 var ign = p.CustomAttributes;
342                                 Assert.Fail ("#11");
343                         } catch (NotImplementedException) {
344                         }
345 #endif
346                         try {
347                                 p.GetCustomAttributesData ();
348                                 Assert.Fail ("#12");
349                         } catch (NotImplementedException) {
350                         }
351
352                         Assert.AreEqual (0x8000000, p.MetadataToken, "#13");
353                         Assert.AreEqual (0, p.Position, "#14");
354                         try {
355                                 var ign = p.DefaultValue;
356                                 Assert.Fail ("#15");
357                         } catch (NotImplementedException) {
358                         }
359                         try {
360                                 var ign = p.RawDefaultValue;
361                                 Assert.Fail ("#16");
362                         } catch (NotImplementedException) {
363                         }
364                         Assert.IsNull (p.Member, "#17");
365                         Assert.AreEqual (ParameterAttributes.None, p.Attributes, "#18");
366                         Assert.IsNull (p.Name, "#19");
367                         Assert.IsNull (p.ParameterType, "#20");
368                 }
369
370                 [Test]
371                 public void SubClassWithValuesSet ()
372                 {
373                         var p = new MyParameterInfo ();
374                         p.SetClassImpl (typeof (Decimal));
375                         Assert.AreEqual (typeof (Decimal), p.ParameterType, "#1");
376                         p.SetClassImpl (null);
377
378                         p.SetDefaultValueImpl ("foo");
379                         try {
380                                 var ign = p.DefaultValue;
381                                 Assert.Fail ("#2");
382                         } catch (NotImplementedException) {
383                         }
384                         p.SetDefaultValueImpl (null);
385
386                         var obj = typeof (object);
387                         p.SetMemberImpl (obj);
388                         Assert.AreEqual (obj, p.Member, "#3");
389                         Assert.AreEqual (0x8000000, p.MetadataToken, "#4");
390                         p.SetMemberImpl (null);
391
392                         var method = typeof (MyParameterInfo).GetMethod ("TestMethod");
393                         p.SetMemberImpl (method);
394                         Assert.IsNotNull (method, "#5");
395                         Assert.AreEqual (method, p.Member, "#6");
396                         Assert.AreEqual (0x8000000, p.MetadataToken, "#7");
397                         p.SetMemberImpl (null);
398
399                         var property = typeof (MyParameterInfo).GetProperty ("Item");
400                         p.SetMemberImpl (property);
401                         Assert.IsNotNull (property, "#8");
402                         Assert.AreEqual (property, p.Member, "#9");
403                         Assert.AreEqual (0x8000000, p.MetadataToken, "#10");
404                         p.SetMemberImpl (null);
405
406                         p.SetNameImpl ("foo");
407                         Assert.AreEqual ("foo", p.Name, "#11");
408                         p.SetNameImpl (null);
409
410                         p.SetPositionImpl (99);
411                         Assert.AreEqual (p.Position, 99, "#12");
412                         Assert.AreEqual (p.MetadataToken, 0x8000000, "#13");
413                         p.SetPositionImpl (0);
414
415                         Assert.IsFalse (p.IsIn, "#14");
416                         p.SetAttrsImpl (ParameterAttributes.In);
417                         Assert.IsTrue (p.IsIn, "#15");
418                 }
419
420                 [Test]
421                 public void SubClassWithOverrides()
422                 {
423                         var p2 = new MyParameterInfo2 ();
424                         Assert.IsFalse (p2.IsIn, "#1");
425                         p2.MyAttrsImpl = ParameterAttributes.In;
426                         Assert.IsTrue (p2.IsIn, "#2");
427 #if NET_4_5
428                         Assert.AreEqual (p2.myList, p2.CustomAttributes, "#3");
429 #endif
430                 }
431 #endif
432         }
433 }