update version number
[mcs.git] / class / corlib / Test / System.Reflection / MonoGenericClassTest.cs
blob17b1b2896cc991a074685315ce524e14afd56eac
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 //
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;
23 namespace MonoTests.System.Reflection.Emit
25 #if NET_2_0
26 [TestFixture]
27 public class MonoGenericClassTest
29 AssemblyBuilder assembly;
30 ModuleBuilder module;
31 int typeCount;
32 static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.MonoGenericClassTest";
34 string MakeName ()
36 return "internal__type"+ typeCount++;
39 [SetUp]
40 public void SetUp ()
42 SetUp (AssemblyBuilderAccess.RunAndSave);
45 void SetUp (AssemblyBuilderAccess access)
47 AssemblyName assemblyName = new AssemblyName ();
48 assemblyName.Name = ASSEMBLY_NAME;
50 assembly =
51 Thread.GetDomain ().DefineDynamicAssembly (
52 assemblyName, access, Path.GetTempPath ());
54 module = assembly.DefineDynamicModule ("module1");
55 typeCount = 0;
59 [Test]
60 public void TestNameMethods ()
62 TypeBuilder tb = module.DefineType ("foo.type");
63 tb.DefineGenericParameters ("T", "K");
65 Type inst = tb.MakeGenericType (typeof (double), typeof (string));
67 Assert.AreEqual ("type", inst.Name, "#1");
68 Assert.AreEqual ("foo", inst.Namespace, "#2");
69 #if NET_4_0
70 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");
71 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");
73 #else
75 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");
76 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");
78 #endif
79 Assert.AreEqual ("foo.type[System.Double,System.String]", inst.ToString (), "#5");
82 static void CheckInst (string prefix, Type inst, int a, int b)
84 var resA = inst.GetMethods (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
85 var resB = inst.GetMethods (BindingFlags.Public | BindingFlags.Instance);
87 Assert.AreEqual (a, resA.Length, prefix + 1);
88 Assert.AreEqual (b, resB.Length, prefix + 2);
91 [Test]
92 [Category ("NotDotNet")]
93 public void GetMethodsWorkWithFunkyInstantiations ()
95 SetUp (AssemblyBuilderAccess.RunAndSave | (AssemblyBuilderAccess)0x800);
96 TypeBuilder tb = module.DefineType ("Base", TypeAttributes.Public, typeof (object));
98 var a = typeof (IList<>).GetGenericArguments () [0];
99 var b = tb.DefineGenericParameters ("T") [0];
101 CheckInst ("#A", typeof (Collection<>).MakeGenericType (new Type [] {a}), 12, 16);
102 CheckInst ("#B", typeof (Collection<>).MakeGenericType (new Type[] { b }), 12, 16);
104 var tb2 = module.DefineType ("Child", TypeAttributes.Public, typeof (Collection<>).MakeGenericType (tb.MakeGenericType (typeof (int))));
105 tb2.DefineGenericParameters ("K");
107 CheckInst ("#C", tb2.MakeGenericType (typeof (double)), 0, 16);
111 [Test]
112 [Category ("NotDotNet")]
113 public void GetEventMustWorkUnderCompilerContext ()
115 SetUp (AssemblyBuilderAccess.RunAndSave | (AssemblyBuilderAccess)0x800);
116 var tb = module.DefineType ("foo.type");
117 tb.DefineGenericParameters ("T");
119 var ginst = tb.MakeGenericType (typeof (double));
121 try {
122 ginst.GetEvent ("foo", BindingFlags.Public | BindingFlags.Instance);
123 } catch (NotSupportedException) {
124 Assert.Fail ("#1");
128 [Test]
129 public void MethodsThatRaiseNotSupported ()
131 var tb = module.DefineType ("foo.type");
132 tb.DefineGenericParameters ("T");
134 var ginst = tb.MakeGenericType (typeof (double));
136 /*try { //FIXME this doesn't work yet
137 ginst.GetElementType ();
138 Assert.Fail ("#1");
139 } catch (NotSupportedException) { }*/
140 try {
141 ginst.GetInterface ("foo", true);
142 Assert.Fail ("#2");
143 } catch (NotSupportedException) { }
144 try {
145 ginst.GetEvent ("foo", BindingFlags.Public | BindingFlags.Instance);
146 Assert.Fail ("#3");
147 } catch (NotSupportedException) { }
148 try {
149 ginst.GetField ("foo", BindingFlags.Public | BindingFlags.Instance);
150 Assert.Fail ("#4");
151 } catch (NotSupportedException) { }
152 try {
153 ginst.GetMembers (BindingFlags.Public | BindingFlags.Instance);
154 Assert.Fail ("#5");
155 } catch (NotSupportedException) { }
156 try {
157 ginst.GetMethod ("Foo");
158 Assert.Fail ("#6");
159 } catch (NotSupportedException) { }
160 try {
161 ginst.GetNestedType ("foo", BindingFlags.Public | BindingFlags.Instance);
162 Assert.Fail ("#7");
163 } catch (NotSupportedException) { }
164 try {
165 ginst.GetProperty ("foo");
166 Assert.Fail ("#8");
167 } catch (NotSupportedException) { }
168 try {
169 var x = ginst.TypeInitializer;
170 Assert.Fail ("#9");
171 } catch (NotSupportedException) { }
172 try {
173 var x = ginst.InvokeMember ("foo", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, null, null);
174 Assert.Fail ("#10");
175 } catch (NotSupportedException) { }
176 try {
177 ginst.IsDefined (typeof (int), true);
178 Assert.Fail ("#11");
179 } catch (NotSupportedException) { }
180 try {
181 ginst.GetCustomAttributes (true);
182 Assert.Fail ("#12");
183 } catch (NotSupportedException) { }
184 try {
185 ginst.GetCustomAttributes (typeof (int), true);
186 Assert.Fail ("#13");
187 } catch (NotSupportedException) { }
190 #endif