Skip to content

Commit

Permalink
xunit/xunit#2846: Optional parameters should not trigger xUnit1041
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Dec 11, 2023
1 parent 9ca8812 commit 4db8815
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public NonTestClass(object _) { }
}
}

public class SupportedNonFixtureTypes
public class SupportedNonFixtureData
{
[Theory]
[InlineData("")]
Expand Down Expand Up @@ -55,6 +55,21 @@ [Fact] public void TestMethod() {{ }}

await Verify.VerifyAnalyzerV3(source);
}

[Fact]
public async void OptionalParameter_DoesNotTrigger()
{
var source = @"
using Xunit;
public class TestClass {
public TestClass(bool value = true) { }
[Fact] public void TestMethod() { }
}";

await Verify.VerifyAnalyzer(source);
}
}

public class ClassFixtures
Expand Down
2 changes: 1 addition & 1 deletion src/xunit.analyzers/X1000/EnsureFixturesHaveASource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ bool MatchCollectionDefinition(INamedTypeSymbol symbol) =>
.Select(a => a.ConstructorArguments[0].Value as ITypeSymbol)
);
foreach (var parameter in ctors[0].Parameters.Where(p => !validConstructorArgumentTypes.Contains(p.Type)))
foreach (var parameter in ctors[0].Parameters.Where(p => !p.IsOptional && !validConstructorArgumentTypes.Contains(p.Type)))
context.ReportDiagnostic(
Diagnostic.Create(
Descriptors.X1041_EnsureFixturesHaveASource,
Expand Down

0 comments on commit 4db8815

Please sign in to comment.