Skip to content

Commit 03b46fa

Browse files
authored
Do not suggest [Not]Contains if method parameter type differs from collection type argument (#75)
1 parent 10e62a5 commit 03b46fa

File tree

4 files changed

+131
-66
lines changed

4 files changed

+131
-66
lines changed

src/AwesomeAssertions.Analyzers.TestUtils/GenerateCode.cs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,7 @@ public static string GenericAssertionOnClassWhichImplementsIList(string genericT
7272
7373
namespace TestNamespace
7474
{
75-
public class RandomClass : IList<{{genericType}}>
76-
{
77-
public {{genericType}} this[int index]
78-
{
79-
get => throw new System.NotImplementedException();
80-
set => throw new System.NotImplementedException();
81-
}
82-
public int Count { get; }
83-
public bool IsReadOnly { get; }
84-
public void Add({{genericType}} item) => throw new System.NotImplementedException();
85-
public void Clear() => throw new System.NotImplementedException();
86-
public bool Contains({{genericType}} item) => throw new System.NotImplementedException();
87-
public void CopyTo({{genericType}}[] array, int arrayIndex) => throw new System.NotImplementedException();
88-
public IEnumerator<{{genericType}}> GetEnumerator() => throw new System.NotImplementedException();
89-
public int IndexOf({{genericType}} item) => throw new System.NotImplementedException();
90-
public void Insert(int index, {{genericType}} item) => throw new System.NotImplementedException();
91-
public bool Remove({{genericType}} item) => throw new System.NotImplementedException();
92-
public void RemoveAt(int index) => throw new System.NotImplementedException();
93-
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
94-
}
75+
{{ClassImplementsIList(genericType)}}
9576
public sealed class TestClass
9677
{
9778
public void TestMethod(RandomClass actual) =>
@@ -100,6 +81,30 @@ public void TestMethod(RandomClass actual) =>
10081
}
10182
""";
10283

84+
public static string ClassImplementsIList(string genericType, string additionalMethods = "") => $$"""
85+
public class RandomClass : IList<{{genericType}}>
86+
{
87+
public {{genericType}} this[int index]
88+
{
89+
get => throw new System.NotImplementedException();
90+
set => throw new System.NotImplementedException();
91+
}
92+
public int Count { get; }
93+
public bool IsReadOnly { get; }
94+
public void Add({{genericType}} item) => throw new System.NotImplementedException();
95+
public void Clear() => throw new System.NotImplementedException();
96+
public bool Contains({{genericType}} item) => throw new System.NotImplementedException();
97+
public void CopyTo({{genericType}}[] array, int arrayIndex) => throw new System.NotImplementedException();
98+
public IEnumerator<{{genericType}}> GetEnumerator() => throw new System.NotImplementedException();
99+
public int IndexOf({{genericType}} item) => throw new System.NotImplementedException();
100+
public void Insert(int index, {{genericType}} item) => throw new System.NotImplementedException();
101+
public bool Remove({{genericType}} item) => throw new System.NotImplementedException();
102+
public void RemoveAt(int index) => throw new System.NotImplementedException();
103+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
104+
{{additionalMethods}}
105+
}
106+
""";
107+
103108
private static string GenericIListAssertion(string bodyExpression) => $$"""
104109
using System.Collections.Generic;
105110
using System.Linq;

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

Lines changed: 89 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,34 @@ public class CollectionTests
247247
[Implemented]
248248
public void CollectionShouldContainItem_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldContainItem_ContainsShouldBeTrue);
249249

250+
[TestMethod]
251+
[AssertionDiagnostic("actual.Contains(expectedItem).Should().BeTrue({0});", "double", "string")]
252+
[AssertionDiagnostic("actual.Contains(expectedItem).Should().BeTrue({0});", "object", "int")]
253+
[Implemented]
254+
public void CollectionShouldContainItem_ContainsArgumentIsOfOtherTypeThanCollectionArgument_TestNoAnalyzer(string assertion, string genericType, string containsType)
255+
{
256+
string source = $$"""
257+
using System;
258+
using System.Collections;
259+
using System.Collections.Generic;
260+
261+
using AwesomeAssertions;
262+
using AwesomeAssertions.Extensions;
263+
264+
namespace TestNamespace
265+
{
266+
{{GenerateCode.ClassImplementsIList(genericType, $"public bool Contains({containsType} item) => true;")}}
267+
public sealed class TestClass
268+
{
269+
public void TestMethod(RandomClass actual, {{containsType}} expectedItem) =>
270+
{{assertion}}
271+
}
272+
}
273+
""";
274+
275+
DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
276+
}
277+
250278
[TestMethod]
251279
[AssertionCodeFix(
252280
oldAssertion: "actual.Contains(expectedItem).Should().BeTrue({0});",
@@ -271,6 +299,34 @@ public class CollectionTests
271299
[Implemented]
272300
public void CollectionShouldNotContainItem_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldNotContainItem_ContainsShouldBeFalse);
273301

302+
[TestMethod]
303+
[AssertionDiagnostic("actual.Contains(expectedItem).Should().BeFalse({0});", "double", "string")]
304+
[AssertionDiagnostic("actual.Contains(expectedItem).Should().BeFalse({0});", "object", "int")]
305+
[Implemented]
306+
public void CollectionShouldNotContainItem_ContainsArgumentIsOfOtherTypeThanCollectionArgument_TestNoAnalyzer(string assertion, string genericType, string containsType)
307+
{
308+
string source = $$"""
309+
using System;
310+
using System.Collections;
311+
using System.Collections.Generic;
312+
313+
using AwesomeAssertions;
314+
using AwesomeAssertions.Extensions;
315+
316+
namespace TestNamespace
317+
{
318+
{{GenerateCode.ClassImplementsIList(genericType, $"public bool Contains({containsType} item) => true;")}}
319+
public sealed class TestClass
320+
{
321+
public void TestMethod(RandomClass actual, {{containsType}} expectedItem) =>
322+
{{assertion}}
323+
}
324+
}
325+
""";
326+
327+
DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
328+
}
329+
274330
[TestMethod]
275331
[AssertionCodeFix(
276332
oldAssertion: "actual.Contains(unexpectedItem).Should().BeFalse({0});",
@@ -337,23 +393,23 @@ public class CollectionTests
337393
[AssertionDiagnostic("(list.Count + 1).Should().Be(1{0}).And.ToString();")]
338394
[AssertionDiagnostic("(list.Count + 1).Should().Be(expectedSize{0}).And.ToString();")]
339395
[Implemented]
340-
public void CollectionShouldHaveCount_CountShouldBe_TestNoAnalyzer(string assertion) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(new StringBuilder()
341-
.AppendLine("using System;")
342-
.AppendLine("using System.Collections.Generic;")
343-
.AppendLine("using System.Linq;")
344-
.AppendLine("using AwesomeAssertions;")
345-
.AppendLine("using AwesomeAssertions.Extensions;")
346-
.AppendLine("namespace TestNamespace")
347-
.AppendLine("{")
348-
.AppendLine(" public class TestClass")
349-
.AppendLine(" {")
350-
.AppendLine(" public void TestMethod(string[] array, List<string> list, int expectedSize)")
351-
.AppendLine(" {")
352-
.AppendLine(assertion)
353-
.AppendLine(" }")
354-
.AppendLine(" }")
355-
.AppendLine("}")
356-
.ToString());
396+
public void CollectionShouldHaveCount_CountShouldBe_TestNoAnalyzer(string assertion) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers($$"""
397+
using System;
398+
using System.Collections.Generic;
399+
using System.Linq;
400+
using AwesomeAssertions;
401+
using AwesomeAssertions.Extensions;
402+
namespace TestNamespace
403+
{
404+
public class TestClass
405+
{
406+
public void TestMethod(string[] array, List<string> list, int expectedSize)
407+
{
408+
{{assertion}}
409+
}
410+
}
411+
}
412+
""");
357413

358414
[TestMethod]
359415
[AssertionDiagnostic(@"var array = new string[0, 0]; array.Length.Should().Be(0{0});")]
@@ -364,21 +420,21 @@ public void CollectionShouldHaveCount_CountShouldBe_TestNoAnalyzer(string assert
364420
[AssertionDiagnostic(@"var array = new string[3, 3, 3]; array.Length.Should().Be(0{0});")]
365421
[AssertionDiagnostic(@"int[] array1 = new[] {{ 1, 2, 3 }}; int[] array2 = new[] {{ 4, 5, 6 }}; int[] both = new int[] {{ 1, 2, 3, 4, 5, 6 }}; (array1.Length + array2.Length).Should().Be(both.Length{0});")]
366422
[Implemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/309")]
367-
public void CollectionShouldHaveCount_LengthShouldBe_TestNoAnalyzer(string assertion) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(new StringBuilder()
368-
.AppendLine("using System;")
369-
.AppendLine("using AwesomeAssertions;")
370-
.AppendLine("using AwesomeAssertions.Extensions;")
371-
.AppendLine("namespace TestNamespace")
372-
.AppendLine("{")
373-
.AppendLine(" public class TestClass")
374-
.AppendLine(" {")
375-
.AppendLine(" public void TestMethod()")
376-
.AppendLine(" {")
377-
.AppendLine(assertion)
378-
.AppendLine(" }")
379-
.AppendLine(" }")
380-
.AppendLine("}")
381-
.ToString());
423+
public void CollectionShouldHaveCount_LengthShouldBe_TestNoAnalyzer(string assertion) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers($$"""
424+
using System;
425+
using AwesomeAssertions;
426+
using AwesomeAssertions.Extensions;
427+
namespace TestNamespace
428+
{
429+
public class TestClass
430+
{
431+
public void TestMethod()
432+
{
433+
{{assertion}}
434+
}
435+
}
436+
}
437+
""");
382438

383439
[TestMethod]
384440
[AssertionDiagnostic("actual.Should().HaveCount(expected.Count() + 1{0});")]
@@ -988,7 +1044,7 @@ public void CollectionShouldHaveElementAt_ClassImplementsIList_TestAnalyzer(stri
9881044
VisitorName = metadata.Name,
9891045
Locations =
9901046
[
991-
new DiagnosticResultLocation("Test0.cs", 33, 13)
1047+
new DiagnosticResultLocation("Test0.cs", 34, 13)
9921048
],
9931049
Severity = DiagnosticSeverity.Info
9941050
});

0 commit comments

Comments
 (0)