Adds Refactor to Equals in Path String, Adds Regression Tests
This commit is contained in:
parent
8f3d894b95
commit
d71c2cb6ce
|
|
@ -213,7 +213,7 @@ namespace Microsoft.AspNetCore.Http
|
||||||
/// <returns>True if both PathString values are equal</returns>
|
/// <returns>True if both PathString values are equal</returns>
|
||||||
public bool Equals(PathString other)
|
public bool Equals(PathString other)
|
||||||
{
|
{
|
||||||
return string.Equals(_value, other._value, StringComparison.OrdinalIgnoreCase);
|
return Equals(other, StringComparison.OrdinalIgnoreCase);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -224,6 +224,10 @@ namespace Microsoft.AspNetCore.Http
|
||||||
/// <returns>True if both PathString values are equal</returns>
|
/// <returns>True if both PathString values are equal</returns>
|
||||||
public bool Equals(PathString other, StringComparison comparisonType)
|
public bool Equals(PathString other, StringComparison comparisonType)
|
||||||
{
|
{
|
||||||
|
if (!HasValue && !other.HasValue)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return string.Equals(_value, other._value, comparisonType);
|
return string.Equals(_value, other._value, comparisonType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -236,9 +240,9 @@ namespace Microsoft.AspNetCore.Http
|
||||||
{
|
{
|
||||||
if (ReferenceEquals(null, obj))
|
if (ReferenceEquals(null, obj))
|
||||||
{
|
{
|
||||||
return false;
|
return !HasValue;
|
||||||
}
|
}
|
||||||
return obj is PathString && Equals((PathString)obj, StringComparison.OrdinalIgnoreCase);
|
return obj is PathString && Equals((PathString)obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -247,7 +251,7 @@ namespace Microsoft.AspNetCore.Http
|
||||||
/// <returns>The hash code</returns>
|
/// <returns>The hash code</returns>
|
||||||
public override int GetHashCode()
|
public override int GetHashCode()
|
||||||
{
|
{
|
||||||
return (_value != null ? StringComparer.OrdinalIgnoreCase.GetHashCode(_value) : 0);
|
return (HasValue ? StringComparer.OrdinalIgnoreCase.GetHashCode(_value) : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -258,7 +262,7 @@ namespace Microsoft.AspNetCore.Http
|
||||||
/// <returns>True if both PathString values are equal</returns>
|
/// <returns>True if both PathString values are equal</returns>
|
||||||
public static bool operator ==(PathString left, PathString right)
|
public static bool operator ==(PathString left, PathString right)
|
||||||
{
|
{
|
||||||
return left.Equals(right, StringComparison.OrdinalIgnoreCase);
|
return left.Equals(right);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -269,7 +273,7 @@ namespace Microsoft.AspNetCore.Http
|
||||||
/// <returns>True if both PathString values are not equal</returns>
|
/// <returns>True if both PathString values are not equal</returns>
|
||||||
public static bool operator !=(PathString left, PathString right)
|
public static bool operator !=(PathString left, PathString right)
|
||||||
{
|
{
|
||||||
return !left.Equals(right, StringComparison.OrdinalIgnoreCase);
|
return !left.Equals(right);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,44 @@ namespace Microsoft.AspNetCore.Http
|
||||||
ExceptionAssert.ThrowsArgument(() => new PathString("hello"), "value", "The path in 'value' must start with '/'.");
|
ExceptionAssert.ThrowsArgument(() => new PathString("hello"), "value", "The path in 'value' must start with '/'.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Equals_EmptyPathStringAndDefaultPathString()
|
||||||
|
{
|
||||||
|
// Act and Assert
|
||||||
|
Assert.Equal(PathString.Empty, default(PathString));
|
||||||
|
Assert.Equal(default(PathString), PathString.Empty);
|
||||||
|
Assert.True(PathString.Empty == default(PathString));
|
||||||
|
Assert.True(default(PathString) == PathString.Empty);
|
||||||
|
Assert.True(PathString.Empty.Equals(default(PathString)));
|
||||||
|
Assert.True(default(PathString).Equals(PathString.Empty));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void NotEquals_DefaultPathStringAndNonNullPathString()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var pathString = new PathString("/hello");
|
||||||
|
|
||||||
|
// Act and Assert
|
||||||
|
Assert.NotEqual(pathString, default(PathString));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void NotEquals_EmptyPathStringAndNonNullPathString()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var pathString = new PathString("/hello");
|
||||||
|
|
||||||
|
// Act and Assert
|
||||||
|
Assert.NotEqual(pathString, PathString.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void HashCode_CheckNullAndEmptyHaveSameHashcodes()
|
||||||
|
{
|
||||||
|
Assert.Equal(PathString.Empty.GetHashCode(), default(PathString).GetHashCode());
|
||||||
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData(null, null)]
|
[InlineData(null, null)]
|
||||||
[InlineData("", null)]
|
[InlineData("", null)]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue