Skip to content

Commit 0a478e0

Browse files
authored
fixed CollectionShouldOnlyHaveUniqueItemsByComparer gives invalid code (#67)
* fixed bug * added another test for issue * nit
1 parent 198ed02 commit 0a478e0

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,6 @@ public class CollectionTests
387387
[AssertionDiagnostic("actual.Should().NotBeEmpty().And.NotBeNull({0});")]
388388
[AssertionDiagnostic("actual.AsEnumerable().Should().NotBeNull().And.NotBeEmpty({0}).And.ToString();")]
389389
[AssertionDiagnostic("actual.AsEnumerable().Should().NotBeEmpty().And.NotBeNull({0}).And.ToString();")]
390-
[AssertionDiagnostic("actual.AsEnumerable().Should().NotBeNull().And.HaveCount(2).And.NotBeEmpty({0}).And.ToString();")]
391-
[AssertionDiagnostic("actual.AsEnumerable().Should().NotBeEmpty().And.HaveCount(2).And.NotBeNull({0}).And.ToString();")]
392390
[Implemented]
393391
public void CollectionShouldNotBeNullOrEmpty_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock<CollectionShouldNotBeNullOrEmptyAnalyzer>(assertion);
394392

@@ -405,12 +403,6 @@ public class CollectionTests
405403
[AssertionCodeFix(
406404
oldAssertion: "actual.Should().NotBeEmpty({0}).And.NotBeNull();",
407405
newAssertion: "actual.Should().NotBeNullOrEmpty({0});")]
408-
[AssertionCodeFix(
409-
oldAssertion: "actual.AsEnumerable().Should().NotBeNull().And.HaveCount(2).And.NotBeEmpty({0}).And.ToString();",
410-
newAssertion: "actual.AsEnumerable().Should().NotBeNullOrEmpty({0}).And.HaveCount(2).And.ToString();")]
411-
[AssertionCodeFix(
412-
oldAssertion: "actual.AsEnumerable().Should().NotBeEmpty().And.HaveCount(2).And.NotBeNull({0}).And.ToString();",
413-
newAssertion: "actual.AsEnumerable().Should().NotBeNullOrEmpty({0}).And.HaveCount(2).And.ToString();")]
414406
[Implemented]
415407
public void CollectionShouldNotBeNullOrEmpty_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFixCodeBlock<CollectionShouldNotBeNullOrEmptyCodeFix, CollectionShouldNotBeNullOrEmptyAnalyzer>(oldAssertion, newAssertion);
416408

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,43 @@ public static void Main()
122122

123123
DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
124124
}
125+
126+
[TestMethod]
127+
[Implemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/63")]
128+
public void Collection_SelectWhereShouldOnlyHaveUniqueItems_ShouldNotTrigger()
129+
{
130+
const string source = @"
131+
using System.Linq;
132+
using FluentAssertions;
133+
using FluentAssertions.Extensions;
134+
135+
namespace TestNamespace
136+
{
137+
public class Program
138+
{
139+
public static void Main()
140+
{
141+
var list = new[] { 1, 2, 3 };
142+
143+
list.Select(e => e.ToString())
144+
.Where(e => e != string.Empty)
145+
.Should()
146+
.OnlyHaveUniqueItems();
147+
}
148+
}
149+
}";
150+
151+
DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
152+
}
153+
154+
[TestMethod]
155+
[Implemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/63")]
156+
public void StringShouldNotBeEmptyAndShouldNotBeNull_ShouldNotTrigger()
157+
{
158+
const string assertion = "actual.Should().NotBeEmpty().And.Should().NotBeNull();";
159+
var source = GenerateCode.StringAssertion(assertion);
160+
161+
DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
162+
}
125163
}
126-
}
164+
}

src/FluentAssertions.Analyzers/Utilities/FluentAssertionsCSharpSyntaxVisitor.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ public class FluentAssertionsCSharpSyntaxVisitor : CSharpSyntaxVisitor
1313

1414
public ImmutableStack<MemberValidator> AllMembers { get; }
1515
public ImmutableStack<MemberValidator> Members { get; private set; }
16-
16+
1717
public virtual bool IsValid(ExpressionSyntax expression) => Members.IsEmpty;
18-
18+
1919
public virtual ImmutableDictionary<string, string> ToDiagnosticProperties() => ImmutableDictionary<string, string>.Empty
2020
.Add(Constants.DiagnosticProperties.VisitorName, GetType().Name)
2121
.ToImmutableDictionary();
@@ -48,6 +48,10 @@ public override void VisitMemberAccessExpression(MemberAccessExpressionSyntax no
4848
{
4949
Members = Members.Pop();
5050
}
51+
else
52+
{
53+
Members = AllMembers;
54+
}
5155
}
5256
else if (node.Parent is MemberAccessExpressionSyntax memberAccess && memberAccess.IsKind(SyntaxKind.SimpleMemberAccessExpression)
5357
&& name == Members.Peek().Name)

0 commit comments

Comments
 (0)