Skip to content

Commit

Permalink
feat: Migrate fluent assertions code-fixers to levarage IOperation (#278
Browse files Browse the repository at this point in the history
)

* feat: Migrate fluent assertions code-fixers to levarage IOperation
  • Loading branch information
Meir017 committed Jan 4, 2024
1 parent c14257a commit 992a2ca
Show file tree
Hide file tree
Showing 49 changed files with 969 additions and 1,341 deletions.
87 changes: 46 additions & 41 deletions src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs

Large diffs are not rendered by default.

24 changes: 14 additions & 10 deletions src/FluentAssertions.Analyzers.Tests/Tips/DictionaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class DictionaryTests
oldAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).ContainsKey(expectedKey).Should().BeTrue({0}).And.ToString();",
newAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainKey(expectedKey{0}).And.ToString();")]
[Implemented]
public void DictionaryShouldContainKey_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldAssertion, newAssertion);
public void DictionaryShouldContainKey_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix(oldAssertion, newAssertion);

[DataTestMethod]
[AssertionDiagnostic("actual.ContainsKey(expectedKey).Should().BeFalse({0});")]
Expand All @@ -37,7 +37,7 @@ public class DictionaryTests
oldAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).ContainsKey(expectedKey).Should().BeFalse({0}).And.ToString();",
newAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().NotContainKey(expectedKey{0}).And.ToString();")]
[Implemented]
public void DictionaryShouldNotContainKey_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldAssertion, newAssertion);
public void DictionaryShouldNotContainKey_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix(oldAssertion, newAssertion);

[DataTestMethod]
[AssertionDiagnostic("actual.ContainsValue(expectedValue).Should().BeTrue({0});")]
Expand All @@ -53,7 +53,7 @@ public class DictionaryTests
oldAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).ContainsValue(expectedValue).Should().BeTrue({0}).And.ToString();",
newAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainValue(expectedValue{0}).And.ToString();")]
[Implemented]
public void DictionaryShouldContainValue_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldAssertion, newAssertion);
public void DictionaryShouldContainValue_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix(oldAssertion, newAssertion);

[DataTestMethod]
[AssertionDiagnostic("actual.ContainsValue(expectedValue).Should().BeFalse({0});")]
Expand All @@ -69,7 +69,7 @@ public class DictionaryTests
oldAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).ContainsValue(expectedValue).Should().BeFalse({0}).And.ToString();",
newAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().NotContainValue(expectedValue{0}).And.ToString();")]
[Implemented]
public void DictionaryShouldNotContainValue_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldAssertion, newAssertion);
public void DictionaryShouldNotContainValue_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix(oldAssertion, newAssertion);

[DataTestMethod]
[AssertionDiagnostic("actual.Should().ContainKey(expectedKey{0}).And.ContainValue(expectedValue);")]
Expand Down Expand Up @@ -113,7 +113,7 @@ public class DictionaryTests
oldAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainValue(expectedValue{0}).And.ContainKey(expectedKey).And.ToString();",
newAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().Contain(expectedKey, expectedValue{0}).And.ToString();")]
[Implemented]
public void DictionaryShouldContainKeyAndValue_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldAssertion, newAssertion);
public void DictionaryShouldContainKeyAndValue_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix(oldAssertion, newAssertion);

[DataTestMethod]
[AssertionDiagnostic("actual.Should().ContainKey(pair.Key{0}).And.ContainValue(pair.Value);")]
Expand Down Expand Up @@ -157,7 +157,7 @@ public class DictionaryTests
oldAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainValue(pair.Value).And.ContainKey(pair.Key{0}).And.ToString();",
newAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().Contain(pair{0}).And.ToString();")]
[Implemented]
public void DictionaryShouldContainPair_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldAssertion, newAssertion);
public void DictionaryShouldContainPair_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix(oldAssertion, newAssertion);

private void VerifyCSharpDiagnostic(string sourceAssersion, DiagnosticMetadata metadata)
{
Expand All @@ -176,14 +176,18 @@ private void VerifyCSharpDiagnostic(string sourceAssersion, DiagnosticMetadata m
});
}

