From 7c76c5d437207ef1dd8987c9a366c8d360001537 Mon Sep 17 00:00:00 2001 From: Meir Blachman Date: Wed, 4 Oct 2023 09:41:34 +0300 Subject: [PATCH] add support for xunit SetAsserts DoesNotContain --- .../Tips/XunitTests.cs | 32 +++++++++++++++++++ .../Tips/Xunit/AssertDoesNotContain.cs | 19 ++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/FluentAssertions.Analyzers.Tests/Tips/XunitTests.cs b/src/FluentAssertions.Analyzers.Tests/Tips/XunitTests.cs index 410e5482..3414de25 100644 --- a/src/FluentAssertions.Analyzers.Tests/Tips/XunitTests.cs +++ b/src/FluentAssertions.Analyzers.Tests/Tips/XunitTests.cs @@ -410,6 +410,38 @@ public void AssertStringDoesNotContain_TestAnalyzer(string assertion) => public void AssertStringDoesNotContain_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix("string actual, string expected", oldAssertion, newAssertion); + + [DataTestMethod] + [DataRow("Assert.DoesNotContain(expected, actual);", "ISet actual, string expected")] + [DataRow("Assert.DoesNotContain(expected, actual);", "IReadOnlySet actual, string expected")] + [DataRow("Assert.DoesNotContain(expected, actual);", "HashSet actual, string expected")] + [DataRow("Assert.DoesNotContain(expected, actual);", "ImmutableHashSet actual, string expected")] + [Implemented] + public void AssertSetDoesNotContain_TestAnalyzer(string assertion, string arguments) => + VerifyCSharpDiagnostic(arguments, assertion); + + [DataTestMethod] + [DataRow( + /* oldAssertion: */ "Assert.DoesNotContain(expected, actual);", + /* newAssertion: */ "actual.Should().NotContain(expected);", + /* arguments: */ "ISet actual, string expected")] + [DataRow( + /* oldAssertion: */ "Assert.DoesNotContain(expected, actual);", + /* newAssertion: */ "actual.Should().NotContain(expected);", + /* arguments: */ "IReadOnlySet actual, string expected")] + [DataRow( + /* oldAssertion: */ "Assert.DoesNotContain(expected, actual);", + /* newAssertion: */ "actual.Should().NotContain(expected);", + /* arguments: */ "HashSet actual, string expected")] + [DataRow( + /* oldAssertion: */ "Assert.DoesNotContain(expected, actual);", + /* newAssertion: */ "actual.Should().NotContain(expected);", + /* arguments: */ "ImmutableHashSet actual, string expected")] + [Implemented] + public void AssertSetDoesNotContain_TestCodeFix(string oldAssertion, string newAssertion, string arguments) + => VerifyCSharpFix(arguments, oldAssertion, newAssertion); + + [DataTestMethod] [DataRow("Assert.Matches(expectedRegexPattern, actual);")] [Implemented] diff --git a/src/FluentAssertions.Analyzers/Tips/Xunit/AssertDoesNotContain.cs b/src/FluentAssertions.Analyzers/Tips/Xunit/AssertDoesNotContain.cs index ef524bed..84e4f9c2 100644 --- a/src/FluentAssertions.Analyzers/Tips/Xunit/AssertDoesNotContain.cs +++ b/src/FluentAssertions.Analyzers/Tips/Xunit/AssertDoesNotContain.cs @@ -21,7 +21,8 @@ public class AssertDoesNotContainAnalyzer : XunitAnalyzer protected override IEnumerable Visitors => new FluentAssertionsCSharpSyntaxVisitor[] { - new AssertDoesNotContainStringSyntaxVisitor() + new AssertDoesNotContainStringSyntaxVisitor(), + new AssertDoesNotContainSetSyntaxVisitor() }; //public static void DoesNotContain(string expectedSubstring, string? actualString) @@ -35,6 +36,21 @@ public AssertDoesNotContainStringSyntaxVisitor() : base( { } } + + //public static void DoesNotContain(T expected, ISet actual) + //public static void DoesNotContain(T expected, IReadOnlySet actual) + //public static void DoesNotContain(T expected, HashSet actual) + //public static void DoesNotContain(T expected, ImmutableHashSet actual) + public class AssertDoesNotContainSetSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor + { + public AssertDoesNotContainSetSyntaxVisitor() : base( + MemberValidator.ArgumentsMatch("DoesNotContain", + ArgumentValidator.Exists(), + ArgumentValidator.IsTypeOrConstructedFromTypeOrImplementsType(SpecialType.System_Collections_IEnumerable)) + ) + { + } + } } [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AssertDoesNotContainCodeFix)), Shared] @@ -49,6 +65,7 @@ protected override ExpressionSyntax GetNewExpression( switch (properties.VisitorName) { case nameof(AssertDoesNotContainAnalyzer.AssertDoesNotContainStringSyntaxVisitor): + case nameof(AssertDoesNotContainAnalyzer.AssertDoesNotContainSetSyntaxVisitor): return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "DoesNotContain", "NotContain"); default: throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}");