2 // ConstructorBuilderTest.cs - NUnit Test Cases for the ConstructorBuilder class
4 // Zoltan Varga (vargaz@freemail.hu)
6 // (C) Ximian, Inc. http://www.ximian.com
9 // - implement 'Signature' (what the hell it does???) and test it
10 // - implement Equals and test it
13 using System.Threading;
14 using System.Reflection;
15 using System.Reflection.Emit;
16 using System.Security;
17 using System.Security.Permissions;
19 using NUnit.Framework;
21 namespace MonoTests.System.Reflection.Emit
25 public class ConstructorBuilderTest : Assertion
27 private TypeBuilder genClass;
29 private ModuleBuilder module;
31 private static int typeIndexer = 0;
34 protected void SetUp () {
35 AssemblyName assemblyName = new AssemblyName();
36 assemblyName.Name = "MonoTests.System.Reflection.Emit.ConstructorBuilderTest";
38 AssemblyBuilder assembly
39 = Thread.GetDomain().DefineDynamicAssembly(
40 assemblyName, AssemblyBuilderAccess.Run);
42 module = assembly.DefineDynamicModule("module1");
44 genClass = module.DefineType(genTypeName (),
45 TypeAttributes.Public);
48 // Return a unique type name
49 private string genTypeName () {
50 return "class" + (typeIndexer ++);
53 public void TestAttributes () {
54 ConstructorBuilder cb = genClass.DefineConstructor (
55 MethodAttributes.Public, 0, new Type [0]);
57 Assert ("Attributes works",
58 (cb.Attributes & MethodAttributes.Public) != 0);
59 Assert ("Attributes works",
60 (cb.Attributes & MethodAttributes.SpecialName) != 0);
63 public void TestCallingConvention () {
64 /* This does not work under MS.NET
65 ConstructorBuilder cb3 = genClass.DefineConstructor (
66 0, CallingConventions.VarArgs, new Type [0]);
67 AssertEquals ("CallingConvetion works",
68 CallingConventions.VarArgs | CallingConventions.HasThis,
69 cb3.CallingConvention);
72 ConstructorBuilder cb4 = genClass.DefineConstructor (
73 MethodAttributes.Static, CallingConventions.Standard, new Type [0]);
74 AssertEquals ("Static implies !HasThis",
75 cb4.CallingConvention,
76 CallingConventions.Standard);
79 public void TestDeclaringType () {
80 ConstructorBuilder cb = genClass.DefineConstructor (
83 AssertEquals ("DeclaringType works",
84 cb.DeclaringType, genClass);
87 public void TestInitLocals () {
88 ConstructorBuilder cb = genClass.DefineConstructor (
91 AssertEquals ("InitLocals defaults to true", cb.InitLocals, true);
92 cb.InitLocals = false;
93 AssertEquals ("InitLocals is settable", cb.InitLocals, false);
97 public void TestMethodHandle () {
98 ConstructorBuilder cb = genClass.DefineConstructor (
101 RuntimeMethodHandle handle = cb.MethodHandle;
104 public void TestName () {
105 ConstructorBuilder cb = genClass.DefineConstructor (0, 0, new Type [0]);
107 AssertEquals ("Name works", ".ctor", cb.Name);
109 ConstructorBuilder cb2 = genClass.DefineConstructor (MethodAttributes.Static, 0, new Type [0]);
110 AssertEquals ("Static constructors have the right name", ".cctor", cb2.Name);
113 public void TestReflectedType () {
114 ConstructorBuilder cb = genClass.DefineConstructor (0, 0, new Type [0]);
116 AssertEquals ("ReflectedType works",
117 genClass, cb.ReflectedType);
120 public void TestReturnType () {
121 ConstructorBuilder cb = genClass.DefineConstructor (0, 0, new Type [0]);
123 AssertEquals ("ReturnType works",
124 null, cb.ReturnType);
127 public void TestDefineParameter () {
128 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
129 ConstructorBuilder cb = tb.DefineConstructor (
130 0, 0, new Type [2] { typeof(int), typeof(int) });
132 // index out of range
134 cb.DefineParameter (0, 0, "param1");
136 } catch (ArgumentOutOfRangeException) {
139 cb.DefineParameter (3, 0, "param1");
141 } catch (ArgumentOutOfRangeException) {
145 cb.DefineParameter (1, 0, "param1");
146 cb.DefineParameter (1, 0, "param1");
147 cb.DefineParameter (2, 0, null);
149 // Can not be called on a created type
150 cb.GetILGenerator ().Emit (OpCodes.Ret);
153 cb.DefineParameter (1, 0, "param1");
156 catch (InvalidOperationException) {
160 public void TestGetCustomAttributes () {
161 ConstructorBuilder cb = genClass.DefineConstructor (
162 0, 0, new Type [1] {typeof(int)});
165 cb.GetCustomAttributes (true);
167 } catch (NotSupportedException) {
171 cb.GetCustomAttributes (null, true);
173 } catch (NotSupportedException) {
177 public void TestMethodImplementationFlags () {
178 ConstructorBuilder cb = genClass.DefineConstructor (
181 AssertEquals ("MethodImplementationFlags defaults to Managed+IL",
182 cb.GetMethodImplementationFlags (),
183 MethodImplAttributes.Managed | MethodImplAttributes.IL);
185 cb.SetImplementationFlags (MethodImplAttributes.OPTIL);
187 AssertEquals ("SetImplementationFlags works",
188 cb.GetMethodImplementationFlags (),
189 MethodImplAttributes.OPTIL);
191 // Can not be called on a created type
192 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
193 ConstructorBuilder cb2 = tb.DefineConstructor (
196 cb2.GetILGenerator ().Emit (OpCodes.Ret);
197 cb2.SetImplementationFlags (MethodImplAttributes.Managed);
200 cb2.SetImplementationFlags (MethodImplAttributes.OPTIL);
203 catch (InvalidOperationException) {
207 public void TestGetModule () {
208 ConstructorBuilder cb = genClass.DefineConstructor (
211 AssertEquals ("GetModule works",
212 module, cb.GetModule ());
215 public void TestGetParameters () {
216 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
217 ConstructorBuilder cb = tb.DefineConstructor (
218 0, 0, new Type [1] {typeof(int)});
219 cb.GetILGenerator ().Emit (OpCodes.Ret);
221 // Can't be called before CreateType ()
222 /* This does not work under mono
226 } catch (InvalidOperationException) {
232 /* This does not work under MS.NET !
237 public void TestGetToken () {
238 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
239 ConstructorBuilder cb = tb.DefineConstructor (
240 0, 0, new Type [1] {typeof(void)});
245 public void TestInvoke () {
246 ConstructorBuilder cb = genClass.DefineConstructor (
248 new Type [1] {typeof(int)});
251 cb.Invoke (null, new object [1] { 42 });
253 } catch (NotSupportedException) {
257 cb.Invoke (null, 0, null, new object [1] { 42 }, null);
259 } catch (NotSupportedException) {
263 public void TestIsDefined () {
264 ConstructorBuilder cb = genClass.DefineConstructor (
266 new Type [1] {typeof(int)});
269 cb.IsDefined (null, true);
271 } catch (NotSupportedException) {
275 public void TestSetCustomAttribute () {
276 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
277 ConstructorBuilder cb = tb.DefineConstructor (
279 new Type [1] {typeof(int)});
280 cb.GetILGenerator ().Emit (OpCodes.Ret);
284 cb.SetCustomAttribute (null);
286 } catch (ArgumentNullException) {
289 byte[] custAttrData = { 1, 0, 0, 0, 0};
290 Type attrType = Type.GetType
291 ("System.Reflection.AssemblyKeyNameAttribute");
292 Type[] paramTypes = new Type[1];
293 paramTypes[0] = typeof(String);
294 ConstructorInfo ctorInfo =
295 attrType.GetConstructor(paramTypes);
297 cb.SetCustomAttribute (ctorInfo, custAttrData);
299 // Null arguments again
301 cb.SetCustomAttribute (null, new byte[2]);
303 } catch (ArgumentNullException) {
307 cb.SetCustomAttribute (ctorInfo, null);
309 } catch (ArgumentNullException) {
314 public void GetCustomAttributes () {
315 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
316 ConstructorBuilder cb = tb.DefineConstructor (
317 MethodAttributes.Public, 0,
318 new Type [1] {typeof(int)});
319 cb.GetILGenerator ().Emit (OpCodes.Ret);
321 Type attrType = typeof (ObsoleteAttribute);
322 ConstructorInfo ctorInfo =
323 attrType.GetConstructor (new Type [] { typeof (String) });
325 cb.SetCustomAttribute (new CustomAttributeBuilder (ctorInfo, new object [] { "FOO" }));
327 Type t = tb.CreateType ();
329 // Try the created type
331 ConstructorInfo ci = t.GetConstructors () [0];
332 object[] attrs = ci.GetCustomAttributes (true);
334 AssertEquals (1, attrs.Length);
335 Assert (attrs [0] is ObsoleteAttribute);
336 AssertEquals ("FOO", ((ObsoleteAttribute)attrs [0]).Message);
339 // Try the type builder
341 ConstructorInfo ci = tb.GetConstructors () [0];
342 object[] attrs = ci.GetCustomAttributes (true);
344 AssertEquals (1, attrs.Length);
345 Assert (attrs [0] is ObsoleteAttribute);
346 AssertEquals ("FOO", ((ObsoleteAttribute)attrs [0]).Message);
351 // Same as in MethodBuilderTest
353 [ExpectedException (typeof (InvalidOperationException))]
354 public void TestAddDeclarativeSecurityAlreadyCreated () {
355 ConstructorBuilder cb = genClass.DefineConstructor (
356 MethodAttributes.Public, 0, new Type [0]);
357 ILGenerator ilgen = cb.GetILGenerator ();
358 ilgen.Emit (OpCodes.Ret);
359 genClass.CreateType ();
361 PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
362 cb.AddDeclarativeSecurity (SecurityAction.Demand, set);
366 [ExpectedException (typeof (ArgumentNullException))]
367 public void TestAddDeclarativeSecurityNullPermissionSet () {
368 ConstructorBuilder cb = genClass.DefineConstructor (
369 MethodAttributes.Public, 0, new Type [0]);
370 cb.AddDeclarativeSecurity (SecurityAction.Demand, null);
374 public void TestAddDeclarativeSecurityInvalidAction () {
375 ConstructorBuilder cb = genClass.DefineConstructor (
376 MethodAttributes.Public, 0, new Type [0]);
378 SecurityAction[] actions = new SecurityAction [] {
379 SecurityAction.RequestMinimum,
380 SecurityAction.RequestOptional,
381 SecurityAction.RequestRefuse };
382 PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
384 foreach (SecurityAction action in actions) {
386 cb.AddDeclarativeSecurity (action, set);
389 catch (ArgumentException) {
395 [ExpectedException (typeof (InvalidOperationException))]
396 public void TestAddDeclarativeSecurityDuplicateAction () {
397 ConstructorBuilder cb = genClass.DefineConstructor (
398 MethodAttributes.Public, 0, new Type [0]);
399 PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
400 cb.AddDeclarativeSecurity (SecurityAction.Demand, set);
401 cb.AddDeclarativeSecurity (SecurityAction.Demand, set);