|
| 1 | +using System; |
| 2 | +using FluentAssertions; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace Light.GuardClauses.Tests.ComparableAssertions; |
| 6 | + |
| 7 | +public static class MustBeApproximatelyTests |
| 8 | +{ |
| 9 | + [Theory] |
| 10 | + [InlineData(5.1, 5.0, 0.2)] |
| 11 | + [InlineData(10.3, 10.3, 0.01)] |
| 12 | + [InlineData(3.14159, 3.14, 0.002)] |
| 13 | + [InlineData(-42.0, -42.0001, 0.001)] |
| 14 | + public static void ValuesApproximatelyEqual_Double(double value, double other, double tolerance) => |
| 15 | + value.MustBeApproximately(other, tolerance).Should().Be(value); |
| 16 | + |
| 17 | + [Theory] |
| 18 | + [InlineData(5.1f, 5.0f, 0.2f)] |
| 19 | + [InlineData(10.3f, 10.3f, 0.01f)] |
| 20 | + [InlineData(3.14159f, 3.14f, 0.002f)] |
| 21 | + [InlineData(-42.0f, -42.0001f, 0.001f)] |
| 22 | + public static void ValuesApproximatelyEqual_Float(float value, float other, float tolerance) => |
| 23 | + value.MustBeApproximately(other, tolerance).Should().Be(value); |
| 24 | + |
| 25 | + [Theory] |
| 26 | + [InlineData(5.0, 5.3, 0.1)] |
| 27 | + [InlineData(100.0, 99.8, 0.1)] |
| 28 | + [InlineData(-20.0, -20.2, 0.1)] |
| 29 | + [InlineData(0.0001, 0.0002, 0.00005)] |
| 30 | + public static void ValuesNotApproximatelyEqual_Double(double value, double other, double tolerance) |
| 31 | + { |
| 32 | + var act = () => value.MustBeApproximately(other, tolerance, nameof(value)); |
| 33 | + |
| 34 | + var exceptionAssertion = act.Should().Throw<ArgumentOutOfRangeException>().Which; |
| 35 | + exceptionAssertion.Message.Should().Contain( |
| 36 | + $"{nameof(value)} must be approximately equal to {other} with a tolerance of {tolerance}, but it actually is {value}." |
| 37 | + ); |
| 38 | + exceptionAssertion.ParamName.Should().BeSameAs(nameof(value)); |
| 39 | + } |
| 40 | + |
| 41 | + [Theory] |
| 42 | + [InlineData(5.0f, 5.3f, 0.1f)] |
| 43 | + [InlineData(100.0f, 99.8f, 0.1f)] |
| 44 | + [InlineData(-20.0f, -20.2f, 0.1f)] |
| 45 | + [InlineData(0.0001f, 0.0002f, 0.00005f)] |
| 46 | + public static void ValuesNotApproximatelyEqual_Float(float value, float other, float tolerance) |
| 47 | + { |
| 48 | + var act = () => value.MustBeApproximately(other, tolerance, nameof(value)); |
| 49 | + |
| 50 | + var exceptionAssertion = act.Should().Throw<ArgumentOutOfRangeException>().Which; |
| 51 | + exceptionAssertion.Message.Should().Contain( |
| 52 | + $"{nameof(value)} must be approximately equal to {other} with a tolerance of {tolerance}, but it actually is {value}." |
| 53 | + ); |
| 54 | + exceptionAssertion.ParamName.Should().BeSameAs(nameof(value)); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public static void DefaultTolerance_Double() |
| 59 | + { |
| 60 | + // Should pass - difference is 0.00005 which is less than default tolerance 0.0001 |
| 61 | + const double value = 1.00005; |
| 62 | + value.MustBeApproximately(1.0).Should().Be(value); |
| 63 | + |
| 64 | + // Should throw - difference is 0.0002 which is greater than default tolerance 0.0001 |
| 65 | + Action act = () => 1.0002.MustBeApproximately(1.0, "parameter"); |
| 66 | + act.Should().Throw<ArgumentOutOfRangeException>() |
| 67 | + .WithParameterName("parameter"); |
| 68 | + } |
| 69 | + |
| 70 | + [Fact] |
| 71 | + public static void DefaultTolerance_Float() |
| 72 | + { |
| 73 | + // Should pass - difference is 0.00005f which is less than default tolerance 0.0001f |
| 74 | + const float value = 1.00005f; |
| 75 | + value.MustBeApproximately(1.0f).Should().Be(value); |
| 76 | + |
| 77 | + // Should throw - difference is 0.0002f which is greater than default tolerance 0.0001f |
| 78 | + Action act = () => 1.0002f.MustBeApproximately(1.0f, "parameter"); |
| 79 | + act.Should().Throw<ArgumentOutOfRangeException>() |
| 80 | + .WithParameterName("parameter"); |
| 81 | + } |
| 82 | + |
| 83 | + [Fact] |
| 84 | + public static void CustomException_Double() => |
| 85 | + Test.CustomException( |
| 86 | + 5.0, |
| 87 | + 5.3, |
| 88 | + (x, y, exceptionFactory) => x.MustBeApproximately(y, exceptionFactory) |
| 89 | + ); |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public static void CustomExceptionWithTolerance_Double() => |
| 93 | + Test.CustomException( |
| 94 | + 5.0, |
| 95 | + 5.5, |
| 96 | + 0.1, |
| 97 | + (x, y, z, exceptionFactory) => x.MustBeApproximately(y, z, exceptionFactory) |
| 98 | + ); |
| 99 | + |
| 100 | + [Fact] |
| 101 | + public static void CustomException_Float() => |
| 102 | + Test.CustomException( |
| 103 | + 5.0f, |
| 104 | + 5.3f, |
| 105 | + (x, y, exceptionFactory) => x.MustBeApproximately(y, exceptionFactory) |
| 106 | + ); |
| 107 | + |
| 108 | + [Fact] |
| 109 | + public static void CustomExceptionWithTolerance_Float() => |
| 110 | + Test.CustomException( |
| 111 | + 5.0f, |
| 112 | + 5.5f, |
| 113 | + 0.1f, |
| 114 | + (x, y, z, exceptionFactory) => x.MustBeApproximately(y, z, exceptionFactory) |
| 115 | + ); |
| 116 | + |
| 117 | + [Fact] |
| 118 | + public static void NoCustomExceptionThrown_Double() => |
| 119 | + 5.0.MustBeApproximately(5.05, 0.1, (_, _, _) => null).Should().Be(5.0); |
| 120 | + |
| 121 | + [Fact] |
| 122 | + public static void NoCustomExceptionThrown_Float() => |
| 123 | + 5.0f.MustBeApproximately(5.05f, 0.1f, (_, _, _) => null).Should().Be(5.0f); |
| 124 | + |
| 125 | + [Fact] |
| 126 | + public static void CustomMessage_Double() => |
| 127 | + Test.CustomMessage<ArgumentOutOfRangeException>( |
| 128 | + message => 100.0.MustBeApproximately(101.0, 0.5, message: message) |
| 129 | + ); |
| 130 | + |
| 131 | + [Fact] |
| 132 | + public static void CustomMessage_Float() => |
| 133 | + Test.CustomMessage<ArgumentOutOfRangeException>( |
| 134 | + message => 100.0f.MustBeApproximately(101.0f, 0.5f, message: message) |
| 135 | + ); |
| 136 | + |
| 137 | + [Fact] |
| 138 | + public static void CallerArgumentExpression_Double() |
| 139 | + { |
| 140 | + const double seventyEightO1 = 78.1; |
| 141 | + |
| 142 | + var act = () => seventyEightO1.MustBeApproximately(3.0); |
| 143 | + |
| 144 | + act.Should().Throw<ArgumentOutOfRangeException>() |
| 145 | + .WithParameterName(nameof(seventyEightO1)); |
| 146 | + } |
| 147 | + |
| 148 | + [Fact] |
| 149 | + public static void CallerArgumentExpressionWithTolerance_Double() |
| 150 | + { |
| 151 | + const double pi = 3.14159; |
| 152 | + |
| 153 | + var act = () => pi.MustBeApproximately(3.0, 0.1); |
| 154 | + |
| 155 | + act.Should().Throw<ArgumentOutOfRangeException>() |
| 156 | + .WithParameterName(nameof(pi)); |
| 157 | + } |
| 158 | + |
| 159 | + [Fact] |
| 160 | + public static void CallerArgumentExpression_Float() |
| 161 | + { |
| 162 | + const float seventyEightO1 = 78.1f; |
| 163 | + |
| 164 | + var act = () => seventyEightO1.MustBeApproximately(3.0f); |
| 165 | + |
| 166 | + act.Should().Throw<ArgumentOutOfRangeException>() |
| 167 | + .WithParameterName(nameof(seventyEightO1)); |
| 168 | + } |
| 169 | + |
| 170 | + [Fact] |
| 171 | + public static void CallerArgumentExpressionWithTolerance_Float() |
| 172 | + { |
| 173 | + const float pi = 3.14159f; |
| 174 | + |
| 175 | + var act = () => pi.MustBeApproximately(3.0f, 0.1f); |
| 176 | + |
| 177 | + act.Should().Throw<ArgumentOutOfRangeException>() |
| 178 | + .WithParameterName(nameof(pi)); |
| 179 | + } |
| 180 | + |
| 181 | +#if NET8_0 |
| 182 | + [Theory] |
| 183 | + [InlineData(5.1, 5.0, 0.2)] |
| 184 | + [InlineData(10.3, 10.3, 0.01)] |
| 185 | + [InlineData(3.14159, 3.14, 0.002)] |
| 186 | + [InlineData(-42.0, -42.0001, 0.001)] |
| 187 | + public static void ValuesApproximatelyEqual_Generic(double value, double other, double tolerance) => |
| 188 | + value.MustBeApproximately<double>(other, tolerance).Should().Be(value); |
| 189 | + |
| 190 | + [Theory] |
| 191 | + [InlineData(5.0, 5.3, 0.1)] |
| 192 | + [InlineData(100.0, 99.8, 0.1)] |
| 193 | + [InlineData(-20.0, -20.2, 0.1)] |
| 194 | + [InlineData(0.0001, 0.0002, 0.00005)] |
| 195 | + public static void ValuesNotApproximatelyEqual_Generic(double value, double other, double tolerance) |
| 196 | + { |
| 197 | + var act = () => value.MustBeApproximately<double>(other, tolerance, nameof(value)); |
| 198 | + |
| 199 | + var exceptionAssertion = act.Should().Throw<ArgumentOutOfRangeException>().Which; |
| 200 | + exceptionAssertion.Message.Should().Contain( |
| 201 | + $"{nameof(value)} must be approximately equal to {other} with a tolerance of {tolerance}, but it actually is {value}." |
| 202 | + ); |
| 203 | + exceptionAssertion.ParamName.Should().BeSameAs(nameof(value)); |
| 204 | + } |
| 205 | + |
| 206 | + [Fact] |
| 207 | + public static void CustomExceptionWithTolerance_Generic() => |
| 208 | + Test.CustomException( |
| 209 | + 5.0, |
| 210 | + 5.3, |
| 211 | + 0.2, |
| 212 | + (x, y, t, exceptionFactory) => x.MustBeApproximately<double>(y, t, exceptionFactory) |
| 213 | + ); |
| 214 | + |
| 215 | + [Fact] |
| 216 | + public static void NoCustomExceptionThrown_Generic() => |
| 217 | + 5.0.MustBeApproximately<double>(5.05, 0.1, (_, _, _) => null).Should().Be(5.0); |
| 218 | + |
| 219 | + [Fact] |
| 220 | + public static void CustomMessage_Generic() => |
| 221 | + Test.CustomMessage<ArgumentOutOfRangeException>( |
| 222 | + message => 100.0.MustBeApproximately<double>(101.0, 0.5, message: message) |
| 223 | + ); |
| 224 | + |
| 225 | + [Fact] |
| 226 | + public static void CallerArgumentExpressionWithTolerance_Generic() |
| 227 | + { |
| 228 | + const double seventyEightO1 = 78.1; |
| 229 | + |
| 230 | + var act = () => seventyEightO1.MustBeApproximately<double>(3.0, 10.0); |
| 231 | + |
| 232 | + act.Should().Throw<ArgumentOutOfRangeException>() |
| 233 | + .WithParameterName(nameof(seventyEightO1)); |
| 234 | + } |
| 235 | +#endif |
| 236 | +} |
0 commit comments