Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add xunit Assert.InRange #281

Merged
merged 1 commit into from Jan 6, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/FluentAssertions.Analyzers.Tests/Tips/XunitTests.cs
Expand Up @@ -648,6 +648,30 @@ public void AssertIsType_TestCodeFix(string oldAssertion, string newAssertion)
public void AssertIsNotType_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix("string actual, Type expected", oldAssertion, newAssertion);

[DataTestMethod]
[DataRow("Assert.InRange(actual, low, high);")]
[Implemented]
public void AssertInRange_TestAnalyzer(string assertion)
{
VerifyCSharpDiagnostic("double actual, double low, double high", assertion);
VerifyCSharpDiagnostic("float actual, float low, float high", assertion);
VerifyCSharpDiagnostic("int actual, int low, int high", assertion);
VerifyCSharpDiagnostic("long actual, long low, long high", assertion);
}

[DataTestMethod]
[DataRow(
/* oldAssertion: */ "Assert.InRange(actual, low, high);",
/* newAssertion: */ "actual.Should().BeInRange(low, high);")]
[Implemented]
public void AssertInRange_TestCodeFix(string oldAssertion, string newAssertion)
{
VerifyCSharpFix("double actual, double low, double high", oldAssertion, newAssertion);
VerifyCSharpFix("float actual, float low, float high", oldAssertion, newAssertion);
VerifyCSharpFix("int actual, int low, int high", oldAssertion, newAssertion);
VerifyCSharpFix("long actual, long low, long high", oldAssertion, newAssertion);
}

private void VerifyCSharpDiagnostic(string methodArguments, string assertion)
{
var source = GenerateCode.XunitAssertion(methodArguments, assertion);
Expand Down
4 changes: 4 additions & 0 deletions src/FluentAssertions.Analyzers/Tips/XunitCodeFixProvider.cs
Expand Up @@ -131,6 +131,10 @@ protected override CreateChangedDocument TryComputeFix(IInvocationOperation invo

return DocumentEditorUtils.RenameMethodToSubjectShouldGenericAssertion(invocation, ImmutableArray.Create(typeOf.TypeOperand), context, "NotBeOfType", subjectIndex: 1, argumentsToRemove: [0]);
}
case "InRange" when ArgumentsCount(invocation, 3): // Assert.InRange<T>(T actual, T low, T high)
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeInRange", subjectIndex: 0, argumentsToRemove: []);
case "NotInRange" when ArgumentsCount(invocation, 3): // Assert.NotInRange<T>(T actual, T low, T high)
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "NotBeInRange", subjectIndex: 0, argumentsToRemove: []);
}
return null;
}
Expand Down