Skip to content

Commit

Permalink
Merge pull request #597 from HenryZhang-ZHY/issue/596
Browse files Browse the repository at this point in the history
fix: IndexOutOfRangeException threw by WithinUsageAnalyzer
  • Loading branch information
manfred-brands authored Sep 22, 2023
2 parents 596df34 + 73f5711 commit 9b7f547
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/nunit.analyzers.tests/WithinUsage/WithinUsageAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,31 @@ public void AnalyzeOnNonGenericCollection()
RoslynAssert.Valid(analyzer, testCode);
}

[Test]
public void AnalyzeNonGenericSubclassOfGenericIEnumerable()
{
string testCode = TestUtility.WrapClassInNamespaceAndAddUsing(@"
[TestFixture]
public class TestClass
{
public void TestMethod()
{
Assert.That(new DoubleCollection(1), Is.EqualTo(new DoubleCollection(2)).Within(0.001));
}
private sealed class DoubleCollection : IEnumerable<double>
{
public DoubleCollection(params double[] numbers) { }
public IEnumerator<double> GetEnumerator() => throw new NotImplementedException();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}",
additionalUsings: @"using System.Collections;
using System.Collections.Generic;");

RoslynAssert.Valid(analyzer, testCode);
}

[TestCase("double")]
[TestCase("int")]
[TestCase("TimeSpan")]
Expand Down
2 changes: 1 addition & 1 deletion src/nunit.analyzers/WithinUsage/WithinUsageAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private static bool IsTypeSupported(ITypeSymbol type, HashSet<ITypeSymbol>? chec
string interfaceTypeName = interfaceType.GetFullMetadataName();
if (interfaceTypeName.StartsWith("System.Collections.Generic.IEnumerable`", StringComparison.Ordinal))
{
return IsTypeSupported(namedType.TypeArguments[0], checkedTypes);
return IsTypeSupported(interfaceType.TypeArguments[0], checkedTypes);
}

if (interfaceTypeName.Equals("System.Collections.IEnumerable", StringComparison.Ordinal))
Expand Down

0 comments on commit 9b7f547

Please sign in to comment.