Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/FluentAssertions.Analyzers.TestUtils/GenerateCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public static string GenericIListExpressionBodyAssertion(string assertion) => Ge
.AppendLine(" {")
.AppendLine(" public bool BooleanProperty { get; set; }")
.AppendLine(" public string Message { get; set; }")
.AppendLine(" public TestComplexClass this[int index] => throw new NotImplementedException();")
.AppendLine(" }")
.AppendLine()
.AppendLine("}")
.ToString();

Expand Down
7 changes: 6 additions & 1 deletion src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ public void CollectionShouldContainSingle_TestAnalyzer_GenericIEnumerableShouldR
[AssertionDiagnostic("actual.ElementAt(k).BooleanProperty.Should().Be(expectedItem.BooleanProperty{0});")]
[AssertionDiagnostic("actual.ElementAt(6).BooleanProperty.Should().Be(expectedItem.BooleanProperty{0});")]
[AssertionDiagnostic("actual.AsEnumerable().ElementAt(k).BooleanProperty.Should().Be(expectedItem.BooleanProperty{0}).And.ToString();")]
[AssertionDiagnostic("actual.AsEnumerable().ElementAt(6).BooleanProperty.Should().Be(expectedItem.BooleanProperty{0}).And.ToString();")]
[AssertionDiagnostic("actual.AsEnumerable().ElementAt(6).BooleanProperty.Should().Be(expectedItem.BooleanProperty{0}).And.ToString();")]
[Implemented]
public void CollectionShouldHaveElementAt_ElementAtIndexShouldBe_TestNoAnalyzer(string assertion) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(GenerateCode.GenericIListCodeBlockAssertion(assertion));

Expand All @@ -793,6 +793,11 @@ public void CollectionShouldContainSingle_TestAnalyzer_GenericIEnumerableShouldR
[Implemented]
public void CollectionShouldHaveElementAt_IndexerShouldBe_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldHaveElementAt_IndexerShouldBe);

[DataTestMethod]
[AssertionDiagnostic("var first = actual[0]; first[6].Should().Be(expectedItem{0});")]
[Implemented]
public void CollectionShouldHaveElementAt_IndexerShouldBe_TestNoAnalyzer(string assertion) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(GenerateCode.GenericIListCodeBlockAssertion(assertion));

[DataTestMethod]
[AssertionDiagnostic("actual.Skip(k).First().Should().Be(expectedItem{0});")]
[AssertionDiagnostic("actual.Skip(6).First().Should().Be(expectedItem{0});")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public FluentAssertionsMetadata(Compilation compilation)
IDictionaryOfT2 = compilation.GetTypeByMetadataName(typeof(IDictionary<,>).FullName);
DictionaryOfT2 = compilation.GetTypeByMetadataName(typeof(Dictionary<,>).FullName);
IReadonlyDictionaryOfT2 = compilation.GetTypeByMetadataName(typeof(IReadOnlyDictionary<,>).FullName);
IListOfT = compilation.GetTypeByMetadataName(typeof(IList<>).FullName);
IReadonlyListOfT = compilation.GetTypeByMetadataName(typeof(IReadOnlyList<>).FullName);
Enumerable = compilation.GetTypeByMetadataName(typeof(Enumerable).FullName);
IEnumerable = compilation.GetTypeByMetadataName(typeof(IEnumerable).FullName);
Math = compilation.GetTypeByMetadataName(typeof(Math).FullName);
Expand All @@ -87,6 +89,8 @@ public FluentAssertionsMetadata(Compilation compilation)
public INamedTypeSymbol IDictionaryOfT2 { get; }
public INamedTypeSymbol DictionaryOfT2 { get; }
public INamedTypeSymbol IReadonlyDictionaryOfT2 { get; }
public INamedTypeSymbol IListOfT { get; }
public INamedTypeSymbol IReadonlyListOfT { get; }
public INamedTypeSymbol BooleanAssertionsOfT1 { get; }
public INamedTypeSymbol NumericAssertionsOfT2 { get; }
public INamedTypeSymbol Enumerable { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, FluentAs
}
else if (subject.TryGetFirstDescendent<IPropertyReferenceOperation>(out var propertyReference) && propertyReference.Property.IsIndexer)
{
if (!propertyReference.Instance.Type.ImplementsOrIsInterface(metadata.IListOfT) && !propertyReference.Instance.Type.ImplementsOrIsInterface(metadata.IReadonlyListOfT)) return;
if (propertyReference.Instance.Type.ImplementsOrIsInterface(metadata.IDictionaryOfT2) || propertyReference.Instance.Type.ImplementsOrIsInterface(metadata.IReadonlyDictionaryOfT2)) return;

context.ReportDiagnostic(CreateDiagnostic(assertion, DiagnosticMetadata.CollectionShouldHaveElementAt_IndexerShouldBe));
Expand Down
Loading