Skip to content

Commit 086f180

Browse files
authored
Migrate String to IOperation (#260)
* Migrate String to IOperation * fix issue with TryGetChainedInvocationAfterAndConstraint
1 parent 2ad09e4 commit 086f180

16 files changed

+208
-485
lines changed

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

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class StringTests
1111
[AssertionDiagnostic("actual.StartsWith(expected).Should().BeTrue({0});")]
1212
[AssertionDiagnostic("actual.ToString().StartsWith(expected).Should().BeTrue({0}).And.ToString();")]
1313
[Implemented]
14-
public void StringShouldStartWith_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<StringShouldStartWithAnalyzer>(assertion);
14+
public void StringShouldStartWith_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic(assertion, DiagnosticMetadata.StringShouldStartWith_StartsWithShouldBeTrue);
1515

1616
[DataTestMethod]
1717
[AssertionCodeFix(
@@ -21,13 +21,13 @@ public class StringTests
2121
oldAssertion: "actual.ToString().StartsWith(expected).Should().BeTrue({0}).And.ToString();",
2222
newAssertion: "actual.ToString().Should().StartWith(expected{0}).And.ToString();")]
2323
[Implemented]
24-
public void StringShouldStartWith_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<StringShouldStartWithCodeFix, StringShouldStartWithAnalyzer>(oldAssertion, newAssertion);
24+
public void StringShouldStartWith_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldAssertion, newAssertion);
2525

2626
[DataTestMethod]
2727
[AssertionDiagnostic("actual.EndsWith(expected).Should().BeTrue({0});")]
2828
[AssertionDiagnostic("actual.ToString().EndsWith(expected).Should().BeTrue({0}).And.ToString();")]
2929
[Implemented]
30-
public void StringShouldEndWith_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<StringShouldEndWithAnalyzer>(assertion);
30+
public void StringShouldEndWith_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic(assertion, DiagnosticMetadata.StringShouldEndWith_EndsWithShouldBeTrue);
3131

3232
[DataTestMethod]
3333
[AssertionCodeFix(
@@ -37,18 +37,29 @@ public class StringTests
3737
oldAssertion: "actual.ToString().EndsWith(expected).Should().BeTrue({0}).And.ToString();",
3838
newAssertion: "actual.ToString().Should().EndWith(expected{0}).And.ToString();")]
3939
[Implemented]
40-
public void StringShouldEndWith_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<StringShouldEndWithCodeFix, StringShouldEndWithAnalyzer>(oldAssertion, newAssertion);
40+
public void StringShouldEndWith_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldAssertion, newAssertion);
41+
42+
[DataTestMethod]
43+
[AssertionDiagnostic("string.IsNullOrEmpty(actual).Should().BeFalse({0});")]
44+
[AssertionDiagnostic("string.IsNullOrEmpty(actual.ToString()).Should().BeFalse({0}).And.ToString();")]
45+
[Implemented]
46+
public void StringShouldNotBeNullOrEmpty_StringIsNullOrEmptyShouldBeFalse_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic(assertion, DiagnosticMetadata.StringShouldNotBeNullOrEmpty_StringIsNullOrEmptyShouldBeFalse);
47+
48+
[DataTestMethod]
49+
[AssertionDiagnostic("actual.Should().NotBeEmpty().And.NotBeNull({0});")]
50+
[AssertionDiagnostic("actual.Should().NotBeEmpty({0}).And.NotBeNull();")]
51+
[AssertionDiagnostic("actual.ToString().Should().NotBeEmpty({0}).And.NotBeNull().And.ToString();")]
52+
[AssertionDiagnostic("actual.ToString().Should().NotBeEmpty().And.NotBeNull({0}).And.ToString();")]
53+
[Implemented]
54+
public void StringShouldNotBeNullOrEmpty_StringShouldNotBeEmptyAndNotBeNull_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic(assertion, DiagnosticMetadata.StringShouldNotBeNullOrEmpty_StringShouldNotBeEmptyAndNotBeNull);
4155

