Skip to content

Commit 852ece7

Browse files
Added TryGetNonNull extension method, and tests, and bumped version to 12.2.0 (#102)
1 parent 2f566f3 commit 852ece7

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

Directory.Build.props

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
<!--GLOBAL-->
44
<PropertyGroup>
5-
<Version>12.1.1</Version>
6-
<PackageVersion>12.1.1</PackageVersion>
7-
<AssemblyVersion>12.1.1</AssemblyVersion>
5+
<Version>12.2.0</Version>
6+
<PackageVersion>12.2.0</PackageVersion>
7+
<AssemblyVersion>12.2.0</AssemblyVersion>
88
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
99
<LangVersion>13</LangVersion>
1010
<Nullable>enable</Nullable>

OnixLabs.Core.UnitTests/ObjectExtensionTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,4 +360,32 @@ public async Task ToSuccessAsyncShouldProduceTheExpectedResult()
360360
Success<string> success = Assert.IsType<Success<string>>(result);
361361
Assert.Equal(expected, success.Value);
362362
}
363+
364+
[Fact(DisplayName = "TryGetNonNull should produce the expected result (true)")]
365+
public void TryGetNotNullShouldProduceExpectedResultTrue()
366+
{
367+
// Given
368+
const string? value = "Hello, World!";
369+
370+
// When
371+
bool result = value.TryGetNonNull(out string output);
372+
373+
// Then
374+
Assert.True(result);
375+
Assert.NotNull(output);
376+
}
377+
378+
[Fact(DisplayName = "TryGetNonNull should produce the expected result (false)")]
379+
public void TryGetNotNullShouldProduceExpectedResultFalse()
380+
{
381+
// Given
382+
const string? value = null;
383+
384+
// When
385+
bool result = value.TryGetNonNull(out string? output);
386+
387+
// Then
388+
Assert.False(result);
389+
Assert.Null(output);
390+
}
363391
}

OnixLabs.Core/Extensions.Object.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Collections;
1717
using System.Collections.Generic;
1818
using System.ComponentModel;
19+
using System.Diagnostics.CodeAnalysis;
1920
using System.Reflection;
2021
using System.Text;
2122
using System.Threading;
@@ -226,4 +227,23 @@ public static async Task<Optional<T>> ToOptionalAsync<T>(this Task<T?> value, Ca
226227
/// <returns>Returns a <see cref="Success{T}"/> representation of the current <see cref="Object"/>.</returns>
227228
public static async Task<Success<T>> ToSuccessAsync<T>(this Task<T> value, CancellationToken token = default) =>
228229
Result<T>.Success(await value.WaitAsync(token).ConfigureAwait(false));
230+
231+
/// <summary>
232+
/// Attempts to extract a non-null value from the current nullable <see cref="Object"/>.
233+
/// </summary>
234+
/// <param name="value">The current nullable <see cref="Object"/> to test for nullability.</param>
235+
/// <param name="result">The non-null value when this method returns <see langword="true"/>; otherwise, <see langword="null"/>.</param>
236+
/// <typeparam name="T">The underlying type of the value.</typeparam>
237+
/// <returns>Returns <see langword="true"/> if the current nullable <see cref="Object"/> is not null; otherwise, <see langword="false"/>.</returns>
238+
public static bool TryGetNonNull<T>(this T? value, [NotNullWhen(true)] out T result)
239+
{
240+
if (value is null)
241+
{
242+
result = default!;
243+
return false;
244+
}
245+
246+
result = value;
247+
return true;
248+
}
229249
}

0 commit comments

Comments
 (0)