Skip to content

Commit

Permalink
Improved equality of nonEmptyStrings as well.
Browse files Browse the repository at this point in the history
  • Loading branch information
KaliCZ committed Jan 4, 2024
1 parent fdbfcdd commit 1d49a96
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
27 changes: 23 additions & 4 deletions src/FuncSharp.Tests/Strings/NonEmptyStringEqualityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,40 @@ public void EqualityTest()

string text = "ASDF123";
NonEmptyString nonEmptyString = NonEmptyString.CreateUnsafe("ASDF123");
NonEmptyString nonEmptyStringWithSameValue = NonEmptyString.CreateUnsafe("ASDF123");
Assert.True(text == nonEmptyString);
Assert.False(text != nonEmptyString);
Assert.True(nonEmptyString == text);
Assert.False(nonEmptyString != text);
Assert.True(nonEmptyString == nonEmptyStringWithSameValue);
Assert.False(nonEmptyString != nonEmptyStringWithSameValue);

Assert.True(text.Equals(nonEmptyString));
Assert.True(nonEmptyString.Equals(text));
Assert.True(nonEmptyString.Equals(nonEmptyStringWithSameValue));

Assert.False(object.Equals(text, nonEmptyString)); // Unfortunately string doesn't override the default Equals method to compare with IEquatable<string> therefore this is false.
Assert.True(object.Equals(nonEmptyString, text));
Assert.True(object.Equals(nonEmptyString, nonEmptyStringWithSameValue));

string differentString = "Text14";
NonEmptyString differentNonEmptyString = NonEmptyString.CreateUnsafe("Totally different text here.");
Assert.False(differentNonEmptyString.Equals(differentString));
Assert.False(differentString.Equals(differentNonEmptyString));
Assert.False(differentNonEmptyString == differentString);
NonEmptyString differentNonEmptyString2 = NonEmptyString.CreateUnsafe("And completely different again.");

Assert.False(differentString == differentNonEmptyString);
Assert.False(object.Equals(differentNonEmptyString, differentString));
Assert.True(differentString != differentNonEmptyString);
Assert.False(differentNonEmptyString == differentString);
Assert.True(differentNonEmptyString != differentString);
Assert.False(differentNonEmptyString == differentNonEmptyString2);
Assert.True(differentNonEmptyString != differentNonEmptyString2);

Assert.False(differentString.Equals(differentNonEmptyString));
Assert.False(differentNonEmptyString.Equals(differentString));
Assert.False(differentNonEmptyString.Equals(differentNonEmptyString2));

Assert.False(object.Equals(differentString, differentNonEmptyString));
Assert.False(object.Equals(differentNonEmptyString, differentString));
Assert.False(object.Equals(differentNonEmptyString, differentNonEmptyString2));

NonEmptyString null1 = null;
string null2 = null;
Expand Down
30 changes: 25 additions & 5 deletions src/FuncSharp/Strings/NonEmptyString.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;

namespace FuncSharp;
Expand All @@ -24,6 +25,7 @@ public static explicit operator NonEmptyString(string s)
return CreateUnsafe(s);
}

[return: NotNull]
public static NonEmptyString CreateUnsafe(string value)
{
if (string.IsNullOrWhiteSpace(value))
Expand Down Expand Up @@ -97,24 +99,42 @@ public static Option<NonEmptyString> Create(string value)

public override int GetHashCode()
{
return Value.GetHashCode();
return (Value != null ? Value.GetHashCode() : 0);
}

public static bool operator ==(NonEmptyString left, NonEmptyString right)
{
if (left is null)
return right is null;

return left.Equals(right);
}

public static bool operator !=(NonEmptyString left, NonEmptyString right)
{
if (left is null)
return right is not null;

return !left.Equals(right);
}

public override bool Equals(object obj)
{
return ReferenceEquals(this, obj) ||
obj is NonEmptyString otherNonEmpty && Equals(otherNonEmpty) ||
return obj is NonEmptyString otherNonEmpty && Equals(otherNonEmpty) ||
obj is string otherString && Equals(otherString);
}

public bool Equals(string other)
{
return Value.Equals(other);
if (ReferenceEquals(null, other))
return false;
return ReferenceEquals(Value, other) || Value == other;
}

public bool Equals(NonEmptyString other)
{
return Value.Equals(other?.Value);
return Equals(other?.Value);

}

public override string ToString()
Expand Down

0 comments on commit 1d49a96

Please sign in to comment.