4256
[DataTestMethod]
4357
[AssertionDiagnostic("actual.Should().NotBeNull().And.NotBeEmpty({0});")]
4458
[AssertionDiagnostic("actual.Should().NotBeNull({0}).And.NotBeEmpty();")]
45-
[AssertionDiagnostic("string.IsNullOrEmpty(actual).Should().BeFalse({0});")]
4659
[AssertionDiagnostic("actual.ToString().Should().NotBeNull().And.NotBeEmpty({0}).And.ToString();")]
4760
[AssertionDiagnostic("actual.ToString().Should().NotBeNull({0}).And.NotBeEmpty().And.ToString();")]
48-
[AssertionDiagnostic("actual.ToString().Should().NotBeEmpty({0}).And.NotBeNull({0}).And.ToString();")]
49-
[AssertionDiagnostic("string.IsNullOrEmpty(actual.ToString()).Should().BeFalse({0}).And.ToString();")]
5061
[Implemented]
51-
public void StringShouldNotBeNullOrEmpty_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<StringShouldNotBeNullOrEmptyAnalyzer>(assertion);
62+
public void StringShouldNotBeNullOrEmpty_StringShouldNotBeNullAndNotBeEmpty_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic(assertion, DiagnosticMetadata.StringShouldNotBeNullOrEmpty_StringShouldNotBeNullAndNotBeEmpty);
5263

5364
[DataTestMethod]
5465
[AssertionCodeFix(
@@ -82,13 +93,13 @@ public class StringTests
8293
oldAssertion: "string.IsNullOrEmpty(actual.ToString()).Should().BeFalse({0}).And.ToString();",
8394
newAssertion: "actual.ToString().Should().NotBeNullOrEmpty({0}).And.ToString();")]
8495
[Implemented]
85-
public void StringShouldNotBeNullOrEmpty_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<StringShouldNotBeNullOrEmptyCodeFix, StringShouldNotBeNullOrEmptyAnalyzer>(oldAssertion, newAssertion);
96+
public void StringShouldNotBeNullOrEmpty_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldAssertion, newAssertion);
8697

8798
[DataTestMethod]
8899
[AssertionDiagnostic("string.IsNullOrEmpty(actual).Should().BeTrue({0});")]
89100
[AssertionDiagnostic("string.IsNullOrEmpty(actual.ToString()).Should().BeTrue({0}).And.ToString();")]
90101
[Implemented]
91-
public void StringShouldBeNullOrEmpty_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<StringShouldBeNullOrEmptyAnalyzer>(assertion);
102+
public void StringShouldBeNullOrEmpty_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic(assertion, DiagnosticMetadata.StringShouldBeNullOrEmpty_StringIsNullOrEmptyShouldBeTrue);
92103

93104
[DataTestMethod]
94105
[AssertionCodeFix(
@@ -98,15 +109,15 @@ public class StringTests
98109
oldAssertion: "string.IsNullOrEmpty(actual.ToString()).Should().BeTrue({0}).And.ToString();",
99110
newAssertion: "actual.ToString().Should().BeNullOrEmpty({0}).And.ToString();")]
100111
[Implemented]
101-
public void StringShouldBeNullOrEmpty_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<StringShouldBeNullOrEmptyCodeFix, StringShouldBeNullOrEmptyAnalyzer>(oldAssertion, newAssertion);
112+
public void StringShouldBeNullOrEmpty_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldAssertion, newAssertion);
102113

103114
[DataTestMethod]
104115
[AssertionDiagnostic("string.IsNullOrWhiteSpace(actual).Should().BeTrue({0});")]
105116
[AssertionDiagnostic("string.IsNullOrWhiteSpace(actual).Should().BeTrue({0}).And.ToString();")]
106117
[AssertionDiagnostic("string.IsNullOrWhiteSpace(actual.ToString()).Should().BeTrue({0});")]
107118
[AssertionDiagnostic("string.IsNullOrWhiteSpace(actual.ToString()).Should().BeTrue({0}).And.ToString();")]
108119
[Implemented]
109-
public void StringShouldBeNullOrWhiteSpace_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<StringShouldBeNullOrWhiteSpaceAnalyzer>(assertion);
120+
public void StringShouldBeNullOrWhiteSpace_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic(assertion, DiagnosticMetadata.StringShouldBeNullOrWhiteSpace_StringIsNullOrWhiteSpaceShouldBeTrue);
110121

