Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit d71c2cb

Browse files
author
Justin Kotalik
committed
Adds Refactor to Equals in Path String, Adds Regression Tests
1 parent 8f3d894 commit d71c2cb

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

src/Microsoft.AspNetCore.Http.Abstractions/PathString.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public string Add(QueryString other)
213213
/// <returns>True if both PathString values are equal</returns>
214214
public bool Equals(PathString other)
215215
{
216-
return string.Equals(_value, other._value, StringComparison.OrdinalIgnoreCase);
216+
return Equals(other, StringComparison.OrdinalIgnoreCase);
217217
}
218218

219219
/// <summary>
@@ -224,6 +224,10 @@ public bool Equals(PathString other)
224224
/// <returns>True if both PathString values are equal</returns>
225225
public bool Equals(PathString other, StringComparison comparisonType)
226226
{
227+
if (!HasValue && !other.HasValue)
228+
{
229+
return true;
230+
}
227231
return string.Equals(_value, other._value, comparisonType);
228232
}
229233

@@ -236,9 +240,9 @@ public override bool Equals(object obj)
236240
{
237241
if (ReferenceEquals(null, obj))
238242
{
239-
return false;
243+
return !HasValue;
240244
}
241-
return obj is PathString && Equals((PathString)obj, StringComparison.OrdinalIgnoreCase);
245+
return obj is PathString && Equals((PathString)obj);
242246
}
243247

244248
/// <summary>
@@ -247,7 +251,7 @@ public override bool Equals(object obj)
247251
/// <returns>The hash code</returns>
248252
public override int GetHashCode()
249253
{
250-
return (_value != null ? StringComparer.OrdinalIgnoreCase.GetHashCode(_value) : 0);
254+
return (HasValue ? StringComparer.OrdinalIgnoreCase.GetHashCode(_value) : 0);
251255
}
252256

253257
/// <summary>
@@ -258,7 +262,7 @@ public override int GetHashCode()
258262
/// <returns>True if both PathString values are equal</returns>
259263
public static bool operator ==(PathString left, PathString right)
260264
{
261-
return left.Equals(right, StringComparison.OrdinalIgnoreCase);
265+
return left.Equals(right);
262266
}
263267

264268
/// <summary>
@@ -269,7 +273,7 @@ public override int GetHashCode()
269273
/// <returns>True if both PathString values are not equal</returns>
270274
public static bool operator !=(PathString left, PathString right)
271275
{
272-
return !left.Equals(right, StringComparison.OrdinalIgnoreCase);
276+
return !left.Equals(right);
273277
}
274278

275279
/// <summary>

test/Microsoft.AspNetCore.Http.Abstractions.Tests/PathStringTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,44 @@ public void CtorThrows_IfPathDoesNotHaveLeadingSlash()
1616
ExceptionAssert.ThrowsArgument(() => new PathString("hello"), "value", "The path in 'value' must start with '/'.");
1717
}
1818

19+
[Fact]
20+
public void Equals_EmptyPathStringAndDefaultPathString()
21+
{
22+
// Act and Assert
23+
Assert.Equal(PathString.Empty, default(PathString));
24+
Assert.Equal(default(PathString), PathString.Empty);
25+
Assert.True(PathString.Empty == default(PathString));
26+
Assert.True(default(PathString) == PathString.Empty);
27+
Assert.True(PathString.Empty.Equals(default(PathString)));
28+
Assert.True(default(PathString).Equals(PathString.Empty));
29+
}
30+
31+
[Fact]
32+
public void NotEquals_DefaultPathStringAndNonNullPathString()
33+
{
34+
// Arrange
35+
var pathString = new PathString("/hello");
36+
37+
// Act and Assert
38+
Assert.NotEqual(pathString, default(PathString));
39+
}
40+
41+
[Fact]
42+
public void NotEquals_EmptyPathStringAndNonNullPathString()
43+
{
44+
// Arrange
45+
var pathString = new PathString("/hello");
46+
47+
// Act and Assert
48+
Assert.NotEqual(pathString, PathString.Empty);
49+
}
50+
51+
[Fact]
52+
public void HashCode_CheckNullAndEmptyHaveSameHashcodes()
53+
{
54+
Assert.Equal(PathString.Empty.GetHashCode(), default(PathString).GetHashCode());
55+
}
56+
1957
[Theory]
2058
[InlineData(null, null)]
2159
[InlineData("", null)]

0 commit comments

Comments
 (0)