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