fixed tests
[mono.git] / mcs / class / corlib / Test / System.Reflection / MethodInfoTest.cs
1 //
2 // System.Reflection.MethodInfo Test Cases
3 //
4 // Authors:
5 //  Zoltan Varga (vargaz@gmail.com)
6 //
7 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using NUnit.Framework;
31 using System;
32 using System.Reflection;
33 using System.Runtime.InteropServices;
34 using System.Runtime.CompilerServices;
35
36 #if NET_2_0
37 using System.Collections.Generic;
38 #endif
39
40 namespace MonoTests.System.Reflection
41 {
42         [TestFixture]
43         public class MethodInfoTest
44         {
45 #if !TARGET_JVM
46                 [DllImport ("libfoo", EntryPoint="foo", CharSet=CharSet.Unicode, ExactSpelling=false, PreserveSig=true, SetLastError=true, BestFitMapping=true, ThrowOnUnmappableChar=true)]
47                 public static extern void dllImportMethod ();
48 #endif
49                 [MethodImplAttribute(MethodImplOptions.PreserveSig)]
50                 public void preserveSigMethod () {
51                 }
52
53                 [MethodImplAttribute(MethodImplOptions.Synchronized)]
54                 public void synchronizedMethod () {
55                 }
56
57 #if NET_2_0
58                 [Test]
59                 [Category ("NotWorking")] // Needs merge of attribute code into gmcs
60                 public void PseudoCustomAttributes ()
61                 {
62                         Type t = typeof (MethodInfoTest);
63
64                         DllImportAttribute attr = (DllImportAttribute)((t.GetMethod ("dllImportMethod").GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
65
66                         Assert.AreEqual (CallingConvention.Winapi, attr.CallingConvention, "#1");
67                         Assert.AreEqual ("foo", attr.EntryPoint, "#2");
68                         Assert.AreEqual ("libfoo", attr.Value, "#3");
69                         Assert.AreEqual (CharSet.Unicode, attr.CharSet, "#4");
70                         Assert.AreEqual (false, attr.ExactSpelling, "#5");
71                         Assert.AreEqual (true, attr.PreserveSig, "#6");
72                         Assert.AreEqual (true, attr.SetLastError, "#7");
73                         Assert.AreEqual (true, attr.BestFitMapping, "#8");
74                         Assert.AreEqual (true, attr.ThrowOnUnmappableChar, "#9");
75
76                         PreserveSigAttribute attr2 = (PreserveSigAttribute)((t.GetMethod ("preserveSigMethod").GetCustomAttributes (true)) [0]);
77
78                         // This doesn't work under MS.NET
79                         /*
80                           MethodImplAttribute attr3 = (MethodImplAttribute)((t.GetMethod ("synchronizedMethod").GetCustomAttributes (true)) [0]);
81                         */
82                 }
83
84                 [return: MarshalAs (UnmanagedType.Interface)]
85                 public void ReturnTypeMarshalAs () {
86                 }
87
88                 [Test]
89                 public void ReturnTypePseudoCustomAttributes () {
90                         MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ReturnTypeMarshalAs");
91
92                         Assert.IsTrue (mi.ReturnTypeCustomAttributes.GetCustomAttributes (typeof (MarshalAsAttribute), true).Length == 1);
93                 }
94 #endif
95
96                 public static int foo (int i, int j)
97                 {
98                         return i + j;
99                 }
100
101                 [Test]
102                 public void StaticInvokeWithObject ()
103                 {
104                         MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo");
105                         
106                         mi.Invoke (new Object (), new object [] { 1, 2 });
107                 }
108
109                 [Test]
110                 public void ByRefInvoke ()
111                 {
112                         MethodInfo met = typeof(MethodInfoTest).GetMethod ("ByRefTest");
113                         object[] parms = new object[] {1};
114                         met.Invoke (null, parms);
115                         Assert.AreEqual (2, parms[0]);
116                 }
117
118                 public static void ByRefTest (ref int a1)
119                 {
120                         if (a1 == 1)
121                                 a1 = 2;
122                 }
123
124                 static int byref_arg;
125
126                 public static void ByrefVtype (ref int i) {
127                         byref_arg = i;
128                         i = 5;
129                 }
130
131                 [Test]
132 #if ONLY_1_1
133                 [Category ("NotDotNet")] // #A2 fails on MS.NET 1.x
134 #endif
135                 public void ByrefVtypeInvoke ()
136                 {
137                         MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ByrefVtype");
138
139                         object o = 1;
140                         object[] args = new object [] { o };
141                         mi.Invoke (null, args);
142                         Assert.AreEqual (1, byref_arg, "#A1");
143                         Assert.AreEqual (1, o, "#A2");
144                         Assert.AreEqual (5, args[0], "#A3");
145
146                         args [0] = null;
147                         mi.Invoke (null, args);
148                         Assert.AreEqual (0, byref_arg, "#B1");
149                         Assert.AreEqual (5, args[0], "#B2");
150                 }
151
152                 public void HeyHey (out string out1, ref string ref1)
153                 {
154                         out1 = null;
155                 }
156
157                 [Test] // bug #76541
158                 public void ToStringByRef ()
159                 {
160                         Assert.AreEqual ("Void HeyHey(System.String ByRef, System.String ByRef)",
161                                 this.GetType ().GetMethod ("HeyHey").ToString ());
162                 }
163
164                 class GBD_A         { public virtual     void f () {} }
165                 class GBD_B : GBD_A { public override    void f () {} }
166                 class GBD_C : GBD_B { public override    void f () {} }
167                 class GBD_D : GBD_C { public new virtual void f () {} }
168                 class GBD_E : GBD_D { public override    void f () {} }
169
170                 [Test]
171                 public void GetBaseDefinition ()
172                 {
173                         Assert.AreEqual (typeof (GBD_A), typeof (GBD_C).GetMethod ("f").GetBaseDefinition ().DeclaringType);
174                         Assert.AreEqual (typeof (GBD_D), typeof (GBD_D).GetMethod ("f").GetBaseDefinition ().DeclaringType);
175                         Assert.AreEqual (typeof (GBD_D), typeof (GBD_E).GetMethod ("f").GetBaseDefinition ().DeclaringType);
176                 }
177
178 #if NET_2_0
179                 [Test]
180                 public void GetMethodBody_Abstract () {
181                         MethodBody mb = typeof (ICloneable).GetMethod ("Clone").GetMethodBody ();
182                         Assert.IsNull (mb);
183                 }
184
185                 [Test]
186                 public void GetMethodBody_Runtime () {
187                         MethodBody mb = typeof (AsyncCallback).GetMethod ("Invoke").GetMethodBody ();
188                         Assert.IsNull (mb);
189                 }
190
191                 [Test]
192                 public void GetMethodBody_Pinvoke () {
193                         MethodBody mb = typeof (MethodInfoTest).GetMethod ("dllImportMethod").GetMethodBody ();
194                         Assert.IsNull (mb);
195                 }
196
197                 [Test]
198                 public void GetMethodBody_Icall () {
199                         foreach (MethodInfo mi in typeof (object).GetMethods (BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance))
200                                 if ((mi.GetMethodImplementationFlags () & MethodImplAttributes.InternalCall) != 0) {
201                                         MethodBody mb = mi.GetMethodBody ();
202                                         Assert.IsNull (mb);
203                                 }
204                 }
205 #if !TARGET_JVM
206                 public static void locals_method () {
207                         byte[] b = new byte [10];
208
209                         unsafe {
210                                 /* This generates a pinned local */
211                                 fixed (byte *p = &b [0]) {
212                                 }
213                         }
214                 }
215 #endif
216                 [Test]
217                 public void GetMethodBody () {
218                         MethodBody mb = typeof (MethodInfoTest).GetMethod ("locals_method").GetMethodBody ();
219
220                         Assert.IsTrue (mb.InitLocals, "#1");
221                         Assert.IsTrue (mb.LocalSignatureMetadataToken > 0, "#2");
222
223                         IList<LocalVariableInfo> locals = mb.LocalVariables;
224
225                         // This might break with different compilers etc.
226                         Assert.AreEqual (2, locals.Count, "#3");
227
228                         Assert.IsTrue ((locals [0].LocalType == typeof (byte[])) || (locals [1].LocalType == typeof (byte[])), "#4");
229                         if (locals [0].LocalType == typeof (byte[]))
230                                 Assert.AreEqual (false, locals [0].IsPinned, "#5");
231                         else
232                                 Assert.AreEqual (false, locals [1].IsPinned, "#6");
233                 }
234
235                 public int return_parameter_test () {
236                         return 0;
237                 }
238
239                 [Test]
240                 public void ReturnParameter () {
241                         ParameterInfo pi = typeof (MethodInfoTest).GetMethod ("return_parameter_test").ReturnParameter;
242
243                         Assert.AreEqual (typeof (int), pi.ParameterType);
244                         Assert.AreEqual (-1, pi.Position);
245                         // This fails on MS
246                         //Assert.AreEqual (True, pi.IsRetval);
247                 }
248
249                 [Test]
250                 [ExpectedException (typeof (InvalidOperationException))]
251                 public void InvokeOnRefOnlyAssembly ()
252                 {
253                         Assembly a = Assembly.ReflectionOnlyLoad (typeof (MethodInfoTest).Assembly.FullName);
254                         Type t = a.GetType (typeof (RefOnlyMethodClass).FullName);
255                         MethodInfo m = t.GetMethod ("RefOnlyMethod", BindingFlags.Static | BindingFlags.NonPublic);
256                         
257                         m.Invoke (null, new object [0]);
258                 }
259
260                 [Test]
261                 public void InvokeGenericVtype ()
262                 {
263                         KeyValuePair<string, uint> kvp = new KeyValuePair<string, uint> ("a", 21);
264                         Type type = kvp.GetType ();
265                         Type [] arguments = type.GetGenericArguments ();
266                         MethodInfo method = typeof (MethodInfoTest).GetMethod ("Go");
267                         MethodInfo generic_method = method.MakeGenericMethod (arguments);
268                         kvp = (KeyValuePair<string, uint>)generic_method.Invoke (null, new object [] { kvp });
269
270                         Assert.AreEqual ("a", kvp.Key);
271                         Assert.AreEqual (21, kvp.Value);
272                 }
273
274         public static KeyValuePair<T1, T2> Go <T1, T2> (KeyValuePair <T1, T2> kvp)
275         {
276                         return kvp;
277         }
278
279                 public void MakeGenericMethodArgsMismatchFoo<T> () {}
280
281                 [Test]
282                 [ExpectedException (typeof (ArgumentException))]
283                 public void MakeGenericMethodArgsMismatch ()
284                 {
285                         MethodInfo gmi = this.GetType ().GetMethod (
286                                 "MakeGenericMethodArgsMismatchFoo")
287                                 .MakeGenericMethod ();
288                 }
289
290                 public static int? pass_nullable (int? i)
291                 {
292                         return i;
293                 }
294
295                 [Test]
296                 public void NullableTests ()
297                 {
298                         MethodInfo mi = typeof (MethodInfoTest).GetMethod ("pass_nullable");
299                         Assert.AreEqual (102, mi.Invoke (null, new object [] { 102 }), "#1");
300                         Assert.AreEqual (null, mi.Invoke (null, new object [] { null }), "#2");
301                 }
302
303                 public static void foo_generic<T> () {
304                 }
305
306                 [Test]
307                 public void IsGenericMethod ()
308                 {
309                         MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo_generic");
310                         Assert.AreEqual (true, mi.IsGenericMethod, "#1");
311                         MethodInfo mi2 = mi.MakeGenericMethod (new Type[] { typeof (int) });
312                         Assert.AreEqual (true, mi2.IsGenericMethod, "#2");
313
314                         MethodInfo mi3 = typeof (GenericHelper<int>).GetMethod ("Test");
315                         Assert.AreEqual (false, mi3.IsGenericMethod, "#3");
316                 }
317
318                 class A<T> {
319
320                         public static void Foo<T2> (T2 i) {
321                         }
322
323                         public static void Bar () {
324                         }
325
326                         public class B {
327                                 public static void Baz () {
328                                 }
329                         }
330                 }
331
332                 [Test]
333                 public void ContainsGenericParameters ()
334                 {
335                         // Non-generic method in open generic type
336                         Assert.IsTrue (typeof (A<int>).GetGenericTypeDefinition ().GetMethod ("Bar").ContainsGenericParameters);
337                         // open generic method in closed generic type
338                         Assert.IsTrue (typeof (A<int>).GetMethod ("Foo").ContainsGenericParameters);
339                         // non-generic method in closed generic type
340                         Assert.IsFalse (typeof (A<int>).GetMethod ("Bar").ContainsGenericParameters);
341                         // closed generic method in closed generic type
342                         Assert.IsFalse (typeof (A<int>).GetMethod ("Foo").MakeGenericMethod (new Type [] { typeof (int) }).ContainsGenericParameters);
343                         // non-generic method in non-generic nested type of closed generic type
344                         Assert.IsFalse (typeof (A<int>.B).GetMethod ("Baz").ContainsGenericParameters);
345                         // non-generic method in non-generic nested type of open generic type
346                         Assert.IsTrue (typeof (A<int>.B).GetGenericTypeDefinition ().GetMethod ("Baz").ContainsGenericParameters);
347                 }
348
349                 class GenericHelper<T>
350                 {
351                         public void Test (T t)
352                         { }
353                 }
354 #endif
355         }
356         
357 #if NET_2_0
358         // Helper class
359         class RefOnlyMethodClass 
360         {
361                 // Helper static method
362                 static void RefOnlyMethod ()
363                 {
364                 }
365         }
366 #endif
367 }
368