From 746943d03be4b9a1d19eb5ee1d5e75b211959a6d Mon Sep 17 00:00:00 2001 From: Matthew Layton Date: Sun, 5 Jan 2025 12:27:27 +0000 Subject: [PATCH] Added result extensions. --- .../ObjectExtensionTests.cs | 30 ++++++ .../OptionalEqualityComparerTests.cs | 2 +- OnixLabs.Core.UnitTests/OptionalTests.cs | 14 +-- OnixLabs.Core.UnitTests/PreconditionTests.cs | 8 +- .../ResultExtensionTests.cs | 6 +- OnixLabs.Core.UnitTests/ResultGenericTests.cs | 93 ++++++++++++------- OnixLabs.Core.UnitTests/ResultTests.cs | 56 +++++------ OnixLabs.Core/Extensions.Object.cs | 20 ++++ OnixLabs.Core/Linq/Extensions.IEnumerable.cs | 4 +- OnixLabs.Core/Optional.cs | 2 +- .../Reflection/TypeDeclarationFlags.cs | 2 +- OnixLabs.Core/Result.Generic.cs | 13 ++- OnixLabs.Core/Result.cs | 2 +- .../TestDataGenerator.cs | 2 +- .../BigDecimal.Arithmetic.Round.cs | 4 +- .../BigDecimal.Convertible.Implicit.cs | 22 ++--- OnixLabs.Numerics/BigDecimal.cs | 2 +- .../NumberInfoParser.Sanitize.cs | 2 +- OnixLabs.Numerics/NumericsExtensions.cs | 6 +- .../Extensions.HashAlgorithm.cs | 8 +- .../NamedHash.Parse.cs | 32 +++++++ OnixLabs.Security.Cryptography/Sha3.cs | 2 +- 22 files changed, 227 insertions(+), 105 deletions(-) diff --git a/OnixLabs.Core.UnitTests/ObjectExtensionTests.cs b/OnixLabs.Core.UnitTests/ObjectExtensionTests.cs index 1c82dc2..8c4a0cc 100644 --- a/OnixLabs.Core.UnitTests/ObjectExtensionTests.cs +++ b/OnixLabs.Core.UnitTests/ObjectExtensionTests.cs @@ -13,6 +13,7 @@ // limitations under the License. using System; +using System.Threading.Tasks; using OnixLabs.Core.UnitTests.Data; using Xunit; @@ -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 result = expected.ToSuccessResult(); + + // Then + Success success = Assert.IsType>(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 task = Task.FromResult(expected); + Result result = await task.ToSuccessResultAsync(); + + // Then + Success success = Assert.IsType>(result); + Assert.Equal(expected, success.Value); + } } diff --git a/OnixLabs.Core.UnitTests/OptionalEqualityComparerTests.cs b/OnixLabs.Core.UnitTests/OptionalEqualityComparerTests.cs index 1180f4a..72ba7ff 100644 --- a/OnixLabs.Core.UnitTests/OptionalEqualityComparerTests.cs +++ b/OnixLabs.Core.UnitTests/OptionalEqualityComparerTests.cs @@ -158,7 +158,7 @@ public void OptionalEqualityComparerEqualsShouldReturnFalseWhenComparingNonIdent public void OptionalEqualityComparerGetHashCodeShouldReturnZeroWhenOptionalValueIsNone() { // Given - const int expected = default; + const int expected = 0; Optional optional = Optional.None; OptionalEqualityComparer comparer = new(); diff --git a/OnixLabs.Core.UnitTests/OptionalTests.cs b/OnixLabs.Core.UnitTests/OptionalTests.cs index 18b2507..7eb8ca9 100644 --- a/OnixLabs.Core.UnitTests/OptionalTests.cs +++ b/OnixLabs.Core.UnitTests/OptionalTests.cs @@ -42,8 +42,8 @@ public void OptionalNoneShouldProduceExpectedResult() public void OptionalOfShouldProduceExpectedResultForImplicitDefaultValues() { // Given / When - Optional number = Optional.Of(default); - Optional text = Optional.Of(default); + Optional number = Optional.Of(0); + Optional text = Optional.Of(null); // Then Assert.True(Optional.IsNone(number)); @@ -100,7 +100,7 @@ public void OptionalOfShouldProduceExpectedResultForNullNullableStructValues() { // Given / When Optional number = Optional.Of((int?)null); - Optional identifier = Optional.Of((Guid?)default); + Optional identifier = Optional.Of((Guid?)null); // Then Assert.False(number.HasValue); @@ -171,7 +171,7 @@ public void OptionalImplicitOperatorShouldProduceExpectedSomeResult() public void OptionalImplicitOperatorShouldProduceExpectedNoneResult() { // Given / When - const string? value = default; + const string? value = null; Optional optional = value; // Then @@ -297,7 +297,7 @@ public void OptionalSomeGetHashCodeShouldProduceExpectedResult() public void OptionalNoneGetHashCodeShouldProduceExpectedResult() { // Given - const int expected = default; + const int expected = 0; Optional optional = Optional.None; // When @@ -335,8 +335,8 @@ public void OptionalNoneGetValueOrDefaultShouldProduceExpectedResult() string? actualText = text.GetValueOrDefault(); // Then - Assert.Equal(default, actualNumber); - Assert.Equal(default, actualText); + Assert.Equal(0, actualNumber); + Assert.Equal(null, actualText); } [Fact(DisplayName = "Optional Some.GetValueOrDefault with default value should produce the expected result.")] diff --git a/OnixLabs.Core.UnitTests/PreconditionTests.cs b/OnixLabs.Core.UnitTests/PreconditionTests.cs index fa9a086..e7069fd 100644 --- a/OnixLabs.Core.UnitTests/PreconditionTests.cs +++ b/OnixLabs.Core.UnitTests/PreconditionTests.cs @@ -217,13 +217,13 @@ public void CheckNotNullOfValueTypeShouldThrowInvalidOperationExceptionWhenCondi { // Given int? expected = null; - int actual = default; + int actual = 0; // When Exception exception = Assert.Throws(() => actual = CheckNotNull(expected)); // Then - Assert.Equal(default, actual); + Assert.Equal(0, actual); Assert.Equal("Argument must not be null.", exception.Message); } @@ -593,13 +593,13 @@ public void RequireNotNullOfValueTypeShouldThrowInvalidOperationExceptionWhenCon { // Given int? expected = null; - int actual = default; + int actual = 0; // When Exception exception = Assert.Throws(() => actual = RequireNotNull(expected)); // Then - Assert.Equal(default, actual); + Assert.Equal(0, actual); Assert.Equal("Argument must not be null.", exception.Message); } diff --git a/OnixLabs.Core.UnitTests/ResultExtensionTests.cs b/OnixLabs.Core.UnitTests/ResultExtensionTests.cs index e3dd2f6..e452169 100644 --- a/OnixLabs.Core.UnitTests/ResultExtensionTests.cs +++ b/OnixLabs.Core.UnitTests/ResultExtensionTests.cs @@ -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); } @@ -364,8 +364,8 @@ public async Task ResultGetValueOrNoneAsyncShouldReturnOptionalValueWhenResultIs public async Task ResultGetValueOrNoneAsyncShouldReturnNoneWhenResultIsSuccessAndValueIsNone() { // Given - Result numberResult = Result.Success(default); - Result textResult = Result.Success(default!); + Result numberResult = Result.Success(0); + Result textResult = Result.Success(null!); // When Optional actualNumber = await Task.FromResult(numberResult).GetValueOrNoneAsync(); diff --git a/OnixLabs.Core.UnitTests/ResultGenericTests.cs b/OnixLabs.Core.UnitTests/ResultGenericTests.cs index d291e5a..3a85bf4 100644 --- a/OnixLabs.Core.UnitTests/ResultGenericTests.cs +++ b/OnixLabs.Core.UnitTests/ResultGenericTests.cs @@ -217,20 +217,49 @@ public void ResultEqualityShouldProduceExpectedResult() Assert.True(Result.Failure(FailureException) == Result.Failure(FailureException)); Assert.False(Result.Success(1) == Result.Success(2)); Assert.False(Result.Success(1) == Result.Failure(FailureException)); - Assert.False(Result.Success(1) == null); - Assert.False(null == Result.Success(1)); + Assert.NotNull(Result.Success(1)); + Assert.NotNull(Result.Success(1)); // Inequality Operator Assert.True(Result.Success(1) != Result.Failure(FailureException)); Assert.True(Result.Failure(FailureException) != Result.Failure(new Exception())); - Assert.True(Result.Success(1) != null); - Assert.True(null != Result.Success(1)); + Assert.NotNull(Result.Success(1)); + Assert.NotNull(Result.Success(1)); // Hash Code Generation Assert.True(Result.Success(1).GetHashCode() == Result.Success(1).GetHashCode()); Assert.True(Result.Failure(FailureException).GetHashCode() == Result.Failure(FailureException).GetHashCode()); } + [Fact(DisplayName = "Result.GetValue should produce the expected result when the result is Success.")] + public void ResultGetValueShouldProduceExpectedResultWhenResultIsSuccess() + { + // Given + const string expected = "abc"; + Result result = expected; + + // When + Result actual = result.GetValue(out string value); + + // Then + Assert.IsType>(actual); + Assert.Equal(expected, value); + } + + [Fact(DisplayName = "Result.GetValue should produce the expected result when the result is Failure.")] + public void ResultGetValueShouldProduceExpectedResultWhenResultIsFailure() + { + // Given + Result result = FailureException; + + // When + Result actual = result.GetValue(out string value); + + // Then + Assert.IsType>(actual); + Assert.Null(value); + } + [Fact(DisplayName = "Result.GetExceptionOrDefault from success should produce the expected result.")] public void ResultGetExceptionOrDefaultFromSuccessShouldProduceExpectedResult() { @@ -428,7 +457,7 @@ public async Task ResultMatchAsyncFuncTCancellationTokenTaskFromSuccessShouldPro Result result = Result.Success(1); bool isSuccess = false; bool isFailure = false; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When await result.MatchAsync( @@ -453,7 +482,7 @@ public async Task ResultMatchAsyncFuncTCancellationTokenTaskFromFailureShouldPro Result result = Result.Failure(FailureException); bool isSuccess = false; bool isFailure = false; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When await result.MatchAsync( @@ -524,7 +553,7 @@ public async Task ResultMatchAsyncActionTFuncExceptionCancellationTokenTaskFromS Result result = Result.Success(1); bool isSuccess = false; bool isFailure = false; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When await result.MatchAsync( @@ -549,7 +578,7 @@ public async Task ResultMatchAsyncActionTFuncExceptionCancellationTokenTaskFromF Result result = Result.Failure(FailureException); bool isSuccess = false; bool isFailure = false; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When await result.MatchAsync( @@ -574,7 +603,7 @@ public async Task ResultMatchAsyncFuncTTaskFuncExceptionTaskFromSuccessShouldPro Result result = Result.Success(1); bool isSuccess = false; bool isFailure = false; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When await result.MatchAsync( @@ -603,7 +632,7 @@ public async Task ResultMatchAsyncFuncTTaskFuncExceptionTaskFromFailureShouldPro Result result = Result.Failure(FailureException); bool isSuccess = false; bool isFailure = false; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When await result.MatchAsync( @@ -632,7 +661,7 @@ public async Task ResultMatchAsyncFuncTCancellationTokenTaskFuncExceptionCancell Result result = Result.Success(1); bool isSuccess = false; bool isFailure = false; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When await result.MatchAsync( @@ -661,7 +690,7 @@ public async Task ResultMatchAsyncFuncTCancellationTokenTaskFuncExceptionCancell Result result = Result.Failure(FailureException); bool isSuccess = false; bool isFailure = false; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When await result.MatchAsync( @@ -757,7 +786,7 @@ public async Task ResultMatchAsyncFuncTCancellationTokenTaskTResultFuncException // Given Result result = Result.Success(1); const int expected = 1; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When int actual = await result.MatchAsync( @@ -776,7 +805,7 @@ public async Task ResultMatchAsyncFuncTCancellationTokenTaskTResultFuncException // Given Result result = Result.Failure(FailureException); const int expected = 1; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When int actual = await result.MatchAsync( @@ -829,7 +858,7 @@ public async Task ResultMatchAsyncFuncTResultFuncExceptionCancellationTokenTaskT // Given Result result = Result.Success(1); const int expected = 1; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When int actual = await result.MatchAsync( @@ -848,7 +877,7 @@ public async Task ResultMatchAsyncFuncTResultFuncExceptionCancellationTokenTaskT // Given Result result = Result.Failure(FailureException); const int expected = 1; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When int actual = await result.MatchAsync( @@ -867,7 +896,7 @@ public async Task ResultMatchAsyncFuncTTaskTResultFuncExceptionTaskTResultFromSu // Given Result result = Result.Success(1); const int expected = 1; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When int actual = await result.MatchAsync( @@ -886,7 +915,7 @@ public async Task ResultMatchAsyncFuncTTaskTResultFuncExceptionTaskTResultFromFa // Given Result result = Result.Failure(FailureException); const int expected = 1; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When int actual = await result.MatchAsync( @@ -905,7 +934,7 @@ public async Task ResultMatchAsyncFuncTCancellationTokenTaskTResultFuncException // Given Result result = Result.Success(1); const int expected = 1; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When int actual = await result.MatchAsync( @@ -924,7 +953,7 @@ public async Task ResultMatchAsyncFuncTCancellationTokenTaskTResultFuncException // Given Result result = Result.Failure(FailureException); const int expected = 1; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When int actual = await result.MatchAsync( @@ -1027,7 +1056,7 @@ public async Task ResultSelectAsyncFuncTCancellationTokenTaskFromFailureShouldPr // Given Result result = Result.Failure(FailureException); Result expected = Result.Failure(FailureException); - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When Result actual = await result.SelectAsync((_, _) => Task.CompletedTask, token); @@ -1042,7 +1071,7 @@ public async Task ResultSelectAsyncFuncTCancellationTokenTaskFromSuccessShouldPr // Given Result result = Result.Success(1); Result expected = Result.Success(); - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When Result actual = await result.SelectAsync((_, _) => Task.CompletedTask, token); @@ -1057,7 +1086,7 @@ public async Task ResultSelectAsyncFuncTCancellationTokenTaskFromSuccessWithExce // Given Result result = Result.Success(1); Result expected = Result.Failure(FailureException); - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When Result actual = await result.SelectAsync((_, _) => throw FailureException, token); @@ -1156,7 +1185,7 @@ public async Task ResultSelectAsyncFunctionOfTResultWithCancellationTokenFromFai // Given Result result = Result.Failure(FailureException); Result expected = Result.Failure(FailureException); - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When Result actual = await result.SelectAsync((_, _) => Task.FromResult(1), token); @@ -1171,7 +1200,7 @@ public async Task ResultSelectAsyncFunctionOfTResultWithCancellationTokenFromSuc // Given Result result = Result.Success(2); Result expected = Result.Success(1); - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When Result actual = await result.SelectAsync((_, _) => Task.FromResult(1), token); @@ -1186,7 +1215,7 @@ public async Task ResultSelectAsyncFunctionOfTResultWithCancellationTokenFromSuc // Given Result result = Result.Success(1); Result expected = Result.Failure(FailureException); - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When Result actual = await result.SelectAsync((_, _) => throw FailureException, token); @@ -1285,7 +1314,7 @@ public async Task ResultSelectManyAsyncFunctionWithCancellationTokenFromFailureS // Given Result result = Result.Failure(FailureException); Result expected = Result.Failure(FailureException); - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When Result actual = await result.SelectManyAsync(async (_, _) => await Task.FromResult(Result.Success()), token); @@ -1300,7 +1329,7 @@ public async Task ResultSelectManyAsyncFunctionWithCancellationTokenFromSuccessS // Given Result result = Result.Success(1); Result expected = Result.Success(); - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When Result actual = await result.SelectManyAsync(async (_, _) => await Task.FromResult(Result.Success()), token); @@ -1315,7 +1344,7 @@ public async Task ResultSelectManyAsyncFunctionWithCancellationTokenFromSuccessW // Given Result result = Result.Success(1); Result expected = Result.Failure(FailureException); - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When Result actual = await result.SelectManyAsync((_, _) => throw FailureException, token); @@ -1414,7 +1443,7 @@ public async Task ResultSelectManyAsyncFunctionOfTResultWithCancellationTokenFro // Given Result result = Result.Failure(FailureException); Result expected = Result.Failure(FailureException); - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When Result actual = await result.SelectManyAsync(async (_, _) => await Task.FromResult(Result.Success(1)), token); @@ -1429,7 +1458,7 @@ public async Task ResultSelectManyAsyncFunctionOfTResultWithCancellationTokenFro // Given Result result = Result.Success(2); Result expected = Result.Success(1); - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When Result actual = await result.SelectManyAsync(async (_, _) => await Task.FromResult(Result.Success(1)), token); @@ -1444,7 +1473,7 @@ public async Task ResultSelectManyAsyncFunctionOfTResultWithCancellationTokenFro // Given Result result = Result.Success(1); Result expected = Result.Failure(FailureException); - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When Result actual = await result.SelectManyAsync((_, _) => throw FailureException, token); diff --git a/OnixLabs.Core.UnitTests/ResultTests.cs b/OnixLabs.Core.UnitTests/ResultTests.cs index e81c110..4982b21 100644 --- a/OnixLabs.Core.UnitTests/ResultTests.cs +++ b/OnixLabs.Core.UnitTests/ResultTests.cs @@ -395,7 +395,7 @@ public async Task ResultMatchAsyncFuncCancellationTokenTaskActionFromSuccessShou Result result = Result.Success(); bool isSuccess = false; bool isFailure = false; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When await result.MatchAsync(Success, Failure, token); @@ -422,7 +422,7 @@ public async Task ResultMatchAsyncFuncCancellationTokenTaskActionFromFailureShou Result result = Result.Failure(FailureException); bool isSuccess = false; bool isFailure = false; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When await result.MatchAsync(Success, Failure, token); @@ -499,7 +499,7 @@ public async Task ResultMatchAsyncActionFuncExceptionCancellationTokenTaskFromSu Result result = Result.Success(); bool isSuccess = false; bool isFailure = false; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When await result.MatchAsync(Success, Failure, token); @@ -525,7 +525,7 @@ public async Task ResultMatchAsyncActionFuncExceptionCancellationTokenTaskFromFa Result result = Result.Failure(FailureException); bool isSuccess = false; bool isFailure = false; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When await result.MatchAsync(Success, Failure, token); @@ -551,7 +551,7 @@ public async Task ResultMatchAsyncFuncTaskFuncExceptionTaskFromSuccessShouldProd Result result = Result.Success(); bool isSuccess = false; bool isFailure = false; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When await result.MatchAsync(Success, Failure, token); @@ -582,7 +582,7 @@ public async Task ResultMatchAsyncFuncTaskFuncExceptionTaskFromFailureShouldProd Result result = Result.Failure(FailureException); bool isSuccess = false; bool isFailure = false; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When await result.MatchAsync(Success, Failure, token); @@ -613,7 +613,7 @@ public async Task ResultMatchAsyncFuncCancellationTokenTaskFuncExceptionCancella Result result = Result.Success(); bool isSuccess = false; bool isFailure = false; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When await result.MatchAsync(Success, Failure, token); @@ -644,7 +644,7 @@ public async Task ResultMatchAsyncFuncCancellationTokenTaskFuncExceptionCancella Result result = Result.Failure(FailureException); bool isSuccess = false; bool isFailure = false; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When await result.MatchAsync(Success, Failure, token); @@ -746,7 +746,7 @@ public async Task ResultMatchAsyncFuncCancellationTokenTaskTResultFuncExceptionT // Given Result result = Result.Success(); const int expected = 1; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When int actual = await result.MatchAsync(Success, Failure, token); @@ -765,7 +765,7 @@ public async Task ResultMatchAsyncFuncCancellationTokenTaskTResultFuncExceptionT // Given Result result = Result.Failure(FailureException); const int expected = 0; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When int actual = await result.MatchAsync(Success, Failure, token); @@ -820,7 +820,7 @@ public async Task ResultMatchAsyncFuncTResultFuncExceptionCancellationTokenTaskT // Given Result result = Result.Success(); const int expected = 1; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When int actual = await result.MatchAsync(Success, Failure, token); @@ -839,7 +839,7 @@ public async Task ResultMatchAsyncFuncTResultFuncExceptionCancellationTokenTaskT // Given Result result = Result.Failure(FailureException); const int expected = 0; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When int actual = await result.MatchAsync(Success, Failure, token); @@ -858,7 +858,7 @@ public async Task ResultMatchAsyncFuncTaskTResultFuncExceptionTaskTResultFromSuc // Given Result result = Result.Success(); const int expected = 1; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When int actual = await result.MatchAsync(Success, Failure, token); @@ -877,7 +877,7 @@ public async Task ResultMatchAsyncFuncTaskTResultFuncExceptionTaskTResultFromFai // Given Result result = Result.Failure(FailureException); const int expected = 0; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When int actual = await result.MatchAsync(Success, Failure, token); @@ -896,7 +896,7 @@ public async Task ResultMatchAsyncFuncCancellationTokenTaskTResultFuncExceptionC // Given Result result = Result.Success(); const int expected = 1; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When int actual = await result.MatchAsync(Success, Failure, token); @@ -915,7 +915,7 @@ public async Task ResultMatchAsyncFuncCancellationTokenTaskTResultFuncExceptionC // Given Result result = Result.Failure(FailureException); const int expected = 0; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When int actual = await result.MatchAsync(Success, Failure, token); @@ -1018,7 +1018,7 @@ public async Task ResultSelectAsyncFuncCancellationTokenTaskFromFailureShouldPro // Given Result result = Result.Failure(FailureException); Result expected = Result.Failure(FailureException); - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When Result actual = await result.SelectAsync(_ => Task.CompletedTask, token); @@ -1033,7 +1033,7 @@ public async Task ResultSelectAsyncFuncCancellationTokenTaskFromSuccessShouldPro // Given Result result = Result.Success(); Result expected = Result.Success(); - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When Result actual = await result.SelectAsync(_ => Task.CompletedTask, token); @@ -1048,7 +1048,7 @@ public async Task ResultSelectAsyncFuncCancellationTokenTaskFromSuccessWithExcep // Given Result result = Result.Success(); Result expected = Result.Failure(FailureException); - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When Result actual = await result.SelectAsync(_ => throw FailureException, token); @@ -1147,7 +1147,7 @@ public async Task ResultSelectAsyncFunctionOfTResultWithCancellationTokenFromFai // Given Result result = Result.Failure(FailureException); Result expected = Result.Failure(FailureException); - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When Result actual = await result.SelectAsync(_ => Task.FromResult(1), token); @@ -1162,7 +1162,7 @@ public async Task ResultSelectAsyncFunctionOfTResultWithCancellationTokenFromSuc // Given Result result = Result.Success(); Result expected = 1; - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When Result actual = await result.SelectAsync(_ => Task.FromResult(1), token); @@ -1177,7 +1177,7 @@ public async Task ResultSelectAsyncFunctionOfTResultWithCancellationTokenFromSuc // Given Result result = Result.Success(); Result expected = Result.Failure(FailureException); - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When Result actual = await result.SelectAsync(_ => throw FailureException, token); @@ -1276,7 +1276,7 @@ public async Task ResultSelectManyAsyncFunctionWithCancellationTokenFromFailureS // Given Result result = Result.Failure(FailureException); Result expected = Result.Failure(FailureException); - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When Result actual = await result.SelectManyAsync(async _ => await Task.FromResult(Result.Success()), token); @@ -1291,7 +1291,7 @@ public async Task ResultSelectManyAsyncFunctionWithCancellationTokenFromSuccessS // Given Result result = Result.Success(); Result expected = Result.Success(); - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When Result actual = await result.SelectManyAsync(async _ => await Task.FromResult(Result.Success()), token); @@ -1306,7 +1306,7 @@ public async Task ResultSelectManyAsyncFunctionWithCancellationTokenFromSuccessW // Given Result result = Result.Success(); Result expected = Result.Failure(FailureException); - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When Result actual = await result.SelectManyAsync(_ => throw FailureException, token); @@ -1405,7 +1405,7 @@ public async Task ResultSelectManyAsyncFunctionOfTResultWithCancellationTokenFro // Given Result result = Result.Failure(FailureException); Result expected = Result.Failure(FailureException); - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When Result actual = await result.SelectManyAsync(async _ => await Task.FromResult(Result.Success(1)), token); @@ -1420,7 +1420,7 @@ public async Task ResultSelectManyAsyncFunctionOfTResultWithCancellationTokenFro // Given Result result = Result.Success(); Result expected = Result.Success(1); - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When Result actual = await result.SelectManyAsync(async _ => await Task.FromResult(Result.Success(1)), token); @@ -1435,7 +1435,7 @@ public async Task ResultSelectManyAsyncFunctionOfTResultWithCancellationTokenFro // Given Result result = Result.Success(); Result expected = Result.Failure(FailureException); - CancellationToken token = new(); + CancellationToken token = CancellationToken.None; // When Result actual = await result.SelectManyAsync(_ => throw FailureException, token); diff --git a/OnixLabs.Core/Extensions.Object.cs b/OnixLabs.Core/Extensions.Object.cs index 399dc2b..5d38c67 100644 --- a/OnixLabs.Core/Extensions.Object.cs +++ b/OnixLabs.Core/Extensions.Object.cs @@ -18,6 +18,8 @@ using System.ComponentModel; using System.Reflection; using System.Text; +using System.Threading; +using System.Threading.Tasks; using OnixLabs.Core.Linq; using OnixLabs.Core.Reflection; using OnixLabs.Core.Text; @@ -170,4 +172,22 @@ public static string ToRecordString(this object? value) /// The current from which to obtain a representation. /// Returns a representation of the current , or a string literal null if the current object is . public static string ToStringOrNull(this object? value) => value?.ToString() ?? Null; + + /// + /// Obtains a representation of the current . + /// + /// The to wrap as a result. + /// The underlying type of the value. + /// Returns a representation of the current . + public static Result ToSuccessResult(this T value) => Result.Success(value); + + /// + /// Asynchronously obtains a representation of the current . + /// + /// The to wrap as a result. + /// The cancellation token that can be used to cancel long-running tasks. + /// The underlying type of the value. + /// Returns a representation of the current . + public static async Task> ToSuccessResultAsync(this Task value, CancellationToken token = default) => + Result.Success(await value.WaitAsync(token).ConfigureAwait(false)); } diff --git a/OnixLabs.Core/Linq/Extensions.IEnumerable.cs b/OnixLabs.Core/Linq/Extensions.IEnumerable.cs index 011f16d..8da96b8 100644 --- a/OnixLabs.Core/Linq/Extensions.IEnumerable.cs +++ b/OnixLabs.Core/Linq/Extensions.IEnumerable.cs @@ -205,7 +205,7 @@ public static void ForEach(this IEnumerable enumerable, Action action) /// Returns the computed content hash code of the current . public static int GetContentHashCode(this IEnumerable? enumerable) { - if (enumerable is null) return default; + if (enumerable is null) return 0; HashCode result = new(); @@ -223,7 +223,7 @@ public static int GetContentHashCode(this IEnumerable? enumerable) /// Returns the computed content hash code of the current . public static int GetContentHashCode(this IEnumerable? enumerable) { - if (enumerable is null) return default; + if (enumerable is null) return 0; HashCode result = new(); diff --git a/OnixLabs.Core/Optional.cs b/OnixLabs.Core/Optional.cs index 996165b..109c3ea 100644 --- a/OnixLabs.Core/Optional.cs +++ b/OnixLabs.Core/Optional.cs @@ -155,7 +155,7 @@ public static Optional Of(TStruct? value) where TStruct : stru /// /// Returns a hash code for the current instance. // ReSharper disable once HeapView.PossibleBoxingAllocation - public sealed override int GetHashCode() => this is Some some ? some.Value.GetHashCode() : default; + public sealed override int GetHashCode() => this is Some some ? some.Value.GetHashCode() : 0; /// /// Gets the underlying value of the current instance, if the underlying value is present; diff --git a/OnixLabs.Core/Reflection/TypeDeclarationFlags.cs b/OnixLabs.Core/Reflection/TypeDeclarationFlags.cs index 2694e21..513ed3e 100644 --- a/OnixLabs.Core/Reflection/TypeDeclarationFlags.cs +++ b/OnixLabs.Core/Reflection/TypeDeclarationFlags.cs @@ -26,7 +26,7 @@ public enum TypeDeclarationFlags /// Specifies that no type name arguments are applied. /// Only simple CLR type names will be used, and excludes generic type arguments and tuple syntax. /// - None = default, + None = 0, /// /// Specifies that namespace qualified CLR type names will be used, where applicable. diff --git a/OnixLabs.Core/Result.Generic.cs b/OnixLabs.Core/Result.Generic.cs index 472df04..57d3572 100644 --- a/OnixLabs.Core/Result.Generic.cs +++ b/OnixLabs.Core/Result.Generic.cs @@ -222,7 +222,7 @@ public async ValueTask DisposeAsync() public sealed override int GetHashCode() => this switch { // ReSharper disable once HeapView.PossibleBoxingAllocation - Success success => success.Value?.GetHashCode() ?? default, + Success success => success.Value?.GetHashCode() ?? 0, Failure failure => failure.Exception.GetHashCode(), _ => throw UnrecognisedResultType }; @@ -300,6 +300,17 @@ public async ValueTask DisposeAsync() /// public T GetValueOrThrow() => this is Success success ? success.Value : throw GetExceptionOrThrow(); + /// + /// Gets the underlying value of the current instance as an parameter. + /// + /// The value of the current instance. + /// Returns the current instance. + public Result GetValue(out T value) + { + value = Match(value => value, _ => default!); + return this; + } + /// /// Executes the delegate that matches the value of the current instance. /// diff --git a/OnixLabs.Core/Result.cs b/OnixLabs.Core/Result.cs index 3af3416..bcab075 100644 --- a/OnixLabs.Core/Result.cs +++ b/OnixLabs.Core/Result.cs @@ -178,7 +178,7 @@ public bool Equals(Result? other) /// Serves as a hash code function for the current instance. /// /// Returns a hash code for the current instance. - public sealed override int GetHashCode() => this is Failure failure ? failure.Exception.GetHashCode() : default; + public sealed override int GetHashCode() => this is Failure failure ? failure.Exception.GetHashCode() : 0; /// /// Gets the underlying exception if the current is in a state, diff --git a/OnixLabs.Numerics.UnitTests.Data/TestDataGenerator.cs b/OnixLabs.Numerics.UnitTests.Data/TestDataGenerator.cs index 02c227d..f95f4c7 100644 --- a/OnixLabs.Numerics.UnitTests.Data/TestDataGenerator.cs +++ b/OnixLabs.Numerics.UnitTests.Data/TestDataGenerator.cs @@ -34,7 +34,7 @@ public static IEnumerable GenerateScaledValues( yield return value.ToDecimal(scale, mode); } - public static IEnumerable GenerateRandomValues(int? count = null, int seed = default) + public static IEnumerable GenerateRandomValues(int? count = null, int seed = 0) { count ??= Provider.RandomCount; Random random = new(seed); diff --git a/OnixLabs.Numerics/BigDecimal.Arithmetic.Round.cs b/OnixLabs.Numerics/BigDecimal.Arithmetic.Round.cs index bf6523c..e84067c 100644 --- a/OnixLabs.Numerics/BigDecimal.Arithmetic.Round.cs +++ b/OnixLabs.Numerics/BigDecimal.Arithmetic.Round.cs @@ -28,7 +28,7 @@ public readonly partial struct BigDecimal /// Returns a value rounded to the specified number of fractional digits. /// If the specified scale is non-negative. /// If the specified rounding mode is invalid. - public static BigDecimal Round(BigDecimal value, int scale = default, MidpointRounding mode = default) + public static BigDecimal Round(BigDecimal value, int scale = 0, MidpointRounding mode = default) { Require(scale >= 0, "Scale must be greater than or equal to zero.", nameof(scale)); RequireIsDefined(mode, nameof(mode)); @@ -65,7 +65,7 @@ public static BigDecimal Round(BigDecimal value, int scale = default, MidpointRo /// Returns a value rounded to the specified number of fractional digits. /// If the specified scale is non-negative. /// If the specified rounding mode is invalid. - public BigDecimal Round(int scale = default, MidpointRounding mode = default) => Round(this, scale, mode); + public BigDecimal Round(int scale = 0, MidpointRounding mode = default) => Round(this, scale, mode); /// /// Rounds the current value up to the smallest integral value greater than or equal to the current number. diff --git a/OnixLabs.Numerics/BigDecimal.Convertible.Implicit.cs b/OnixLabs.Numerics/BigDecimal.Convertible.Implicit.cs index 52b3c01..a81df51 100644 --- a/OnixLabs.Numerics/BigDecimal.Convertible.Implicit.cs +++ b/OnixLabs.Numerics/BigDecimal.Convertible.Implicit.cs @@ -24,77 +24,77 @@ public readonly partial struct BigDecimal /// /// The value to convert. /// Returns a value representing the specified value. - public static implicit operator BigDecimal(BigInteger value) => new(value, default, ScaleMode.Integral); + public static implicit operator BigDecimal(BigInteger value) => new(value, 0, ScaleMode.Integral); /// /// Converts the specified value to a value. /// /// The value to convert. /// Returns a value representing the specified value. - public static implicit operator BigDecimal(sbyte value) => new(value, default, ScaleMode.Integral); + public static implicit operator BigDecimal(sbyte value) => new(value, 0, ScaleMode.Integral); /// /// Converts the specified value to a value. /// /// The value to convert. /// Returns a value representing the specified value. - public static implicit operator BigDecimal(byte value) => new(value, default, ScaleMode.Integral); + public static implicit operator BigDecimal(byte value) => new(value, 0, ScaleMode.Integral); /// /// Converts the specified value to a value. /// /// The value to convert. /// Returns a value representing the specified value. - public static implicit operator BigDecimal(short value) => new(value, default, ScaleMode.Integral); + public static implicit operator BigDecimal(short value) => new(value, 0, ScaleMode.Integral); /// /// Converts the specified value to a value. /// /// The value to convert. /// Returns a value representing the specified value. - public static implicit operator BigDecimal(ushort value) => new(value, default, ScaleMode.Integral); + public static implicit operator BigDecimal(ushort value) => new(value, 0, ScaleMode.Integral); /// /// Converts the specified value to a value. /// /// The value to convert. /// Returns a value representing the specified value. - public static implicit operator BigDecimal(int value) => new(value, default, ScaleMode.Integral); + public static implicit operator BigDecimal(int value) => new(value, 0, ScaleMode.Integral); /// /// Converts the specified value to a value. /// /// The value to convert. /// Returns a value representing the specified value. - public static implicit operator BigDecimal(uint value) => new(value, default, ScaleMode.Integral); + public static implicit operator BigDecimal(uint value) => new(value, 0, ScaleMode.Integral); /// /// Converts the specified value to a value. /// /// The value to convert. /// Returns a value representing the specified value. - public static implicit operator BigDecimal(long value) => new(value, default, ScaleMode.Integral); + public static implicit operator BigDecimal(long value) => new(value, 0, ScaleMode.Integral); /// /// Converts the specified value to a value. /// /// The value to convert. /// Returns a value representing the specified value. - public static implicit operator BigDecimal(ulong value) => new(value, default, ScaleMode.Integral); + public static implicit operator BigDecimal(ulong value) => new(value, 0, ScaleMode.Integral); /// /// Converts the specified value to a value. /// /// The value to convert. /// Returns a value representing the specified value. - public static implicit operator BigDecimal(Int128 value) => new(value, default, ScaleMode.Integral); + public static implicit operator BigDecimal(Int128 value) => new(value, 0, ScaleMode.Integral); /// /// Converts the specified value to a value. /// /// The value to convert. /// Returns a value representing the specified value. - public static implicit operator BigDecimal(UInt128 value) => new(value, default, ScaleMode.Integral); + public static implicit operator BigDecimal(UInt128 value) => new(value, 0, ScaleMode.Integral); /// /// Converts the specified value to a value. diff --git a/OnixLabs.Numerics/BigDecimal.cs b/OnixLabs.Numerics/BigDecimal.cs index 3ea6b12..77ea6d9 100644 --- a/OnixLabs.Numerics/BigDecimal.cs +++ b/OnixLabs.Numerics/BigDecimal.cs @@ -35,7 +35,7 @@ namespace OnixLabs.Numerics; /// The unscaled integer value from which to construct a value. /// The scale of the value. /// The scale mode that determines how the specified value should be scaled. - public BigDecimal(BigInteger value, int scale = default, ScaleMode mode = default) + public BigDecimal(BigInteger value, int scale = 0, ScaleMode mode = default) { Require(scale >= 0, "Scale must be greater than or equal to zero.", nameof(scale)); RequireIsDefined(mode, nameof(mode)); diff --git a/OnixLabs.Numerics/NumberInfoParser.Sanitize.cs b/OnixLabs.Numerics/NumberInfoParser.Sanitize.cs index 5193885..89c807e 100644 --- a/OnixLabs.Numerics/NumberInfoParser.Sanitize.cs +++ b/OnixLabs.Numerics/NumberInfoParser.Sanitize.cs @@ -127,7 +127,7 @@ private bool TryTrimParentheses(ref ReadOnlySpan value, out bool hasParent private bool TryTrimExponent(ref ReadOnlySpan value, out int exponent) { - exponent = default; + exponent = 0; int index = value.IndexOf(ExponentSymbol, Comparison); int lastIndex = value.LastIndexOf(ExponentSymbol, Comparison); diff --git a/OnixLabs.Numerics/NumericsExtensions.cs b/OnixLabs.Numerics/NumericsExtensions.cs index 7af1f01..3b9f94f 100644 --- a/OnixLabs.Numerics/NumericsExtensions.cs +++ b/OnixLabs.Numerics/NumericsExtensions.cs @@ -86,7 +86,7 @@ private static BigInteger GetUnscaledInteger(this T value, int scale, ScaleMo /// The scale mode that determines how the current value should be scaled. /// The underlying type of the value to convert. /// Returns a representing the current value. - public static BigDecimal ToBigDecimal(this T value, int scale = default, ScaleMode mode = ScaleMode.Integral) where T : IBinaryInteger => + public static BigDecimal ToBigDecimal(this T value, int scale = 0, ScaleMode mode = ScaleMode.Integral) where T : IBinaryInteger => new(value.ToBigInteger(), scale, mode); /// @@ -128,7 +128,7 @@ public static BigDecimal ToBigDecimal(this T value, int scale = default, Scal /// The scale mode that determines how the current value should be scaled. /// The underlying type of the value to convert. /// Returns a new representing the current value. - public static decimal ToDecimal(this T value, int scale = default, ScaleMode mode = default) where T : IBinaryInteger + public static decimal ToDecimal(this T value, int scale = 0, ScaleMode mode = default) where T : IBinaryInteger { Require(scale.IsBetween(0, 28), "Scale must be between 0 and 28."); RequireIsDefined(mode, nameof(mode)); @@ -152,7 +152,7 @@ public static decimal ToDecimal(this T value, int scale = default, ScaleMode /// The scale mode that determines how the specified value should be scaled. /// The underlying type. /// Returns a representing the current value. - public static NumberInfo ToNumberInfo(this T value, int scale = default, ScaleMode mode = default) where T : IBinaryInteger + public static NumberInfo ToNumberInfo(this T value, int scale = 0, ScaleMode mode = default) where T : IBinaryInteger { Require(scale >= 0, "Scale must be greater than or equal to zero", nameof(scale)); RequireIsDefined(mode, nameof(mode)); diff --git a/OnixLabs.Security.Cryptography/Extensions.HashAlgorithm.cs b/OnixLabs.Security.Cryptography/Extensions.HashAlgorithm.cs index f651029..8d86e62 100644 --- a/OnixLabs.Security.Cryptography/Extensions.HashAlgorithm.cs +++ b/OnixLabs.Security.Cryptography/Extensions.HashAlgorithm.cs @@ -159,18 +159,18 @@ public static bool TryComputeHash(this HashAlgorithm algorithm, ReadOnlySpan 0) { if (!algorithm.TryComputeHash(source, destination, out result)) { - bytesWritten = default; + bytesWritten = 0; return false; } @@ -182,7 +182,7 @@ public static bool TryComputeHash(this HashAlgorithm algorithm, ReadOnlySpan + /// Parses the specified value into a value. + /// + /// The value to parse. + /// The format provider that will be used to decode the specified value. + /// Returns a new instance, parsed from the specified value. public static NamedHash Parse(string value, IFormatProvider? provider = null) => Parse(value.AsSpan(), provider); + /// + /// Parses the specified value into a value. + /// + /// The value to parse. + /// The format provider that will be used to decode the specified value. + /// Returns a new instance, parsed from the specified value. public static NamedHash Parse(ReadOnlySpan value, IFormatProvider? provider = null) { if (TryParse(value, provider, out NamedHash result)) return result; throw new FormatException($"The input string '{value}' was not in a correct format."); } + /// + /// Tries to parse the specified value into a value. + /// + /// The value to parse. + /// The format provider that will be used to decode the specified value. + /// + /// A new instance, parsed from the specified value, or the default + /// value if the specified could not be parsed. + /// + /// Returns if the specified value was decoded successfully; otherwise, . public static bool TryParse([NotNullWhen(true)] string? value, IFormatProvider? provider, out NamedHash result) => TryParse(value.AsSpan(), provider, out result); + /// + /// Tries to parse the specified value into a value. + /// + /// The value to parse. + /// The format provider that will be used to decode the specified value. + /// + /// A new instance, parsed from the specified value, or the default + /// value if the specified could not be parsed. + /// + /// Returns if the specified value was decoded successfully; otherwise, . public static bool TryParse(ReadOnlySpan value, IFormatProvider? provider, out NamedHash result) { result = default; diff --git a/OnixLabs.Security.Cryptography/Sha3.cs b/OnixLabs.Security.Cryptography/Sha3.cs index 800a7cd..b358659 100644 --- a/OnixLabs.Security.Cryptography/Sha3.cs +++ b/OnixLabs.Security.Cryptography/Sha3.cs @@ -78,7 +78,7 @@ protected Sha3(int rateBytes, int delimiter, int bitLength) public sealed override void Initialize() { // ReSharper disable HeapView.ObjectAllocation.Evident - blockSize = default; + blockSize = 0; state = new ulong[25]; result = new byte[HashSize / 8]; }