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