Fix warnings in mscorlib's Test suite + bring a couple more tests + fix thread rename...
[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 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
8 //
9
10 #if !MONOTOUCH
11
12 using System;
13 using System.Collections;
14 using System.Collections.Generic;
15 using System.Collections.ObjectModel;
16 using System.Threading;
17 using System.Reflection;
18 using System.Reflection.Emit;
19 using System.IO;
20 using System.Security;
21 using System.Security.Permissions;
22 using System.Runtime.InteropServices;
23 using NUnit.Framework;
24 using System.Runtime.CompilerServices;
25
26 namespace MonoTests.System.Reflection.Emit
27 {
28         [TestFixture]
29         public class MonoGenericClassTest
30         {
31                 AssemblyBuilder assembly;
32                 ModuleBuilder module;
33                 int typeCount;
34                 static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.MonoGenericClassTest";
35
36                 string MakeName ()
37                 {
38                         return "internal__type"+ typeCount++;
39                 }
40
41                 [SetUp]
42                 public void SetUp ()
43                 {
44                         SetUp (AssemblyBuilderAccess.RunAndSave);
45                 }
46                 
47                 void SetUp (AssemblyBuilderAccess access)
48                 {
49                         AssemblyName assemblyName = new AssemblyName ();
50                         assemblyName.Name = ASSEMBLY_NAME;
51
52                         assembly =
53                                 Thread.GetDomain ().DefineDynamicAssembly (
54                                         assemblyName, access, Path.GetTempPath ());
55
56                         module = assembly.DefineDynamicModule ("module1");
57                         typeCount = 0;
58                 }
59
60
61                 [Test]
62                 public void TestNameMethods ()
63                 {
64                         TypeBuilder tb = module.DefineType ("foo.type");
65                         tb.DefineGenericParameters ("T", "K");
66
67                         Type inst = tb.MakeGenericType (typeof (double), typeof (string));
68
69                         Assert.AreEqual ("type", inst.Name, "#1");
70                         Assert.AreEqual ("foo", inst.Namespace, "#2");
71 #if NET_4_0
72                         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");
73                         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");
74 #elif NET_2_1
75                         Assert.AreEqual ("foo.type[[System.Double, mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]", inst.FullName, "#3");
76                         Assert.AreEqual ("foo.type[[System.Double, mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], MonoTests.System.Reflection.Emit.MonoGenericClassTest, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", inst.AssemblyQualifiedName, "#4");
77 #else
78                         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");
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]], MonoTests.System.Reflection.Emit.MonoGenericClassTest, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", inst.AssemblyQualifiedName, "#4");
80 #endif
81                         Assert.AreEqual ("foo.type[System.Double,System.String]", inst.ToString (), "#5");
82                 }
83
84                 static void CheckInst (string prefix, Type inst, int a, int b)
85                 {
86                         var resA = inst.GetMethods (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
87                         var resB = inst.GetMethods (BindingFlags.Public | BindingFlags.Instance);
88
89                         Assert.AreEqual (a, resA.Length, prefix + 1);
90                         Assert.AreEqual (b, resB.Length, prefix + 2);
91                 }
92
93                 [Test]
94                 public void MethodsThatRaiseNotSupported ()
95                 {
96                         var tb = module.DefineType ("foo.type");
97                         tb.DefineGenericParameters ("T");
98
99                         var ginst = tb.MakeGenericType (typeof (double));
100
101                         try {
102                                 ginst.GetElementType ();
103                                 Assert.Fail ("#1");
104                         } catch (NotSupportedException) {  }
105                         try {
106                                 ginst.GetInterface ("foo", true);
107                                 Assert.Fail ("#2");
108                         } catch (NotSupportedException) {  }
109                         try {
110                                 ginst.GetEvent ("foo", BindingFlags.Public | BindingFlags.Instance);
111                                 Assert.Fail ("#3");
112                         } catch (NotSupportedException) {  }
113                         try {
114                                 ginst.GetField ("foo", BindingFlags.Public | BindingFlags.Instance);
115                                 Assert.Fail ("#4");
116                         } catch (NotSupportedException) {  }
117                         try {
118                                 ginst.GetMembers (BindingFlags.Public | BindingFlags.Instance);
119                                 Assert.Fail ("#5");
120                         } catch (NotSupportedException) {  }
121                         try {
122                                 ginst.GetMethod ("Foo");
123                                 Assert.Fail ("#6");
124                         } catch (NotSupportedException) {  }
125                         try {
126                                 ginst.GetNestedType ("foo", BindingFlags.Public | BindingFlags.Instance);
127                                 Assert.Fail ("#7");
128                         } catch (NotSupportedException) {  }
129                         try {
130                                 ginst.GetProperty ("foo");
131                                 Assert.Fail ("#8");
132                         } catch (NotSupportedException) {  }
133                         try {
134                                 var x = ginst.TypeInitializer;
135                                 Assert.Fail ("#9");
136                         } catch (NotSupportedException) {  }
137                         try {
138                                 var x = ginst.InvokeMember ("foo", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, null, null);
139                                 Assert.Fail ("#10");
140                         } catch (NotSupportedException) {  }
141                         try {
142                                 ginst.IsDefined (typeof (int), true);
143                                 Assert.Fail ("#11");
144                         } catch (NotSupportedException) {  }
145                         try {
146                                 ginst.GetCustomAttributes (true);
147                                 Assert.Fail ("#12");
148                         } catch (NotSupportedException) {  }
149                         try {
150                                 ginst.GetCustomAttributes (typeof (int), true);
151                                 Assert.Fail ("#13");
152                         } catch (NotSupportedException) {  }
153                         try {
154                                 ginst.IsAssignableFrom (ginst);
155                                 Assert.Fail ("#14");
156                         } catch (NotSupportedException) {  }
157                         try {
158                                 ginst.GetNestedTypes (BindingFlags.Public);
159                                 Assert.Fail ("#14");
160                         } catch (NotSupportedException) {  }
161                 }
162
163                 [Test]
164                 public void ClassMustNotBeRegisteredAfterTypeBuilderIsFinished ()
165                 {
166                         TypeBuilder tb = module.DefineType ("foo.type");
167                         tb.DefineGenericParameters ("T");
168
169                         var c = tb.CreateType ();
170
171                         var sreInst = tb.MakeGenericType (typeof (int));
172                         var rtInst = c.MakeGenericType (typeof (int));
173
174                         Assert.AreNotSame (sreInst, rtInst, "#1");
175
176                         /*This must not throw*/
177                         rtInst.IsDefined (typeof (int), true);
178                 }
179
180                 public class Bar<T> {
181                         public class Foo<U> {}
182                 }
183
184                 [Test]
185                 public void DeclaringTypeMustReturnNonInflatedType ()
186                 {
187                         var ut = new TypeDelegator (typeof (int));
188                         var ut2 = typeof(Bar<>.Foo<>);
189                         var t = ut2.MakeGenericType (ut, ut);
190                         Assert.AreSame (typeof (Bar<>), t.DeclaringType, "#1");
191                 }
192
193                 public class Base<T> {}
194                 public class SubClass<K> : Base<K> {}
195
196                 [Test]
197                 public void BaseTypeMustReturnNonInflatedType ()
198                 {
199                         var ut = new TypeDelegator (typeof (int));
200                         var ut2 = typeof(SubClass<>);
201                         var t = ut2.MakeGenericType (ut);
202                         //This is Base<K> where K is SubClass::K
203                         var expected = typeof (Base<>).MakeGenericType (typeof (SubClass<>).GetGenericArguments ()[0]);
204                         Assert.AreSame (expected, t.BaseType, "#1");
205                         
206                 }
207         }
208 }
209
210 #endif