Skip to content

Commit 992a2ca

Browse files
authored
feat: Migrate fluent assertions code-fixers to levarage IOperation (#278)
* feat: Migrate fluent assertions code-fixers to levarage IOperation
1 parent c14257a commit 992a2ca

File tree

49 files changed

+969
-1341
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+969
-1341
lines changed

src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs

Lines changed: 46 additions & 41 deletions
Large diffs are not rendered by default.

src/FluentAssertions.Analyzers.Tests/Tips/DictionaryTests.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class DictionaryTests
2121
oldAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).ContainsKey(expectedKey).Should().BeTrue({0}).And.ToString();",
2222
newAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainKey(expectedKey{0}).And.ToString();")]
2323
[Implemented]
24-
public void DictionaryShouldContainKey_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldAssertion, newAssertion);
24+
public void DictionaryShouldContainKey_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix(oldAssertion, newAssertion);
2525

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

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

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

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

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

162162
private void VerifyCSharpDiagnostic(string sourceAssersion, DiagnosticMetadata metadata)
163163
{
@@ -176,14 +176,18 @@ private void VerifyCSharpDiagnostic(string sourceAssersion, DiagnosticMetadata m
176176
});
177177
}
178178

179-
private void VerifyCSharpFix<TCodeFixProvider, TDiagnosticAnalyzer>(string oldSourceAssertion, string newSourceAssertion)
180-
where TCodeFixProvider : Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider, new()
181-
where TDiagnosticAnalyzer : Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer, new()
179+
private void VerifyCSharpFix(string oldSourceAssertion, string newSourceAssertion)
182180
{
183181
var oldSource = GenerateCode.GenericIDictionaryAssertion(oldSourceAssertion);
184182
var newSource = GenerateCode.GenericIDictionaryAssertion(newSourceAssertion);
185183

186-
DiagnosticVerifier.VerifyCSharpFix<TCodeFixProvider, TDiagnosticAnalyzer>(oldSource, newSource);
184+
DiagnosticVerifier.VerifyFix(new CodeFixVerifierArguments()
185+
.WithSources(oldSource)
186+
.WithFixedSources(newSource)
187+
.WithDiagnosticAnalyzer<FluentAssertionsOperationAnalyzer>()
188+
.WithCodeFixProvider<FluentAssertionsCodeFixProvider>()
189+
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0)
190+
);
187191
}
188192
}
189193
}

src/FluentAssertions.Analyzers.Tests/Tips/ExceptionsTests.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public class ExceptionsTests
105105
oldAssertion: "action.Should().Throw<Exception>().And.Message.Should().EndWith(\"a constant string\"{0});",
106106
newAssertion: "action.Should().Throw<Exception>().WithMessage(\"*a constant string\"{0});")]
107107
[Implemented]
108-
public void ExceptionShouldThrowWithMessage_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldAssertion, newAssertion);
108+
public void ExceptionShouldThrowWithMessage_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix(oldAssertion, newAssertion);
109109

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

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

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

282282
private void VerifyCSharpDiagnostic(string sourceAssertion, DiagnosticMetadata metadata)
283283
{
@@ -296,14 +296,18 @@ private void VerifyCSharpDiagnostic(string sourceAssertion, DiagnosticMetadata m
296296
});
297297
}
298298

299-
private void VerifyCSharpFix<TCodeFixProvider, TDiagnosticAnalyzer>(string oldSourceAssertion, string newSourceAssertion)
300-
where TCodeFixProvider : Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider, new()
301-
where TDiagnosticAnalyzer : Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer, new()
299+
private void VerifyCSharpFix(string oldSourceAssertion, string newSourceAssertion)
302300
{
303301
var oldSource = GenerateCode.ExceptionAssertion(oldSourceAssertion);
304302
var newSource = GenerateCode.ExceptionAssertion(newSourceAssertion);
305303

306-
DiagnosticVerifier.VerifyCSharpFix<TCodeFixProvider, TDiagnosticAnalyzer>(oldSource, newSource);
304+
DiagnosticVerifier.VerifyFix(new CodeFixVerifierArguments()
305+
.WithSources(oldSource)
306+
.WithFixedSources(newSource)
307+
.WithDiagnosticAnalyzer<FluentAssertionsOperationAnalyzer>()
308+
.WithCodeFixProvider<FluentAssertionsCodeFixProvider>()
309+
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0)
310+
);
307311
}
308312
}
309313
}

