Skip to content

Commit 1ea0b76

Browse files
committed
fix: preserve inferred settings during instance source conversion (#7445)
1 parent a2f4c81 commit 1ea0b76

3 files changed

Lines changed: 35 additions & 7 deletions

File tree

‎src/TUnit.Core/Attributes/TestData/GenericClassRowPartition.cs‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -268,13 +268,7 @@ internal async Task<Evaluation> EvaluateAsync(DataGeneratorMetadata dataGenerato
268268
var row = await rowFactory().ConfigureAwait(false) is { } fetched ? EvaluateWrappedFactory(fetched) : null;
269269
// Fixed declared row types infer through constructor positions, not necessarily
270270
// the first runtime value. Own-class sources can also infer from null values.
271-
var source = dataSources[index];
272-
var infers = DeclaredRowTypes.TryGetValue(source, out var argumentTypes)
273-
? argumentTypes.SequenceEqual(ClassTypeArguments)
274-
: source is MethodDataSourceAttribute { InferredClassTypeArguments: { } declared } methodSource
275-
? row is { Length: > 0 } && declared.SequenceEqual(ClassTypeArguments)
276-
&& (methodSource.ClassProvidingDataSource is null || HasNonNullValues(row))
277-
: InfersTypeArguments(row, ClassTypeArguments);
271+
var infers = RowInfers(dataSources[index], row, ClassTypeArguments);
278272
anyRowInfers |= infers;
279273
rows.Add(new EvaluatedRow(rowFactory, row, infers));
280274
}
@@ -286,6 +280,22 @@ internal async Task<Evaluation> EvaluateAsync(DataGeneratorMetadata dataGenerato
286280
}
287281
}
288282

283+
private static bool RowInfers(IDataSourceAttribute source, object?[]? row, Type[] classTypeArguments)
284+
{
285+
if (DeclaredRowTypes.TryGetValue(source, out var argumentTypes))
286+
{
287+
return argumentTypes.SequenceEqual(classTypeArguments);
288+
}
289+
290+
if (source is MethodDataSourceAttribute { InferredClassTypeArguments: { } declared } methodSource)
291+
{
292+
return row is { Length: > 0 } && declared.SequenceEqual(classTypeArguments)
293+
&& (methodSource.ClassProvidingDataSource is null || HasNonNullValues(row));
294+
}
295+
296+
return InfersTypeArguments(row, classTypeArguments);
297+
}
298+
289299
/// <summary>
290300
/// One build's evaluation, started by the first wrapper that reads it, and the number of the build's
291301
/// wrappers that have not read it yet.

‎src/TUnit.Core/Attributes/TestData/MethodDataSourceAttribute.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ internal InstanceMethodDataSourceAttribute ToInstanceVariant()
152152
converted.Arguments = Arguments;
153153
converted.SkipIfEmpty = SkipIfEmpty;
154154
converted.DeferEnumeration = DeferEnumeration;
155+
converted.FilterClassRows = FilterClassRows;
156+
converted.InferredClassTypeArguments = InferredClassTypeArguments;
155157

156158
return converted;
157159
}

‎tests/TUnit.UnitTests/GenericClassSourceRuntimeTests.cs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ namespace TUnit.UnitTests;
44

55
public class GenericClassSourceRuntimeTests
66
{
7+
[Test]
8+
public async Task Instance_Conversion_Preserves_Inferred_Source_Settings()
9+
{
10+
var source = new MethodDataSourceAttribute(typeof(GenericClassSourceRuntimeTests), "Rows");
11+
GenericClassRowPartition.ConfigureInferredSource(source, true, typeof(string));
12+
13+
var converted = source.ToInstanceVariant();
14+
await Assert.That(converted.FilterClassRows).IsTrue();
15+
await Assert.That(converted.InferredClassTypeArguments).IsEquivalentTo(new[] { typeof(string) });
16+
await Assert.That(converted.SkipIfEmpty).IsFalse();
17+
18+
// The converted external source must still discard an all-null inferred row.
19+
converted.Factory = _ => OneRow(() => Task.FromResult<object?[]?>([null]));
20+
await Assert.That((await ReadFactories(converted)).Count).IsEqualTo(0);
21+
}
22+
723
[Test]
824
[Arguments(false, 1)]
925
[Arguments(true, 0)]

0 commit comments

Comments
 (0)