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

Migrate String to IOperation #260

Merged
merged 2 commits into from
Dec 16, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 32 additions & 24 deletions src/FluentAssertions.Analyzers.Tests/Tips/StringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class StringTests
[AssertionDiagnostic("actual.StartsWith(expected).Should().BeTrue({0});")]
[AssertionDiagnostic("actual.ToString().StartsWith(expected).Should().BeTrue({0}).And.ToString();")]
[Implemented]
public void StringShouldStartWith_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<StringShouldStartWithAnalyzer>(assertion);
public void StringShouldStartWith_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic(assertion, DiagnosticMetadata.StringShouldStartWith_StartsWithShouldBeTrue);

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

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

[DataTestMethod]
[AssertionCodeFix(
Expand All @@ -37,18 +37,29 @@ public class StringTests
oldAssertion: "actual.ToString().EndsWith(expected).Should().BeTrue({0}).And.ToString();",
newAssertion: "actual.ToString().Should().EndWith(expected{0}).And.ToString();")]
[Implemented]
public void StringShouldEndWith_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<StringShouldEndWithCodeFix, StringShouldEndWithAnalyzer>(oldAssertion, newAssertion);
public void StringShouldEndWith_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<FluentAssertionsCodeFix, FluentAssertionsOperationAnalyzer>(oldAssertion, newAssertion);

[DataTestMethod]
[AssertionDiagnostic("string.IsNullOrEmpty(actual).Should().BeFalse({0});")]
[AssertionDiagnostic("string.IsNullOrEmpty(actual.ToString()).Should().BeFalse({0}).And.ToString();")]
[Implemented]
public void StringShouldNotBeNullOrEmpty_StringIsNullOrEmptyShouldBeFalse_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic(assertion, DiagnosticMetadata.StringShouldNotBeNullOrEmpty_StringIsNullOrEmptyShouldBeFalse);

[DataTestMethod]
[AssertionDiagnostic("actual.Should().NotBeEmpty().And.NotBeNull({0});")]
[AssertionDiagnostic("actual.Should().NotBeEmpty({0}).And.NotBeNull();")]
[AssertionDiagnostic("actual.ToString().Should().NotBeEmpty({0}).And.NotBeNull().And.ToString();")]
[AssertionDiagnostic("actual.ToString().Should().NotBeEmpty().And.NotBeNull({0}).And.ToString();")]
[Implemented]
public void StringShouldNotBeNullOrEmpty_StringShouldNotBeEmptyAndNotBeNull_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic(assertion, DiagnosticMetadata.StringShouldNotBeNullOrEmpty_StringShouldNotBeEmptyAndNotBeNull);

[DataTestMethod]
[AssertionDiagnostic("actual.Should().NotBeNull().And.NotBeEmpty({0});")]
[AssertionDiagnostic("actual.Should().NotBeNull({0}).And.NotBeEmpty();")]
[AssertionDiagnostic("string.IsNullOrEmpty(actual).Should().BeFalse({0});")]
[AssertionDiagnostic("actual.ToString().Should().NotBeNull().And.NotBeEmpty({0}).And.ToString();")]
[AssertionDiagnostic("actual.ToString().Should().NotBeNull({0}).And.NotBeEmpty().And.ToString();")]
[AssertionDiagnostic("actual.ToString().Should().NotBeEmpty({0}).And.NotBeNull({0}).And.ToString();")]
[AssertionDiagnostic("string.IsNullOrEmpty(actual.ToString()).Should().BeFalse({0}).And.ToString();")]
[Implemented]
public void StringShouldNotBeNullOrEmpty_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<StringShouldNotBeNullOrEmptyAnalyzer>(assertion);
public void StringShouldNotBeNullOrEmpty_StringShouldNotBeNullAndNotBeEmpty_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic(assertion, DiagnosticMetadata.StringShouldNotBeNullOrEmpty_StringShouldNotBeNullAndNotBeEmpty);

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

[DataTestMethod]
[AssertionDiagnostic("string.IsNullOrEmpty(actual).Should().BeTrue({0});")]
[AssertionDiagnostic("string.IsNullOrEmpty(actual.ToString()).Should().BeTrue({0}).And.ToString();")]
[Implemented]
public void StringShouldBeNullOrEmpty_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<StringShouldBeNullOrEmptyAnalyzer>(assertion);
public void StringShouldBeNullOrEmpty_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic(assertion, DiagnosticMetadata.StringShouldBeNullOrEmpty_StringIsNullOrEmptyShouldBeTrue);

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

[DataTestMethod]
[AssertionDiagnostic("string.IsNullOrWhiteSpace(actual).Should().BeTrue({0});")]
[AssertionDiagnostic("string.IsNullOrWhiteSpace(actual).Should().BeTrue({0}).And.ToString();")]
[AssertionDiagnostic("string.IsNullOrWhiteSpace(actual.ToString()).Should().BeTrue({0});")]
[AssertionDiagnostic("string.IsNullOrWhiteSpace(actual.ToString()).Should().BeTrue({0}).And.ToString();")]
[Implemented]
public void StringShouldBeNullOrWhiteSpace_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<StringShouldBeNullOrWhiteSpaceAnalyzer>(assertion);
public void StringShouldBeNullOrWhiteSpace_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic(assertion, DiagnosticMetadata.StringShouldBeNullOrWhiteSpace_StringIsNullOrWhiteSpaceShouldBeTrue);

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

