Skip to content

Commit 5000541

Browse files
authored
bugfix: limit CollectionShouldHaveCount_LengthShouldBe to one dimension arrays only (#310)
1 parent ff27bf4 commit 5000541

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

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

+24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Text;
12
using FluentAssertions.Analyzers.TestUtils;
23
using Microsoft.CodeAnalysis;
34
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -320,6 +321,29 @@ public class CollectionTests
320321
[Implemented]
321322
public void CollectionShouldHaveCount_LengthShouldBe_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldHaveCount_LengthShouldBe);
322323

324+
[DataTestMethod]
325+
[AssertionDiagnostic(@"var array = new string[0, 0]; array.Length.Should().Be(0{0});")]
326+
[AssertionDiagnostic(@"var array = new string[0, 0, 0]; array.Length.Should().Be(0{0});")]
327+
[AssertionDiagnostic(@"var array = new string[0, 0, 0, 0]; array.Length.Should().Be(0{0});")]
328+
[AssertionDiagnostic(@"var array = new string[1, 1]; array.Length.Should().Be(0{0});")]
329+
[AssertionDiagnostic(@"var array = new string[2, 2]; array.Length.Should().Be(0{0});")]
330+
[AssertionDiagnostic(@"var array = new string[3, 3, 3]; array.Length.Should().Be(0{0});")]
331+
public void CollectionShouldHaveCount_LengthShouldBe_TestNoAnalyzer(string assertion) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(new StringBuilder()
332+
.AppendLine("using System;")
333+
.AppendLine("using FluentAssertions;")
334+
.AppendLine("using FluentAssertions.Extensions;")
335+
.AppendLine("namespace TestNamespace")
336+
.AppendLine("{")
337+
.AppendLine(" public class TestClass")
338+
.AppendLine(" {")
339+
.AppendLine(" public void TestMethod()")
340+
.AppendLine(" {")
341+
.AppendLine(assertion)
342+
.AppendLine(" }")
343+
.AppendLine(" }")
344+
.AppendLine("}")
345+
.ToString());
346+
323347
[DataTestMethod]
324348
[AssertionDiagnostic("actual.Should().HaveCount(expected.Count() + 1{0});")]
325349
[AssertionDiagnostic("actual.Should().HaveCount(expected.Count() + unexpected.Count(){0});")]

src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, FluentAs
336336
{
337337
switch (propertyBeforeShould.Property.Name)
338338
{
339-
case nameof(Array.Length) when propertyBeforeShould.IsContainedInType(SpecialType.System_Array):
339+
case nameof(Array.Length) when propertyBeforeShould.IsContainedInType(SpecialType.System_Array) && propertyBeforeShould.Instance.Type is IArrayTypeSymbol { Rank: 1 }:
340340
context.ReportDiagnostic(CreateDiagnostic(assertion, DiagnosticMetadata.CollectionShouldHaveCount_LengthShouldBe));
341341
return;
342342
case nameof(string.Length) when propertyBeforeShould.IsContainedInType(SpecialType.System_String):

0 commit comments

Comments
 (0)