|
15 | 15 | using System; |
16 | 16 | using System.Threading.Tasks; |
17 | 17 | using OnixLabs.Core.UnitTests.Data; |
18 | | -using Xunit; |
19 | 18 |
|
20 | 19 | namespace OnixLabs.Core.UnitTests; |
21 | 20 |
|
22 | 21 | public sealed class ObjectExtensionTests |
23 | 22 | { |
24 | | - [Theory(DisplayName = "IsWithinRangeInclusive should produce the expected result")] |
25 | | - [InlineData(2, 1, 3, true)] |
26 | | - [InlineData(1, 1, 3, true)] |
27 | | - [InlineData(3, 1, 3, true)] |
28 | | - [InlineData(0, 1, 3, false)] |
29 | | - [InlineData(4, 1, 3, false)] |
30 | | - public void IsWithinRangeInclusiveShouldProduceExpectedResult(int value, int min, int max, bool expected) |
| 23 | + [Fact(DisplayName = "Apply should produce the expected result (reference type)")] |
| 24 | + public void ApplyShouldProduceExpectedResultReferenceType() |
31 | 25 | { |
| 26 | + // Given |
| 27 | + Mutable value = new() { Value = 123 }; |
| 28 | + |
32 | 29 | // When |
33 | | - bool actual = value.IsWithinRangeInclusive(min, max); |
| 30 | + Mutable result = value.Apply(it => it.Value = 456); |
34 | 31 |
|
35 | 32 | // Then |
36 | | - Assert.Equal(expected, actual); |
| 33 | + Assert.Equal(456, value.Value); |
| 34 | + Assert.Same(value, result); |
37 | 35 | } |
38 | 36 |
|
39 | | - [Theory(DisplayName = "IsWithinRangeExclusive should produce the expected result")] |
40 | | - [InlineData(2, 1, 3, true)] |
41 | | - [InlineData(1, 1, 3, false)] |
42 | | - [InlineData(3, 1, 3, false)] |
43 | | - [InlineData(0, 1, 3, false)] |
44 | | - [InlineData(4, 1, 3, false)] |
45 | | - public void IsWithinRangeExclusiveShouldProduceExpectedResult(int value, int min, int max, bool expected) |
| 37 | + [Fact(DisplayName = "Apply should produce the expected result (value type)")] |
| 38 | + public void ApplyShouldProduceExpectedResultValueType() |
46 | 39 | { |
| 40 | + // Given |
| 41 | + int value = 123; |
| 42 | + |
47 | 43 | // When |
48 | | - bool actual = value.IsWithinRangeExclusive(min, max); |
| 44 | + value = value.Apply(it => it * 2); |
49 | 45 |
|
50 | 46 | // Then |
51 | | - Assert.Equal(expected, actual); |
| 47 | + Assert.Equal(246, value); |
52 | 48 | } |
53 | 49 |
|
54 | 50 | [Fact(DisplayName = "CompareToObject should produce zero if the current IComparable<T> is equal to the specified object.")] |
@@ -113,6 +109,51 @@ public void CompareToObjectShouldThrowArgumentExceptionIfSpecifiedObjectIsOfInco |
113 | 109 | Assert.Equal("Object must be of type System.Int32 (Parameter 'right')", exception.Message); |
114 | 110 | } |
115 | 111 |
|
| 112 | + [Theory(DisplayName = "IsWithinRangeInclusive should produce the expected result")] |
| 113 | + [InlineData(2, 1, 3, true)] |
| 114 | + [InlineData(1, 1, 3, true)] |
| 115 | + [InlineData(3, 1, 3, true)] |
| 116 | + [InlineData(0, 1, 3, false)] |
| 117 | + [InlineData(4, 1, 3, false)] |
| 118 | + public void IsWithinRangeInclusiveShouldProduceExpectedResult(int value, int min, int max, bool expected) |
| 119 | + { |
| 120 | + // When |
| 121 | + bool actual = value.IsWithinRangeInclusive(min, max); |
| 122 | + |
| 123 | + // Then |
| 124 | + Assert.Equal(expected, actual); |
| 125 | + } |
| 126 | + |
| 127 | + [Theory(DisplayName = "IsWithinRangeExclusive should produce the expected result")] |
| 128 | + [InlineData(2, 1, 3, true)] |
| 129 | + [InlineData(1, 1, 3, false)] |
| 130 | + [InlineData(3, 1, 3, false)] |
| 131 | + [InlineData(0, 1, 3, false)] |
| 132 | + [InlineData(4, 1, 3, false)] |
| 133 | + public void IsWithinRangeExclusiveShouldProduceExpectedResult(int value, int min, int max, bool expected) |
| 134 | + { |
| 135 | + // When |
| 136 | + bool actual = value.IsWithinRangeExclusive(min, max); |
| 137 | + |
| 138 | + // Then |
| 139 | + Assert.Equal(expected, actual); |
| 140 | + } |
| 141 | + |
| 142 | + [Fact(DisplayName = "Let should produce the expected result")] |
| 143 | + public void LetShouldProduceExpectedResult() |
| 144 | + { |
| 145 | + // Given |
| 146 | + const string value = "123"; |
| 147 | + |
| 148 | + // When |
| 149 | + int result = value |
| 150 | + .Let(int.Parse) |
| 151 | + .Let(it => it * 2); |
| 152 | + |
| 153 | + // Then |
| 154 | + Assert.Equal(246, result); |
| 155 | + } |
| 156 | + |
116 | 157 | [Fact(DisplayName = "ToRecordString should produce null when the object is null")] |
117 | 158 | public void ToRecordStringShouldProduceNullWhenObjectIsNull() |
118 | 159 | { |
@@ -361,4 +402,32 @@ public async Task ToSuccessAsyncShouldProduceTheExpectedResult() |
361 | 402 | Success<string> success = Assert.IsType<Success<string>>(result); |
362 | 403 | Assert.Equal(expected, success.Value); |
363 | 404 | } |
| 405 | + |
| 406 | + [Fact(DisplayName = "TryGetNonNull should produce the expected result (true)")] |
| 407 | + public void TryGetNotNullShouldProduceExpectedResultTrue() |
| 408 | + { |
| 409 | + // Given |
| 410 | + const string? value = "Hello, World!"; |
| 411 | + |
| 412 | + // When |
| 413 | + bool result = value.TryGetNonNull(out string output); |
| 414 | + |
| 415 | + // Then |
| 416 | + Assert.True(result); |
| 417 | + Assert.NotNull(output); |
| 418 | + } |
| 419 | + |
| 420 | + [Fact(DisplayName = "TryGetNonNull should produce the expected result (false)")] |
| 421 | + public void TryGetNotNullShouldProduceExpectedResultFalse() |
| 422 | + { |
| 423 | + // Given |
| 424 | + const string? value = null; |
| 425 | + |
| 426 | + // When |
| 427 | + bool result = value.TryGetNonNull(out string? output); |
| 428 | + |
| 429 | + // Then |
| 430 | + Assert.False(result); |
| 431 | + Assert.Null(output); |
| 432 | + } |
364 | 433 | } |
0 commit comments