EventBuilderTest.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // EventBuilder.cs - NUnit Test Cases for the EventBuilder class
  3. //
  4. // Zoltan Varga ([email protected])
  5. //
  6. // (C) Ximian, Inc. http://www.ximian.com
  7. using System;
  8. using System.Threading;
  9. using System.Reflection;
  10. using System.Reflection.Emit;
  11. using System.Runtime.CompilerServices;
  12. using NUnit.Framework;
  13. namespace MonoTests.System.Reflection.Emit
  14. {
  15. [TestFixture]
  16. public class EventBuilderTest : Assertion
  17. {
  18. public delegate void AnEvent (object o);
  19. private TypeBuilder tb;
  20. private ModuleBuilder module;
  21. private EventBuilder eb;
  22. private MethodBuilder mb;
  23. [SetUp]
  24. protected void SetUp () {
  25. AssemblyName assemblyName = new AssemblyName();
  26. assemblyName.Name = GetType().FullName;
  27. AssemblyBuilder assembly
  28. = Thread.GetDomain().DefineDynamicAssembly(
  29. assemblyName, AssemblyBuilderAccess.Run);
  30. module = assembly.DefineDynamicModule("module1");
  31. tb = module.DefineType("class1",
  32. TypeAttributes.Public);
  33. eb = tb.DefineEvent ("event1", EventAttributes.None, typeof (AnEvent));
  34. mb =
  35. tb.DefineMethod ("OnAnEvent",
  36. MethodAttributes.Public, typeof (void),
  37. new Type [] { typeof (AnEvent) });
  38. ILGenerator ilgen = mb.GetILGenerator();
  39. ilgen.Emit (OpCodes.Ret);
  40. // These two are required
  41. eb.SetAddOnMethod (mb);
  42. eb.SetRemoveOnMethod (mb);
  43. }
  44. [Test]
  45. [ExpectedException (typeof (ArgumentNullException))]
  46. public void TestSetAddOnMethod1 () {
  47. eb.SetAddOnMethod (null);
  48. }
  49. [Test]
  50. [ExpectedException (typeof (InvalidOperationException))]
  51. public void TestSetAddOnMethod2 () {
  52. tb.CreateType ();
  53. eb.SetAddOnMethod (mb);
  54. }
  55. [Test]
  56. [ExpectedException (typeof (ArgumentNullException))]
  57. public void TestSetRaiseMethod1 () {
  58. eb.SetRaiseMethod (null);
  59. }
  60. [Test]
  61. [ExpectedException (typeof (InvalidOperationException))]
  62. public void TestSetRaiseMethod2 () {
  63. tb.CreateType ();
  64. eb.SetRaiseMethod (mb);
  65. }
  66. [Test]
  67. [ExpectedException (typeof (ArgumentNullException))]
  68. public void TestSetRemoveAddOnMethod1 () {
  69. eb.SetRemoveOnMethod (null);
  70. }
  71. [Test]
  72. [ExpectedException (typeof (InvalidOperationException))]
  73. public void TestSetRemoveAddOnMethod2 () {
  74. tb.CreateType ();
  75. eb.SetRemoveOnMethod (mb);
  76. }
  77. [Test]
  78. [ExpectedException (typeof (ArgumentNullException))]
  79. public void TestAddOtherMethod1 () {
  80. eb.AddOtherMethod (null);
  81. }
  82. [Test]
  83. [ExpectedException (typeof (InvalidOperationException))]
  84. public void TestAddOtherMethod2 () {
  85. tb.CreateType ();
  86. eb.AddOtherMethod (mb);
  87. }
  88. [Test]
  89. [ExpectedException (typeof (ArgumentNullException))]
  90. public void TestSetCustomAttribute1 () {
  91. eb.SetCustomAttribute (null);
  92. }
  93. [Test]
  94. [ExpectedException (typeof (ArgumentNullException))]
  95. public void TestSetCustomAttribute2 () {
  96. eb.SetCustomAttribute (null, new byte [1]);
  97. }
  98. [Test]
  99. [ExpectedException (typeof (ArgumentNullException))]
  100. public void TestSetCustomAttribute3 () {
  101. ConstructorInfo con = typeof (String).GetConstructors ()[0];
  102. eb.SetCustomAttribute (con, null);
  103. }
  104. [Test]
  105. [ExpectedException (typeof (InvalidOperationException))]
  106. public void TestSetCustomAttribute4 () {
  107. tb.CreateType ();
  108. byte[] custAttrData = { 1, 0, 0, 0, 0};
  109. Type attrType = Type.GetType
  110. ("System.Reflection.AssemblyKeyNameAttribute");
  111. Type[] paramTypes = new Type[1];
  112. paramTypes[0] = typeof(String);
  113. ConstructorInfo ctorInfo =
  114. attrType.GetConstructor(paramTypes);
  115. eb.SetCustomAttribute (ctorInfo, custAttrData);
  116. }
  117. [Test]
  118. [ExpectedException (typeof (InvalidOperationException))]
  119. public void TestSetCustomAttribute5 () {
  120. tb.CreateType ();
  121. eb.SetCustomAttribute (new CustomAttributeBuilder (typeof (MethodImplAttribute).GetConstructor (new Type[1] { typeof (short) }), new object[1] {(short)MethodImplAttributes.Synchronized}));
  122. }
  123. [Test]
  124. public void TestCreation () {
  125. eb = tb.DefineEvent ("event2", EventAttributes.SpecialName, typeof (AnEvent));
  126. eb.SetRaiseMethod (mb);
  127. eb.SetAddOnMethod (mb);
  128. eb.SetRemoveOnMethod (mb);
  129. eb.AddOtherMethod (mb);
  130. eb.AddOtherMethod (mb);
  131. Type t = tb.CreateType ();
  132. MethodInfo mi = t.GetMethod ("OnAnEvent");
  133. EventInfo[] events = t.GetEvents ();
  134. AssertEquals (2, events.Length);
  135. {
  136. EventInfo ev = t.GetEvent ("event1");
  137. AssertEquals ("event1", ev.Name);
  138. AssertEquals (EventAttributes.None, ev.Attributes);
  139. AssertEquals (t, ev.DeclaringType);
  140. AssertEquals (typeof (AnEvent), ev.EventHandlerType);
  141. AssertEquals (true, ev.IsMulticast);
  142. AssertEquals (false, ev.IsSpecialName);
  143. AssertEquals (mi, ev.GetAddMethod ());
  144. AssertEquals (null, ev.GetRaiseMethod ());
  145. AssertEquals (mi, ev.GetRemoveMethod ());
  146. }
  147. {
  148. EventInfo ev = t.GetEvent ("event2");
  149. AssertEquals ("event2", ev.Name);
  150. AssertEquals (EventAttributes.SpecialName, ev.Attributes);
  151. AssertEquals (t, ev.DeclaringType);
  152. AssertEquals (typeof (AnEvent), ev.EventHandlerType);
  153. AssertEquals (true, ev.IsMulticast);
  154. AssertEquals (true, ev.IsSpecialName);
  155. AssertEquals (mi, ev.GetAddMethod ());
  156. AssertEquals (mi, ev.GetRaiseMethod ());
  157. AssertEquals (mi, ev.GetRemoveMethod ());
  158. }
  159. }
  160. }
  161. }