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

add support for xunit SetAsserts DoesNotContain #235

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
32 changes: 32 additions & 0 deletions src/FluentAssertions.Analyzers.Tests/Tips/XunitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,38 @@ public void AssertStringDoesNotContain_TestAnalyzer(string assertion) =>
public void AssertStringDoesNotContain_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix<AssertDoesNotContainCodeFix, AssertDoesNotContainAnalyzer>("string actual, string expected", oldAssertion, newAssertion);


[DataTestMethod]
[DataRow("Assert.DoesNotContain(expected, actual);", "ISet<string> actual, string expected")]
[DataRow("Assert.DoesNotContain(expected, actual);", "IReadOnlySet<string> actual, string expected")]
[DataRow("Assert.DoesNotContain(expected, actual);", "HashSet<string> actual, string expected")]
[DataRow("Assert.DoesNotContain(expected, actual);", "ImmutableHashSet<string> actual, string expected")]
[Implemented]
public void AssertSetDoesNotContain_TestAnalyzer(string assertion, string arguments) =>
VerifyCSharpDiagnostic<AssertDoesNotContainAnalyzer>(arguments, assertion);

[DataTestMethod]
[DataRow(
/* oldAssertion: */ "Assert.DoesNotContain(expected, actual);",
/* newAssertion: */ "actual.Should().NotContain(expected);",
/* arguments: */ "ISet<string> actual, string expected")]
[DataRow(
/* oldAssertion: */ "Assert.DoesNotContain(expected, actual);",
/* newAssertion: */ "actual.Should().NotContain(expected);",
/* arguments: */ "IReadOnlySet<string> actual, string expected")]
[DataRow(
/* oldAssertion: */ "Assert.DoesNotContain(expected, actual);",
/* newAssertion: */ "actual.Should().NotContain(expected);",
/* arguments: */ "HashSet<string> actual, string expected")]
[DataRow(
/* oldAssertion: */ "Assert.DoesNotContain(expected, actual);",
/* newAssertion: */ "actual.Should().NotContain(expected);",
/* arguments: */ "ImmutableHashSet<string> actual, string expected")]
[Implemented]
public void AssertSetDoesNotContain_TestCodeFix(string oldAssertion, string newAssertion, string arguments)
=> VerifyCSharpFix<AssertDoesNotContainCodeFix, AssertDoesNotContainAnalyzer>(arguments, oldAssertion, newAssertion);


[DataTestMethod]
[DataRow("Assert.Matches(expectedRegexPattern, actual);")]
[Implemented]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public class AssertDoesNotContainAnalyzer : XunitAnalyzer

protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors => new FluentAssertionsCSharpSyntaxVisitor[]
{
new AssertDoesNotContainStringSyntaxVisitor()
new AssertDoesNotContainStringSyntaxVisitor(),
new AssertDoesNotContainSetSyntaxVisitor()
};

//public static void DoesNotContain(string expectedSubstring, string? actualString)
Expand All @@ -35,6 +36,21 @@ public AssertDoesNotContainStringSyntaxVisitor() : base(
{
}
}

//public static void DoesNotContain<T>(T expected, ISet<T> actual)
//public static void DoesNotContain<T>(T expected, IReadOnlySet<T> actual)
//public static void DoesNotContain<T>(T expected, HashSet<T> actual)
//public static void DoesNotContain<T>(T expected, ImmutableHashSet<T> 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]
Expand All @@ -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}");
Expand Down