src/FluentAssertions.Analyzers.Tests/Tips/NumericTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ private void VerifyCSharpFix(string oldSourceAssertion, string newSourceAssertio
142142
var newSource = GenerateCode.NumericAssertion(newSourceAssertion, numericType);
143143

144144
DiagnosticVerifier.VerifyFix(new CodeFixVerifierArguments()
145-
.WithCodeFixProvider<FluentAssertionsCodeFix>()
145+
.WithCodeFixProvider<FluentAssertionsCodeFixProvider>()
146146
.WithDiagnosticAnalyzer<FluentAssertionsOperationAnalyzer>()
147147
.WithSources(oldSource)
148148
.WithFixedSources(newSource)

src/FluentAssertions.Analyzers.Tests/Tips/SanityTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ public class MyCollectionType { }";
460460
.WithSources(source)
461461
.WithFixedSources(fixedSource)
462462
.WithDiagnosticAnalyzer<FluentAssertionsOperationAnalyzer>()
463-
.WithCodeFixProvider<FluentAssertionsCodeFix>()
463+
.WithCodeFixProvider<FluentAssertionsCodeFixProvider>()
464464
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0)
465465
);
466466
}

src/FluentAssertions.Analyzers.Tests/Tips/ShouldEqualsTests.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void ShouldEquals_ShouldBe_ObjectType_TestCodeFix()
1919
var oldSource = GenerateCode.ObjectStatement("actual.Should().Equals(expected);");
2020
var newSource = GenerateCode.ObjectStatement("actual.Should().Be(expected);");
2121

22-
DiagnosticVerifier.VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldSource, newSource);
22+
VerifyFix(oldSource, newSource);
2323
}
2424

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

37-
DiagnosticVerifier.VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldSource, newSource);
37+
VerifyFix(oldSource, newSource);
3838
}
3939

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

52-
DiagnosticVerifier.VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldSource, newSource);
52+
VerifyFix(oldSource, newSource);
5353
}
5454

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

69-
DiagnosticVerifier.VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldSource, newSource);
69+
VerifyFix(oldSource, newSource);
7070
}
7171

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

79-
DiagnosticVerifier.VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldSource, newSource);
79+
VerifyFix(oldSource, newSource);
8080
}
8181

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

89-
DiagnosticVerifier.VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldSource, newSource);
89+
VerifyFix(oldSource, newSource);
9090
}
9191

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

99-
DiagnosticVerifier.VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldSource, newSource);
99+
VerifyFix(oldSource, newSource);
100100
}
101101

102102
private void VerifyCSharpDiagnosticExpressionBody(string sourceAssertion, DiagnosticMetadata metadata) => VerifyCSharpDiagnosticExpressionBody(sourceAssertion, 10, 13, metadata);
@@ -115,5 +115,14 @@ private void VerifyCSharpDiagnosticExpressionBody(string sourceAssertion, int li
115115
Severity = DiagnosticSeverity.Info
116116
});
117117
}
118+
119+
private void VerifyFix(string oldSource, string newSource)
120+
=> DiagnosticVerifier.VerifyFix(new CodeFixVerifierArguments()
121+
.WithSources(oldSource)
122+
.WithFixedSources(newSource)
123+
.WithDiagnosticAnalyzer<FluentAssertionsOperationAnalyzer>()
124+
.WithCodeFixProvider<FluentAssertionsCodeFixProvider>()
125+
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0)
126+
);
118127
}
119128
}

0 commit comments

Comments
 (0)