Skip to content

Commit

Permalink
xunit/xunit#2749: Update xUnit1028 to only trigger on Fact and Theory (
Browse files Browse the repository at this point in the history
  • Loading branch information
Vannevelj committed Jul 16, 2023
1 parent c4e83c5 commit 06027ff
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,26 @@ public ValueTask TestMethod() {

await Verify.VerifyAnalyzerV3(LanguageVersion.CSharp7, source);
}

[Theory]
[InlineData("MyTest")]
[InlineData("MyTestAttribute")]
public async void CustomTestAttribute(string attribute)
{
var sourceTemplate = @"
using Xunit;
class MyTestAttribute : FactAttribute {{ }}
public class TestClass {{
[{0}]
public int TestMethod() {{
return 0;
}}
}}";

var source = string.Format(sourceTemplate, attribute);

await Verify.VerifyAnalyzer(source);
}
}
7 changes: 4 additions & 3 deletions src/xunit.analyzers/X1000/TestMethodSupportedReturnType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ public override void AnalyzeCompilation(
CompilationStartAnalysisContext context,
XunitContext xunitContext)
{
if (xunitContext.Core.FactAttributeType is null || xunitContext.Core.TheoryAttributeType is null)
return;

context.RegisterSymbolAction(context =>
{
if (xunitContext.Core.FactAttributeType is null)
return;
if (context.Symbol is not IMethodSymbol method)
return;
if (!method.GetAttributes().Any(a => a.AttributeClass is not null && xunitContext.Core.FactAttributeType.IsAssignableFrom(a.AttributeClass)))
if (!method.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(xunitContext.Core.FactAttributeType, a.AttributeClass) || SymbolEqualityComparer.Default.Equals(xunitContext.Core.TheoryAttributeType, a.AttributeClass)))
return;
var validReturnTypes = GetValidReturnTypes(context.Compilation, xunitContext);
Expand Down

0 comments on commit 06027ff

Please sign in to comment.