MonoGenericClassTest.cs 6.7 KB

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