Skip to content

Commit 10e62a5

Browse files
authored
Fix codefix for array length 0 or 1 (#73)
1 parent 14bfdef commit 10e62a5

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,12 @@ public class CollectionTests
309309
[Implemented]
310310
public void CollectionShouldHaveCount_CountShouldBe0_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldBeEmpty_CountShouldBe0);
311311

312+
[TestMethod]
313+
[AssertionDiagnostic("actual.Length.Should().Be(0{0});")]
314+
[AssertionDiagnostic("actual.ToArray().Length.Should().Be(0{0}).And.ToString();")]
315+
[Implemented]
316+
public void CollectionShouldHaveCount_LengthShouldBe0_TestAnalyzer(string assertion) => VerifyArrayCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldBeEmpty_LengthShouldBe0);
317+
312318
[TestMethod]
313319
[AssertionDiagnostic("actual.Count().Should().Be(1{0});")]
314320
[AssertionDiagnostic("actual.AsEnumerable().Count().Should().Be(1{0}).And.ToString();")]
@@ -317,6 +323,12 @@ public class CollectionTests
317323
[Implemented]
318324
public void CollectionShouldHaveCount_CountShouldBe1_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldContainSingle_CountShouldBe1);
319325

326+
[TestMethod]
327+
[AssertionDiagnostic("actual.Length.Should().Be(1{0});")]
328+
[AssertionDiagnostic("actual.ToArray().Length.Should().Be(1{0}).And.ToString();")]
329+
[Implemented]
330+
public void CollectionShouldHaveCount_ArrayLengthShouldBe1_TestAnalyzer(string assertion) => VerifyArrayCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldContainSingle_LengthShouldBe1);
331+
320332
[TestMethod]
321333
[AssertionDiagnostic("(array.Count() + 1).Should().Be(0{0}).And.ToString();")]
322334
[AssertionDiagnostic("(array.Count() + 1).Should().Be(1{0}).And.ToString();")]
@@ -394,9 +406,15 @@ public void CollectionShouldHaveCount_LengthShouldBe_TestNoAnalyzer(string asser
394406
[AssertionCodeFix(
395407
oldAssertion: "actual.ToList().Count.Should().Be(0{0});",
396408
newAssertion: "actual.ToList().Should().BeEmpty({0});")]
409+
[AssertionCodeFix(
410+
oldAssertion: "actual.ToArray().Length.Should().Be(0{0});",
411+
newAssertion: "actual.ToArray().Should().BeEmpty({0});")]
397412
[AssertionCodeFix(
398413
oldAssertion: "actual.ToList().Count.Should().Be(1{0});",
399414
newAssertion: "actual.ToList().Should().ContainSingle({0});")]
415+
[AssertionCodeFix(
416+
oldAssertion: "actual.ToArray().Length.Should().Be(1{0});",
417+
newAssertion: "actual.ToArray().Should().ContainSingle({0});")]
400418
[AssertionCodeFix(
401419
oldAssertion: "actual.ToList().Count.Should().Be(6{0});",
402420
newAssertion: "actual.ToList().Should().HaveCount(6{0});")]

src/AwesomeAssertions.Analyzers/Tips/AwesomeAssertionsAnalyzer.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,18 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, AwesomeA
339339
switch (propertyBeforeShould.Property.Name)
340340
{
341341
case nameof(Array.Length) when propertyBeforeShould.IsContainedInType(SpecialType.System_Array) && propertyBeforeShould.Instance.Type is IArrayTypeSymbol { Rank: 1 }:
342-
context.ReportDiagnostic(CreateDiagnostic(assertion, DiagnosticMetadata.CollectionShouldHaveCount_LengthShouldBe));
342+
if (assertion.Arguments[0].IsLiteralValue(1))
343+
{
344+
context.ReportDiagnostic(CreateDiagnostic(assertion, DiagnosticMetadata.CollectionShouldContainSingle_LengthShouldBe1));
345+
}
346+
else if (assertion.Arguments[0].IsLiteralValue(0))
347+
{
348+
context.ReportDiagnostic(CreateDiagnostic(assertion, DiagnosticMetadata.CollectionShouldBeEmpty_LengthShouldBe0));
349+
}
350+
else
351+
{
352+
context.ReportDiagnostic(CreateDiagnostic(assertion, DiagnosticMetadata.CollectionShouldHaveCount_LengthShouldBe));
353+
}
343354
return;
344355
case nameof(string.Length) when propertyBeforeShould.IsContainedInType(SpecialType.System_String):
345356
context.ReportDiagnostic(CreateDiagnostic(assertion, DiagnosticMetadata.StringShouldHaveLength_LengthShouldBe));

src/AwesomeAssertions.Analyzers/Tips/AwesomeAssertionsCodeFixProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,14 @@ CreateChangedDocument RemoveExpressionBeforeShouldAndRenameAssertionWithoutFirst
134134
}
135135
]);
136136
case nameof(DiagnosticMetadata.CollectionShouldBeEmpty_CountPropertyShouldBe0):
137+
case nameof(DiagnosticMetadata.CollectionShouldBeEmpty_LengthShouldBe0):
137138
return RemoveExpressionBeforeShouldAndRenameAssertionWithoutFirstArgumentWithArgumentsFromRemoved("BeEmpty");
138139
case nameof(DiagnosticMetadata.CollectionShouldBeEmpty_CountShouldBe0):
139140
return RemoveMethodBeforeShouldAndRenameAssertionWithoutFirstArgumentWithArgumentsFromRemoved("BeEmpty");
140141
case nameof(DiagnosticMetadata.CollectionShouldContainSingle_CountShouldBe1):
141142
return RemoveMethodBeforeShouldAndRenameAssertionWithoutFirstArgumentWithArgumentsFromRemoved("ContainSingle");
142143
case nameof(DiagnosticMetadata.CollectionShouldContainSingle_CountPropertyShouldBe1):
144+
case nameof(DiagnosticMetadata.CollectionShouldContainSingle_LengthShouldBe1):
143145
return RemoveExpressionBeforeShouldAndRenameAssertionWithoutFirstArgumentWithArgumentsFromRemoved("ContainSingle");
144146
case nameof(DiagnosticMetadata.CollectionShouldHaveCount_CountShouldBe):
145147
return RemoveExpressionBeforeShouldAndRenameAssertion("HaveCount");

