Skip to content

Commit

Permalink
Merge pull request #581 from manfred-brands/issue580_WithinOnNullable
Browse files Browse the repository at this point in the history
Within is allowed on Nullable<T> when T  is supported.
  • Loading branch information
mikkelbu authored Sep 5, 2023
2 parents beb4407 + 908fb80 commit eebf3f3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/nunit.analyzers.tests/WithinUsage/WithinUsageAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,5 +272,20 @@ public void AnalyzeOnNonGenericCollection()

RoslynAssert.Valid(analyzer, testCode);
}

[TestCase("double")]
[TestCase("int")]
[TestCase("TimeSpan")]
public void AnalyzeWhenAppliedToEqualityConstraintForNullableValidTypes(string type)
{
string testCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@"
[TestCase(null, null)]
public void Test({type}? a, {type}? b)
{{
Assert.That(a, Is.EqualTo(b).Within(0.1));
}}");

RoslynAssert.Valid(analyzer, testCode);
}
}
}
6 changes: 6 additions & 0 deletions src/nunit.analyzers/WithinUsage/WithinUsageAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ private static bool IsTypeSupported(ITypeSymbol type, HashSet<ITypeSymbol>? chec
if (fullName.Equals("System.DateTimeOffset", StringComparison.Ordinal))
return true;

// Check for Nullable<T>
if (fullName.Equals("System.Nullable`1", StringComparison.Ordinal))
{
return IsTypeSupported(namedType.TypeArguments[0]);
}

if (fullName.StartsWith("System.Collections.Generic.KeyValuePair`", StringComparison.Ordinal))
{
// We pass tolerance to the Value Type.
Expand Down

0 comments on commit eebf3f3

Please sign in to comment.