-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add nunit boolean assertions (#291)
- Loading branch information
Showing
6 changed files
with
181 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/FluentAssertions.Analyzers.Tests/Tips/NunitTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using FluentAssertions.Analyzers.TestUtils; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace FluentAssertions.Analyzers.Tests.Tips; | ||
|
||
[TestClass] | ||
public class NunitTests | ||
{ | ||
|
||
[DataTestMethod] | ||
[AssertionDiagnostic("Assert.True(actual{0});")] | ||
[AssertionDiagnostic("Assert.True(bool.Parse(\"true\"){0});")] | ||
[AssertionDiagnostic("Assert.IsTrue(actual{0});")] | ||
[AssertionDiagnostic("Assert.IsTrue(bool.Parse(\"true\"){0});")] | ||
[Implemented] | ||
public void AssertTrue_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic("bool actual", assertion); | ||
|
||
[DataTestMethod] | ||
[AssertionCodeFix( | ||
oldAssertion: "Assert.True(actual{0});", | ||
newAssertion: "actual.Should().BeTrue({0});")] | ||
[AssertionCodeFix( | ||
oldAssertion: "Assert.True(bool.Parse(\"true\"){0});", | ||
newAssertion: "bool.Parse(\"true\").Should().BeTrue({0});")] | ||
[AssertionCodeFix( | ||
oldAssertion: "Assert.True(!actual{0});", | ||
newAssertion: "(!actual).Should().BeTrue({0});")] | ||
[AssertionCodeFix( | ||
oldAssertion: "Assert.True(actual == false{0});", | ||
newAssertion: "(actual == false).Should().BeTrue({0});")] | ||
[AssertionCodeFix( | ||
oldAssertion: "Assert.IsTrue(actual{0});", | ||
newAssertion: "actual.Should().BeTrue({0});")] | ||
[AssertionCodeFix( | ||
oldAssertion: "Assert.IsTrue(bool.Parse(\"true\"){0});", | ||
newAssertion: "bool.Parse(\"true\").Should().BeTrue({0});")] | ||
[AssertionCodeFix( | ||
oldAssertion: "Assert.IsTrue(!actual{0});", | ||
newAssertion: "(!actual).Should().BeTrue({0});")] | ||
[AssertionCodeFix( | ||
oldAssertion: "Assert.IsTrue(actual == false{0});", | ||
newAssertion: "(actual == false).Should().BeTrue({0});")] | ||
[Implemented] | ||
public void AssertTrue_TestCodeFix(string oldAssertion, string newAssertion) | ||
=> VerifyCSharpFix("bool actual", oldAssertion, newAssertion); | ||
|
||
[DataTestMethod] | ||
[AssertionDiagnostic("Assert.False(actual{0});")] | ||
[AssertionDiagnostic("Assert.False(bool.Parse(\"false\"){0});")] | ||
[AssertionDiagnostic("Assert.IsFalse(actual{0});")] | ||
[AssertionDiagnostic("Assert.IsFalse(bool.Parse(\"false\"){0});")] | ||
[Implemented] | ||
public void AssertFalse_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic("bool actual", assertion); | ||
|
||
[DataTestMethod] | ||
[AssertionCodeFix( | ||
oldAssertion: "Assert.False(actual{0});", | ||
newAssertion: "actual.Should().BeFalse({0});")] | ||
[AssertionCodeFix( | ||
oldAssertion: "Assert.False(bool.Parse(\"false\"){0});", | ||
newAssertion: "bool.Parse(\"false\").Should().BeFalse({0});")] | ||
[AssertionCodeFix( | ||
oldAssertion: "Assert.IsFalse(actual{0});", | ||
newAssertion: "actual.Should().BeFalse({0});")] | ||
[AssertionCodeFix( | ||
oldAssertion: "Assert.IsFalse(bool.Parse(\"false\"){0});", | ||
newAssertion: "bool.Parse(\"false\").Should().BeFalse({0});")] | ||
[Implemented] | ||
public void AssertFalse_TestCodeFix(string oldAssertion, string newAssertion) | ||
=> VerifyCSharpFix("bool actual", oldAssertion, newAssertion); | ||
|
||
private void VerifyCSharpDiagnostic(string methodArguments, string assertion) | ||
{ | ||
var source = GenerateCode.Nunit3Assertion(methodArguments, assertion); | ||
DiagnosticVerifier.VerifyDiagnostic(new DiagnosticVerifierArguments() | ||
.WithAllAnalyzers() | ||
.WithSources(source) | ||
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0, PackageReference.Nunit_3_14_0) | ||
.WithExpectedDiagnostics(new DiagnosticResult | ||
{ | ||
Id = AssertAnalyzer.NUnitRule.Id, | ||
Message = AssertAnalyzer.Message, | ||
Locations = new DiagnosticResultLocation[] | ||
{ | ||
new("Test0.cs", 15, 13) | ||
}, | ||
Severity = DiagnosticSeverity.Info | ||
}) | ||
); | ||
} | ||
|
||
private void VerifyCSharpFix(string methodArguments, string oldAssertion, string newAssertion) | ||
{ | ||
var oldSource = GenerateCode.Nunit3Assertion(methodArguments, oldAssertion); | ||
var newSource = GenerateCode.Nunit3Assertion(methodArguments, newAssertion); | ||
|
||
DiagnosticVerifier.VerifyFix(new CodeFixVerifierArguments() | ||
.WithDiagnosticAnalyzer<AssertAnalyzer>() | ||
.WithCodeFixProvider<NunitCodeFixProvider>() | ||
.WithSources(oldSource) | ||
.WithFixedSources(newSource) | ||
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0, PackageReference.Nunit_3_14_0) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/FluentAssertions.Analyzers/Tips/NunitCodeFixProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.Operations; | ||
using CreateChangedDocument = System.Func<System.Threading.CancellationToken, System.Threading.Tasks.Task<Microsoft.CodeAnalysis.Document>>; | ||
using System; | ||
|
||
namespace FluentAssertions.Analyzers; | ||
|
||
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(NunitCodeFixProvider)), Shared] | ||
public class NunitCodeFixProvider : TestingFrameworkCodeFixProvider | ||
{ | ||
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(AssertAnalyzer.NUnitRule.Id); | ||
protected override CreateChangedDocument TryComputeFixCore(IInvocationOperation invocation, CodeFixContext context, TestingFrameworkCodeFixContext t, Diagnostic diagnostic) | ||
{ | ||
var assertType = invocation.TargetMethod.ContainingType; | ||
return assertType.Name switch | ||
{ | ||
"Assert" => TryComputeFixForAssert(invocation, context, t), | ||
//"StringAssert" => TryComputeFixForStringAssert(invocation, context, testContext), | ||
//"CollectionAssert" => TryComputeFixForCollectionAssert(invocation, context, testContext), | ||
_ => null | ||
}; | ||
} | ||
|
||
private CreateChangedDocument TryComputeFixForAssert(IInvocationOperation invocation, CodeFixContext context, TestingFrameworkCodeFixContext t) | ||
{ | ||
switch (invocation.TargetMethod.Name) | ||
{ | ||
case "True": | ||
case "IsTrue": | ||
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeTrue", subjectIndex: 0, argumentsToRemove: []); | ||
|
||
case "False": | ||
case "IsFalse": | ||
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeFalse", subjectIndex: 0, argumentsToRemove: []); | ||
} | ||
|
||
return null; | ||
} | ||
} |