Fixed.
[mono.git] / mcs / class / corlib / Test / System.Reflection.Emit / ConstructorOnTypeBuilderInstTest.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 ConstructorOnTypeBuilderInstTest
43         {
44                 private static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.ConstructorOnTypeBuilderInstTest";
45
46                 private AssemblyBuilder assembly;
47                 private ModuleBuilder module;
48                 private Type typeBarOfInt32;
49                 private ConstructorBuilder cb;
50                 private ConstructorInfo ci;
51                 private TypeBuilder tb;
52
53                 [SetUp]
54                 public void SetUp ()
55                 {
56                         SetUp (AssemblyBuilderAccess.RunAndSave);
57                 }
58                 
59                 void SetUp (AssemblyBuilderAccess access)
60                 {
61                         AssemblyName assemblyName = new AssemblyName ();
62                         assemblyName.Name = ASSEMBLY_NAME;
63
64                         assembly = AppDomain.CurrentDomain.DefineDynamicAssembly (
65                                 assemblyName, access,
66                                 Path.GetTempPath ());
67
68                         module = assembly.DefineDynamicModule ("module1");
69
70                         tb = module.DefineType ("Bar");
71                         GenericTypeParameterBuilder [] typeParams = tb.DefineGenericParameters ("T");
72
73                         cb = tb.DefineConstructor (MethodAttributes.Public,
74                                 CallingConventions.Standard,
75                                 new Type [] { typeof (string), typeof (int) });
76                         ILGenerator ig = cb.GetILGenerator ();
77                         ig.Emit (OpCodes.Ret);
78
79                         typeBarOfInt32 = tb.MakeGenericType (typeof (int));
80                         ci = TypeBuilder.GetConstructor (typeBarOfInt32, cb);
81                 }
82
83                 [Test]
84                 [Category ("NotWorking")]
85                 public void Attributes ()
86                 {
87                         Assert.AreEqual (MethodAttributes.PrivateScope |
88                                 MethodAttributes.Public | MethodAttributes.SpecialName,
89                                 ci.Attributes, "#1");
90                         tb.CreateType ();
91                         Assert.AreEqual (MethodAttributes.PrivateScope |
92                                 MethodAttributes.Public | MethodAttributes.SpecialName,
93                                 ci.Attributes, "#2");
94                 }
95
96                 [Test]
97                 [Category ("NotWorking")]
98                 public void CallingConvention ()
99                 {
100                         Assert.AreEqual (CallingConventions.HasThis,
101                                 ci.CallingConvention, "#1");
102                         tb.CreateType ();
103                         Assert.AreEqual (CallingConventions.HasThis,
104                                 ci.CallingConvention, "#2");
105                 }
106
107                 [Test]
108                 public void ContainsGenericParameters ()
109                 {
110                         Assert.IsFalse (ci.ContainsGenericParameters, "#1");
111                         tb.CreateType ();
112                         Assert.IsFalse (ci.ContainsGenericParameters, "#2");
113                 }
114
115                 [Test]
116                 public void DeclaringType ()
117                 {
118                         Assert.AreSame (typeBarOfInt32, ci.DeclaringType, "#1");
119                         tb.CreateType ();
120                         Assert.AreSame (typeBarOfInt32, ci.DeclaringType, "#2");
121                 }
122
123                 [Test] // GetCustomAttributes (Boolean)
124                 public void GetCustomAttributes1 ()
125                 {
126                         try {
127                                 ci.GetCustomAttributes (false);
128                                 Assert.Fail ("#A1");
129                         } catch (NotSupportedException ex) {
130                                 // The invoked member is not supported in a dynamic module
131                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
132                                 Assert.IsNull (ex.InnerException, "#A3");
133                                 Assert.IsNotNull (ex.Message, "#A4");
134                         }
135
136                         tb.CreateType ();
137
138                         try {
139                                 ci.GetCustomAttributes (false);
140                                 Assert.Fail ("#B1");
141                         } catch (NotSupportedException ex) {
142                                 // The invoked member is not supported in a dynamic module
143                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
144                                 Assert.IsNull (ex.InnerException, "#B3");
145                                 Assert.IsNotNull (ex.Message, "#B4");
146                         }
147                 }
148
149                 [Test] // GetCustomAttributes (Type, Boolean)
150                 public void GetCustomAttributes2 ()
151                 {
152                         try {
153                                 ci.GetCustomAttributes (typeof (FlagsAttribute), false);
154                                 Assert.Fail ("#A1");
155                         } catch (NotSupportedException ex) {
156                                 // The invoked member is not supported in a dynamic module
157                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
158                                 Assert.IsNull (ex.InnerException, "#A3");
159                                 Assert.IsNotNull (ex.Message, "#A4");
160                         }
161
162                         tb.CreateType ();
163
164                         try {
165                                 ci.GetCustomAttributes (typeof (FlagsAttribute), false);
166                                 Assert.Fail ("#B1");
167                         } catch (NotSupportedException ex) {
168                                 // The invoked member is not supported in a dynamic module
169                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
170                                 Assert.IsNull (ex.InnerException, "#B3");
171                                 Assert.IsNotNull (ex.Message, "#B4");
172                         }
173                 }
174
175                 [Test]
176                 public void GetGenericArguments ()
177                 {
178                         try {
179                                 ci.GetGenericArguments ();
180                                 Assert.Fail ("#A1");
181                         } catch (NotSupportedException ex) {
182                                 // Derived classes must provide an implementation
183                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
184                                 Assert.IsNull (ex.InnerException, "#A3");
185                                 Assert.IsNotNull (ex.Message, "#A4");
186                         }
187
188                         tb.CreateType ();
189
190                         try {
191                                 ci.GetGenericArguments ();
192                                 Assert.Fail ("#B1");
193                         } catch (NotSupportedException ex) {
194                                 // Derived classes must provide an implementation
195                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
196                                 Assert.IsNull (ex.InnerException, "#B3");
197                                 Assert.IsNotNull (ex.Message, "#B4");
198                         }
199                 }
200
201                 [Test]
202                 public void GetMethodImplementationFlags ()
203                 {
204                         Assert.AreEqual (MethodImplAttributes.Managed,
205                                 ci.GetMethodImplementationFlags (), "#1");
206                         tb.CreateType ();
207                         Assert.AreEqual (MethodImplAttributes.Managed,
208                                 ci.GetMethodImplementationFlags (), "#2");
209                 }
210
211                 [Test]
212                 public void GetParameters ()
213                 {
214                         try {
215                                 ci.GetParameters ();
216                                 Assert.Fail ("#A1");
217                         } catch (NotSupportedException ex) {
218                                 // Type has not been created
219                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
220                                 Assert.IsNull (ex.InnerException, "#A3");
221                                 Assert.IsNotNull (ex.Message, "#A4");
222                         }
223
224                         tb.CreateType ();
225
226                         ParameterInfo [] parameters = ci.GetParameters ();
227                         Assert.IsNotNull (parameters, "#B1");
228                         Assert.AreEqual (2, parameters.Length, "#B2");
229
230                         Assert.AreEqual (ParameterAttributes.None, parameters [0].Attributes, "#C1");
231                         Assert.IsNull (parameters [0].Name, "#C2");
232                         Assert.AreEqual (typeof (string), parameters [0].ParameterType, "#C3");
233                         Assert.AreEqual (0, parameters [0].Position, "#C4");
234
235                         Assert.AreEqual (ParameterAttributes.None, parameters [1].Attributes, "#D1");
236                         Assert.IsNull (parameters [1].Name, "#D2");
237                         Assert.AreEqual (typeof (int), parameters [1].ParameterType, "#D3");
238                         Assert.AreEqual (1, parameters [1].Position, "#D4");
239                 }
240
241                 [Test] // Invoke (Object [])
242                 public void Invoke1 ()
243                 {
244                         try {
245                                 ci.Invoke (new object [0]);
246                                 Assert.Fail ("#A1");
247                         } catch (InvalidOperationException ex) {
248                                 // Operation is not valid due to the current
249                                 // state of the object
250                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
251                                 Assert.IsNull (ex.InnerException, "#A3");
252                                 Assert.IsNotNull (ex.Message, "#A4");
253                         }
254
255                         tb.CreateType ();
256
257                         try {
258                                 ci.Invoke (new object [0]);
259                                 Assert.Fail ("#B1");
260                         } catch (InvalidOperationException ex) {
261                                 // Operation is not valid due to the current
262                                 // state of the object
263                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
264                                 Assert.IsNull (ex.InnerException, "#B3");
265                                 Assert.IsNotNull (ex.Message, "#B4");
266                         }
267                 }
268
269                 [Test] // Invoke (Object, Object [])
270                 public void Invoke2 ()
271                 {
272                         try {
273                                 ci.Invoke (null, new object [0]);
274                                 Assert.Fail ("#A1");
275                         } catch (NotSupportedException ex) {
276                                 // Specified method is not supported
277                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
278                                 Assert.IsNull (ex.InnerException, "#A3");
279                                 Assert.IsNotNull (ex.Message, "#A4");
280                         }
281
282                         tb.CreateType ();
283
284                         try {
285                                 ci.Invoke (null, new object [0]);
286                                 Assert.Fail ("#B1");
287                         } catch (NotSupportedException ex) {
288                                 // Specified method is not supported
289                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
290                                 Assert.IsNull (ex.InnerException, "#B3");
291                                 Assert.IsNotNull (ex.Message, "#B4");
292                         }
293                 }
294
295                 [Test] // Invoke (BindingFlags, Binder, Object [], CultureInfo)
296                 public void Invoke3 ()
297                 {
298                         try {
299                                 ci.Invoke (BindingFlags.Default, null, new object [0],
300                                         CultureInfo.InvariantCulture);
301                                 Assert.Fail ("#A1");
302                         } catch (InvalidOperationException ex) {
303                                 // Operation is not valid due to the current
304                                 // state of the object
305                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
306                                 Assert.IsNull (ex.InnerException, "#A3");
307                                 Assert.IsNotNull (ex.Message, "#A4");
308                         }
309
310                         tb.CreateType ();
311
312                         try {
313                                 ci.Invoke (BindingFlags.Default, null, new object [0],
314                                         CultureInfo.InvariantCulture);
315                                 Assert.Fail ("#B1");
316                         } catch (InvalidOperationException ex) {
317                                 // Operation is not valid due to the current
318                                 // state of the object
319                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
320                                 Assert.IsNull (ex.InnerException, "#B3");
321                                 Assert.IsNotNull (ex.Message, "#B4");
322                         }
323                 }
324
325                 [Test] // Invoke (Object, BindingFlags, Binder, Object [], CultureInfo)
326                 public void Invoke4 ()
327                 {
328                         try {
329                                 ci.Invoke (null, BindingFlags.Default, null,
330                                         new object [0], CultureInfo.InvariantCulture);
331                                 Assert.Fail ("#A1");
332                         } catch (NotSupportedException ex) {
333                                 // Specified method is not supported
334                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
335                                 Assert.IsNull (ex.InnerException, "#A3");
336                                 Assert.IsNotNull (ex.Message, "#A4");
337                         }
338
339                         tb.CreateType ();
340
341                         try {
342                                 ci.Invoke (null, BindingFlags.Default, null,
343                                         new object [0], CultureInfo.InvariantCulture);
344                                 Assert.Fail ("#B1");
345                         } catch (NotSupportedException ex) {
346                                 // Specified method is not supported
347                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
348                                 Assert.IsNull (ex.InnerException, "#B3");
349                                 Assert.IsNotNull (ex.Message, "#B4");
350                         }
351                 }
352
353                 [Test]
354                 public void IsDefined ()
355                 {
356                         try {
357                                 ci.IsDefined (typeof (FlagsAttribute), false);
358                                 Assert.Fail ("#A1");
359                         } catch (NotSupportedException ex) {
360                                 // The invoked member is not supported in a dynamic module
361                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
362                                 Assert.IsNull (ex.InnerException, "#A3");
363                                 Assert.IsNotNull (ex.Message, "#A4");
364                         }
365
366                         tb.CreateType ();
367
368                         try {
369                                 ci.IsDefined (typeof (FlagsAttribute), false);
370                                 Assert.Fail ("#B1");
371                         } catch (NotSupportedException ex) {
372                                 // The invoked member is not supported in a dynamic module
373                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
374                                 Assert.IsNull (ex.InnerException, "#B3");
375                                 Assert.IsNotNull (ex.Message, "#B4");
376                         }
377                 }
378
379                 [Test]
380                 public void IsGenericMethodDefinition ()
381                 {
382                         Assert.IsFalse (ci.IsGenericMethodDefinition, "#1");
383                         tb.CreateType ();
384                         Assert.IsFalse (ci.IsGenericMethodDefinition, "#2");
385                 }
386
387                 [Test]
388                 public void IsGenericMethod ()
389                 {
390                         Assert.IsFalse (ci.IsGenericMethod, "#1");
391                         tb.CreateType ();
392                         Assert.IsFalse (ci.IsGenericMethod, "#2");
393                 }
394
395                 [Test]
396                 public void MemberType ()
397                 {
398                         Assert.AreEqual (MemberTypes.Constructor, ci.MemberType, "#1");
399                         tb.CreateType ();
400                         Assert.AreEqual (MemberTypes.Constructor, ci.MemberType, "#2");
401                 }
402
403                 [Test]
404                 public void MethodHandle ()
405                 {
406                         try {
407                                 RuntimeMethodHandle handle = ci.MethodHandle;
408                                 Assert.Fail ("#A1:" + handle);
409                         } catch (NotSupportedException ex) {
410                                 // The invoked member is not supported in a dynamic module
411                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
412                                 Assert.IsNull (ex.InnerException, "#A3");
413                                 Assert.IsNotNull (ex.Message, "#A4");
414                         }
415
416                         tb.CreateType ();
417
418                         try {
419                                 RuntimeMethodHandle handle = ci.MethodHandle;
420                                 Assert.Fail ("#B1:" + handle);
421                         } catch (NotSupportedException ex) {
422                                 // The invoked member is not supported in a dynamic module
423                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
424                                 Assert.IsNull (ex.InnerException, "#B3");
425                                 Assert.IsNotNull (ex.Message, "#B4");
426                         }
427                 }
428
429                 [Test]
430                 public void Module ()
431                 {
432                         Assert.AreSame (module, ci.Module, "#1");
433                         tb.CreateType ();
434                         Assert.AreSame (module, ci.Module, "#2");
435                 }
436
437                 [Test]
438                 public void Name ()
439                 {
440                         Assert.AreEqual (".ctor", ci.Name, "#1");
441                         tb.CreateType ();
442                         Assert.AreEqual (".ctor", ci.Name, "#2");
443                 }
444
445                 [Test]
446                 public void ReflectedType ()
447                 {
448                         Assert.AreSame (typeBarOfInt32, ci.ReflectedType, "#1");
449                         tb.CreateType ();
450                         Assert.AreSame (typeBarOfInt32, ci.ReflectedType, "#2");
451                 }
452
453                 [Test]
454                 [Category ("NotDotNet")]
455                 public void MetadataTokenWorksUnderCompilerContext  ()
456                 {
457                         SetUp (AssemblyBuilderAccess.RunAndSave | (AssemblyBuilderAccess)0x800);
458                         int cb_token = cb.MetadataToken;
459                         int inst_token = ci.MetadataToken;
460                         Assert.AreEqual (cb_token, inst_token, "#1");
461                 }
462         }
463 }
464
465 #endif