MonoGenericClassTest.cs 6.4 KB

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