Skip to content

feature/result-extensions #91

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

Merged
merged 1 commit into from
Jan 5, 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
30 changes: 30 additions & 0 deletions OnixLabs.Core.UnitTests/ObjectExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

using System;
using System.Threading.Tasks;
using OnixLabs.Core.UnitTests.Data;
using Xunit;

Expand Down Expand Up @@ -223,4 +224,33 @@ public void ToStringOrNullShouldProduceExpectedResultWhenObjectIsNotNull()
// Then
Assert.Equal(expected, actual);
}

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

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

// 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()
{
// Given
const string expected = "abc";

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

// Then
Success<string> success = Assert.IsType<Success<string>>(result);
Assert.Equal(expected, success.Value);
}
}
2 changes: 1 addition & 1 deletion OnixLabs.Core.UnitTests/OptionalEqualityComparerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void OptionalEqualityComparerEqualsShouldReturnFalseWhenComparingNonIdent
public void OptionalEqualityComparerGetHashCodeShouldReturnZeroWhenOptionalValueIsNone()
{
// Given
const int expected = default;
const int expected = 0;
Optional<string> optional = Optional<string>.None;
OptionalEqualityComparer<string> comparer = new();

Expand Down
14 changes: 7 additions & 7 deletions OnixLabs.Core.UnitTests/OptionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
public void OptionalOfShouldProduceExpectedResultForImplicitDefaultValues()
{
// Given / When
Optional<int> number = Optional<int>.Of(default);
Optional<string> text = Optional<string>.Of(default);
Optional<int> number = Optional<int>.Of(0);
Optional<string> text = Optional<string>.Of(null);

// Then
Assert.True(Optional<int>.IsNone(number));
Expand Down Expand Up @@ -100,7 +100,7 @@
{
// Given / When
Optional<int> number = Optional<int>.Of((int?)null);
Optional<Guid> identifier = Optional<Guid>.Of((Guid?)default);
Optional<Guid> identifier = Optional<Guid>.Of((Guid?)null);

// Then
Assert.False(number.HasValue);
Expand Down Expand Up @@ -171,7 +171,7 @@
public void OptionalImplicitOperatorShouldProduceExpectedNoneResult()
{
// Given / When
const string? value = default;
const string? value = null;
Optional<string> optional = value;

// Then
Expand Down Expand Up @@ -297,7 +297,7 @@
public void OptionalNoneGetHashCodeShouldProduceExpectedResult()
{
// Given
const int expected = default;
const int expected = 0;
Optional<int> optional = Optional<int>.None;

// When
Expand Down Expand Up @@ -335,8 +335,8 @@
string? actualText = text.GetValueOrDefault();

// Then
Assert.Equal(default, actualNumber);
Assert.Equal(default, actualText);
Assert.Equal(0, actualNumber);
Assert.Equal(null, actualText);

Check warning on line 339 in OnixLabs.Core.UnitTests/OptionalTests.cs

View workflow job for this annotation

GitHub Actions / build

Do not use Assert.Equal() to check for null value. Use Assert.Null instead. (https://xunit.net/xunit.analyzers/rules/xUnit2003)

Check warning on line 339 in OnixLabs.Core.UnitTests/OptionalTests.cs

View workflow job for this annotation

GitHub Actions / build

Do not use Assert.Equal() to check for null value. Use Assert.Null instead. (https://xunit.net/xunit.analyzers/rules/xUnit2003)

Check warning on line 339 in OnixLabs.Core.UnitTests/OptionalTests.cs

View workflow job for this annotation

GitHub Actions / build

Do not use Assert.Equal() to check for null value. Use Assert.Null instead. (https://xunit.net/xunit.analyzers/rules/xUnit2003)

Check warning on line 339 in OnixLabs.Core.UnitTests/OptionalTests.cs

View workflow job for this annotation

GitHub Actions / build

Do not use Assert.Equal() to check for null value. Use Assert.Null instead. (https://xunit.net/xunit.analyzers/rules/xUnit2003)
}

[Fact(DisplayName = "Optional Some.GetValueOrDefault with default value should produce the expected result.")]
Expand Down
8 changes: 4 additions & 4 deletions OnixLabs.Core.UnitTests/PreconditionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,13 @@ public void CheckNotNullOfValueTypeShouldThrowInvalidOperationExceptionWhenCondi
{
// Given
int? expected = null;
int actual = default;
int actual = 0;

// When
Exception exception = Assert.Throws<InvalidOperationException>(() => actual = CheckNotNull(expected));

// Then
Assert.Equal(default, actual);
Assert.Equal(0, actual);
Assert.Equal("Argument must not be null.", exception.Message);
}

Expand Down Expand Up @@ -593,13 +593,13 @@ public void RequireNotNullOfValueTypeShouldThrowInvalidOperationExceptionWhenCon
{
// Given
int? expected = null;
int actual = default;
int actual = 0;

// When
Exception exception = Assert.Throws<ArgumentNullException>(() => actual = RequireNotNull(expected));

// Then
Assert.Equal(default, actual);
Assert.Equal(0, actual);
Assert.Equal("Argument must not be null.", exception.Message);
}

Expand Down
6 changes: 3 additions & 3 deletions OnixLabs.Core.UnitTests/ResultExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public async Task ResultGetValueOrDefaultAsyncShouldReturnDefaultWhenResultIsFai
string? actualText = await textTask.GetValueOrDefaultAsync();

// Then
Assert.Equal(default, actualNumber);
Assert.Equal(0, actualNumber);
Assert.Null(actualText);
}

Expand Down Expand Up @@ -364,8 +364,8 @@ public async Task ResultGetValueOrNoneAsyncShouldReturnOptionalValueWhenResultIs
public async Task ResultGetValueOrNoneAsyncShouldReturnNoneWhenResultIsSuccessAndValueIsNone()
{
// Given
Result<int> numberResult = Result<int>.Success(default);
Result<string> textResult = Result<string>.Success(default!);
Result<int> numberResult = Result<int>.Success(0);
Result<string> textResult = Result<string>.Success(null!);

// When
Optional<int> actualNumber = await Task.FromResult(numberResult).GetValueOrNoneAsync();
Expand Down
Loading
Loading