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