| 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 MethodBuilderToString |
| 10 |
{ |
| 11 |
[Fact] |
| 12 |
public void ToString_AllFieldsSet() |
| 13 |
{ |
| 14 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); |
| 15 |
MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); |
| 16 |
|
| 17 |
ILGenerator ilGenerator = method.GetILGenerator(); |
| 18 |
ilGenerator.Emit(OpCodes.Ret); |
| 19 |
|
| 20 |
GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T"); |
| 21 |
GenericTypeParameterBuilder returnType = typeParameters[0]; |
| 22 |
|
| 23 |
method.SetSignature(returnType.AsType(), null, null, null, null, null); |
| 24 |
Assert.Contains(ExpectedToStrin(method), method.ToString()); |
| 25 |
} |
| 26 |
|
| 27 |
[Fact] |
| 28 |
public void ToString_NameAndAttributeSet() |
| 29 |
{ |
| 30 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); |
| 31 |
MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); |
| 32 |
|
| 33 |
Assert.Contains(ExpectedToStrin(method), method.ToString()); |
| 34 |
} |
| 35 |
|
| 36 |
[Fact] |
| 37 |
public void ToString_NameAttributeAndSignatureSetSet() |
| 38 |
{ |
| 39 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); |
| 40 |
MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); |
| 41 |
|
| 42 |
method.SetSignature(typeof(void), null, null, null, null, null); |
| 43 |
Assert.Contains(ExpectedToStrin(method), method.ToString()); |
| 44 |
} |
| 45 |
|
| 46 |
[Fact] |
| 47 |
public void ToString_NonGenericMethod() |
| 48 |
{ |
| 49 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); |
| 50 |
MethodBuilder method = type.DefineMethod("method1", MethodAttributes.Public | MethodAttributes.Static, typeof(int), new Type[0]); |
| 51 |
|
| 52 |
string toString = method.ToString(); |
| 53 |
Assert.True(toString.LastIndexOf("Name: method1") != -1 && |
| 54 |
toString.LastIndexOf("Attributes: 22") != -1 && |
| 55 |
toString.LastIndexOf("Method Signature: Length: 3") != -1 && |
| 56 |
toString.LastIndexOf("Arguments: 0") != -1 && |
| 57 |
toString.LastIndexOf("Signature:") != -1 && |
| 58 |
toString.LastIndexOf("0 0 8 0") != -1); |
| 59 |
} |
| 60 |
|
| 61 |
[Fact] |
| 62 |
public void ToString_GenericMethod() |
| 63 |
{ |
| 64 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); |
| 65 |
MethodBuilder method = type.DefineMethod("method1", MethodAttributes.Public, typeof(int), new Type[0]); |
| 66 |
method.DefineGenericParameters("T", "U", "V"); |
| 67 |
method.MakeGenericMethod(typeof(string), typeof(int), typeof(object)); |
| 68 |
|
| 69 |
string toString = method.ToString(); |
| 70 |
Assert.True(toString.LastIndexOf("Name: method1") != -1 && |
| 71 |
toString.LastIndexOf("Attributes: 6") != -1 && |
| 72 |
toString.LastIndexOf("Method Signature: Length: 4") != -1 && |
| 73 |
toString.LastIndexOf("Arguments: 0") != -1 && |
| 74 |
toString.LastIndexOf("Signature:") != -1 && |
| 75 |
toString.LastIndexOf("48 3 0 8 0") != -1); |
| 76 |
} |
| 77 |
|
| 78 |
private static string ExpectedToStrin(MethodBuilder method) |
| 79 |
{ |
| 80 |
return "Name: " + method.Name + " " + Environment.NewLine + |
| 81 |
"Attributes: " + ((int)method.Attributes).ToString() + Environment.NewLine + |
| 82 |
"Method Signature: "; |
| 83 |
} |
| 84 |
} |
| 85 |
} |