|
| 1 | +// Copyright 2020 ONIXLabs |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using System; |
| 16 | +using System.Collections.Generic; |
| 17 | +using Xunit; |
| 18 | + |
| 19 | +namespace OnixLabs.Core.UnitTests; |
| 20 | + |
| 21 | +public class OptionalEqualityComparerTests |
| 22 | +{ |
| 23 | + [Fact(DisplayName = "OptionalEqualityComparer.Equals should return true when comparing null values")] |
| 24 | + public void OptionalEqualityComparerEqualsShouldReturnTrueWhenComparingNullValues() |
| 25 | + { |
| 26 | + // Given |
| 27 | + Optional<string>? x = null; |
| 28 | + Optional<string>? y = null; |
| 29 | + OptionalEqualityComparer<string> comparer = new(); |
| 30 | + |
| 31 | + // When |
| 32 | + bool result = comparer.Equals(x, y); |
| 33 | + |
| 34 | + // Then |
| 35 | + Assert.True(result); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact(DisplayName = "OptionalEqualityComparer.Equals should return false when comparing null and non-null values")] |
| 39 | + public void OptionalEqualityComparerEqualsShouldReturnFalseWhenComparingNullAndNonNullValues() |
| 40 | + { |
| 41 | + // Given |
| 42 | + Optional<string>? x = null; |
| 43 | + Optional<string> y = "abc"; |
| 44 | + OptionalEqualityComparer<string> comparer = new(); |
| 45 | + |
| 46 | + // When |
| 47 | + bool result1 = comparer.Equals(x, y); |
| 48 | + bool result2 = comparer.Equals(y, x); |
| 49 | + |
| 50 | + // Then |
| 51 | + Assert.False(result1); |
| 52 | + Assert.False(result2); |
| 53 | + } |
| 54 | + |
| 55 | + [Fact(DisplayName = "OptionalEqualityComparer.Equals should return true when comparing None values")] |
| 56 | + public void OptionalEqualityComparerEqualsShouldReturnTrueWhenComparingNoneValues() |
| 57 | + { |
| 58 | + // Given |
| 59 | + Optional<string> x = Optional<string>.None; |
| 60 | + Optional<string> y = Optional<string>.None; |
| 61 | + OptionalEqualityComparer<string> comparer = new(); |
| 62 | + |
| 63 | + // When |
| 64 | + bool result1 = comparer.Equals(x, y); |
| 65 | + bool result2 = comparer.Equals(y, x); |
| 66 | + |
| 67 | + // Then |
| 68 | + Assert.True(result1); |
| 69 | + Assert.True(result2); |
| 70 | + } |
| 71 | + |
| 72 | + [Fact(DisplayName = "OptionalEqualityComparer.Equals should return false when comparing None and Some values")] |
| 73 | + public void OptionalEqualityComparerEqualsShouldReturnFalseWhenComparingNoneAndSomeValues() |
| 74 | + { |
| 75 | + // Given |
| 76 | + Optional<string> x = Optional<string>.None; |
| 77 | + Optional<string> y = "abc"; |
| 78 | + OptionalEqualityComparer<string> comparer = new(); |
| 79 | + |
| 80 | + // When |
| 81 | + bool result1 = comparer.Equals(x, y); |
| 82 | + bool result2 = comparer.Equals(y, x); |
| 83 | + |
| 84 | + // Then |
| 85 | + Assert.False(result1); |
| 86 | + Assert.False(result2); |
| 87 | + } |
| 88 | + |
| 89 | + [Fact(DisplayName = "OptionalEqualityComparer.Equals should return true when comparing identical Some values")] |
| 90 | + public void OptionalEqualityComparerEqualsShouldReturnTrueWhenComparingIdenticalSomeValues() |
| 91 | + { |
| 92 | + // Given |
| 93 | + Optional<string> x = "abc"; |
| 94 | + Optional<string> y = "abc"; |
| 95 | + OptionalEqualityComparer<string> comparer = new(); |
| 96 | + |
| 97 | + // When |
| 98 | + bool result1 = comparer.Equals(x, y); |
| 99 | + bool result2 = comparer.Equals(y, x); |
| 100 | + |
| 101 | + // Then |
| 102 | + Assert.True(result1); |
| 103 | + Assert.True(result2); |
| 104 | + } |
| 105 | + |
| 106 | + [Fact(DisplayName = "OptionalEqualityComparer.Equals should return false when comparing non-identical Some values")] |
| 107 | + public void OptionalEqualityComparerEqualsShouldReturnFalseWhenComparingNonIdenticalSomeValues() |
| 108 | + { |
| 109 | + // Given |
| 110 | + Optional<string> x = "abc"; |
| 111 | + Optional<string> y = "xyz"; |
| 112 | + OptionalEqualityComparer<string> comparer = new(); |
| 113 | + |
| 114 | + // When |
| 115 | + bool result1 = comparer.Equals(x, y); |
| 116 | + bool result2 = comparer.Equals(y, x); |
| 117 | + |
| 118 | + // Then |
| 119 | + Assert.False(result1); |
| 120 | + Assert.False(result2); |
| 121 | + } |
| 122 | + |
| 123 | + [Fact(DisplayName = "OptionalEqualityComparer.Equals should return true when comparing identical Some values with value comparer")] |
| 124 | + public void OptionalEqualityComparerEqualsShouldReturnTrueWhenComparingIdenticalSomeValuesWithValueComparer() |
| 125 | + { |
| 126 | + // Given |
| 127 | + Optional<string> x = "abc"; |
| 128 | + Optional<string> y = "ABC"; |
| 129 | + OptionalEqualityComparer<string> comparer = new(new CaseInsensitiveStringComparer()); |
| 130 | + |
| 131 | + // When |
| 132 | + bool result1 = comparer.Equals(x, y); |
| 133 | + bool result2 = comparer.Equals(y, x); |
| 134 | + |
| 135 | + // Then |
| 136 | + Assert.True(result1); |
| 137 | + Assert.True(result2); |
| 138 | + } |
| 139 | + |
| 140 | + [Fact(DisplayName = "OptionalEqualityComparer.Equals should return false when comparing non-identical Some values with value comparer")] |
| 141 | + public void OptionalEqualityComparerEqualsShouldReturnFalseWhenComparingNonIdenticalSomeValuesWithValueComparer() |
| 142 | + { |
| 143 | + // Given |
| 144 | + Optional<string> x = "abc"; |
| 145 | + Optional<string> y = "XYZ"; |
| 146 | + OptionalEqualityComparer<string> comparer = new(new CaseInsensitiveStringComparer()); |
| 147 | + |
| 148 | + // When |
| 149 | + bool result1 = comparer.Equals(x, y); |
| 150 | + bool result2 = comparer.Equals(y, x); |
| 151 | + |
| 152 | + // Then |
| 153 | + Assert.False(result1); |
| 154 | + Assert.False(result2); |
| 155 | + } |
| 156 | + |
| 157 | + [Fact(DisplayName = "OptionalEqualityComparer.GetHashCode should return zero when the optional value is none.")] |
| 158 | + public void OptionalEqualityComparerGetHashCodeShouldReturnZeroWhenOptionalValueIsNone() |
| 159 | + { |
| 160 | + // Given |
| 161 | + const int expected = default; |
| 162 | + Optional<string> optional = Optional<string>.None; |
| 163 | + OptionalEqualityComparer<string> comparer = new(); |
| 164 | + |
| 165 | + // When |
| 166 | + int actual = comparer.GetHashCode(optional); |
| 167 | + |
| 168 | + // Then |
| 169 | + Assert.Equal(expected, actual); |
| 170 | + } |
| 171 | + |
| 172 | + private sealed class CaseInsensitiveStringComparer : EqualityComparer<string> |
| 173 | + { |
| 174 | + public override bool Equals(string? x, string? y) => string.Equals(x, y, StringComparison.InvariantCultureIgnoreCase); |
| 175 | + public override int GetHashCode(string obj) => obj.GetHashCode(); |
| 176 | + } |
| 177 | +} |
0 commit comments