From 8e4acf303b0398276ff08b3daef859be6928c06e Mon Sep 17 00:00:00 2001
From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com>
Date: Sun, 1 Nov 2026 17:23:17 +0000
Subject: [PATCH 1/3] fix: bind a tuple row whole to a tuple parameter followed
by optional or params parameters (#7212)
DataUnwrapper spread a row holding only a tuple whenever the method had
more than one data parameter, so ((int, int) pair, int extra = 5) received
pair = 25 and failed with a cast error in both modes. When the tuple is
already an instance of the first data parameter's tuple type and every
later data parameter is optional or params, the tuple now binds whole and
the defaults apply. Rows with a value for every parameter, and nested
tuple rows, are spread as before.
Typed tuple sources stay rejected by TUnit0001 (follow-up #7340); an
analyzer test records that.
---
src/TUnit.Engine/Helpers/DataUnwrapper.cs | 31 +++++++
.../MethodDataSourceAnalyzerTests.cs | 31 +++++++
tests/TUnit.Engine.Tests/Issue7212Tests.cs | 23 +++++
...upleParameterWithOptionalParameterTests.cs | 86 +++++++++++++++++++
4 files changed, 171 insertions(+)
create mode 100644 tests/TUnit.Engine.Tests/Issue7212Tests.cs
create mode 100644 tests/TUnit.TestProject/Bugs/_7212/TupleParameterWithOptionalParameterTests.cs
diff --git a/src/TUnit.Engine/Helpers/DataUnwrapper.cs b/src/TUnit.Engine/Helpers/DataUnwrapper.cs
index 4194587c8ba..06e56a7e6cc 100644
--- a/src/TUnit.Engine/Helpers/DataUnwrapper.cs
+++ b/src/TUnit.Engine/Helpers/DataUnwrapper.cs
@@ -84,11 +84,17 @@ public static (object?[] Data, TestDataRowMetadata? Metadata) UnwrapWithTypesAnd
// The engine supplies CancellationToken parameters wherever they are declared, so the row
// binds to the data parameters only.
var dataParameterCount = 0;
+ var firstDataParameterIndex = -1;
var lastDataParameterIndex = -1;
for (var i = 0; i < expectedParameters.Length; i++)
{
if (expectedParameters[i].Type != typeof(CancellationToken))
{
+ if (dataParameterCount == 0)
+ {
+ firstDataParameterIndex = i;
+ }
+
dataParameterCount++;
lastDataParameterIndex = i;
}
@@ -107,6 +113,17 @@ public static (object?[] Data, TestDataRowMetadata? Metadata) UnwrapWithTypesAnd
return values;
}
+ // A tuple that is already an instance of the first data parameter's tuple type binds to it
+ // whole when every later data parameter can be omitted (optional or params).
+ var firstDataParameterType = expectedParameters[firstDataParameterIndex].Type;
+ if (dataParameterCount > 1
+ && TupleHelper.IsTupleType(firstDataParameterType)
+ && firstDataParameterType.IsInstanceOfType(values[0])
+ && RemainingDataParametersAreOmittable(expectedParameters, firstDataParameterIndex))
+ {
+ return values;
+ }
+
var elements = DataSourceHelpers.UnwrapTupleAot(values[0]);
// A trailing array data parameter collects every remaining element. Otherwise extra
@@ -120,4 +137,18 @@ public static (object?[] Data, TestDataRowMetadata? Metadata) UnwrapWithTypesAnd
Array.Copy(elements, capped, dataParameterCount);
return capped;
}
+
+ private static bool RemainingDataParametersAreOmittable(ParameterMetadata[] parameters, int firstDataParameterIndex)
+ {
+ for (var i = firstDataParameterIndex + 1; i < parameters.Length; i++)
+ {
+ var parameter = parameters[i];
+ if (parameter.Type != typeof(CancellationToken) && !parameter.IsOptional && !parameter.IsParams)
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
}
diff --git a/tests/TUnit.Analyzers.Tests/MethodDataSourceAnalyzerTests.cs b/tests/TUnit.Analyzers.Tests/MethodDataSourceAnalyzerTests.cs
index c51ab13f1b7..a7cf7a8c037 100644
--- a/tests/TUnit.Analyzers.Tests/MethodDataSourceAnalyzerTests.cs
+++ b/tests/TUnit.Analyzers.Tests/MethodDataSourceAnalyzerTests.cs
@@ -400,6 +400,37 @@ public void MyTest(int value, string value2, bool value3{{GetTimeoutCancellation
);
}
+ // An object[] row holding only the tuple binds whole (#7212), but a typed tuple row is spread
+ // before binding in source-generated mode, so TUnit0001 keeps rejecting it.
+ [Test]
+ public async Task Method_Data_Source_Is_Flagged_When_Typed_Tuple_Precedes_Optional_Parameter()
+ {
+ await Verifier.VerifyAnalyzerAsync(
+ """
+ using System.Collections.Generic;
+ using TUnit.Core;
+
+ public class MyClass
+ {
+ public static IEnumerable<(int, int)> Rows()
+ {
+ yield return (25, 26);
+ }
+
+ [{|#0:MethodDataSource(nameof(Rows))|}]
+ [Test]
+ public void MyTest((int, int) pair, int extra = 5)
+ {
+ }
+ }
+ """,
+
+ Verifier.Diagnostic(Rules.WrongArgumentTypeTestData)
+ .WithLocation(0)
+ .WithArguments("int, int", "(int, int), int")
+ );
+ }
+
[Test]
[Arguments(true)]
[Arguments(false)]
diff --git a/tests/TUnit.Engine.Tests/Issue7212Tests.cs b/tests/TUnit.Engine.Tests/Issue7212Tests.cs
new file mode 100644
index 00000000000..457dac41c6a
--- /dev/null
+++ b/tests/TUnit.Engine.Tests/Issue7212Tests.cs
@@ -0,0 +1,23 @@
+using Shouldly;
+using TUnit.Engine.Tests.Enums;
+
+namespace TUnit.Engine.Tests;
+
+///
+/// A tuple row binds whole to a tuple parameter followed by optional or params parameters (#7212).
+///
+public class Issue7212Tests(TestMode testMode) : InvokableTestBase(testMode)
+{
+ [Test]
+ public async Task Tuple_Parameter_Followed_By_Optional_Parameters_Binds_Whole()
+ {
+ await RunTestsWithFilter(
+ "/*/TUnit.TestProject.Bugs._7212/*/*",
+ [
+ result => result.ResultSummary.Outcome.ShouldBe("Completed"),
+ result => result.ResultSummary.Counters.Total.ShouldBe(7),
+ result => result.ResultSummary.Counters.Passed.ShouldBe(7),
+ result => result.ResultSummary.Counters.Failed.ShouldBe(0)
+ ]);
+ }
+}
diff --git a/tests/TUnit.TestProject/Bugs/_7212/TupleParameterWithOptionalParameterTests.cs b/tests/TUnit.TestProject/Bugs/_7212/TupleParameterWithOptionalParameterTests.cs
new file mode 100644
index 00000000000..678214750c3
--- /dev/null
+++ b/tests/TUnit.TestProject/Bugs/_7212/TupleParameterWithOptionalParameterTests.cs
@@ -0,0 +1,86 @@
+using TUnit.TestProject.Attributes;
+
+namespace TUnit.TestProject.Bugs._7212;
+
+///
+/// A row whose single value is a tuple of the first data parameter's type binds to that parameter
+/// whole, and the remaining optional or params parameters take their defaults, in both
+/// source-generated and reflection mode (#7212).
+///
+[EngineTest(ExpectedResult.Pass)]
+public class TupleParameterWithOptionalParameterTests
+{
+ public static IEnumerable