| 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 System.Collections.Generic; |
| 6 |
using Xunit; |
| 7 |
|
| 8 |
namespace System.Reflection.Emit.Tests |
| 9 |
{ |
| 10 |
public class MethodBuilderEquals |
| 11 |
{ |
| 12 |
public static IEnumerable<object[]> Equals_TestData() |
| 13 |
{ |
| 14 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); |
| 15 |
MethodBuilder method1 = type.DefineMethod("TestMethod1", MethodAttributes.Public | MethodAttributes.Static, typeof(int), new Type[0]); |
| 16 |
MethodBuilder method2 = type.DefineMethod("TestMethod2", MethodAttributes.Public | MethodAttributes.Static, typeof(int), new Type[0]); |
| 17 |
MethodBuilder method3 = type.DefineMethod("TestMethod1", MethodAttributes.Public, typeof(int), new Type[0]); |
| 18 |
|
| 19 |
MethodBuilder method4 = type.DefineMethod("TestMethod3", MethodAttributes.Public | MethodAttributes.Static); |
| 20 |
method4.SetSignature(typeof(int), new Type[] { typeof(string) }, null, null, null, null); |
| 21 |
|
| 22 |
MethodBuilder method5 = type.DefineMethod("TestMethod3", MethodAttributes.Public | MethodAttributes.Static); |
| 23 |
method5.SetSignature(typeof(int), new Type[] { typeof(object) }, null, null, null, null); |
| 24 |
|
| 25 |
MethodBuilder method6 = type.DefineMethod("TestMethod3", MethodAttributes.Public | MethodAttributes.Static); |
| 26 |
method6.SetSignature(typeof(int), new Type[] { typeof(string) }, null, null, null, null); |
| 27 |
|
| 28 |
MethodBuilder method7 = type.DefineMethod("TestMethod4", MethodAttributes.Public | MethodAttributes.Static); |
| 29 |
method7.SetSignature(typeof(int), new Type[] { typeof(string) }, null, null, null, null); |
| 30 |
method7.DefineGenericParameters(new string[] { "T", "U", "V" }); |
| 31 |
|
| 32 |
MethodBuilder method8 = type.DefineMethod("TestMethod4", MethodAttributes.Public | MethodAttributes.Static); |
| 33 |
method8.SetSignature(typeof(int), new Type[] { typeof(string) }, null, null, null, null); |
| 34 |
method8.DefineGenericParameters(new string[] { "T", "U" }); |
| 35 |
|
| 36 |
MethodBuilder method9 = type.DefineMethod("TestMethod5", MethodAttributes.Public); |
| 37 |
MethodBuilder method10 = type.DefineMethod("TestMethod5", MethodAttributes.Public); |
| 38 |
|
| 39 |
MethodBuilder method11 = type.DefineMethod("TestMethod6", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.NewSlot); |
| 40 |
MethodBuilder method12 = type.DefineMethod("TestMethod6", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.NewSlot); |
| 41 |
|
| 42 |
MethodBuilder method13 = type.DefineMethod("TestMethod7", MethodAttributes.Public, typeof(void), new Type[] { typeof(int), typeof(string) }); |
| 43 |
MethodBuilder method14 = type.DefineMethod("TestMethod7", MethodAttributes.Public, typeof(void), new Type[] { typeof(int), typeof(string) }); |
| 44 |
|
| 45 |
MethodBuilder method15 = type.DefineMethod("TestMethod8", MethodAttributes.Public, typeof(void), new Type[] { typeof(int), typeof(string) }); |
| 46 |
MethodBuilder method16 = type.DefineMethod("TestMethod8", MethodAttributes.Public, typeof(int), new Type[] { typeof(int), typeof(string) }); |
| 47 |
|
| 48 |
MethodBuilder method17 = type.DefineMethod("TestMethod9", MethodAttributes.Public, typeof(void), new Type[] { typeof(int), typeof(float) }); |
| 49 |
MethodBuilder method18 = type.DefineMethod("TestMethod9", MethodAttributes.Public, typeof(void), new Type[] { typeof(string), typeof(float) }); |
| 50 |
|
| 51 |
MethodBuilder method19 = type.DefineMethod("TestMethod10", MethodAttributes.Public, typeof(void), new Type[] { typeof(int), typeof(string) }); |
| 52 |
MethodBuilder method20 = type.DefineMethod("TestMethod10", MethodAttributes.Public, typeof(void), new Type[] { typeof(string), typeof(int) }); |
| 53 |
|
| 54 |
yield return new object[] { method1, type, false }; // Random object |
| 55 |
yield return new object[] { method1, null, false }; // Null |
| 56 |
|
| 57 |
yield return new object[] { method1, method2, false }; // Different name |
| 58 |
yield return new object[] { method1, method3, false }; // Different attributes |
| 59 |
yield return new object[] { method4, method5, false }; // Different signature |
| 60 |
yield return new object[] { method4, method6, true }; // Same signature |
| 61 |
yield return new object[] { method7, method8, false }; // Different generic parameters |
| 62 |
|
| 63 |
yield return new object[] { method9, method9, true }; // Same instance |
| 64 |
yield return new object[] { method9, method10, true }; // Same name |
| 65 |
yield return new object[] { method11, method12, true }; // Same name, same attributes |
| 66 |
yield return new object[] { method13, method14, true }; // Same name, same attributes, same parameters |
| 67 |
|
| 68 |
yield return new object[] { method15, method16, false }; // Different return type |
| 69 |
yield return new object[] { method17, method18, false }; // Different parameters |
| 70 |
yield return new object[] { method19, method20, false }; // Different parameter ordering |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
[Theory] |
| 75 |
[MemberData(nameof(Equals_TestData))] |
| 76 |
public void Equals(MethodBuilder builder, object obj, bool expected) |
| 77 |
{ |
| 78 |
Assert.Equal(expected, builder.Equals(obj)); |
| 79 |
} |
| 80 |
} |
| 81 |
} |