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