2
0

AssemblyBuilderTest.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // AssemblyBuilderTest.cs - NUnit Test Cases for the AssemblyBuilder class
  3. //
  4. // Zoltan Varga ([email protected])
  5. //
  6. // (C) Ximian, Inc. http://www.ximian.com
  7. //
  8. //
  9. using System;
  10. using System.Threading;
  11. using System.Reflection;
  12. using System.Reflection.Emit;
  13. using System.IO;
  14. using NUnit.Framework;
  15. namespace MonoTests.System.Reflection.Emit
  16. {
  17. [TestFixture]
  18. public class AssemblyBuilderTest : Assertion
  19. {
  20. [AttributeUsage (AttributeTargets.Assembly)]
  21. public sealed class FooAttribute : Attribute
  22. {
  23. public FooAttribute (string arg)
  24. {
  25. }
  26. }
  27. static int nameIndex = 0;
  28. static AppDomain domain;
  29. static AssemblyBuilder ab;
  30. [SetUp]
  31. protected void SetUp () {
  32. domain = Thread.GetDomain ();
  33. ab = genAssembly ();
  34. }
  35. private AssemblyName genAssemblyName () {
  36. AssemblyName assemblyName = new AssemblyName();
  37. assemblyName.Name = "MonoTests.System.Reflection.Emit.AssemblyBuilderTest" + (nameIndex ++);
  38. return assemblyName;
  39. }
  40. private AssemblyBuilder genAssembly () {
  41. return domain.DefineDynamicAssembly (genAssemblyName (),
  42. AssemblyBuilderAccess.RunAndSave);
  43. }
  44. private MethodInfo genEntryFunction (AssemblyBuilder assembly) {
  45. ModuleBuilder module = assembly.DefineDynamicModule("module1");
  46. TypeBuilder tb = module.DefineType ("A");
  47. MethodBuilder mb = tb.DefineMethod ("A",
  48. MethodAttributes.Static, typeof (void), new Type [0]);
  49. mb.GetILGenerator ().Emit (OpCodes.Ret);
  50. return mb;
  51. }
  52. public void TestCodeBase () {
  53. try {
  54. string codebase = ab.CodeBase;
  55. Fail ();
  56. }
  57. catch (NotSupportedException) {
  58. }
  59. }
  60. public void TestEntryPoint () {
  61. AssertEquals ("EntryPoint defaults to null",
  62. null, ab.EntryPoint);
  63. MethodInfo mi = genEntryFunction (ab);
  64. ab.SetEntryPoint (mi);
  65. AssertEquals ("EntryPoint works", mi, ab.EntryPoint);
  66. }
  67. public void TestSetEntryPoint () {
  68. // Check invalid arguments
  69. try {
  70. ab.SetEntryPoint (null);
  71. Fail ();
  72. }
  73. catch (ArgumentNullException) {
  74. }
  75. // Check method from other assembly
  76. try {
  77. ab.SetEntryPoint (typeof (AssemblyBuilderTest).GetMethod ("TestSetEntryPoint"));
  78. Fail ();
  79. }
  80. catch (InvalidOperationException) {
  81. }
  82. }
  83. public void TestIsDefined () {
  84. CustomAttributeBuilder cab = new CustomAttributeBuilder (typeof (FooAttribute).GetConstructor (new Type [1] {typeof (string)}), new object [1] { "A" });
  85. ab.SetCustomAttribute (cab);
  86. AssertEquals ("IsDefined works",
  87. true, ab.IsDefined (typeof (FooAttribute), false));
  88. AssertEquals ("IsDefined works",
  89. false, ab.IsDefined (typeof (AssemblyVersionAttribute), false));
  90. }
  91. [ExpectedException (typeof (NotSupportedException))]
  92. public void TestGetManifestResourceNames () {
  93. ab.GetManifestResourceNames ();
  94. }
  95. [ExpectedException (typeof (NotSupportedException))]
  96. public void TestGetManifestResourceInfo () {
  97. ab.GetManifestResourceInfo ("foo");
  98. }
  99. [ExpectedException (typeof (NotSupportedException))]
  100. public void TestGetManifestResourceStream1 () {
  101. ab.GetManifestResourceStream ("foo");
  102. }
  103. [ExpectedException (typeof (NotSupportedException))]
  104. public void TestGetManifestResourceStream2 () {
  105. ab.GetManifestResourceStream (typeof (int), "foo");
  106. }
  107. [ExpectedException (typeof (NotSupportedException))]
  108. public void TestGetFiles1 () {
  109. ab.GetFiles ();
  110. }
  111. [ExpectedException (typeof (NotSupportedException))]
  112. public void TestGetFiles2 () {
  113. ab.GetFiles (true);
  114. }
  115. [ExpectedException (typeof (NotSupportedException))]
  116. public void TestGetFile () {
  117. ab.GetFile ("foo");
  118. }
  119. [ExpectedException (typeof (NotSupportedException))]
  120. public void TestGetExportedTypes () {
  121. ab.GetExportedTypes ();
  122. }
  123. [ExpectedException (typeof (ArgumentNullException))]
  124. public void TestGetDynamicModule1 () {
  125. ab.GetDynamicModule (null);
  126. }
  127. [ExpectedException (typeof (ArgumentException))]
  128. public void TestGetDynamicModule2 () {
  129. ab.GetDynamicModule ("");
  130. }
  131. public void TestGetDynamicModule3 () {
  132. AssertNull (ab.GetDynamicModule ("FOO2"));
  133. ModuleBuilder mb = ab.DefineDynamicModule ("FOO");
  134. AssertEquals (mb, ab.GetDynamicModule ("FOO"));
  135. AssertNull (ab.GetDynamicModule ("FOO4"));
  136. }
  137. }
  138. }