ModuleTest.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // ModuleTest - NUnit Test Cases for the Module 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 System.Collections;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.Reflection
  17. {
  18. [TestFixture]
  19. public class ModuleTest : Assertion
  20. {
  21. static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.Reflection.ModuleTest");
  22. [SetUp]
  23. public void SetUp () {
  24. while (Directory.Exists (TempFolder))
  25. TempFolder = Path.Combine (TempFolder, "2");
  26. Directory.CreateDirectory (TempFolder);
  27. }
  28. [TearDown]
  29. public void TearDown () {
  30. try {
  31. // This throws an exception under MS.NET, since the directory contains loaded
  32. // assemblies.
  33. Directory.Delete (TempFolder, true);
  34. }
  35. catch (Exception) {
  36. }
  37. }
  38. // Some of these tests overlap with the tests for ModuleBuilder
  39. [Test]
  40. public void TestGlobalData () {
  41. string name = "moduletest-assembly";
  42. string fileName = name + ".dll";
  43. AssemblyName assemblyName = new AssemblyName();
  44. assemblyName.Name = name;
  45. AssemblyBuilder ab
  46. = Thread.GetDomain().DefineDynamicAssembly(
  47. assemblyName, AssemblyBuilderAccess.RunAndSave, TempFolder);
  48. string resfile = Path.Combine (TempFolder, "res");
  49. using (StreamWriter sw = new StreamWriter (resfile)) {
  50. sw.WriteLine ("FOO");
  51. }
  52. ab.AddResourceFile ("res", "res");
  53. ModuleBuilder mb = ab.DefineDynamicModule(fileName, fileName);
  54. mb.DefineInitializedData ("DATA", new byte [100], FieldAttributes.Public);
  55. mb.DefineInitializedData ("DATA2", new byte [100], FieldAttributes.Public);
  56. mb.DefineInitializedData ("DATA3", new byte [99], FieldAttributes.Public);
  57. mb.DefineUninitializedData ("DATA4", 101, FieldAttributes.Public);
  58. mb.DefineInitializedData ("DATA_PRIVATE", new byte [100], 0);
  59. mb.CreateGlobalFunctions ();
  60. ab.Save (fileName);
  61. Assembly assembly = Assembly.LoadFrom (Path.Combine (TempFolder, fileName));
  62. Module module = assembly.GetLoadedModules ()[0];
  63. string[] expectedFieldNames = new string [] {
  64. "DATA", "DATA2", "DATA3", "DATA4"
  65. };
  66. ArrayList fieldNames = new ArrayList ();
  67. foreach (FieldInfo fi in module.GetFields ()) {
  68. fieldNames.Add (fi.Name);
  69. }
  70. AssertArrayEqualsSorted (expectedFieldNames, fieldNames.ToArray (typeof (string)));
  71. try {
  72. module.GetField (null);
  73. Fail ();
  74. }
  75. catch (ArgumentNullException) {
  76. }
  77. try {
  78. module.GetField (null, 0);
  79. Fail ();
  80. }
  81. catch (ArgumentNullException) {
  82. }
  83. AssertEquals (module.GetField ("DATA") != null, true);
  84. AssertEquals (module.GetField ("DATA2") != null, true);
  85. AssertEquals (module.GetField ("DATA3") != null, true);
  86. AssertEquals (module.GetField ("DATA4") != null, true);
  87. AssertEquals (module.GetField ("DATA_PRIVATE"), null);
  88. AssertEquals (module.GetField ("DATA_PRIVATE", BindingFlags.NonPublic | BindingFlags.Static) != null, true);
  89. // Check that these methods work correctly on resource modules
  90. Module m2 = assembly.GetModule ("res");
  91. AssertEquals (m2 != null, true);
  92. AssertEquals (m2.GetFields ().Length, 0);
  93. AssertEquals (m2.GetField ("DATA"), null);
  94. AssertEquals (m2.GetField ("DATA", BindingFlags.Public), null);
  95. }
  96. private static void AssertArrayEqualsSorted (Array o1, Array o2) {
  97. Array s1 = (Array)o1.Clone ();
  98. Array s2 = (Array)o2.Clone ();
  99. Array.Sort (s1);
  100. Array.Sort (s2);
  101. AssertEquals (s1.Length, s2.Length);
  102. for (int i = 0; i < s1.Length; ++i)
  103. AssertEquals (s1.GetValue (i), s2.GetValue (i));
  104. }
  105. }
  106. }