111122
[DataTestMethod]
112123
[AssertionCodeFix(
@@ -122,15 +133,15 @@ public class StringTests
122133
oldAssertion: "string.IsNullOrWhiteSpace(actual.ToString()).Should().BeTrue({0}).And.ToString();",
123134
newAssertion: "actual.ToString().Should().BeNullOrWhiteSpace({0}).And.ToString();")]
124135
[Implemented]
125-
public void StringShouldBeNullOrWhiteSpace_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<StringShouldBeNullOrWhiteSpaceCodeFix, StringShouldBeNullOrWhiteSpaceAnalyzer>(oldAssertion, newAssertion);
136+
public void StringShouldBeNullOrWhiteSpace_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldAssertion, newAssertion);
126137

127138
[DataTestMethod]
128139
[AssertionDiagnostic("string.IsNullOrWhiteSpace(actual).Should().BeFalse({0});")]
129140
[AssertionDiagnostic("string.IsNullOrWhiteSpace(actual).Should().BeFalse({0}).And.ToString();")]
130141
[AssertionDiagnostic("string.IsNullOrWhiteSpace(actual.ToString()).Should().BeFalse({0});")]
131142
[AssertionDiagnostic("string.IsNullOrWhiteSpace(actual.ToString()).Should().BeFalse({0}).And.ToString();")]
132143
[Implemented]
133-
public void StringShouldNotBeNullOrWhiteSpace_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<StringShouldNotBeNullOrWhiteSpaceAnalyzer>(assertion);
144+
public void StringShouldNotBeNullOrWhiteSpace_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic(assertion, DiagnosticMetadata.StringShouldNotBeNullOrWhiteSpace_StringShouldNotBeNullOrWhiteSpace);
134145

135146
[DataTestMethod]
136147
[AssertionCodeFix(
@@ -146,13 +157,13 @@ public class StringTests
146157
oldAssertion: "string.IsNullOrWhiteSpace(actual.ToString()).Should().BeFalse({0}).And.ToString();",
147158
newAssertion: "actual.ToString().Should().NotBeNullOrWhiteSpace({0}).And.ToString();")]
148159
[Implemented]
149-
public void StringShouldNotBeNullOrWhiteSpace_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<StringShouldNotBeNullOrWhiteSpaceCodeFix, StringShouldNotBeNullOrWhiteSpaceAnalyzer>(oldAssertion, newAssertion);
160+
public void StringShouldNotBeNullOrWhiteSpace_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldAssertion, newAssertion);
150161

151162
[DataTestMethod]
152163
[AssertionDiagnostic("actual.Length.Should().Be(k{0});")]
153164
[AssertionDiagnostic("actual.ToString().Length.Should().Be(k{0}).And.ToString();")]
154165
[Implemented]
155-
public void StringShouldHaveLength_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<StringShouldHaveLengthAnalyzer>(assertion);
166+
public void StringShouldHaveLength_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic(assertion, DiagnosticMetadata.StringShouldHaveLength_LengthShouldBe);
156167

157168
[DataTestMethod]
158169
[AssertionCodeFix(
@@ -162,20 +173,17 @@ public class StringTests
162173
oldAssertion: "actual.ToString().Length.Should().Be(k{0}).And.ToString();",
163174
newAssertion: "actual.ToString().Should().HaveLength(k{0}).And.ToString();")]
164175
[Implemented]
165-
public void StringShouldHaveLength_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<StringShouldHaveLengthCodeFix, StringShouldHaveLengthAnalyzer>(oldAssertion, newAssertion);
176+
public void StringShouldHaveLength_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldAssertion, newAssertion);
166177

