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
16 changes: 8 additions & 8 deletions TUnit.Analyzers.Tests/GlobalTestHooksAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ await Verifier
"""
using TUnit.Core;
using static TUnit.Core.HookType;

public class Tests
{
[BeforeEvery(Test)]
public static void {|#0:SetUp|}(string unknown)
public static void SetUp(string {|#0:unknown|})
{
}
}
Expand Down Expand Up @@ -269,11 +269,11 @@ await Verifier
"""
using TUnit.Core;
using static TUnit.Core.HookType;

public class Tests
{
[AfterEvery(Test)]
public static void {|#0:CleanUp|}(string unknown)
public static void CleanUp(string {|#0:unknown|})
{
}
}
Expand All @@ -292,11 +292,11 @@ await Verifier
"""
using TUnit.Core;
using static TUnit.Core.HookType;

public class Tests
{
[AfterEvery(Class)]
public static void {|#0:CleanUp|}(int unknown1, string unknown2)
public static void CleanUp(int {|#0:unknown1|}, string unknown2)
{
}
}
Expand All @@ -315,11 +315,11 @@ await Verifier
"""
using TUnit.Core;
using static TUnit.Core.HookType;

public class Tests
{
[AfterEvery(Assembly)]
public static void {|#0:CleanUp|}(object unknown)
public static void CleanUp(object {|#0:unknown|})
{
}
}
Expand Down
2 changes: 1 addition & 1 deletion TUnit.Analyzers.Tests/TestMethodParametersAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ await Verifier
public class MyClass
{
[Test]
public void {|#0:MyTest|}(int value)
public void MyTest(int {|#0:value|})
{
}
}
Expand Down
19 changes: 18 additions & 1 deletion TUnit.Analyzers/AssemblyTestHooksAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ private void AnalyzeSymbol(SymbolAnalysisContext context)

if (!IsAssemblyHookContextParameter(methodSymbol))
{
var firstBadParam = FindFirstUnknownParameter(methodSymbol);
context.ReportDiagnostic(Diagnostic.Create(Rules.UnknownParameters,
context.Symbol.Locations.FirstOrDefault(),
firstBadParam?.Locations.FirstOrDefault() ?? context.Symbol.Locations.FirstOrDefault(),
"empty or only contain `AssemblyHookContext` and `CancellationToken`")
);
}
Expand Down Expand Up @@ -105,4 +106,20 @@ private static bool IsAssemblyHookContextParameter(IMethodSymbol methodSymbol)

return true;
}

private static IParameterSymbol? FindFirstUnknownParameter(IMethodSymbol methodSymbol)
{
foreach (var parameter in methodSymbol.Parameters)
{
if (parameter.Type.GloballyQualified() !=
WellKnown.AttributeFullyQualifiedClasses.AssemblyHookContext.WithGlobalPrefix &&
parameter.Type.GloballyQualified() !=
WellKnown.AttributeFullyQualifiedClasses.CancellationToken.WithGlobalPrefix)
{
return parameter;
}
}

return null;
}
}
19 changes: 18 additions & 1 deletion TUnit.Analyzers/ClassHooksAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ private void AnalyzeSymbol(SymbolAnalysisContext context)

if (!IsClassHookContextParameter(methodSymbol))
{
var firstBadParam = FindFirstUnknownParameter(methodSymbol);
context.ReportDiagnostic(Diagnostic.Create(Rules.UnknownParameters,
context.Symbol.Locations.FirstOrDefault(),
firstBadParam?.Locations.FirstOrDefault() ?? context.Symbol.Locations.FirstOrDefault(),
"empty or only contain `ClassHookContext`")
);
}
Expand Down Expand Up @@ -97,4 +98,20 @@ private static bool IsClassHookContextParameter(IMethodSymbol methodSymbol)

return true;
}

private static IParameterSymbol? FindFirstUnknownParameter(IMethodSymbol methodSymbol)
{
foreach (var parameter in methodSymbol.Parameters)
{
if (parameter.Type.GloballyQualified() !=
WellKnown.AttributeFullyQualifiedClasses.ClassHookContext.WithGlobalPrefix &&
parameter.Type.GloballyQualified() !=
WellKnown.AttributeFullyQualifiedClasses.CancellationToken.WithGlobalPrefix)
{
return parameter;
}
}

return null;
}
}
20 changes: 18 additions & 2 deletions TUnit.Analyzers/GlobalTestHooksAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ private void AnalyzeSymbol(SymbolAnalysisContext context)
HookLevel.Assembly => "AssemblyHookContext",
_ => "context"
};
var firstBadParam = FindFirstUnknownParameter(methodSymbol, contextType!);
context.ReportDiagnostic(Diagnostic.Create(
Rules.HookUnknownParameters,
methodSymbol.Locations.FirstOrDefault(),
Rules.HookUnknownParameters,
firstBadParam?.Locations.FirstOrDefault() ?? methodSymbol.Locations.FirstOrDefault(),
expectedContextTypeName));
break;
}
Expand Down Expand Up @@ -182,4 +183,19 @@ private static HookParameterStatus CheckHookParameters(IMethodSymbol methodSymbo
// Anything else is unknown/invalid
return HookParameterStatus.UnknownParameters;
}

private static IParameterSymbol? FindFirstUnknownParameter(IMethodSymbol methodSymbol, string contextType)
{
foreach (var parameter in methodSymbol.Parameters)
{
var paramType = parameter.Type.GloballyQualifiedNonGeneric();
if (paramType != contextType &&
paramType != "global::System.Threading.CancellationToken")
{
return parameter;
}
}

return null;
}
}
19 changes: 18 additions & 1 deletion TUnit.Analyzers/InstanceTestHooksAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ private void AnalyzeSymbol(SymbolAnalysisContext context)

if (!IsContextParameter(methodSymbol))
{
var firstBadParam = FindFirstUnknownParameter(methodSymbol);
context.ReportDiagnostic(Diagnostic.Create(Rules.MethodMustBeParameterless,
context.Symbol.Locations.FirstOrDefault())
firstBadParam?.Locations.FirstOrDefault() ?? context.Symbol.Locations.FirstOrDefault())
);
}

Expand Down Expand Up @@ -83,4 +84,20 @@ private static bool IsContextParameter(IMethodSymbol methodSymbol)

return true;
}

