Skip to content

Commit

Permalink
fix: IndexOutOfRangeException threw by WithinUsageAnalyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryZhang-ZHY committed Sep 22, 2023
1 parent 596df34 commit 73f5711
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 73f5711

Please sign in to comment.