Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 10 additions & 1 deletion TUnit.Core.SourceGenerator/Generators/TestMetadataGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,16 @@ private static void GenerateMethodDataSourceAttribute(CodeWriter writer, Attribu
{
// MethodDataSource(string) overload
methodName = attr.ConstructorArguments[0].Value?.ToString();
targetType = typeSymbol;

// Check for MethodDataSource<T> generic variant - extract T from type arguments
if (attr.AttributeClass is { IsGenericType: true, TypeArguments.Length: > 0 })
{
targetType = attr.AttributeClass.TypeArguments[0];
}
else
{
targetType = typeSymbol;
}
}

if (string.IsNullOrEmpty(methodName))
Expand Down
24 changes: 23 additions & 1 deletion TUnit.Core/Attributes/TestData/MethodDataSourceAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ public MethodDataSourceAttribute(
}

// Compute paramTypes once to avoid repeated LINQ allocations
var paramTypes = GetParameterTypes(dataGeneratorMetadata.TestInformation?.Parameters);
// Use MembersToGenerate which contains the correct parameters for this data generator type
// (constructor params for class-level, method params for method-level)
var paramTypes = GetMemberTypes(dataGeneratorMetadata.MembersToGenerate);

// If it's IAsyncEnumerable, handle it specially
if (IsAsyncEnumerable(methodResult.GetType()))
Expand Down Expand Up @@ -325,6 +327,26 @@ public MethodDataSourceAttribute(
return types;
}

private static Type[]? GetMemberTypes(IMemberMetadata[]? members)
{
if (members == null || members.Length == 0)
{
return null;
}

var types = new Type[members.Length];
for (var i = 0; i < members.Length; i++)
{
types[i] = members[i] switch
{
ParameterMetadata param => param.Type,
PropertyMetadata prop => prop.Type,
_ => typeof(object)
};
}
return types;
}

private static bool IsAsyncEnumerable([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] Type type)
{
return type.GetInterfaces()
Expand Down
29 changes: 29 additions & 0 deletions TUnit.TestProject/Bugs/5118/AsyncClassMethodDataSourceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using TUnit.TestProject.Attributes;

namespace TUnit.TestProject.Bugs._5118;

public class AsyncTestDataSources
{
public static async IAsyncEnumerable<(int Id, string Name, DateTime CreatedAt)> GetAsyncTestData()
{
for (var i = 1; i <= 3; i++)
{
await Task.Delay(10).ConfigureAwait(false);

yield return (Id: i, Name: $"Item_{i}", CreatedAt: DateTime.UtcNow.AddDays(-i));
}
}
}

[EngineTest(ExpectedResult.Pass)]
[MethodDataSource<AsyncTestDataSources>(nameof(AsyncTestDataSources.GetAsyncTestData))]
public class AsyncClassMethodDataSourceTests(int Id, string Name, DateTime CreatedAt)
{
[Test, Timeout(5000)]
public async Task TestWithAsyncComplexData(CancellationToken token)
{
await Assert.That(Id).IsGreaterThan(0);
await Assert.That(Name).StartsWith("Item_");
await Assert.That(CreatedAt).IsLessThan(DateTime.UtcNow);
}
}
Loading