167-
private void VerifyCSharpDiagnostic<TDiagnosticAnalyzer>(string sourceAssertion) where TDiagnosticAnalyzer : Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer, new()
178+
private void VerifyCSharpDiagnostic(string sourceAssertion, DiagnosticMetadata metadata)
168179
{
169180
var source = GenerateCode.StringAssertion(sourceAssertion);
170181

171-
var type = typeof(TDiagnosticAnalyzer);
172-
var diagnosticId = (string)type.GetField("DiagnosticId").GetValue(null);
173-
var message = (string)type.GetField("Message").GetValue(null);
174-
175182
DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source, new DiagnosticResult
176183
{
177-
Id = diagnosticId,
178-
Message = message,
184+
Id = FluentAssertionsOperationAnalyzer.DiagnosticId,
185+
Message = metadata.Message,
186+
VisitorName = metadata.Name,
179187
Locations = new DiagnosticResultLocation[]
180188
{
181189
new DiagnosticResultLocation("Test0.cs", 9, 13)

src/FluentAssertions.Analyzers/Tips/DiagnosticMetadata.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,15 @@ private DiagnosticMetadata(string message, string helpLink, [CallerMemberName] s
6060
public static DiagnosticMetadata NumericShouldBeInRange_BeLessOrEqualToAndBeGreaterOrEqualTo { get; } = new("Use .Should().BeInRange()", string.Empty);
6161
public static DiagnosticMetadata NumericShouldBeApproximately_MathAbsShouldBeLessOrEqualTo { get; } = new("Use .Should().BeApproximately()", string.Empty);
6262

63+
public static DiagnosticMetadata StringShouldStartWith_StartsWithShouldBeTrue { get; } = new("Use .Should().StartWith()", GetHelpLink("Strings-1"));
64+
public static DiagnosticMetadata StringShouldEndWith_EndsWithShouldBeTrue { get; } = new("Use .Should().EndWith()", GetHelpLink("Strings-2"));
65+
public static DiagnosticMetadata StringShouldNotBeNullOrEmpty_StringShouldNotBeNullAndNotBeEmpty { get; } = new("Use .Should().NotBeNullOrEmpty()", GetHelpLink("Strings-3"));
66+
public static DiagnosticMetadata StringShouldNotBeNullOrEmpty_StringShouldNotBeEmptyAndNotBeNull { get; } = new("Use .Should().NotBeNullOrEmpty()", GetHelpLink("Strings-3"));
67+
public static DiagnosticMetadata StringShouldBeNullOrEmpty_StringIsNullOrEmptyShouldBeTrue { get; } = new("Use .Should().BeNullOrEmpty()", GetHelpLink("Strings-4"));
68+
public static DiagnosticMetadata StringShouldNotBeNullOrEmpty_StringIsNullOrEmptyShouldBeFalse { get; } = new("Use .Should().NotBeNullOrEmpty()", GetHelpLink("Strings-5"));
69+
public static DiagnosticMetadata StringShouldBeNullOrWhiteSpace_StringIsNullOrWhiteSpaceShouldBeTrue { get; } = new("Use .Should().BeNullOrWhiteSpace()", GetHelpLink("Strings-6"));
70+
public static DiagnosticMetadata StringShouldNotBeNullOrWhiteSpace_StringShouldNotBeNullOrWhiteSpace { get; } = new("Use .Should().NotBeNullOrWhiteSpace()", GetHelpLink("Strings-7"));
71+
public static DiagnosticMetadata StringShouldHaveLength_LengthShouldBe { get; } = new("Use .Should().HaveLength()", GetHelpLink("Strings-8"));
72+
6373
private static string GetHelpLink(string id) => $"https://fluentassertions.com/tips/#{id}";
6474
}

src/FluentAssertions.Analyzers/Tips/FluentAssertionsCodeFix.CollectionShouldNotBeNullOrEmpty.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)