src/AwesomeAssertions.Analyzers/Tips/DiagnosticMetadata.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ private DiagnosticMetadata(string message, string helpLink, [CallerMemberName] s
3535
public static DiagnosticMetadata CollectionShouldContainSingle_ShouldHaveCount1 { get; } = new("Use .Should().ContainSingle()", GetHelpLink("Collections-15"));
3636
public static DiagnosticMetadata CollectionShouldContainSingle_CountShouldBe1 { get; } = new("Use .Should().ContainSingle()", GetHelpLink("Collections-15"));
3737
public static DiagnosticMetadata CollectionShouldContainSingle_CountPropertyShouldBe1 { get; } = new("Use .Should().ContainSingle()", GetHelpLink("Collections-15"));
38+
public static DiagnosticMetadata CollectionShouldContainSingle_LengthShouldBe1 { get; } = new("Use .Should().ContainSingle()", GetHelpLink("Collections-15"));
3839
public static DiagnosticMetadata CollectionShouldBeEmpty_ShouldHaveCount0 { get; } = new("Use .Should().BeEmpty()", GetHelpLink("Collections-16"));
3940
public static DiagnosticMetadata CollectionShouldBeEmpty_CountShouldBe0 { get; } = new("Use .Should().BeEmpty()", GetHelpLink("Collections-16"));
4041
public static DiagnosticMetadata CollectionShouldBeEmpty_CountPropertyShouldBe0 { get; } = new("Use .Should().BeEmpty()", GetHelpLink("Collections-16"));
42+
public static DiagnosticMetadata CollectionShouldBeEmpty_LengthShouldBe0 { get; } = new("Use .Should().BeEmpty()", GetHelpLink("Collections-16"));
4143
public static DiagnosticMetadata CollectionShouldHaveSameCount_ShouldHaveCountOtherCollectionCount { get; } = new("Use .Should().HaveSameCount()", GetHelpLink("Collections-17"));
4244
public static DiagnosticMetadata CollectionShouldNotHaveSameCount_CountShouldNotBeOtherCollectionCount { get; } = new("Use .Should().NotHaveSameCount()", GetHelpLink("Collections-18"));
4345
public static DiagnosticMetadata CollectionShouldContainProperty_WhereShouldNotBeEmpty { get; } = new("Use .Should().Contain()", GetHelpLink("Collections-19"));

0 commit comments

Comments
 (0)