private static IParameterSymbol? FindFirstUnknownParameter(IMethodSymbol methodSymbol)
{
foreach (var parameter in methodSymbol.Parameters)
{
if (parameter.Type.GloballyQualified() !=
WellKnown.AttributeFullyQualifiedClasses.TestContext.WithGlobalPrefix &&
parameter.Type.GloballyQualified() !=
WellKnown.AttributeFullyQualifiedClasses.CancellationToken.WithGlobalPrefix)
{
return parameter;
}
}

return null;
}
}
2 changes: 1 addition & 1 deletion TUnit.Analyzers/MatrixAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private void CheckMatrixErrors(SymbolAnalysisContext context, ImmutableArray<Att
if (!conversion.Exists)
{
context.ReportDiagnostic(Diagnostic.Create(Rules.WrongArgumentTypeTestData,
context.Symbol.Locations.FirstOrDefault(),
parameterSymbol.Locations.FirstOrDefault() ?? context.Symbol.Locations.FirstOrDefault(),
arrayItem.Type,
parameterSymbol.Type));

Expand Down
3 changes: 2 additions & 1 deletion TUnit.Analyzers/TestMethodParametersAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ private void AnalyzeSymbol(SymbolAnalysisContext context)

if (!methodSymbol.HasDataDrivenAttributes())
{
context.ReportDiagnostic(Diagnostic.Create(Rules.NoDataSourceProvided, methodSymbol.Locations.FirstOrDefault()));
context.ReportDiagnostic(Diagnostic.Create(Rules.NoDataSourceProvided,
parameters[0].Locations.FirstOrDefault() ?? methodSymbol.Locations.FirstOrDefault()));
}
}
}
2 changes: 1 addition & 1 deletion TUnit.Analyzers/TimeoutCancellationTokenAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private void AnalyzeSymbol(SymbolAnalysisContext context)
{
context.ReportDiagnostic(
Diagnostic.Create(Rules.MissingTimeoutCancellationTokenAttributes,
context.Symbol.Locations.FirstOrDefault())
lastParameter.Locations.FirstOrDefault() ?? context.Symbol.Locations.FirstOrDefault())
);
}
}
Expand Down
Loading