2003-06-15 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / Test / System.Reflection.Emit / ConstructorBuilderTest.cs
1 //\r
2 // ConstructorBuilderTest.cs - NUnit Test Cases for the ConstructorBuilder class\r
3 //\r
4 // Zoltan Varga (vargaz@freemail.hu)\r
5 //\r
6 // (C) Ximian, Inc.  http://www.ximian.com\r
7 \r
8 // TODO:\r
9 //  - implement 'Signature' (what the hell it does???) and test it\r
10 //  - implement Equals and test it\r
11 //  - AddDeclarativeSecurity\r
12 \r
13 using System;\r
14 using System.Threading;\r
15 using System.Reflection;\r
16 using System.Reflection.Emit;\r
17 \r
18 using NUnit.Framework;\r
19 \r
20 namespace MonoTests.System.Reflection.Emit\r
21 {\r
22 \r
23 [TestFixture]\r
24 public class ConstructorBuilderTest : Assertion\r
25 {       \r
26     private TypeBuilder genClass;\r
27 \r
28         private ModuleBuilder module;\r
29 \r
30         [SetUp]\r
31         protected void SetUp () {\r
32                 AssemblyName assemblyName = new AssemblyName();\r
33                 assemblyName.Name = "MonoTests.System.Reflection.Emit.ConstructorBuilderTest";\r
34 \r
35                 AssemblyBuilder assembly \r
36                         = Thread.GetDomain().DefineDynamicAssembly(\r
37                                 assemblyName, AssemblyBuilderAccess.Run);\r
38 \r
39                 module = assembly.DefineDynamicModule("module1");\r
40                 \r
41                 genClass = module.DefineType("class1", \r
42                                                                          TypeAttributes.Public);\r
43         }\r
44 \r
45         public void TestAttributes () {\r
46                 ConstructorBuilder cb = genClass.DefineConstructor (\r
47                          MethodAttributes.Public, 0, new Type [0]);\r
48 \r
49                 AssertEquals ("Attributes works", \r
50                                           cb.Attributes, MethodAttributes.Public | MethodAttributes.SpecialName);\r
51         }\r
52 \r
53         public void TestCallingConvention () {\r
54                 /* This does not work under MS.NET\r
55                 ConstructorBuilder cb3 = genClass.DefineConstructor (\r
56                         0, CallingConventions.VarArgs, new Type [0]);\r
57                 AssertEquals ("CallingConvetion works",\r
58                                           CallingConventions.VarArgs | CallingConventions.HasThis,\r
59                                           cb3.CallingConvention);\r
60                 */\r
61 \r
62                 ConstructorBuilder cb4 = genClass.DefineConstructor (\r
63                          MethodAttributes.Static, CallingConventions.Standard, new Type [0]);\r
64                 AssertEquals ("Static implies !HasThis",\r
65                                           cb4.CallingConvention,\r
66                                           CallingConventions.Standard);\r
67         }\r
68 \r
69         public void TestDeclaringType () {\r
70                 ConstructorBuilder cb = genClass.DefineConstructor (\r
71                          0, 0, new Type[0]);\r
72 \r
73                 AssertEquals ("DeclaringType works",\r
74                                           cb.DeclaringType, genClass);\r
75         }\r
76 \r
77         public void TestInitLocals () {\r
78                 ConstructorBuilder cb = genClass.DefineConstructor (\r
79                          0, 0, new Type[0]);\r
80 \r
81                 AssertEquals ("InitLocals defaults to true", cb.InitLocals, true);\r
82                 cb.InitLocals = false;\r
83                 AssertEquals ("InitLocals is settable", cb.InitLocals, false);\r
84         }\r
85 \r
86         public void TestMethodHandle () {\r
87                 ConstructorBuilder cb = genClass.DefineConstructor (\r
88                          0, 0, new Type [0]);\r
89 \r
90                 try {\r
91                         RuntimeMethodHandle handle = cb.MethodHandle;\r
92                         Fail ();\r
93                 } catch (NotSupportedException) {\r
94                 }\r
95         }\r
96 \r
97         public void TestName () {\r
98                 ConstructorBuilder cb = genClass.DefineConstructor (0, 0, new Type [0]);\r
99 \r
100                 AssertEquals ("Name works", ".ctor", cb.Name);\r
101 \r
102                 ConstructorBuilder cb2 = genClass.DefineConstructor (MethodAttributes.Static, 0, new Type [0]);\r
103                 AssertEquals ("Static constructors have the right name", ".cctor", cb2.Name);\r
104         }\r
105 \r
106         public void TestReflectedType () {\r
107                 ConstructorBuilder cb = genClass.DefineConstructor (0, 0, new Type [0]);\r
108 \r
109                 AssertEquals ("ReflectedType works", \r
110                                           genClass, cb.ReflectedType);\r
111         }\r
112 \r
113         public void TestReturnType () {\r
114                 ConstructorBuilder cb = genClass.DefineConstructor (0, 0, new Type [0]);\r
115 \r
116                 AssertEquals ("ReturnType works", \r
117                                           null, cb.ReturnType);\r
118         }\r
119 \r
120         public void TestDefineParameter () {\r
121                 TypeBuilder tb = module.DefineType ("class7", TypeAttributes.Public);\r
122                 ConstructorBuilder cb = tb.DefineConstructor (\r
123                          0, 0, new Type [2] { typeof(int), typeof(int) });\r
124 \r
125                 // index out of range\r
126                 try {\r
127                         cb.DefineParameter (0, 0, "param1");\r
128                         Fail ();\r
129                 } catch (ArgumentOutOfRangeException) {\r
130                 }\r
131                 try {\r
132                         cb.DefineParameter (3, 0, "param1");\r
133                         Fail ();\r
134                 } catch (ArgumentOutOfRangeException) {\r
135                 }\r
136 \r
137                 // Normal usage\r
138                 cb.DefineParameter (1, 0, "param1");\r
139                 cb.DefineParameter (1, 0, "param1");\r
140                 cb.DefineParameter (2, 0, null);\r
141 \r
142                 // Can not be called on a created type\r
143                 cb.GetILGenerator ().Emit (OpCodes.Ret);\r
144                 tb.CreateType ();\r
145                 try {\r
146                         cb.DefineParameter (1, 0, "param1");\r
147                         Fail ();\r
148                 }\r
149                 catch (InvalidOperationException) {\r
150                 }\r
151         }\r
152 \r
153         public void TestGetCustomAttributes () {\r
154                 ConstructorBuilder cb = genClass.DefineConstructor (\r
155                         0, 0, new Type [1] {typeof(int)});\r
156 \r
157                 try {\r
158                         cb.GetCustomAttributes (true);\r
159                         Fail ();\r
160                 } catch (NotSupportedException) {\r
161                 }\r
162 \r
163                 try {\r
164                         cb.GetCustomAttributes (null, true);\r
165                         Fail ();\r
166                 } catch (NotSupportedException) {\r
167                 }\r
168         }\r
169 \r
170         public void TestMethodImplementationFlags () {\r
171                 ConstructorBuilder cb = genClass.DefineConstructor (\r
172                          0, 0, new Type [0]);\r
173 \r
174                 AssertEquals ("MethodImplementationFlags defaults to Managed+IL",\r
175                                           cb.GetMethodImplementationFlags (),\r
176                                           MethodImplAttributes.Managed | MethodImplAttributes.IL);\r
177 \r
178                 cb.SetImplementationFlags (MethodImplAttributes.OPTIL);\r
179 \r
180                 AssertEquals ("SetImplementationFlags works",\r
181                                           cb.GetMethodImplementationFlags (),\r
182                                           MethodImplAttributes.OPTIL);\r
183 \r
184                 // Can not be called on a created type\r
185                 TypeBuilder tb = module.DefineType ("class14", TypeAttributes.Public);\r
186                 ConstructorBuilder cb2 = tb.DefineConstructor (\r
187                          0, 0, new Type [0]);\r
188 \r
189                 cb2.GetILGenerator ().Emit (OpCodes.Ret);\r
190                 cb2.SetImplementationFlags (MethodImplAttributes.Managed);\r
191                 tb.CreateType ();\r
192                 try {\r
193                         cb2.SetImplementationFlags (MethodImplAttributes.OPTIL);\r
194                         Fail ();\r
195                 }\r
196                 catch (InvalidOperationException) {\r
197                 }\r
198         }\r
199 \r
200         public void TestGetModule () {\r
201                 ConstructorBuilder cb = genClass.DefineConstructor (\r
202                          0, 0, new Type [0]);\r
203 \r
204                 AssertEquals ("GetModule works",\r
205                                           module, cb.GetModule ());\r
206         }\r
207 \r
208         public void TestGetParameters () {\r
209                 TypeBuilder tb = module.DefineType ("class16", TypeAttributes.Public);\r
210                 ConstructorBuilder cb = tb.DefineConstructor (\r
211                          0, 0, new Type [1] {typeof(int)});\r
212                 cb.GetILGenerator ().Emit (OpCodes.Ret);\r
213 \r
214                 // Can't be called before CreateType ()\r
215                 /* This does not work under mono\r
216                 try {\r
217                         cb.GetParameters ();\r
218                         Fail ();\r
219                 } catch (InvalidOperationException) {\r
220                 }\r
221                 */\r
222 \r
223                 tb.CreateType ();\r
224 \r
225                 /* This does not work under MS.NET !\r
226                 cb.GetParameters ();\r
227                 */\r
228         }\r
229 \r
230         public void TestGetToken () {\r
231                 TypeBuilder tb = module.DefineType ("class17", TypeAttributes.Public);\r
232                 ConstructorBuilder cb = tb.DefineConstructor (\r
233                          0, 0, new Type [1] {typeof(void)});\r
234 \r
235                 cb.GetToken ();\r
236         }\r
237 \r
238         public void TestInvoke () {\r
239                 ConstructorBuilder cb = genClass.DefineConstructor (\r
240                          0, 0, \r
241                         new Type [1] {typeof(int)});\r
242 \r
243                 try {\r
244                         cb.Invoke (null, new object [1] { 42 });\r
245                         Fail ();\r
246                 } catch (NotSupportedException) {\r
247                 }\r
248 \r
249                 try {\r
250                         cb.Invoke (null, 0, null, new object [1] { 42 }, null);\r
251                         Fail ();\r
252                 } catch (NotSupportedException) {\r
253                 }\r
254         }\r
255 \r
256         public void TestIsDefined () {\r
257                 ConstructorBuilder cb = genClass.DefineConstructor (\r
258                          0, 0, \r
259                         new Type [1] {typeof(int)});\r
260 \r
261                 try {\r
262                         cb.IsDefined (null, true);\r
263                         Fail ();\r
264                 } catch (NotSupportedException) {\r
265                 }\r
266         }\r
267 \r
268         public void TestSetCustomAttribute () {\r
269                 TypeBuilder tb = module.DefineType ("class21", TypeAttributes.Public);\r
270                 ConstructorBuilder cb = tb.DefineConstructor (\r
271                          0, 0, \r
272                         new Type [1] {typeof(int)});\r
273                 cb.GetILGenerator ().Emit (OpCodes.Ret);\r
274 \r
275                 // Null argument\r
276                 try {\r
277                         cb.SetCustomAttribute (null);\r
278                         Fail ();\r
279                 } catch (ArgumentNullException) {\r
280                 }\r
281 \r
282                 byte[] custAttrData = { 1, 0, 0, 0, 0};\r
283                 Type attrType = Type.GetType\r
284                         ("System.Reflection.AssemblyKeyNameAttribute");\r
285                 Type[] paramTypes = new Type[1];\r
286                 paramTypes[0] = typeof(String);\r
287                 ConstructorInfo ctorInfo =\r
288                         attrType.GetConstructor(paramTypes);\r
289 \r
290                 cb.SetCustomAttribute (ctorInfo, custAttrData);\r
291 \r
292                 // Null arguments again\r
293                 try {\r
294                         cb.SetCustomAttribute (null, new byte[2]);\r
295                         Fail ();\r
296                 } catch (ArgumentNullException) {\r
297                 }\r
298 \r
299                 try {\r
300                         cb.SetCustomAttribute (ctorInfo, null);\r
301                         Fail ();\r
302                 } catch (ArgumentNullException) {\r
303                 }\r
304         }\r
305 }\r
306 }\r