2010-03-12 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / corlib / Test / System.Reflection / MonoGenericClassTest.cs
1 //
2 // MonoGenericClassTest.cs - NUnit Test Cases for MonoGenericClassTest
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.Collections.Generic;
12 using System.Collections.ObjectModel;
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 #if NET_2_0
24 using System.Collections.Generic;
25 #endif
26
27 namespace MonoTests.System.Reflection.Emit
28 {
29 #if NET_2_0
30         [TestFixture]
31         public class MonoGenericClassTest
32         {
33                 AssemblyBuilder assembly;
34                 ModuleBuilder module;
35                 int typeCount;
36                 static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.MonoGenericClassTest";
37
38                 string MakeName ()
39                 {
40                         return "internal__type"+ typeCount++;
41                 }
42
43                 [SetUp]
44                 public void SetUp ()
45                 {
46                         SetUp (AssemblyBuilderAccess.RunAndSave);
47                 }
48                 
49                 void SetUp (AssemblyBuilderAccess access)
50                 {
51                         AssemblyName assemblyName = new AssemblyName ();
52                         assemblyName.Name = ASSEMBLY_NAME;
53
54                         assembly =
55                                 Thread.GetDomain ().DefineDynamicAssembly (
56                                         assemblyName, access, Path.GetTempPath ());
57
58                         module = assembly.DefineDynamicModule ("module1");
59                         typeCount = 0;
60                 }
61
62
63                 [Test]
64                 public void TestNameMethods ()
65                 {
66                         TypeBuilder tb = module.DefineType ("foo.type");
67                         tb.DefineGenericParameters ("T", "K");
68
69                         Type inst = tb.MakeGenericType (typeof (double), typeof (string));
70
71                         Assert.AreEqual ("type", inst.Name, "#1");
72                         Assert.AreEqual ("foo", inst.Namespace, "#2");
73 #if NET_4_0
74                         Assert.AreEqual ("foo.type[[System.Double, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]", inst.FullName, "#3");
75                         Assert.AreEqual ("foo.type[[System.Double, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], MonoTests.System.Reflection.Emit.MonoGenericClassTest, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", inst.AssemblyQualifiedName, "#4");
76
77 #else
78
79                         Assert.AreEqual ("foo.type[[System.Double, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]", inst.FullName, "#3");
80                         Assert.AreEqual ("foo.type[[System.Double, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], MonoTests.System.Reflection.Emit.MonoGenericClassTest, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", inst.AssemblyQualifiedName, "#4");
81
82 #endif
83                         Assert.AreEqual ("foo.type[System.Double,System.String]", inst.ToString (), "#5");
84                 }
85
86                 static void CheckInst (string prefix, Type inst, int a, int b)
87                 {
88                         var resA = inst.GetMethods (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
89                         var resB = inst.GetMethods (BindingFlags.Public | BindingFlags.Instance);
90
91                         Assert.AreEqual (a, resA.Length, prefix + 1);
92                         Assert.AreEqual (b, resB.Length, prefix + 2);
93                 }
94
95                 [Test]
96                 [Category ("NotDotNet")]
97                 public void GetMethodsWorkWithFunkyInstantiations ()
98                 {
99                         SetUp (AssemblyBuilderAccess.RunAndSave | (AssemblyBuilderAccess)0x800);
100                         TypeBuilder tb = module.DefineType ("Base", TypeAttributes.Public, typeof (object));
101
102                         var a = typeof (IList<>).GetGenericArguments () [0];
103                         var b = tb.DefineGenericParameters ("T") [0];
104
105                         CheckInst ("#A", typeof (Collection<>).MakeGenericType (new Type [] {a}), 12, 16);
106                         CheckInst ("#B", typeof (Collection<>).MakeGenericType (new Type[] { b }), 12, 16);
107
108                         var tb2 = module.DefineType ("Child", TypeAttributes.Public, typeof (Collection<>).MakeGenericType (tb.MakeGenericType (typeof (int))));
109                         tb2.DefineGenericParameters ("K");
110
111                         CheckInst ("#C", tb2.MakeGenericType (typeof (double)), 0, 16);
112                         
113                 }
114
115                 [Test]
116                 [Category ("NotDotNet")]
117                 public void GetEventMustWorkUnderCompilerContext ()
118                 {
119                         SetUp (AssemblyBuilderAccess.RunAndSave | (AssemblyBuilderAccess)0x800);
120                         var tb = module.DefineType ("foo.type");
121                         tb.DefineGenericParameters ("T");
122
123                         var ginst = tb.MakeGenericType (typeof (double));
124                         
125                         try {
126                                 ginst.GetEvent ("foo", BindingFlags.Public | BindingFlags.Instance);
127                         } catch (NotSupportedException) {
128                                 Assert.Fail ("#1");
129                         }
130                 }
131
132                 [Test]
133                 public void MethodsThatRaiseNotSupported ()
134                 {
135                         var tb = module.DefineType ("foo.type");
136                         tb.DefineGenericParameters ("T");
137
138                         var ginst = tb.MakeGenericType (typeof (double));
139
140                         /*try { //FIXME this doesn't work yet
141                                 ginst.GetElementType ();
142                                 Assert.Fail ("#1");
143                         } catch (NotSupportedException) {  }*/
144                         try {
145                                 ginst.GetInterface ("foo", true);
146                                 Assert.Fail ("#2");
147                         } catch (NotSupportedException) {  }
148                         try {
149                                 ginst.GetEvent ("foo", BindingFlags.Public | BindingFlags.Instance);
150                                 Assert.Fail ("#3");
151                         } catch (NotSupportedException) {  }
152                         try {
153                                 ginst.GetField ("foo", BindingFlags.Public | BindingFlags.Instance);
154                                 Assert.Fail ("#4");
155                         } catch (NotSupportedException) {  }
156                         try {
157                                 ginst.GetMembers (BindingFlags.Public | BindingFlags.Instance);
158                                 Assert.Fail ("#5");
159                         } catch (NotSupportedException) {  }
160                         try {
161                                 ginst.GetMethod ("Foo");
162                                 Assert.Fail ("#6");
163                         } catch (NotSupportedException) {  }
164                         try {
165                                 ginst.GetNestedType ("foo", BindingFlags.Public | BindingFlags.Instance);
166                                 Assert.Fail ("#7");
167                         } catch (NotSupportedException) {  }
168                         try {
169                                 ginst.GetProperty ("foo");
170                                 Assert.Fail ("#8");
171                         } catch (NotSupportedException) {  }
172                         try {
173                                 var x = ginst.TypeInitializer;
174                                 Assert.Fail ("#9");
175                         } catch (NotSupportedException) {  }
176                         try {
177                                 var x = ginst.InvokeMember ("foo", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, null, null);
178                                 Assert.Fail ("#10");
179                         } catch (NotSupportedException) {  }
180                         try {
181                                 ginst.IsDefined (typeof (int), true);
182                                 Assert.Fail ("#11");
183                         } catch (NotSupportedException) {  }
184                         try {
185                                 ginst.GetCustomAttributes (true);
186                                 Assert.Fail ("#12");
187                         } catch (NotSupportedException) {  }
188                         try {
189                                 ginst.GetCustomAttributes (typeof (int), true);
190                                 Assert.Fail ("#13");
191                         } catch (NotSupportedException) {  }
192                 }
193         }
194 #endif
195 }