Skip to content

Commit

Permalink
bugfix: limit CollectionShouldHaveCount_LengthShouldBe to one dimensi…
Browse files Browse the repository at this point in the history
…on arrays only (#310)
  • Loading branch information
Meir017 authored Feb 29, 2024
1 parent ff27bf4 commit 5000541
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text;
using FluentAssertions.Analyzers.TestUtils;
using Microsoft.CodeAnalysis;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -320,6 +321,29 @@ public class CollectionTests
[Implemented]
public void CollectionShouldHaveCount_LengthShouldBe_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldHaveCount_LengthShouldBe);

[DataTestMethod]
[AssertionDiagnostic(@"var array = new string[0, 0]; array.Length.Should().Be(0{0});")]
[AssertionDiagnostic(@"var array = new string[0, 0, 0]; array.Length.Should().Be(0{0});")]
[AssertionDiagnostic(@"var array = new string[0, 0, 0, 0]; array.Length.Should().Be(0{0});")]
[AssertionDiagnostic(@"var array = new string[1, 1]; array.Length.Should().Be(0{0});")]
[AssertionDiagnostic(@"var array = new string[2, 2]; array.Length.Should().Be(0{0});")]
[AssertionDiagnostic(@"var array = new string[3, 3, 3]; array.Length.Should().Be(0{0});")]
public void CollectionShouldHaveCount_LengthShouldBe_TestNoAnalyzer(string assertion) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(new StringBuilder()
.AppendLine("using System;")
.AppendLine("using FluentAssertions;")
.AppendLine("using FluentAssertions.Extensions;")
.AppendLine("namespace TestNamespace")
.AppendLine("{")
.AppendLine(" public class TestClass")
.AppendLine(" {")
.AppendLine(" public void TestMethod()")
.AppendLine(" {")
.AppendLine(assertion)
.AppendLine(" }")
.AppendLine(" }")
.AppendLine("}")
.ToString());

[DataTestMethod]
[AssertionDiagnostic("actual.Should().HaveCount(expected.Count() + 1{0});")]
[AssertionDiagnostic("actual.Should().HaveCount(expected.Count() + unexpected.Count(){0});")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, FluentAs
{
switch (propertyBeforeShould.Property.Name)
{
case nameof(Array.Length) when propertyBeforeShould.IsContainedInType(SpecialType.System_Array):
case nameof(Array.Length) when propertyBeforeShould.IsContainedInType(SpecialType.System_Array) && propertyBeforeShould.Instance.Type is IArrayTypeSymbol { Rank: 1 }:
context.ReportDiagnostic(CreateDiagnostic(assertion, DiagnosticMetadata.CollectionShouldHaveCount_LengthShouldBe));
return;
case nameof(string.Length) when propertyBeforeShould.IsContainedInType(SpecialType.System_String):
Expand Down

0 comments on commit 5000541

Please sign in to comment.