Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CanBeEqual: Add special case for Uri.Equals(string) #584

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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