private void VerifyCSharpFix<TCodeFixProvider, TDiagnosticAnalyzer>(string oldSourceAssertion, string newSourceAssertion)
where TCodeFixProvider : Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider, new()
where TDiagnosticAnalyzer : Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer, new()
private void VerifyCSharpFix(string oldSourceAssertion, string newSourceAssertion)
{
var oldSource = GenerateCode.GenericIDictionaryAssertion(oldSourceAssertion);
var newSource = GenerateCode.GenericIDictionaryAssertion(newSourceAssertion);

DiagnosticVerifier.VerifyCSharpFix<TCodeFixProvider, TDiagnosticAnalyzer>(oldSource, newSource);
DiagnosticVerifier.VerifyFix(new CodeFixVerifierArguments()
.WithSources(oldSource)
.WithFixedSources(newSource)
.WithDiagnosticAnalyzer<FluentAssertionsOperationAnalyzer>()
.WithCodeFixProvider<FluentAssertionsCodeFixProvider>()
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0)
);
}
}
}
20 changes: 12 additions & 8 deletions src/FluentAssertions.Analyzers.Tests/Tips/ExceptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public class ExceptionsTests
oldAssertion: "action.Should().Throw<Exception>().And.Message.Should().EndWith(\"a constant string\"{0});",
newAssertion: "action.Should().Throw<Exception>().WithMessage(\"*a constant string\"{0});")]
[Implemented]
public void ExceptionShouldThrowWithMessage_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldAssertion, newAssertion);
public void ExceptionShouldThrowWithMessage_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix(oldAssertion, newAssertion);

[DataTestMethod]
[AssertionDiagnostic("action.Should().ThrowExactly<Exception>().And.Message.Should().Be(expectedMessage{0});")]
Expand Down Expand Up @@ -205,7 +205,7 @@ public class ExceptionsTests
oldAssertion: "action.Should().ThrowExactly<Exception>().And.Message.Should().EndWith(\"a constant string\"{0});",
newAssertion: "action.Should().ThrowExactly<Exception>().WithMessage(\"*a constant string\"{0});")]
[Implemented]
public void ExceptionShouldThrowExactlyWithMessage_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldAssertion, newAssertion);
public void ExceptionShouldThrowExactlyWithMessage_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix(oldAssertion, newAssertion);

[DataTestMethod]
[AssertionDiagnostic("action.Should().ThrowExactly<Exception>().And.InnerException.Should().BeOfType<ArgumentException>({0});")]
Expand Down Expand Up @@ -241,7 +241,7 @@ public class ExceptionsTests
oldAssertion: "action.Should().ThrowExactly<Exception>().Which.InnerException.Should().BeOfType<ArgumentException>({0});",
newAssertion: "action.Should().ThrowExactly<Exception>().WithInnerExceptionExactly<ArgumentException>({0});")]
[Implemented]
public void ExceptionShouldThrowWithInnerExceptionExactly_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldAssertion, newAssertion);
public void ExceptionShouldThrowWithInnerExceptionExactly_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix(oldAssertion, newAssertion);

[DataTestMethod]
[AssertionDiagnostic("action.Should().ThrowExactly<Exception>().And.InnerException.Should().BeAssignableTo<ArgumentException>({0});")]
Expand Down Expand Up @@ -277,7 +277,7 @@ public class ExceptionsTests
oldAssertion: "action.Should().ThrowExactly<Exception>().Which.InnerException.Should().BeAssignableTo<ArgumentException>({0});",
newAssertion: "action.Should().ThrowExactly<Exception>().WithInnerException<ArgumentException>({0});")]
[Implemented]
public void ExceptionShouldThrowWithInnerException_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldAssertion, newAssertion);
public void ExceptionShouldThrowWithInnerException_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix(oldAssertion, newAssertion);

private void VerifyCSharpDiagnostic(string sourceAssertion, DiagnosticMetadata metadata)
{
Expand All @@ -296,14 +296,18 @@ private void VerifyCSharpDiagnostic(string sourceAssertion, DiagnosticMetadata m
});
}

private void VerifyCSharpFix<TCodeFixProvider, TDiagnosticAnalyzer>(string oldSourceAssertion, string newSourceAssertion)
where TCodeFixProvider : Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider, new()
where TDiagnosticAnalyzer : Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer, new()
private void VerifyCSharpFix(string oldSourceAssertion, string newSourceAssertion)
{
var oldSource = GenerateCode.ExceptionAssertion(oldSourceAssertion);
var newSource = GenerateCode.ExceptionAssertion(newSourceAssertion);

DiagnosticVerifier.VerifyCSharpFix<TCodeFixProvider, TDiagnosticAnalyzer>(oldSource, newSource);
DiagnosticVerifier.VerifyFix(new CodeFixVerifierArguments()
.WithSources(oldSource)
.WithFixedSources(newSource)
.WithDiagnosticAnalyzer<FluentAssertionsOperationAnalyzer>()
.WithCodeFixProvider<FluentAssertionsCodeFixProvider>()
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0)
);
}
}
}
2 changes: 1 addition & 1 deletion src/FluentAssertions.Analyzers.Tests/Tips/NumericTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private void VerifyCSharpFix(string oldSourceAssertion, string newSourceAssertio
var newSource = GenerateCode.NumericAssertion(newSourceAssertion, numericType);

DiagnosticVerifier.VerifyFix(new CodeFixVerifierArguments()
.WithCodeFixProvider<FluentAssertionsCodeFix>()
.WithCodeFixProvider<FluentAssertionsCodeFixProvider>()
.WithDiagnosticAnalyzer<FluentAssertionsOperationAnalyzer>()
.WithSources(oldSource)
.WithFixedSources(newSource)
Expand Down
2 changes: 1 addition & 1 deletion src/FluentAssertions.Analyzers.Tests/Tips/SanityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ public class MyCollectionType { }";
.WithSources(source)
.WithFixedSources(fixedSource)
.WithDiagnosticAnalyzer<FluentAssertionsOperationAnalyzer>()
.WithCodeFixProvider<FluentAssertionsCodeFix>()
.WithCodeFixProvider<FluentAssertionsCodeFixProvider>()
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0)
);
}
Expand Down
23 changes: 16 additions & 7 deletions src/FluentAssertions.Analyzers.Tests/Tips/ShouldEqualsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void ShouldEquals_ShouldBe_ObjectType_TestCodeFix()
var oldSource = GenerateCode.ObjectStatement("actual.Should().Equals(expected);");
var newSource = GenerateCode.ObjectStatement("actual.Should().Be(expected);");

