Skip to content

Commit

Permalink
leverage UnsafeAccessor
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Nov 8, 2023
1 parent f7e099a commit 31fd777
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Verify.NUnit/Verifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@

public static partial class Verifier
{
#if NET8_0_OR_GREATER
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_test")]
static extern ref Test GetTest(TestContext.TestAdapter adapter);
#else
static FieldInfo field;

static Test GetTest(TestContext.TestAdapter adapter) =>
(Test) field.GetValue(adapter)!;

static Verifier()
{
var temp = typeof(TestContext.TestAdapter)
.GetField("_test", BindingFlags.Instance | BindingFlags.NonPublic);

field = temp ?? throw new("Could not find field `_test` on TestContext.TestAdapter.");
}
#endif

static InnerVerifier BuildVerifier(string sourceFile, VerifySettings settings, bool useUniqueDirectory)
{
Expand All @@ -19,9 +27,10 @@ static InnerVerifier BuildVerifier(string sourceFile, VerifySettings settings, b
{
settings.UseUniqueDirectory();
}

var context = TestContext.CurrentContext;
var adapter = context.Test;
var test = (Test) field.GetValue(adapter)!;
var test = GetTest(adapter);
var typeInfo = test.TypeInfo;
if (typeInfo is null || test.Method is null)
{
Expand Down

0 comments on commit 31fd777

Please sign in to comment.