2009-07-14 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mcs / class / corlib / Test / System.Reflection.Emit / GenericTypeParameterBuilderTest.cs
1 //
2 // GenericTypeParameterBuilderTest.cs - NUnit Test Cases for GenericTypeParameterBuilder
3 //
4 // Rodrigo Kumpera <rkumpera@novell.com>
5 //
6 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
7 //
8
9 #if NET_2_0
10
11 using System;
12 using System.Collections;
13 using System.Threading;
14 using System.Reflection;
15 using System.Reflection.Emit;
16 using System.IO;
17 using System.Security;
18 using System.Security.Permissions;
19 using System.Runtime.InteropServices;
20 using NUnit.Framework;
21 using System.Runtime.CompilerServices;
22
23 using System.Collections.Generic;
24
25 namespace MonoTests.System.Reflection.Emit
26 {
27         [TestFixture]
28         public class GenericTypeParameterBuilderTest
29         {
30                 AssemblyBuilder assembly;
31                 ModuleBuilder module;
32                 int typeCount;
33                 static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
34
35                 [SetUp]
36                 protected void SetUp ()
37                 {
38                         SetUp (AssemblyBuilderAccess.RunAndSave);
39                 }
40
41                 protected void SetUp (AssemblyBuilderAccess mode)
42                 {
43                         AssemblyName assemblyName = new AssemblyName ();
44                         assemblyName.Name = ASSEMBLY_NAME;
45
46                         assembly =
47                                 Thread.GetDomain ().DefineDynamicAssembly (
48                                         assemblyName, mode, Path.GetTempPath ());
49
50                         module = assembly.DefineDynamicModule ("module1");
51                         typeCount = 0;
52                 }
53
54                 [Test]
55                 public void PropertiesValue ()
56                 {
57                         TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
58                         var gparam = tb.DefineGenericParameters ("A", "B")[1];
59
60                         Assert.AreEqual (assembly, gparam.Assembly, "#1");
61                         Assert.AreEqual (null, gparam.AssemblyQualifiedName, "#2");
62                         Assert.AreEqual (null, gparam.BaseType, "#3");
63                         Assert.AreEqual (null, gparam.FullName, "#4");
64                         Assert.AreEqual (module, gparam.Module, "#5");
65                         Assert.AreEqual (null, gparam.Namespace, "#6");
66                         Assert.AreEqual (gparam, gparam.UnderlyingSystemType, "#7");
67                         Assert.AreEqual ("B", gparam.Name, "#8");
68
69                         try {
70                                 object x = gparam.GUID;
71                                 Assert.Fail ("#9");
72                         } catch (NotSupportedException) {}
73
74                         try {
75                                 object x = gparam.TypeHandle;
76                                 Assert.Fail ("#10");
77                         } catch (NotSupportedException) {}
78
79                         try {
80                                 object x = gparam.StructLayoutAttribute;
81                                 Assert.Fail ("#11");
82                         } catch (NotSupportedException) {}
83
84                         try {
85                                         var x = gparam.Attributes;
86                                         Assert.Fail ("#12");
87                         } catch (NotSupportedException) {}
88
89                         Assert.IsFalse (gparam.HasElementType, "#13");
90                         Assert.IsFalse (gparam.IsArray, "#14");
91                         Assert.IsFalse (gparam.IsByRef, "#15");
92                         Assert.IsFalse (gparam.IsCOMObject, "#16");
93                         Assert.IsFalse (gparam.IsPointer, "#17");
94                         Assert.IsFalse (gparam.IsPrimitive, "#18");
95
96                 }       
97
98                 [Test]
99                 public void Methods ()
100                 {
101                         TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
102                         var gparam = tb.DefineGenericParameters ("A", "B")[1];
103
104                         try {
105                                 gparam.GetInterface ("foo", true);
106                                 Assert.Fail ("#1");
107                         } catch (NotSupportedException) {
108
109                         }
110         
111                         try {
112                                 gparam.GetInterfaces ();
113                                 Assert.Fail ("#2");
114                         } catch (NotSupportedException) {
115
116                         }
117         
118                         try {
119                                 gparam.GetElementType ();
120                                 Assert.Fail ("#3");
121                         } catch (NotSupportedException) {
122
123                         }
124         
125                         try {
126                                 gparam.GetEvent ("foo", BindingFlags.Public);
127                                 Assert.Fail ("#4");
128                         } catch (NotSupportedException) {
129
130                         }
131         
132                         try {
133                                 gparam.GetEvents (BindingFlags.Public);
134                                 Assert.Fail ("#5");
135                         } catch (NotSupportedException) {
136
137                         }
138         
139                         try {
140                                 gparam.GetField ("foo", BindingFlags.Public);
141                                 Assert.Fail ("#6");
142                         } catch (NotSupportedException) {
143
144                         }
145         
146                         try {
147                                 gparam.GetFields (BindingFlags.Public);
148                                 Assert.Fail ("#7");
149                         } catch (NotSupportedException) {
150
151                         }
152         
153                         try {
154                                 gparam.GetMembers (BindingFlags.Public);
155                                 Assert.Fail ("#8");
156                         } catch (NotSupportedException) {
157
158                         }
159         
160                         try {
161                                 gparam.GetMethod ("Sort");
162                                 Assert.Fail ("#9");
163                         } catch (NotSupportedException) {
164
165                         }
166         
167                         try {
168                                 gparam.GetMethods (BindingFlags.Public);
169                                 Assert.Fail ("#9");
170                         } catch (NotSupportedException) {
171
172                         }
173         
174                         try {
175                                 gparam.GetNestedType ("bla", BindingFlags.Public);
176                                 Assert.Fail ("#10");
177                         } catch (NotSupportedException) {
178
179                         }
180         
181                         try {
182                                 gparam.GetNestedTypes (BindingFlags.Public);
183                                 Assert.Fail ("#11");
184                         } catch (NotSupportedException) {
185
186                         }
187         
188                         try {
189                                 gparam.GetProperties (BindingFlags.Public);
190                                 Assert.Fail ("#12");
191                         } catch (NotSupportedException) {
192
193                         }       
194         
195                         try {
196                                 gparam.GetProperty ("Length");
197                                 Assert.Fail ("#13");
198                         } catch (NotSupportedException) {
199
200                         }
201         
202                         try {
203                                 gparam.GetConstructor (new Type[] { typeof (int) });
204                                 Assert.Fail ("#14");
205                         } catch (NotSupportedException) {
206
207                         }
208         
209                         try {
210                                 gparam.GetArrayRank ();
211                                 Assert.Fail ("#15");
212                         } catch (NotSupportedException) {
213
214                         }
215
216                         try {
217                                 gparam.GetConstructors (BindingFlags.Public);
218                                 Assert.Fail ("#16");
219                         } catch (NotSupportedException) {}
220
221                         try {
222                                 gparam.InvokeMember ("GetLength", BindingFlags.Public, null, null, null);
223                                 Assert.Fail ("#17");
224                         } catch (NotSupportedException) {}
225
226                         try {
227                                 gparam.IsSubclassOf (gparam);
228                                 Assert.Fail ("#18");
229                         } catch (NotSupportedException) {}
230
231                         try {
232                                 gparam.IsAssignableFrom (gparam);
233                                 Assert.Fail ("#19");
234                         } catch (NotSupportedException) {}
235
236                         try {
237                                 gparam.GetInterfaceMap (typeof (IEnumerable));
238                                 Assert.Fail ("#20");
239                         } catch (NotSupportedException) {}
240
241                         try {
242                                 gparam.IsInstanceOfType (new object());
243                                 Assert.Fail ("#21");
244                         } catch (NotSupportedException) {}
245                 }
246
247                 [Test]
248                 public void MakeGenericType ()
249                 {
250                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
251                         var gparam = tb.DefineGenericParameters ("A", "B")[1];
252                         try {
253                                 gparam.MakeGenericType (new Type[] { typeof (string) });
254                                 Assert.Fail ("#1");
255                         } catch (InvalidOperationException) {}
256                 }
257
258
259                 [Test]
260                 public void GenericParameterAttributes ()
261                 {
262                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
263                         var gparam = tb.DefineGenericParameters ("A", "B")[1];
264                         try {
265                                 object attr = gparam.GenericParameterAttributes;
266                                 Assert.Fail ("#1");
267                         } catch (NotSupportedException) {}
268                 }
269
270                 [Test]
271                 public void MakeArrayType ()
272                 {
273                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
274                         var gparam = tb.DefineGenericParameters ("A", "B")[1];
275                         Type res = gparam.MakeArrayType ();
276                         Assert.IsNotNull (res, "#1");
277                         Assert.IsTrue (res.IsArray, "#2");
278
279                         res = gparam.MakeArrayType (2);
280                         Assert.IsNotNull (res, "#3");
281                         Assert.IsTrue (res.IsArray, "#4");
282                 }
283
284                 [Test]
285                 public void MakeByRefType ()
286                 {
287                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
288                         var gparam = tb.DefineGenericParameters ("A", "B")[1];
289                         Type res = gparam.MakeByRefType ();
290
291                         Assert.IsNotNull (res, "#1");
292                         Assert.IsTrue (res.IsByRef, "#2");
293                 }
294
295                 [Test]
296                 public void MakePointerType ()
297                 {
298                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
299                         var gparam = tb.DefineGenericParameters ("A", "B")[1];
300                         Type res = gparam.MakePointerType ();
301
302                         Assert.IsNotNull (res, "#1");
303                         Assert.IsTrue (res.IsPointer, "#2");
304                 }
305
306
307                 [Test]
308                 public void GenericTypeMembers ()
309                 {
310                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
311                         var gparam = tb.DefineGenericParameters ("A", "B")[1];
312
313                         try {
314                                 gparam.GetGenericArguments ();
315                                 Assert.Fail ("#1");
316                         } catch (InvalidOperationException) {}
317
318                         try {
319                                 gparam.GetGenericParameterConstraints ();
320                                 Assert.Fail ("#2");
321                         } catch (InvalidOperationException) {}
322
323                         try {
324                                 gparam.GetGenericTypeDefinition ();
325                                 Assert.Fail ("#3");
326                         } catch (InvalidOperationException) {}
327                 
328                         Assert.IsTrue (gparam.ContainsGenericParameters, "#4");
329                         try {
330                                 var x = gparam.GenericParameterAttributes;
331                                 Assert.Fail ("#5");
332                         } catch (NotSupportedException) {}
333
334                         Assert.AreEqual (1, gparam.GenericParameterPosition, "#6");
335
336                         Assert.IsTrue (gparam.ContainsGenericParameters, "#7");
337
338                         Assert.IsTrue (gparam.IsGenericParameter, "#8");
339                         Assert.IsFalse (gparam.IsGenericType, "#9");
340                         Assert.IsFalse (gparam.IsGenericTypeDefinition, "#10");
341                 }
342
343                 [Test]
344                 [Category ("NotDotNet")]
345                 public void GetAttributeFlagsImplWorksUnderCompilerContext ()
346                 {
347                         SetUp (AssemblyBuilderAccess.RunAndSave  | (AssemblyBuilderAccess)0x800);
348                         TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
349                         var gparam = tb.DefineGenericParameters ("A", "B")[1];
350
351                         Assert.AreEqual (TypeAttributes.Public, gparam.Attributes, "#1");
352                 }
353         }
354 }
355
356 #endif