* corlib_test.dll.sources: Added ConstructorOnTypeBuilderInstTest.cs.
[mono.git] / mcs / class / corlib / Test / System.Reflection.Emit / MethodOnTypeBuilderInstTest.cs
1 //
2 // MethodOnTypeBuilderInstTest - NUnit Test Cases for MethodOnTypeBuilderInst
3 //
4 // Author:
5 //   Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // Copyright (C) 2008 Gert Driesen
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_2_0
30
31 using System;
32 using System.Globalization;
33 using System.IO;
34 using System.Reflection;
35 using System.Reflection.Emit;
36
37 using NUnit.Framework;
38
39 namespace MonoTests.System.Reflection.Emit
40 {
41         [TestFixture]
42         public class MethodOnTypeBuilderInstTest
43         {
44                 private static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.MethodOnTypeBuilderInstTest";
45                 
46                 private AssemblyBuilder assembly;
47                 private ModuleBuilder module;
48                 private MethodBuilder mb_create;
49                 private MethodBuilder mb_edit;
50                 private Type typeBarOfT;
51                 private Type typeBarOfInt32;
52                 private MethodInfo method_create;
53                 private MethodInfo method_edit;
54
55                 [SetUp]
56                 public void SetUp ()
57                 {
58                         AssemblyName assemblyName = new AssemblyName ();
59                         assemblyName.Name = ASSEMBLY_NAME;
60
61                         assembly = AppDomain.CurrentDomain.DefineDynamicAssembly (
62                                 assemblyName, AssemblyBuilderAccess.RunAndSave,
63                                 Path.GetTempPath ());
64
65                         module = assembly.DefineDynamicModule ("module1");
66
67                         TypeBuilder tb = module.DefineType ("Bar");
68                         GenericTypeParameterBuilder [] typeParams = tb.DefineGenericParameters ("T");
69
70                         ConstructorBuilder cb = tb.DefineConstructor (MethodAttributes.Public, CallingConventions.Standard, Type.EmptyTypes);
71                         ILGenerator ig = cb.GetILGenerator ();
72                         ig.Emit (OpCodes.Ret);
73
74                         typeBarOfT = tb.MakeGenericType (typeParams [0]);
75
76                         mb_create = tb.DefineMethod ("create",
77                                 MethodAttributes.Public | MethodAttributes.Static,
78                                 typeBarOfT, Type.EmptyTypes);
79                         ig = mb_create.GetILGenerator ();
80                         ig.Emit (OpCodes.Newobj, TypeBuilder.GetConstructor (
81                                 typeBarOfT, cb));
82                         ig.Emit (OpCodes.Ret);
83
84                         mb_edit = tb.DefineMethod ("edit",
85                                 MethodAttributes.Public | MethodAttributes.Static,
86                                 typeBarOfT, Type.EmptyTypes);
87                         ig = mb_edit.GetILGenerator ();
88                         ig.Emit (OpCodes.Newobj, TypeBuilder.GetConstructor (
89                                 typeBarOfT, cb));
90                         ig.Emit (OpCodes.Ret);
91                         mb_edit.SetParameters (mb_edit.DefineGenericParameters ("X"));
92
93                         typeBarOfInt32 = tb.MakeGenericType (typeof (int));
94
95                         method_create = TypeBuilder.GetMethod (typeBarOfInt32, mb_create);
96                         method_edit = TypeBuilder.GetMethod (typeBarOfInt32, mb_edit);
97                 }
98
99                 [Test]
100                 public void Attributes ()
101                 {
102                         MethodAttributes attrs;
103
104                         attrs = method_create.Attributes;
105                         Assert.AreEqual (MethodAttributes.PrivateScope |
106                                 MethodAttributes.Public | MethodAttributes.Static,
107                                 attrs, "#1");
108                         attrs = method_edit.Attributes;
109                         Assert.AreEqual (MethodAttributes.PrivateScope |
110                                 MethodAttributes.Public | MethodAttributes.Static,
111                                 attrs, "#2");
112                 }
113
114                 [Test]
115                 public void CallingConvention ()
116                 {
117                         CallingConventions conv;
118
119                         conv = method_create.CallingConvention;
120                         Assert.AreEqual (CallingConventions.Standard, conv, "#1");
121                         conv = method_edit.CallingConvention;
122                         Assert.AreEqual (CallingConventions.Standard, conv, "#2");
123                 }
124
125                 [Test]
126                 [Category ("NotWorking")]
127                 public void ContainsGenericParameters ()
128                 {
129                         try {
130                                 bool genparam = method_create.ContainsGenericParameters;
131                                 Assert.Fail ("#A1:" + genparam);
132                         } catch (NotSupportedException ex) {
133                                 // Specified method is not supported
134                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
135                                 Assert.IsNull (ex.InnerException, "#A3");
136                                 Assert.IsNotNull (ex.Message, "#A4");
137                         }
138
139                         try {
140                                 bool genparam = method_edit.ContainsGenericParameters;
141                                 Assert.Fail ("#B1:" + genparam);
142                         } catch (NotSupportedException ex) {
143                                 // Specified method is not supported
144                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
145                                 Assert.IsNull (ex.InnerException, "#B3");
146                                 Assert.IsNotNull (ex.Message, "#B4");
147                         }
148                 }
149
150                 [Test]
151                 public void DeclaringType ()
152                 {
153                         Assert.AreSame (typeBarOfInt32, method_create.DeclaringType, "#1");
154                         Assert.AreSame (typeBarOfInt32, method_edit.DeclaringType, "#2");
155                 }
156
157                 [Test]
158                 [Category ("NotWorking")]
159                 public void GetBaseDefinition ()
160                 {
161                         try {
162                                 method_create.GetBaseDefinition ();
163                                 Assert.Fail ("#A1");
164                         } catch (NotSupportedException ex) {
165                                 // Specified method is not supported
166                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
167                                 Assert.IsNull (ex.InnerException, "#A3");
168                                 Assert.IsNotNull (ex.Message, "#A4");
169                         }
170
171                         try {
172                                 method_edit.GetBaseDefinition ();
173                                 Assert.Fail ("#B1");
174                         } catch (NotSupportedException ex) {
175                                 // Specified method is not supported
176                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
177                                 Assert.IsNull (ex.InnerException, "#B3");
178                                 Assert.IsNotNull (ex.Message, "#B4");
179                         }
180                 }
181
182                 [Test] // GetCustomAttributes (Boolean)
183                 [Category ("NotWorking")]
184                 public void GetCustomAttributes1 ()
185                 {
186                         try {
187                                 method_create.GetCustomAttributes (false);
188                                 Assert.Fail ("#A1");
189                         } catch (NotSupportedException ex) {
190                                 // The invoked member is not supported in a dynamic module
191                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
192                                 Assert.IsNull (ex.InnerException, "#A3");
193                                 Assert.IsNotNull (ex.Message, "#A4");
194                         }
195
196                         try {
197                                 method_edit.GetCustomAttributes (false);
198                                 Assert.Fail ("#B1");
199                         } catch (NotSupportedException ex) {
200                                 // The invoked member is not supported in a dynamic module
201                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
202                                 Assert.IsNull (ex.InnerException, "#B3");
203                                 Assert.IsNotNull (ex.Message, "#B4");
204                         }
205                 }
206
207                 [Test] // GetCustomAttributes (Type, Boolean)
208                 [Category ("NotWorking")]
209                 public void GetCustomAttributes2 ()
210                 {
211                         try {
212                                 method_create.GetCustomAttributes (typeof (FlagsAttribute), false);
213                                 Assert.Fail ("#A1");
214                         } catch (NotSupportedException ex) {
215                                 // The invoked member is not supported in a dynamic module
216                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
217                                 Assert.IsNull (ex.InnerException, "#A3");
218                                 Assert.IsNotNull (ex.Message, "#A4");
219                         }
220
221                         try {
222                                 method_edit.GetCustomAttributes (typeof (FlagsAttribute), false);
223                                 Assert.Fail ("#B1");
224                         } catch (NotSupportedException ex) {
225                                 // The invoked member is not supported in a dynamic module
226                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
227                                 Assert.IsNull (ex.InnerException, "#B3");
228                                 Assert.IsNotNull (ex.Message, "#B4");
229                         }
230                 }
231
232                 [Test]
233                 [Category ("NotWorking")]
234                 public void GetGenericArguments ()
235                 {
236                         Type [] args;
237                         
238                         args = method_create.GetGenericArguments ();
239                         Assert.IsNull (args, "#A");
240
241                         args = method_edit.GetGenericArguments ();
242                         Assert.IsNotNull (args, "#B1");
243                         Assert.AreEqual (1, args.Length, "#B2");
244                         Assert.AreEqual ("X", args [0].Name, "#B3");
245                 }
246
247                 [Test]
248                 [Category ("NotWorking")]
249                 public void GetGenericMethodDefinition ()
250                 {
251                         MethodInfo method_def;
252
253                         method_def = method_create.GetGenericMethodDefinition ();
254                         Assert.IsNotNull (method_def, "#A1");
255                         Assert.AreSame (mb_create, method_def, "#A2");
256
257                         method_def = method_edit.GetGenericMethodDefinition ();
258                         Assert.IsNotNull (method_def, "#B1");
259                         Assert.AreSame (mb_edit, method_def, "#B2");
260                 }
261
262                 [Test]
263                 public void GetMethodImplementationFlags ()
264                 {
265                         MethodImplAttributes flags;
266                         
267                         flags = method_create.GetMethodImplementationFlags ();
268                         Assert.AreEqual (MethodImplAttributes.Managed, flags, "#1");
269                         flags = method_edit.GetMethodImplementationFlags ();
270                         Assert.AreEqual (MethodImplAttributes.Managed, flags, "#2");
271                 }
272
273                 [Test]
274                 [Category ("NotWorking")]
275                 public void GetParameters ()
276                 {
277                         try {
278                                 method_create.GetParameters ();
279                                 Assert.Fail ("#A1");
280                         } catch (NotSupportedException ex) {
281                                 // Type has not been created
282                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
283                                 Assert.IsNull (ex.InnerException, "#A3");
284                                 Assert.IsNotNull (ex.Message, "#A4");
285                         }
286
287                         try {
288                                 method_edit.GetParameters ();
289                                 Assert.Fail ("#B1");
290                         } catch (NotSupportedException ex) {
291                                 // Type has not been created
292                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
293                                 Assert.IsNull (ex.InnerException, "#B3");
294                                 Assert.IsNotNull (ex.Message, "#B4");
295                         }
296                 }
297
298                 [Test]
299                 [Category ("NotWorking")]
300                 public void Invoke ()
301                 {
302                         try {
303                                 method_create.Invoke (null, BindingFlags.Default, null,
304                                         new object [0], CultureInfo.InvariantCulture);
305                                 Assert.Fail ("#A1");
306                         } catch (NotSupportedException ex) {
307                                 // Specified method is not supported
308                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
309                                 Assert.IsNull (ex.InnerException, "#A3");
310                                 Assert.IsNotNull (ex.Message, "#A4");
311                         }
312
313                         try {
314                                 method_edit.Invoke (null, BindingFlags.Default, null,
315                                         new object [0], CultureInfo.InvariantCulture);
316                                 Assert.Fail ("#B1");
317                         } catch (NotSupportedException ex) {
318                                 // Specified method is not supported
319                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
320                                 Assert.IsNull (ex.InnerException, "#B3");
321                                 Assert.IsNotNull (ex.Message, "#B4");
322                         }
323                 }
324
325                 [Test]
326                 [Category ("NotWorking")]
327                 public void IsDefined ()
328                 {
329                         try {
330                                 method_create.IsDefined (typeof (FlagsAttribute), false);
331                                 Assert.Fail ("#A1");
332                         } catch (NotSupportedException ex) {
333                                 // The invoked member is not supported in a dynamic module
334                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
335                                 Assert.IsNull (ex.InnerException, "#A3");
336                                 Assert.IsNotNull (ex.Message, "#A4");
337                         }
338
339                         try {
340                                 method_edit.IsDefined (typeof (FlagsAttribute), false);
341                                 Assert.Fail ("#B1");
342                         } catch (NotSupportedException ex) {
343                                 // The invoked member is not supported in a dynamic module
344                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
345                                 Assert.IsNull (ex.InnerException, "#B3");
346                                 Assert.IsNotNull (ex.Message, "#B4");
347                         }
348                 }
349
350                 [Test]
351                 public void IsGenericMethodDefinition ()
352                 {
353                         Assert.IsFalse (method_create.IsGenericMethodDefinition, "#1");
354                         Assert.IsTrue (method_edit.IsGenericMethodDefinition, "#2");
355                 }
356
357                 [Test]
358                 public void IsGenericMethod ()
359                 {
360                         Assert.IsFalse (method_create.IsGenericMethod, "#1");
361                         Assert.IsTrue (method_edit.IsGenericMethod, "#2");
362                 }
363
364                 [Test]
365                 [Category ("NotWorking")]
366                 public void MakeGenericMethod ()
367                 {
368                         try {
369                                 method_create.MakeGenericMethod (typeof (int));
370                                 Assert.Fail ("#A1");
371                         } catch (InvalidOperationException ex) {
372                                 // create is not a GenericMethodDefinition.
373                                 // MakeGenericMethod may only be called on a
374                                 // method for which MethodBase.IsGenericMethodDefinition
375                                 // is true
376                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
377                                 Assert.IsNull (ex.InnerException, "#A3");
378                                 Assert.IsNotNull (ex.Message, "#A4");
379                         }
380
381                         MethodInfo genEdit = method_edit.MakeGenericMethod (typeof (int));
382                         Assert.IsFalse (genEdit.ContainsGenericParameters, "#B1");
383                         Assert.IsTrue (genEdit.IsGenericMethod, "#B2");
384                         Assert.IsFalse (genEdit.IsGenericMethodDefinition, "#B3");
385                 }
386
387                 [Test]
388                 public void MemberType ()
389                 {
390                         Assert.AreEqual (MemberTypes.Method, method_create.MemberType, "#1");
391                         Assert.AreEqual (MemberTypes.Method, method_edit.MemberType, "#2");
392                 }
393
394                 [Test]
395                 [Category ("NotWorking")]
396                 public void MethodHandle ()
397                 {
398                         try {
399                                 RuntimeMethodHandle handle = method_create.MethodHandle;
400                                 Assert.Fail ("#A1:" + handle);
401                         } catch (NotSupportedException ex) {
402                                 // The invoked member is not supported in a dynamic module
403                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
404                                 Assert.IsNull (ex.InnerException, "#A3");
405                                 Assert.IsNotNull (ex.Message, "#A4");
406                         }
407
408                         try {
409                                 RuntimeMethodHandle handle = method_edit.MethodHandle;
410                                 Assert.Fail ("#B1:" + handle);
411                         } catch (NotSupportedException ex) {
412                                 // The invoked member is not supported in a dynamic module
413                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
414                                 Assert.IsNull (ex.InnerException, "#B3");
415                                 Assert.IsNotNull (ex.Message, "#B4");
416                         }
417                 }
418
419                 [Test]
420                 public void Module ()
421                 {
422                         Assert.AreSame (module, method_create.Module, "#1");
423                         Assert.AreSame (module, method_edit.Module, "#2");
424                 }
425
426                 [Test]
427                 public void Name ()
428                 {
429                         Assert.AreEqual ("create", method_create.Name, "#1");
430                         Assert.AreEqual ("edit", method_edit.Name, "#2");
431                 }
432
433                 [Test]
434                 public void ReflectedType ()
435                 {
436                         Assert.AreSame (typeBarOfInt32, method_create.ReflectedType, "#1");
437                         Assert.AreSame (typeBarOfInt32, method_edit.ReflectedType, "#2");
438                 }
439
440                 [Test]
441                 public void ReturnParameter ()
442                 {
443                         try {
444                                 ParameterInfo ret = method_create.ReturnParameter;
445                                 Assert.Fail ("#A1:" + (ret != null));
446                         } catch (NotSupportedException ex) {
447                                 // Specified method is not supported
448                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
449                                 Assert.IsNull (ex.InnerException, "#A3");
450                                 Assert.IsNotNull (ex.Message, "#A4");
451                         }
452
453                         try {
454                                 ParameterInfo ret = method_create.ReturnParameter;
455                                 Assert.Fail ("#B1:" + (ret != null));
456                         } catch (NotSupportedException ex) {
457                                 // Specified method is not supported
458                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
459                                 Assert.IsNull (ex.InnerException, "#B3");
460                                 Assert.IsNotNull (ex.Message, "#B4");
461                         }
462                 }
463
464                 [Test]
465                 [Category ("NotWorking")]
466                 public void ReturnType ()
467                 {
468                         Type ret;
469                         
470                         ret = method_create.ReturnType;
471                         Assert.AreSame (typeBarOfT, ret, "#1");
472                         ret = method_edit.ReturnType;
473                         Assert.AreSame (typeBarOfT, ret, "#2");
474                 }
475
476                 [Test]
477                 [Category ("NotWorking")]
478                 public void ReturnTypeCustomAttributes ()
479                 {
480                         try {
481                                 ICustomAttributeProvider attr_prov = method_create.ReturnTypeCustomAttributes;
482                                 Assert.Fail ("#A1:" + (attr_prov != null));
483                         } catch (NotSupportedException ex) {
484                                 // Specified method is not supported
485                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
486                                 Assert.IsNull (ex.InnerException, "#A3");
487                                 Assert.IsNotNull (ex.Message, "#A4");
488                         }
489
490                         try {
491                                 ICustomAttributeProvider attr_prov = method_edit.ReturnTypeCustomAttributes;
492                                 Assert.Fail ("#B1:" + (attr_prov != null));
493                         } catch (NotSupportedException ex) {
494                                 // Specified method is not supported
495                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
496                                 Assert.IsNull (ex.InnerException, "#B3");
497                                 Assert.IsNotNull (ex.Message, "#B4");
498                         }
499                 }
500         }
501 }
502
503 #endif