Skip to content

Commit 9ca05f6

Browse files
authored
fixed CollectionShouldNotContainProperty being too aggresive (#68)
* wip * fixed test
1 parent 27c9ec1 commit 9ca05f6

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ public class CollectionTests
9191
[AssertionDataTestMethod]
9292
[AssertionDiagnostic("actual.Any(x => x.BooleanProperty).Should().BeFalse({0});")]
9393
[AssertionDiagnostic("actual.Where(x => x.BooleanProperty).Should().BeEmpty({0});")]
94-
[AssertionDiagnostic("actual.Should().OnlyContain(x => !x.BooleanProperty{0});")]
94+
[AssertionDiagnostic("actual.Should().OnlyContain(x => !x.BooleanProperty{0});", ignore: true)]
9595
[AssertionDiagnostic("actual.AsEnumerable().Any(x => x.BooleanProperty).Should().BeFalse({0}).And.ToString();")]
9696
[AssertionDiagnostic("actual.AsEnumerable().Where(x => x.BooleanProperty).Should().BeEmpty({0}).And.ToString();")]
97-
[AssertionDiagnostic("actual.AsEnumerable().Should().OnlyContain(x => !x.BooleanProperty{0}).And.ToString();")]
97+
[AssertionDiagnostic("actual.AsEnumerable().Should().OnlyContain(x => !x.BooleanProperty{0}).And.ToString();", ignore: true)]
9898
[Implemented]
9999
public void CollectionShouldNotContainProperty_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock<CollectionShouldNotContainPropertyAnalyzer>(assertion);
100100

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,25 @@ public static void Main()
189189

190190
DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
191191
}
192+
193+
[TestMethod]
194+
[Implemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/64")]
195+
public void CollectionShouldNotContainProperty_WhenAssertionIsIdiomatic_ShouldNotTrigger()
196+
{
197+
const string source = @"
198+
using FluentAssertions;
199+
using FluentAssertions.Extensions;
200+
201+
public class TestClass
202+
{
203+
public static void Main()
204+
{
205+
var list = new[] { string.Empty };
206+
list.Should().OnlyContain(e => e.Contains(string.Empty));
207+
}
208+
}";
209+
210+
DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
211+
}
192212
}
193213
}

src/FluentAssertions.Analyzers/Tips/Collections/CollectionShouldNotContainProperty.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors
2323
{
2424
yield return new AnyShouldBeFalseSyntaxVisitor();
2525
yield return new WhereShouldBeEmptySyntaxVisitor();
26-
yield return new ShouldOnlyContainNotSyntaxVisitor();
26+
// TODO: yield return new ShouldOnlyContainNotSyntaxVisitor();
2727
}
2828
}
2929

@@ -70,10 +70,12 @@ protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression
7070

7171
return GetNewExpression(newExpression, NodeReplacement.RenameAndPrependArguments("BeEmpty", "NotContain", remove.Arguments));
7272
}
73+
/*
7374
else if (properties.VisitorName == nameof(CollectionShouldNotContainPropertyAnalyzer.ShouldOnlyContainNotSyntaxVisitor))
7475
{
7576
return GetNewExpression(expression, NodeReplacement.RenameAndNegateLambda("OnlyContain", "NotContain"));
7677
}
78+
*/
7779
throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}");
7880
}
7981
}

0 commit comments

Comments
 (0)