Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6
10 changes: 10 additions & 0 deletions src/Verify.TUnit.Tests/ParamsArrayTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class ParamsArrayTests
{
// TestMethodArguments holds the raw pre-binding arguments, which TUnit bundles into
// the params array at invocation time. So the argument count does not match the
// parameter count and the arguments cannot be used for snapshot naming.
[Test]
[Arguments(1, 2, 3)]
public Task ParamsArray(params int[] values) =>
Verify(values.Sum());
}
30 changes: 26 additions & 4 deletions src/Verify.TUnit/Verifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ static Task AddFile(string path)
public static void AddAttachmentEvents() =>
VerifierSettings.AddTestAttachment(AddFile);

// TestContext.Current is null outside a test, for example in a
// [Before(TestSession)] hook or in code that does not flow the test context.
// Dereferencing it there gives a bare NullReferenceException.
internal static TestDetails CurrentTestDetails()
{
var context = TestContext.Current;
if (context is null)
{
throw new("TestContext.Current is null. Verify can only be used from within a test method.");
}

return context.Metadata.TestDetails;
}

public static InnerVerifier BuildVerifier(string sourceFile, VerifySettings settings, bool useUniqueDirectory = false, int lineNumber = 0)
{
Guards.AgainstBadSourceFile(sourceFile);
Expand All @@ -28,16 +42,24 @@ public static InnerVerifier BuildVerifier(string sourceFile, VerifySettings sett
settings.UseUniqueDirectory();
}

var details = TestContext.Current!.Metadata.TestDetails;
var details = CurrentTestDetails();
var type = details.MethodMetadata.Class.Type;
var classArguments = details.TestClassArguments;
var methodArguments = details.TestMethodArguments;
var parameterNames = details.GetParameterNames();
if (!settings.HasParameters &&
(classArguments.Length > 0 ||
methodArguments.Length > 0))
{
settings.SetParameters([.. classArguments, .. methodArguments]);
settings.SetClassArgumentCount(classArguments.Length);
// Only apply when the argument count matches the parameter count. A params
// array exposes raw pre-binding arguments, which TUnit bundles at invocation
// time, so the counts differ and parameterized snapshot naming would throw.
// MSTest and XunitV3 apply the same guard.
if (classArguments.Length + methodArguments.Length == parameterNames?.Count)
{
settings.SetParameters([.. classArguments, .. methodArguments]);
settings.SetClassArgumentCount(classArguments.Length);
}
}

VerifierSettings.AssignTargetAssembly(type.Assembly);
Expand All @@ -49,7 +71,7 @@ public static InnerVerifier BuildVerifier(string sourceFile, VerifySettings sett
settings,
type.NameWithParent(),
method.Name,
details.GetParameterNames(),
parameterNames,
pathInfo,
lineNumber);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Verify.TUnit/VerifyChecks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public static class VerifyChecks
{
public static Task Run()
{
var details = TestContext.Current!.Metadata.TestDetails;
var details = Verifier.CurrentTestDetails();
var type = details.MethodMetadata.Class.Type;
VerifierSettings.AssignTargetAssembly(type.Assembly);
return InnerVerifyChecks.Run(type.Assembly);
Expand Down
Loading