[DataTestMethod]
[AssertionDiagnostic("string.IsNullOrWhiteSpace(actual).Should().BeFalse({0});")]
[AssertionDiagnostic("string.IsNullOrWhiteSpace(actual).Should().BeFalse({0}).And.ToString();")]
[AssertionDiagnostic("string.IsNullOrWhiteSpace(actual.ToString()).Should().BeFalse({0});")]
[AssertionDiagnostic("string.IsNullOrWhiteSpace(actual.ToString()).Should().BeFalse({0}).And.ToString();")]
[Implemented]
public void StringShouldNotBeNullOrWhiteSpace_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<StringShouldNotBeNullOrWhiteSpaceAnalyzer>(assertion);
public void StringShouldNotBeNullOrWhiteSpace_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic(assertion, DiagnosticMetadata.StringShouldNotBeNullOrWhiteSpace_StringShouldNotBeNullOrWhiteSpace);

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

[DataTestMethod]
[AssertionDiagnostic("actual.Length.Should().Be(k{0});")]
[AssertionDiagnostic("actual.ToString().Length.Should().Be(k{0}).And.ToString();")]
[Implemented]
public void StringShouldHaveLength_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<StringShouldHaveLengthAnalyzer>(assertion);
public void StringShouldHaveLength_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic(assertion, DiagnosticMetadata.StringShouldHaveLength_LengthShouldBe);

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

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

var type = typeof(TDiagnosticAnalyzer);
var diagnosticId = (string)type.GetField("DiagnosticId").GetValue(null);
var message = (string)type.GetField("Message").GetValue(null);

DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source, new DiagnosticResult
{
Id = diagnosticId,
Message = message,
Id = FluentAssertionsOperationAnalyzer.DiagnosticId,
Message = metadata.Message,
VisitorName = metadata.Name,
Locations = new DiagnosticResultLocation[]
{
new DiagnosticResultLocation("Test0.cs", 9, 13)
Expand Down
10 changes: 10 additions & 0 deletions src/FluentAssertions.Analyzers/Tips/DiagnosticMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,15 @@ private DiagnosticMetadata(string message, string helpLink, [CallerMemberName] s
public static DiagnosticMetadata NumericShouldBeInRange_BeLessOrEqualToAndBeGreaterOrEqualTo { get; } = new("Use .Should().BeInRange()", string.Empty);
public static DiagnosticMetadata NumericShouldBeApproximately_MathAbsShouldBeLessOrEqualTo { get; } = new("Use .Should().BeApproximately()", string.Empty);

public static DiagnosticMetadata StringShouldStartWith_StartsWithShouldBeTrue { get; } = new("Use .Should().StartWith()", GetHelpLink("Strings-1"));
public static DiagnosticMetadata StringShouldEndWith_EndsWithShouldBeTrue { get; } = new("Use .Should().EndWith()", GetHelpLink("Strings-2"));
public static DiagnosticMetadata StringShouldNotBeNullOrEmpty_StringShouldNotBeNullAndNotBeEmpty { get; } = new("Use .Should().NotBeNullOrEmpty()", GetHelpLink("Strings-3"));
public static DiagnosticMetadata StringShouldNotBeNullOrEmpty_StringShouldNotBeEmptyAndNotBeNull { get; } = new("Use .Should().NotBeNullOrEmpty()", GetHelpLink("Strings-3"));
public static DiagnosticMetadata StringShouldBeNullOrEmpty_StringIsNullOrEmptyShouldBeTrue { get; } = new("Use .Should().BeNullOrEmpty()", GetHelpLink("Strings-4"));
public static DiagnosticMetadata StringShouldNotBeNullOrEmpty_StringIsNullOrEmptyShouldBeFalse { get; } = new("Use .Should().NotBeNullOrEmpty()", GetHelpLink("Strings-5"));
public static DiagnosticMetadata StringShouldBeNullOrWhiteSpace_StringIsNullOrWhiteSpaceShouldBeTrue { get; } = new("Use .Should().BeNullOrWhiteSpace()", GetHelpLink("Strings-6"));
public static DiagnosticMetadata StringShouldNotBeNullOrWhiteSpace_StringShouldNotBeNullOrWhiteSpace { get; } = new("Use .Should().NotBeNullOrWhiteSpace()", GetHelpLink("Strings-7"));
public static DiagnosticMetadata StringShouldHaveLength_LengthShouldBe { get; } = new("Use .Should().HaveLength()", GetHelpLink("Strings-8"));

private static string GetHelpLink(string id) => $"https://fluentassertions.com/tips/#{id}";
}

This file was deleted.

Loading
Loading