Debian Codegraph @

external/corefx/src/System.Reflection.Emit/tests/EventBuilder/EventBuilderSetRaiseMethod.cs

raw
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 using Xunit;
6
7 namespace System.Reflection.Emit.Tests
8 {
9 public class EventBuilderSetRaiseMethod
10 {
11 public delegate void TestEventHandler(object sender, object arg);
12
13 [Fact]
14 public void SetRaiseMethod_AbstractMethod_Twice()
15 {
16 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
17 EventBuilder eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(TestEventHandler));
18 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Abstract | MethodAttributes.Virtual);
19
20 eventBuilder.SetRaiseMethod(method);
21 eventBuilder.SetRaiseMethod(method);
22 }
23
24 [Fact]
25 public void SetRaiseMethod_InstanceMethod_Twice()
26 {
27 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
28 EventBuilder eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(TestEventHandler));
29 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public);
30 ILGenerator ilGenerator = method.GetILGenerator();
31 ilGenerator.Emit(OpCodes.Ret);
32
33 eventBuilder.SetRaiseMethod(method);
34 eventBuilder.SetRaiseMethod(method);
35 }
36
37 [Fact]
38 public void SetRaiseMethod_StaticMethod_Twice()
39 {
40 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
41 EventBuilder eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(TestEventHandler));
42 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Static);
43 ILGenerator ilGenerator = method.GetILGenerator();
44 ilGenerator.Emit(OpCodes.Ret);
45
46 eventBuilder.SetRaiseMethod(method);
47 eventBuilder.SetRaiseMethod(method);
48 }
49
50 [Fact]
51 public void SetRaiseMethod_PInvokeImplMethod_Twice()
52 {
53 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
54 EventBuilder eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(TestEventHandler));
55 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.PinvokeImpl);
56
57 eventBuilder.SetRaiseMethod(method);
58 eventBuilder.SetRaiseMethod(method);
59 }
60
61 [Fact]
62 public void SetRaiseMethod_MultipleDifferentMethods()
63 {
64 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
65 EventBuilder eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(TestEventHandler));
66 MethodBuilder method1 = type.DefineMethod("PInvokeMethod", MethodAttributes.PinvokeImpl);
67 MethodBuilder method2 = type.DefineMethod("InstanceMethod", MethodAttributes.Public);
68 ILGenerator ilGenerator = method2.GetILGenerator();
69 ilGenerator.Emit(OpCodes.Ret);
70 MethodBuilder method3 = type.DefineMethod("StaticMethod", MethodAttributes.Static);
71 MethodBuilder method4 = type.DefineMethod("AbstractMethod", MethodAttributes.Abstract | MethodAttributes.Virtual);
72
73 eventBuilder.SetRaiseMethod(method1);
74 eventBuilder.SetRaiseMethod(method2);
75 eventBuilder.SetRaiseMethod(method3);
76 eventBuilder.SetRaiseMethod(method4);
77 }
78
79 [Fact]
80 public void SetRaiseMethod_NullMethod_ThrowsArgumentNullException()
81 {
82 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
83 EventBuilder eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(TestEventHandler));
84 AssertExtensions.Throws<ArgumentNullException>("mdBuilder", () => eventBuilder.SetRaiseMethod(null));
85 }
86
87 [Fact]
88 public void SetRaiseMethod_TypeCreated_ThrowsInvalidOperationException()
89 {
90 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
91 EventBuilder eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(TestEventHandler));
92 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Abstract | MethodAttributes.Virtual);
93 type.CreateTypeInfo().AsType();
94
95 Assert.Throws<InvalidOperationException>(() => eventBuilder.SetRaiseMethod(method));
96 }
97 }
98 }