Skip to content

Commit

Permalink
Merge pull request #584 from manfred-brands/issue561_UriVsString
Browse files Browse the repository at this point in the history
CanBeEqual: Add special case for Uri.Equals(string)
  • Loading branch information
mikkelbu authored Sep 8, 2023
2 parents 81c26ba + e429d3b commit 761ff86
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -951,5 +951,34 @@ public void AnalyzeWhenThrowsWithTypeConstraintIsUsedWithDifferentType(string co

RoslynAssert.Diagnostics(analyzer, testCode);
}

[Test]
public void AnalyzeWhenComparingUriWithString()
{
// Uri.Equals(object? comparand) has code that checks if comparand is a string.
// There is no separate overload for Uri.Equals(string?) to check for.
var testCode = TestUtility.WrapInTestMethod(@"
const string testString = ""test"";
Uri testUri = new Uri(""test"", UriKind.Relative);
Assert.That(testUri, Is.EqualTo(testString));
");

RoslynAssert.Valid(analyzer, testCode);
}

[Test]
public void AnalyzeWhenComparingStringWithUri()
{
// string.Equals(Uri) always fails
var testCode = TestUtility.WrapInTestMethod(@"
const string testString = ""test"";
Uri testUri = new Uri(""test"", UriKind.Relative);
Assert.That(testString, Is.EqualTo(↓testUri));
");

RoslynAssert.Diagnostics(analyzer, testCode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,21 @@ public void TrueForArrayWithElementErrorType()
AssertThatCommutativeEqual(leftTypeSymbol, rightTypeSymbol, compilation, Is.True);
}

[Test]
public void UriCanEqualStringButStringCanNotEqualUri()
{
var compilation = TestHelpers.CreateCompilation();

var stringSymbol = compilation.GetSpecialType(SpecialType.System_String);
var uriSymbol = compilation.GetTypeByMetadataName("System.Uri");

Assert.Multiple(() =>
{
Assert.That(NUnitEqualityComparerHelper.CanBeEqual(uriSymbol, stringSymbol, compilation), Is.True);
Assert.That(NUnitEqualityComparerHelper.CanBeEqual(stringSymbol, uriSymbol, compilation), Is.False);
});
}

private static void AssertThatCommutativeEqual(ITypeSymbol? leftTypeSymbol, ITypeSymbol? rightTypeSymbol, Compilation compilation, Constraint constraint)
{
Assert.Multiple(() =>
Expand Down
9 changes: 9 additions & 0 deletions src/nunit.analyzers/Helpers/NUnitEqualityComparerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ public static bool CanBeEqual(
return CanBeEqual(actualKeyType, expectedKeyType, compilation, checkedTypes)
&& CanBeEqual(actualValueType, expectedValueType, compilation, checkedTypes);
}

// Uri.Equals(string) works, but not string.Equals(Uri)
if (IsUri(actualFullName) && expectedType.SpecialType == SpecialType.System_String)
return true;
}

// IEnumerables
Expand Down Expand Up @@ -182,6 +186,11 @@ private static bool IsTuple(string fullName)
return fullName.StartsWith("System.Tuple`", StringComparison.Ordinal);
}

private static bool IsUri(string fullName)
{
return fullName.Equals("System.Uri", StringComparison.Ordinal);
}

private static bool IsIEquatable(ITypeSymbol typeSymbol, ITypeSymbol equatableTypeArguments)
{
return typeSymbol.AllInterfaces.Any(i => i.TypeArguments.Length == 1
Expand Down

0 comments on commit 761ff86

Please sign in to comment.