-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use ILVerification library instead of ilverify tool (#313)
ilverify used the same library so there's no loss of functionality / coverage. btoh not launching a separate process for each test speeds test executation time
- Loading branch information
Showing
6 changed files
with
305 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
Cecilifier.Core.Tests/Framework/Extensions/SystemReflectionMetadataExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System.Reflection.Metadata; | ||
using System.Text; | ||
using Cecilifier.Core.Tests.Framework.ILVerification; | ||
|
||
namespace Cecilifier.Core.Tests.Framework.Extensions; | ||
|
||
public static class Extensions | ||
{ | ||
public static string GetMethodSignature (this MethodDefinitionHandle handle, MetadataReader metadataReader) | ||
{ | ||
var method = metadataReader.GetMethodDefinition (handle); | ||
SignatureProvider signatureProvider = new(); | ||
StringBuilder sb = new(); | ||
var signature = method.DecodeSignature(signatureProvider, new object ()); | ||
sb.Append(metadataReader.GetString(method.Name)); | ||
sb.Append ('('); | ||
int paramIndex = 0; | ||
foreach (var typeName in signature.ParameterTypes) { | ||
if (paramIndex > 0) | ||
sb.Append (','); | ||
|
||
sb.Append (typeName); | ||
|
||
paramIndex++; | ||
} | ||
|
||
sb.Append (')'); | ||
return sb.ToString (); | ||
} | ||
|
||
public static string GetMethodDeclaringTypeFullName (this MethodDefinitionHandle handle, MetadataReader metadataReader) | ||
{ | ||
var definition = metadataReader.GetMethodDefinition (handle); | ||
var declaringType = definition.GetDeclaringType (); | ||
return declaringType.GetTypeFullName (metadataReader); | ||
} | ||
|
||
public static string GetTypeFullName (this TypeDefinitionHandle handle, MetadataReader metadataReader) | ||
{ | ||
var typeDefinition = metadataReader.GetTypeDefinition (handle); | ||
var declaringType = typeDefinition.GetDeclaringType (); | ||
|
||
var builder = new StringBuilder (); | ||
if (!declaringType.IsNil) | ||
{ | ||
builder.Append(GetTypeFullName (declaringType, metadataReader)) | ||
.Append ('+') | ||
.Append (metadataReader.GetString (typeDefinition.Name)); | ||
} else | ||
{ | ||
builder.Append(metadataReader.GetString (typeDefinition.Namespace)) | ||
.Append ('.') | ||
.Append (metadataReader.GetString (typeDefinition.Name)); | ||
} | ||
|
||
return builder.ToString (); | ||
} | ||
|
||
public static string GetTypeFullName (this TypeReferenceHandle handle, MetadataReader metadataReader) | ||
{ | ||
var typeReference = metadataReader.GetTypeReference (handle); | ||
|
||
var builder = new StringBuilder (); | ||
builder.Append (metadataReader.GetString(typeReference.Namespace)) | ||
.Append ('.') | ||
.Append (metadataReader.GetString(typeReference.Name)); | ||
|
||
return builder.ToString (); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
Cecilifier.Core.Tests/Framework/ILVerification/ILVerifierResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System.Reflection.Metadata; | ||
using Cecilifier.Core.Tests.Framework.Extensions; | ||
using System.Text; | ||
using ILVerify; | ||
|
||
namespace Cecilifier.Core.Tests.Framework.ILVerification; | ||
|
||
public class ILVerifierResult | ||
{ | ||
public readonly VerificationResult Result; | ||
public readonly string TypeFullName; | ||
public readonly string MethodSignature; | ||
|
||
public ILVerifierResult(VerificationResult result, string typeFullName, string methodSignature) | ||
{ | ||
Result = result; | ||
TypeFullName = typeFullName; | ||
MethodSignature = methodSignature; | ||
} | ||
|
||
public static ILVerifierResult From(VerificationResult r, MetadataReader metadataReader) => | ||
new( | ||
r, | ||
r.Type.IsNil | ||
? r.Method.GetMethodDeclaringTypeFullName(metadataReader) | ||
: r.Type.GetTypeFullName(metadataReader), | ||
r.Method.IsNil | ||
? string.Empty | ||
: r.Method.GetMethodSignature(metadataReader)); | ||
|
||
public string GetErrorMessage() | ||
{ | ||
var sb = new StringBuilder(); | ||
if (string.IsNullOrEmpty(MethodSignature)) | ||
sb.Append(TypeFullName); | ||
else | ||
{ | ||
sb.Append(TypeFullName); | ||
sb.Append('.'); | ||
sb.Append(MethodSignature); | ||
} | ||
|
||
sb.Append($" - {Result.Code}: "); | ||
sb.Append(Result.Message); | ||
if (Result.ErrorArguments?.Length > 0) | ||
{ | ||
sb.Append(" - "); | ||
foreach (var argument in Result.ErrorArguments) | ||
{ | ||
sb.Append(FormatArgument(argument)); | ||
} | ||
} | ||
|
||
return sb.ToString(); | ||
|
||
static string FormatArgument(ErrorArgument argument) | ||
{ | ||
if (argument.Name == "Token" && argument.Value is int token) | ||
return $" {argument.Name} 0x{token:X8}"; | ||
if (argument.Name == "Offset" && argument.Value is int offset) | ||
return $" {argument.Name} IL_{offset:X4}"; | ||
return $" {argument.Name} {argument.Value}"; | ||
} | ||
} | ||
} |
Oops, something went wrong.