-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refs/heads/main' into release
- Loading branch information
Showing
9 changed files
with
457 additions
and
8 deletions.
There are no files selected for viewing
177 changes: 177 additions & 0 deletions
177
OnixLabs.Core.UnitTests/OptionalEqualityComparerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
// Copyright 2020 ONIXLabs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Xunit; | ||
|
||
namespace OnixLabs.Core.UnitTests; | ||
|
||
public class OptionalEqualityComparerTests | ||
{ | ||
[Fact(DisplayName = "OptionalEqualityComparer.Equals should return true when comparing null values")] | ||
public void OptionalEqualityComparerEqualsShouldReturnTrueWhenComparingNullValues() | ||
{ | ||
// Given | ||
Optional<string>? x = null; | ||
Optional<string>? y = null; | ||
OptionalEqualityComparer<string> comparer = new(); | ||
|
||
// When | ||
bool result = comparer.Equals(x, y); | ||
|
||
// Then | ||
Assert.True(result); | ||
} | ||
|
||
[Fact(DisplayName = "OptionalEqualityComparer.Equals should return false when comparing null and non-null values")] | ||
public void OptionalEqualityComparerEqualsShouldReturnFalseWhenComparingNullAndNonNullValues() | ||
{ | ||
// Given | ||
Optional<string>? x = null; | ||
Optional<string> y = "abc"; | ||
OptionalEqualityComparer<string> comparer = new(); | ||
|
||
// When | ||
bool result1 = comparer.Equals(x, y); | ||
bool result2 = comparer.Equals(y, x); | ||
|
||
// Then | ||
Assert.False(result1); | ||
Assert.False(result2); | ||
} | ||
|
||
[Fact(DisplayName = "OptionalEqualityComparer.Equals should return true when comparing None values")] | ||
public void OptionalEqualityComparerEqualsShouldReturnTrueWhenComparingNoneValues() | ||
{ | ||
// Given | ||
Optional<string> x = Optional<string>.None; | ||
Optional<string> y = Optional<string>.None; | ||
OptionalEqualityComparer<string> comparer = new(); | ||
|
||
// When | ||
bool result1 = comparer.Equals(x, y); | ||
bool result2 = comparer.Equals(y, x); | ||
|
||
// Then | ||
Assert.True(result1); | ||
Assert.True(result2); | ||
} | ||
|
||
[Fact(DisplayName = "OptionalEqualityComparer.Equals should return false when comparing None and Some values")] | ||
public void OptionalEqualityComparerEqualsShouldReturnFalseWhenComparingNoneAndSomeValues() | ||
{ | ||
// Given | ||
Optional<string> x = Optional<string>.None; | ||
Optional<string> y = "abc"; | ||
OptionalEqualityComparer<string> comparer = new(); | ||
|
||
// When | ||
bool result1 = comparer.Equals(x, y); | ||
bool result2 = comparer.Equals(y, x); | ||
|
||
// Then | ||
Assert.False(result1); | ||
Assert.False(result2); | ||
} | ||
|
||
[Fact(DisplayName = "OptionalEqualityComparer.Equals should return true when comparing identical Some values")] | ||
public void OptionalEqualityComparerEqualsShouldReturnTrueWhenComparingIdenticalSomeValues() | ||
{ | ||
// Given | ||
Optional<string> x = "abc"; | ||
Optional<string> y = "abc"; | ||
OptionalEqualityComparer<string> comparer = new(); | ||
|
||
// When | ||
bool result1 = comparer.Equals(x, y); | ||
bool result2 = comparer.Equals(y, x); | ||
|
||
// Then | ||
Assert.True(result1); | ||
Assert.True(result2); | ||
} | ||
|
||
[Fact(DisplayName = "OptionalEqualityComparer.Equals should return false when comparing non-identical Some values")] | ||
public void OptionalEqualityComparerEqualsShouldReturnFalseWhenComparingNonIdenticalSomeValues() | ||
{ | ||
// Given | ||
Optional<string> x = "abc"; | ||
Optional<string> y = "xyz"; | ||
OptionalEqualityComparer<string> comparer = new(); | ||
|
||
// When | ||
bool result1 = comparer.Equals(x, y); | ||
bool result2 = comparer.Equals(y, x); | ||
|
||
// Then | ||
Assert.False(result1); | ||
Assert.False(result2); | ||
} | ||
|
||
[Fact(DisplayName = "OptionalEqualityComparer.Equals should return true when comparing identical Some values with value comparer")] | ||
public void OptionalEqualityComparerEqualsShouldReturnTrueWhenComparingIdenticalSomeValuesWithValueComparer() | ||
{ | ||
// Given | ||
Optional<string> x = "abc"; | ||
Optional<string> y = "ABC"; | ||
OptionalEqualityComparer<string> comparer = new(new CaseInsensitiveStringComparer()); | ||
|
||
// When | ||
bool result1 = comparer.Equals(x, y); | ||
bool result2 = comparer.Equals(y, x); | ||
|
||
// Then | ||
Assert.True(result1); | ||
Assert.True(result2); | ||
} | ||
|
||
[Fact(DisplayName = "OptionalEqualityComparer.Equals should return false when comparing non-identical Some values with value comparer")] | ||
public void OptionalEqualityComparerEqualsShouldReturnFalseWhenComparingNonIdenticalSomeValuesWithValueComparer() | ||
{ | ||
// Given | ||
Optional<string> x = "abc"; | ||
Optional<string> y = "XYZ"; | ||
OptionalEqualityComparer<string> comparer = new(new CaseInsensitiveStringComparer()); | ||
|
||
// When | ||
bool result1 = comparer.Equals(x, y); | ||
bool result2 = comparer.Equals(y, x); | ||
|
||
// Then | ||
Assert.False(result1); | ||
Assert.False(result2); | ||
} | ||
|
||
[Fact(DisplayName = "OptionalEqualityComparer.GetHashCode should return zero when the optional value is none.")] | ||
public void OptionalEqualityComparerGetHashCodeShouldReturnZeroWhenOptionalValueIsNone() | ||
{ | ||
// Given | ||
const int expected = default; | ||
Optional<string> optional = Optional<string>.None; | ||
OptionalEqualityComparer<string> comparer = new(); | ||
|
||
// When | ||
int actual = comparer.GetHashCode(optional); | ||
|
||
// Then | ||
Assert.Equal(expected, actual); | ||
} | ||
|
||
private sealed class CaseInsensitiveStringComparer : EqualityComparer<string> | ||
{ | ||
public override bool Equals(string? x, string? y) => string.Equals(x, y, StringComparison.InvariantCultureIgnoreCase); | ||
public override int GetHashCode(string obj) => obj.GetHashCode(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
// Copyright 2020 ONIXLabs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Xunit; | ||
|
||
namespace OnixLabs.Core.UnitTests; | ||
|
||
public class ResultEqualityComparerTests | ||
{ | ||
[Fact(DisplayName = "ResultEqualityComparer.Equals should return true when comparing null values")] | ||
public void ResultEqualityComparerEqualsShouldReturnTrueWhenComparingNullValues() | ||
{ | ||
// Given | ||
Result<string>? x = null; | ||
Result<string>? y = null; | ||
ResultEqualityComparer<string> comparer = new(); | ||
|
||
// When | ||
bool result = comparer.Equals(x, y); | ||
|
||
// Then | ||
Assert.True(result); | ||
} | ||
|
||
[Fact(DisplayName = "ResultEqualityComparer.Equals should return false when comparing null and non-null values")] | ||
public void ResultEqualityComparerEqualsShouldReturnFalseWhenComparingNullAndNonNullValues() | ||
{ | ||
// Given | ||
Result<string>? x = null; | ||
Result<string> y = "abc"; | ||
ResultEqualityComparer<string> comparer = new(); | ||
|
||
// When | ||
bool result1 = comparer.Equals(x, y); | ||
bool result2 = comparer.Equals(y, x); | ||
|
||
// Then | ||
Assert.False(result1); | ||
Assert.False(result2); | ||
} | ||
|
||
[Fact(DisplayName = "ResultEqualityComparer.Equals should return true when comparing Failure values")] | ||
public void ResultEqualityComparerEqualsShouldReturnTrueWhenComparingNoneValues() | ||
{ | ||
// Given | ||
Exception exception = new("Failure"); | ||
Result<string> x = exception; | ||
Result<string> y = exception; | ||
ResultEqualityComparer<string> comparer = new(); | ||
|
||
// When | ||
bool result1 = comparer.Equals(x, y); | ||
bool result2 = comparer.Equals(y, x); | ||
|
||
// Then | ||
Assert.True(result1); | ||
Assert.True(result2); | ||
} | ||
|
||
[Fact(DisplayName = "ResultEqualityComparer.Equals should return false when comparing Success and Failure values")] | ||
public void ResultEqualityComparerEqualsShouldReturnFalseWhenComparingNoneAndSomeValues() | ||
{ | ||
// Given | ||
Exception exception = new("Failure"); | ||
Result<string> x = exception; | ||
Result<string> y = "abc"; | ||
ResultEqualityComparer<string> comparer = new(); | ||
|
||
// When | ||
bool result1 = comparer.Equals(x, y); | ||
bool result2 = comparer.Equals(y, x); | ||
|
||
// Then | ||
Assert.False(result1); | ||
Assert.False(result2); | ||
} | ||
|
||
[Fact(DisplayName = "ResultEqualityComparer.Equals should return true when comparing identical Success values")] | ||
public void ResultEqualityComparerEqualsShouldReturnTrueWhenComparingIdenticalSomeValues() | ||
{ | ||
// Given | ||
Result<string> x = "abc"; | ||
Result<string> y = "abc"; | ||
ResultEqualityComparer<string> comparer = new(); | ||
|
||
// When | ||
bool result1 = comparer.Equals(x, y); | ||
bool result2 = comparer.Equals(y, x); | ||
|
||
// Then | ||
Assert.True(result1); | ||
Assert.True(result2); | ||
} | ||
|
||
[Fact(DisplayName = "ResultEqualityComparer.Equals should return false when comparing non-identical Success values")] | ||
public void ResultEqualityComparerEqualsShouldReturnFalseWhenComparingNonIdenticalSomeValues() | ||
{ | ||
// Given | ||
Result<string> x = "abc"; | ||
Result<string> y = "xyz"; | ||
ResultEqualityComparer<string> comparer = new(); | ||
|
||
// When | ||
bool result1 = comparer.Equals(x, y); | ||
bool result2 = comparer.Equals(y, x); | ||
|
||
// Then | ||
Assert.False(result1); | ||
Assert.False(result2); | ||
} | ||
|
||
[Fact(DisplayName = "ResultEqualityComparer.Equals should return true when comparing identical Success values with value comparer")] | ||
public void ResultEqualityComparerEqualsShouldReturnTrueWhenComparingIdenticalSomeValuesWithValueComparer() | ||
{ | ||
// Given | ||
Result<string> x = "abc"; | ||
Result<string> y = "ABC"; | ||
ResultEqualityComparer<string> comparer = new(new CaseInsensitiveStringComparer()); | ||
|
||
// When | ||
bool result1 = comparer.Equals(x, y); | ||
bool result2 = comparer.Equals(y, x); | ||
|
||
// Then | ||
Assert.True(result1); | ||
Assert.True(result2); | ||
} | ||
|
||
[Fact(DisplayName = "ResultEqualityComparer.Equals should return false when comparing non-identical Success values with value comparer")] | ||
public void ResultEqualityComparerEqualsShouldReturnFalseWhenComparingNonIdenticalSomeValuesWithValueComparer() | ||
{ | ||
// Given | ||
Result<string> x = "abc"; | ||
Result<string> y = "XYZ"; | ||
ResultEqualityComparer<string> comparer = new(new CaseInsensitiveStringComparer()); | ||
|
||
// When | ||
bool result1 = comparer.Equals(x, y); | ||
bool result2 = comparer.Equals(y, x); | ||
|
||
// Then | ||
Assert.False(result1); | ||
Assert.False(result2); | ||
} | ||
|
||
private sealed class CaseInsensitiveStringComparer : EqualityComparer<string> | ||
{ | ||
public override bool Equals(string? x, string? y) => string.Equals(x, y, StringComparison.InvariantCultureIgnoreCase); | ||
public override int GetHashCode(string obj) => obj.GetHashCode(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.