DiagnosticVerifier.VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldSource, newSource);
VerifyFix(oldSource, newSource);
}

[TestMethod]
Expand All @@ -34,7 +34,7 @@ public void ShouldEquals_NestedInsideIfBlock_ShouldBe_ObjectType_TestCodeFix()
var oldSource = GenerateCode.ObjectStatement("if(true) { actual.Should().Equals(expected); }");
var newSource = GenerateCode.ObjectStatement("if(true) { actual.Should().Be(expected); }");

DiagnosticVerifier.VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldSource, newSource);
VerifyFix(oldSource, newSource);
}

[TestMethod]
Expand All @@ -49,7 +49,7 @@ public void ShouldEquals_NestedInsideWhileBlock_ShouldBe_ObjectType_TestCodeFix(
var oldSource = GenerateCode.ObjectStatement("while(true) { actual.Should().Equals(expected); }");
var newSource = GenerateCode.ObjectStatement("while(true) { actual.Should().Be(expected); }");

DiagnosticVerifier.VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldSource, newSource);
VerifyFix(oldSource, newSource);
}

[TestMethod]
Expand All @@ -66,7 +66,7 @@ public void ShouldEquals_ActualIsMethodInvoaction_ShouldBe_ObjectType_TestCodeFi
var oldSource = GenerateCode.ObjectStatement(methodInvocation + "ResultSupplier().Should().Equals(expected);");
var newSource = GenerateCode.ObjectStatement(methodInvocation + "ResultSupplier().Should().Be(expected);");

DiagnosticVerifier.VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldSource, newSource);
VerifyFix(oldSource, newSource);
}

[TestMethod]
Expand All @@ -76,7 +76,7 @@ public void ShouldEquals_ShouldBe_NumberType_TestCodeFix()
var oldSource = GenerateCode.DoubleAssertion("actual.Should().Equals(expected);");
var newSource = GenerateCode.DoubleAssertion("actual.Should().Be(expected);");

DiagnosticVerifier.VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldSource, newSource);
VerifyFix(oldSource, newSource);
}

[TestMethod]
Expand All @@ -86,7 +86,7 @@ public void ShouldEquals_ShouldBe_StringType_TestCodeFix()
var oldSource = GenerateCode.StringAssertion("actual.Should().Equals(expected);");
var newSource = GenerateCode.StringAssertion("actual.Should().Be(expected);");

DiagnosticVerifier.VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldSource, newSource);
VerifyFix(oldSource, newSource);
}

[TestMethod]
Expand All @@ -96,7 +96,7 @@ public void ShouldEquals_ShouldEqual_EnumerableType_TestCodeFix()
var oldSource = GenerateCode.GenericIListCodeBlockAssertion("actual.Should().Equals(expected);");
var newSource = GenerateCode.GenericIListCodeBlockAssertion("actual.Should().Equal(expected);");

DiagnosticVerifier.VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldSource, newSource);
VerifyFix(oldSource, newSource);
}

private void VerifyCSharpDiagnosticExpressionBody(string sourceAssertion, DiagnosticMetadata metadata) => VerifyCSharpDiagnosticExpressionBody(sourceAssertion, 10, 13, metadata);
Expand All @@ -115,5 +115,14 @@ private void VerifyCSharpDiagnosticExpressionBody(string sourceAssertion, int li
Severity = DiagnosticSeverity.Info
});
}

private void VerifyFix(string oldSource, string newSource)
=> DiagnosticVerifier.VerifyFix(new CodeFixVerifierArguments()
.WithSources(oldSource)
.WithFixedSources(newSource)
.WithDiagnosticAnalyzer<FluentAssertionsOperationAnalyzer>()
.WithCodeFixProvider<FluentAssertionsCodeFixProvider>()
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0)
);
}
}

0 comments on commit 992a2ca

Please sign in to comment.