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
29 changes: 29 additions & 0 deletions TUnit.Analyzers.Tests/MethodDataSourceAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,35 @@ public void MyTest()
);
}

[Test]
public async Task Method_Data_Source_Is_Not_Flagged_When_Does_Match_Field()
{
await Verifier
.VerifyAnalyzerAsync(
"""
using TUnit.Core;
using System;
using System.Collections.Generic;

public class MyClass
{
private static readonly IEnumerable<Func<int>> TestData = new List<Func<int>>
{
() => 1,
() => 2,
() => 3
};

[MethodDataSource(nameof(TestData))]
[Test]
public void MyTest(int value)
{
}
}
"""
);
}

[Test]
public async Task Arguments_Are_Flagged_When_Does_Not_Match_Parameter_Type()
{
Expand Down
1 change: 1 addition & 0 deletions TUnit.Analyzers/Helpers/WellKnown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static class AttributeFullyQualifiedClasses
public static readonly FullyQualifiedTypeName Arguments = GetTypeName("ArgumentsAttribute");
public static readonly FullyQualifiedTypeName MethodDataSource = GetTypeName("MethodDataSourceAttribute");
public static readonly FullyQualifiedTypeName GenericMethodDataSource = GetTypeName("MethodDataSourceAttribute`1");
public static readonly FullyQualifiedTypeName InstanceMethodDataSource = GetTypeName("InstanceMethodDataSourceAttribute");
public static readonly FullyQualifiedTypeName ClassDataSource = GetTypeName("ClassDataSourceAttribute");
public static readonly FullyQualifiedTypeName ClassConstructor = GetTypeName("ClassConstructorAttribute");

Expand Down
4 changes: 2 additions & 2 deletions TUnit.Analyzers/TestDataAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,12 +425,12 @@ private void CheckMethodDataSource(SymbolAnalysisContext context,
MatchesParameters(context, argumentForMethodCallTypes, methodSymbol))
?? methodSymbols.FirstOrDefault(x => x.Name == methodName);

// If no method found, check for properties
// If no method found, check for properties or fields
if (dataSourceMethod is null)
{
var propertySymbols = (type as INamedTypeSymbol)?.GetSelfAndBaseTypes()
.SelectMany(x => x.GetMembers())
.OfType<IPropertySymbol>()
.Where(x => x is IPropertySymbol or IFieldSymbol)
.ToArray() ?? Array.Empty<IPropertySymbol>();

var dataSourceProperty = propertySymbols.FirstOrDefault(x => x.Name == methodName);
Expand Down
Loading