Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/to-optional #95

Merged
merged 1 commit into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<NeutralLanguage>en</NeutralLanguage>
<Copyright>Copyright © ONIXLabs 2020</Copyright>
<RepositoryUrl>https://github.com/onix-labs/onixlabs-dotnet</RepositoryUrl>
<Version>11.3.0</Version>
<PackageVersion>11.3.0</PackageVersion>
<AssemblyVersion>11.3.0</AssemblyVersion>
<Version>12.0.0</Version>
<PackageVersion>12.0.0</PackageVersion>
<AssemblyVersion>12.0.0</AssemblyVersion>
</PropertyGroup>
</Project>
120 changes: 114 additions & 6 deletions OnixLabs.Core.UnitTests/ObjectExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,29 +225,137 @@ public void ToStringOrNullShouldProduceExpectedResultWhenObjectIsNotNull()
Assert.Equal(expected, actual);
}

[Fact(DisplayName = "ToSuccessResult should produce the expected result")]
public void ToSuccessResultShouldProduceTheExpectedResult()
[Fact(DisplayName = "ToOptional should produce the expected result when using a non-null reference type")]
public void ToOptionalShouldProduceExpectedResultWhenUsingNonNullReferenceType()
{
// Given
const string expected = "abc";

// When
Result<string> result = expected.ToSuccessResult();
Optional<string> optional = expected.ToOptional();

// Then
Some<string> some = Assert.IsType<Some<string>>(optional);
Assert.Equal(expected, some.Value);
}

[Fact(DisplayName = "ToOptional should produce the expected result when using a null reference type")]
public void ToOptionalShouldProduceExpectedResultWhenUsingNullReferenceType()
{
// Given
const string? expected = null;

// When
Optional<string> optional = expected.ToOptional();

// Then
Assert.IsType<None<string>>(optional);
}

[Fact(DisplayName = "ToOptional should produce the expected result when using a non-null value type")]
public void ToOptionalShouldProduceExpectedResultWhenUsingNonNullValueType()
{
// Given
const int expected = 123;

// When
Optional<int> optional = expected.ToOptional();

// Then
Some<int> some = Assert.IsType<Some<int>>(optional);
Assert.Equal(expected, some.Value);
}

[Fact(DisplayName = "ToOptional should produce the expected result when using a null value type")]
public void ToOptionalShouldProduceExpectedResultWhenUsingNullValueType()
{
// Given
int? expected = null;

// When
Optional<int> optional = expected.ToOptional();

// Then
Assert.IsType<None<int>>(optional);
}

[Fact(DisplayName = "ToOptionalAsync should produce the expected result when using a non-null reference type")]
public async Task ToOptionalAsyncShouldProduceExpectedResultWhenUsingNonNullReferenceType()
{
// Given
const string expected = "abc";

// When
Optional<string> optional = await Task.FromResult<string?>(expected).ToOptionalAsync();

// Then
Some<string> some = Assert.IsType<Some<string>>(optional);
Assert.Equal(expected, some.Value);
}

[Fact(DisplayName = "ToOptionalAsync should produce the expected result when using a null reference type")]
public async Task ToOptionalAsyncShouldProduceExpectedResultWhenUsingNullReferenceType()
{
// Given
const string? expected = null;

// When
Optional<string> optional = await Task.FromResult(expected).ToOptionalAsync();

// Then
Assert.IsType<None<string>>(optional);
}

[Fact(DisplayName = "ToOptionalAsync should produce the expected result when using a non-null value type")]
public async Task ToOptionalAsyncShouldProduceExpectedResultWhenUsingNonNullValueType()
{
// Given
const int expected = 123;

// When
Optional<int> optional = await Task.FromResult(expected).ToOptionalAsync();

// Then
Some<int> some = Assert.IsType<Some<int>>(optional);
Assert.Equal(expected, some.Value);
}

[Fact(DisplayName = "ToOptionalAsync should produce the expected result when using a null value type")]
public async Task ToOptionalAsyncShouldProduceExpectedResultWhenUsingNullValueType()
{
// Given
int? expected = null;

// When
Optional<int> optional = await Task.FromResult(expected).ToOptionalAsync();

// Then
Assert.IsType<None<int>>(optional);
}

[Fact(DisplayName = "ToSuccess should produce the expected result")]
public void ToSuccessShouldProduceTheExpectedResult()
{
// Given
const string expected = "abc";

// When
Result<string> result = expected.ToSuccess();

// Then
Success<string> success = Assert.IsType<Success<string>>(result);
Assert.Equal(expected, success.Value);
}

