Debian Codegraph @

external/corefx/src/System.Reflection.Emit/tests/EventBuilder/EventBuilderSetAddOnMethod.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 EventBuilderSetAddOnMethod
10 {
11 public delegate void TestEventHandler(object sender, object arg);
12
13 [Fact]
14 public void SetAddOnMethod_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.SetAddOnMethod(method);
21 eventBuilder.SetAddOnMethod(method);
22 }
23
24
25 [Fact]
26 public void SetAddOnMethod_InstanceMethod_Twice()
27 {
28 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
29 EventBuilder eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(TestEventHandler));
30 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public);
31 ILGenerator ilGenerator = method.GetILGenerator();
32 ilGenerator.Emit(OpCodes.Ret);
33
34 eventBuilder.SetAddOnMethod(method);
35 eventBuilder.SetAddOnMethod(method);
36 }
37
38 [Fact]
39 public void SetAddOnMethod_StaticMethod_Twice()
40 {
41 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
42 EventBuilder eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(TestEventHandler));
43 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Static);
44 ILGenerator ilGenerator = method.GetILGenerator();
45 ilGenerator.Emit(OpCodes.Ret);
46
47 eventBuilder.SetAddOnMethod(method);
48 eventBuilder.SetAddOnMethod(method);
49 }
50
51 [Fact]
52 public void SetAddOnMethod_PInvokeImplMethod_Twice()
53 {
54 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
55 EventBuilder eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(TestEventHandler));
56 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.PinvokeImpl);
57
58 eventBuilder.SetAddOnMethod(method);
59 eventBuilder.SetAddOnMethod(method);
60 }
61
62 [Fact]
63 public void SetAddOnMethod_MultipleDifferentMethods()
64 {
65 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
66 EventBuilder eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(TestEventHandler));
67 MethodBuilder method1 = type.DefineMethod("PInvokeMethod", MethodAttributes.PinvokeImpl);
68 MethodBuilder method2 = type.DefineMethod("InstanceMethod", MethodAttributes.Public);
69 ILGenerator ilGenerator = method2.GetILGenerator();
70 ilGenerator.Emit(OpCodes.Ret);
71 MethodBuilder method3 = type.DefineMethod("StaticMethod", MethodAttributes.Static);
72 MethodBuilder method4 = type.DefineMethod("AbstractMethod", MethodAttributes.Abstract | MethodAttributes.Virtual);
73
74 eventBuilder.SetAddOnMethod(method1);
75 eventBuilder.SetAddOnMethod(method2);
76 eventBuilder.SetAddOnMethod(method3);
77 eventBuilder.SetAddOnMethod(method4);
78 }
79
80 [Fact]
81 public void SetAddOnMethod_NullMethod_ThrowsArgumentNullException()
82 {
83 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
84 EventBuilder eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(TestEventHandler));
85 AssertExtensions.Throws<ArgumentNullException>("mdBuilder", () => eventBuilder.SetAddOnMethod(null));
86 }
87
88 [Fact]
89 public void SetAddOnMethod_TypeCreated_ThrowsInvalidOperationException()
90 {
91 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
92 EventBuilder eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(TestEventHandler));
93 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Abstract | MethodAttributes.Virtual);
94 type.CreateTypeInfo().AsType();
95
96 Assert.Throws<InvalidOperationException>(() => eventBuilder.SetAddOnMethod(method));
97 }
98 }
99 }