2 // MethodOnTypeBuilderInstTest - NUnit Test Cases for MethodOnTypeBuilderInst
5 // Gert Driesen (drieseng@users.sourceforge.net)
7 // Copyright (C) 2008 Gert Driesen
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
32 using System
.Globalization
;
34 using System
.Reflection
;
35 using System
.Reflection
.Emit
;
37 using NUnit
.Framework
;
39 namespace MonoTests
.System
.Reflection
.Emit
42 public class ConstructorOnTypeBuilderInstTest
44 private static string ASSEMBLY_NAME
= "MonoTests.System.Reflection.Emit.ConstructorOnTypeBuilderInstTest";
46 private AssemblyBuilder assembly
;
47 private ModuleBuilder module
;
48 private Type typeBarOfInt32
;
49 private ConstructorBuilder cb
;
50 private ConstructorInfo ci
;
51 private TypeBuilder tb
;
56 SetUp (AssemblyBuilderAccess
.RunAndSave
);
59 void SetUp (AssemblyBuilderAccess access
)
61 AssemblyName assemblyName
= new AssemblyName ();
62 assemblyName
.Name
= ASSEMBLY_NAME
;
64 assembly
= AppDomain
.CurrentDomain
.DefineDynamicAssembly (
68 module
= assembly
.DefineDynamicModule ("module1");
70 tb
= module
.DefineType ("Bar");
71 GenericTypeParameterBuilder
[] typeParams
= tb
.DefineGenericParameters ("T");
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
);
79 typeBarOfInt32
= tb
.MakeGenericType (typeof (int));
80 ci
= TypeBuilder
.GetConstructor (typeBarOfInt32
, cb
);
84 [Category ("NotWorking")]
85 public void Attributes ()
87 Assert
.AreEqual (MethodAttributes
.PrivateScope
|
88 MethodAttributes
.Public
| MethodAttributes
.SpecialName
,
91 Assert
.AreEqual (MethodAttributes
.PrivateScope
|
92 MethodAttributes
.Public
| MethodAttributes
.SpecialName
,
97 [Category ("NotWorking")]
98 public void CallingConvention ()
100 Assert
.AreEqual (CallingConventions
.HasThis
,
101 ci
.CallingConvention
, "#1");
103 Assert
.AreEqual (CallingConventions
.HasThis
,
104 ci
.CallingConvention
, "#2");
108 public void ContainsGenericParameters ()
110 Assert
.IsFalse (ci
.ContainsGenericParameters
, "#1");
112 Assert
.IsFalse (ci
.ContainsGenericParameters
, "#2");
116 public void DeclaringType ()
118 Assert
.AreSame (typeBarOfInt32
, ci
.DeclaringType
, "#1");
120 Assert
.AreSame (typeBarOfInt32
, ci
.DeclaringType
, "#2");
123 [Test
] // GetCustomAttributes (Boolean)
124 public void GetCustomAttributes1 ()
127 ci
.GetCustomAttributes (false);
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");
139 ci
.GetCustomAttributes (false);
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");
149 [Test
] // GetCustomAttributes (Type, Boolean)
150 public void GetCustomAttributes2 ()
153 ci
.GetCustomAttributes (typeof (FlagsAttribute
), false);
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");
165 ci
.GetCustomAttributes (typeof (FlagsAttribute
), false);
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");
176 public void GetGenericArguments ()
179 ci
.GetGenericArguments ();
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");
191 ci
.GetGenericArguments ();
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");
202 public void GetMethodImplementationFlags ()
204 Assert
.AreEqual (MethodImplAttributes
.Managed
,
205 ci
.GetMethodImplementationFlags (), "#1");
207 Assert
.AreEqual (MethodImplAttributes
.Managed
,
208 ci
.GetMethodImplementationFlags (), "#2");
212 public void GetParameters ()
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");
226 ParameterInfo
[] parameters
= ci
.GetParameters ();
227 Assert
.IsNotNull (parameters
, "#B1");
228 Assert
.AreEqual (2, parameters
.Length
, "#B2");
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");
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");
241 [Test
] // Invoke (Object [])
242 public void Invoke1 ()
245 ci
.Invoke (new object [0]);
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");
258 ci
.Invoke (new object [0]);
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");
269 [Test
] // Invoke (Object, Object [])
270 public void Invoke2 ()
273 ci
.Invoke (null, new object [0]);
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");
285 ci
.Invoke (null, new object [0]);
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");
295 [Test
] // Invoke (BindingFlags, Binder, Object [], CultureInfo)
296 public void Invoke3 ()
299 ci
.Invoke (BindingFlags
.Default
, null, new object [0],
300 CultureInfo
.InvariantCulture
);
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");
313 ci
.Invoke (BindingFlags
.Default
, null, new object [0],
314 CultureInfo
.InvariantCulture
);
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");
325 [Test
] // Invoke (Object, BindingFlags, Binder, Object [], CultureInfo)
326 public void Invoke4 ()
329 ci
.Invoke (null, BindingFlags
.Default
, null,
330 new object [0], CultureInfo
.InvariantCulture
);
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");
342 ci
.Invoke (null, BindingFlags
.Default
, null,
343 new object [0], CultureInfo
.InvariantCulture
);
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");
354 public void IsDefined ()
357 ci
.IsDefined (typeof (FlagsAttribute
), false);
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");
369 ci
.IsDefined (typeof (FlagsAttribute
), false);
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");
380 public void IsGenericMethodDefinition ()
382 Assert
.IsFalse (ci
.IsGenericMethodDefinition
, "#1");
384 Assert
.IsFalse (ci
.IsGenericMethodDefinition
, "#2");
388 public void IsGenericMethod ()
390 Assert
.IsFalse (ci
.IsGenericMethod
, "#1");
392 Assert
.IsFalse (ci
.IsGenericMethod
, "#2");
396 public void MemberType ()
398 Assert
.AreEqual (MemberTypes
.Constructor
, ci
.MemberType
, "#1");
400 Assert
.AreEqual (MemberTypes
.Constructor
, ci
.MemberType
, "#2");
404 public void MethodHandle ()
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");
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");
430 public void Module ()
432 Assert
.AreSame (module
, ci
.Module
, "#1");
434 Assert
.AreSame (module
, ci
.Module
, "#2");
440 Assert
.AreEqual (".ctor", ci
.Name
, "#1");
442 Assert
.AreEqual (".ctor", ci
.Name
, "#2");
446 public void ReflectedType ()
448 Assert
.AreSame (typeBarOfInt32
, ci
.ReflectedType
, "#1");
450 Assert
.AreSame (typeBarOfInt32
, ci
.ReflectedType
, "#2");
454 [Category ("NotDotNet")]
455 public void MetadataTokenWorksUnderCompilerContext ()
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");