[Fact(DisplayName = "ToSuccessResultAsync should produce the expected result")]
public async Task ToSuccessResultAsyncShouldProduceTheExpectedResult()
[Fact(DisplayName = "ToSuccessAsync should produce the expected result")]
public async Task ToSuccessAsyncShouldProduceTheExpectedResult()
{
// Given
const string expected = "abc";

// When
Task<string> task = Task.FromResult(expected);
Result<string> result = await task.ToSuccessResultAsync();
Result<string> result = await task.ToSuccessAsync();

// Then
Success<string> success = Assert.IsType<Success<string>>(result);
Expand Down
40 changes: 38 additions & 2 deletions OnixLabs.Core/Extensions.Object.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,49 @@ public static string ToRecordString(this object? value)
/// <returns>Returns a <see cref="String"/> representation of the current <see cref="Object"/>, or a string literal null if the current object is <see langword="null"/>.</returns>
public static string ToStringOrNull(this object? value) => value?.ToString() ?? Null;

/// <summary>
/// Obtains an <see cref="Optional{T}"/> representation of the current <see cref="Object"/>.
/// </summary>
/// <param name="value">The <see cref="Object"/> to wrap as an <see cref="Optional{T}"/> value.</param>
/// <typeparam name="T">The underlying type of the value.</typeparam>
/// <returns>Returns an <see cref="Optional{T}"/> representation of the current <see cref="Object"/>.</returns>
public static Optional<T> ToOptional<T>(this T? value) where T : notnull => Optional<T>.Of(value);

/// <summary>
/// Obtains an <see cref="Optional{T}"/> representation of the current <see cref="Object"/>.
/// </summary>
/// <param name="value">The <see cref="Object"/> to wrap as an <see cref="Optional{T}"/> value.</param>
/// <typeparam name="T">The underlying type of the value.</typeparam>
/// <returns>Returns an <see cref="Optional{T}"/> representation of the current <see cref="Object"/>.</returns>
public static Optional<T> ToOptional<T>(this T? value) where T : struct => Optional<T>.Of(value);

/// <summary>
/// Asynchronously obtains an <see cref="Optional{T}"/> representation of the current <see cref="Object"/>.
/// </summary>
/// <param name="value">The <see cref="Object"/> to wrap as an <see cref="Optional{T}"/> value.</param>
/// /// <param name="token">The cancellation token that can be used to cancel long-running tasks.</param>
/// <typeparam name="T">The underlying type of the value.</typeparam>
/// <returns>Returns an <see cref="Optional{T}"/> representation of the current <see cref="Object"/>.</returns>
public static async Task<Optional<T>> ToOptionalAsync<T>(this Task<T?> value, CancellationToken token = default) where T : notnull =>
Optional<T>.Of(await value.WaitAsync(token).ConfigureAwait(false));

/// <summary>
/// Asynchronously obtains an <see cref="Optional{T}"/> representation of the current <see cref="Object"/>.
/// </summary>
/// <param name="value">The <see cref="Object"/> to wrap as an <see cref="Optional{T}"/> value.</param>
/// /// <param name="token">The cancellation token that can be used to cancel long-running tasks.</param>
/// <typeparam name="T">The underlying type of the value.</typeparam>
/// <returns>Returns an <see cref="Optional{T}"/> representation of the current <see cref="Object"/>.</returns>
public static async Task<Optional<T>> ToOptionalAsync<T>(this Task<T?> value, CancellationToken token = default) where T : struct =>
Optional<T>.Of(await value.WaitAsync(token).ConfigureAwait(false));

/// <summary>
/// Obtains a <see cref="Success{T}"/> representation of the current <see cref="Object"/>.
/// </summary>
/// <param name="value">The <see cref="Object"/> to wrap as a <see cref="Success{T}"/> result.</param>
/// <typeparam name="T">The underlying type of the value.</typeparam>
/// <returns>Returns a <see cref="Success{T}"/> representation of the current <see cref="Object"/>.</returns>
public static Result<T> ToSuccessResult<T>(this T value) => Result<T>.Success(value);
public static Success<T> ToSuccess<T>(this T value) => Result<T>.Success(value);

/// <summary>
/// Asynchronously obtains a <see cref="Success{T}"/> representation of the current <see cref="Object"/>.
Expand All @@ -188,6 +224,6 @@ public static string ToRecordString(this object? value)
/// <param name="token">The cancellation token that can be used to cancel long-running tasks.</param>
/// <typeparam name="T">The underlying type of the value.</typeparam>
/// <returns>Returns a <see cref="Success{T}"/> representation of the current <see cref="Object"/>.</returns>
public static async Task<Result<T>> ToSuccessResultAsync<T>(this Task<T> value, CancellationToken token = default) =>
public static async Task<Success<T>> ToSuccessAsync<T>(this Task<T> value, CancellationToken token = default) =>
Result<T>.Success(await value.WaitAsync(token).ConfigureAwait(false));
}