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