-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #421 from ProphetLamb/inline_payload_retrieval
Inline payload retrieval
- Loading branch information
Showing
5 changed files
with
338 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
CSharpFunctionalExtensions.Tests/ResultTests/Methods/TryGetTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System; | ||
|
||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace CSharpFunctionalExtensions.Tests.ResultTests.Methods | ||
{ | ||
public class TryGetTests | ||
{ | ||
public const string ErrorMessage = "Error from result"; | ||
|
||
[Fact] | ||
public void Simple_result_tryGetError_is_false_Success_value_expected() | ||
{ | ||
Result result = Result.Success(ErrorMessage); | ||
result.TryGetError(out string error).Should().BeFalse(); | ||
error.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void Simple_result_tryGetError_is_true_Failure_value_expected() | ||
{ | ||
Result result = Result.Failure(ErrorMessage); | ||
result.TryGetError(out string error).Should().BeTrue(); | ||
error.Should().Be(ErrorMessage); | ||
} | ||
|
||
[Fact] | ||
public void Generic_result_tryGetError_is_false_Success_value_expected() | ||
{ | ||
Result<string> result = Result.Success("Success"); | ||
result.TryGetError(out string error).Should().BeFalse(); | ||
error.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void Generic_result_tryGetError_is_true_Failure_value_expected() | ||
{ | ||
Result<string> result = Result.Failure<string>(ErrorMessage); | ||
result.TryGetError(out string error).Should().BeTrue(); | ||
error.Should().Be(ErrorMessage); | ||
} | ||
|
||
[Fact] | ||
public void Generic_result_tryGetSuccess_is_false_Failure_value_expected() | ||
{ | ||
Result<string> result = Result.Failure<string>(ErrorMessage); | ||
result.TryGetValue(out string value).Should().BeFalse(); | ||
value.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void Generic_result_tryGetSuccess_is_true_Success_value_expected() | ||
{ | ||
Result<string> result = Result.Success("Success"); | ||
result.TryGetValue(out string value).Should().BeTrue(); | ||
value.Should().Be("Success"); | ||
} | ||
|
||
[Fact] | ||
public void Value_Error_Generic_result_tryGetError_is_false_Success_value_expected() | ||
{ | ||
Result<string, string> result = Result.Success<string, string>("Success"); | ||
result.TryGetError(out string error).Should().BeFalse(); | ||
error.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void Value_Error_Generic_result_tryGetError_is_true_Failure_value_expected() | ||
{ | ||
Result<string, string> result = Result.Failure<string, string>(ErrorMessage); | ||
result.TryGetError(out string error).Should().BeTrue(); | ||
error.Should().Be(ErrorMessage); | ||
} | ||
|
||
[Fact] | ||
public void Value_Error_Generic_result_tryGetSuccess_is_false_Failure_value_expected() | ||
{ | ||
Result<string, string> result = Result.Failure<string, string>(ErrorMessage); | ||
result.TryGetValue(out string value).Should().BeFalse(); | ||
value.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void Value_Error_Generic_result_tryGetSuccess_is_true_Success_value_expected() | ||
{ | ||
Result<string, string> result = Result.Success<string, string>("Success"); | ||
result.TryGetValue(out string value).Should().BeTrue(); | ||
value.Should().Be("Success"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
#if NETCORE || NETSTANDARD || NET45_OR_GREATER | ||
using System.Runtime.CompilerServices; | ||
#endif | ||
#if NET_5_0_OR_GREATER | ||
using System.Diagnostics.CodeAnalysis; | ||
#endif | ||
|
||
namespace CSharpFunctionalExtensions | ||
{ | ||
partial struct Result | ||
{ | ||
#if NETCORE || NETSTANDARD || NET45_OR_GREATER | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
#endif | ||
public bool TryGetError( | ||
#if NET_5_0_OR_GREATER | ||
[NotNullWhen(true), MaybeNullWhen(false)] | ||
#endif | ||
out string error) | ||
{ | ||
error = _error; | ||
return IsFailure; | ||
} | ||
} | ||
|
||
partial struct Result<T> | ||
{ | ||
#if NETCORE || NETSTANDARD || NET45_OR_GREATER | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
#endif | ||
public bool TryGetValue( | ||
#if NET_5_0_OR_GREATER | ||
[NotNullWhen(true), MaybeNullWhen(false)] | ||
#endif | ||
out T value) | ||
{ | ||
value = _value; | ||
return IsSuccess; | ||
} | ||
|
||
#if NETCORE || NETSTANDARD || NET45_OR_GREATER | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
#endif | ||
public bool TryGetError( | ||
#if NET_5_0_OR_GREATER | ||
[NotNullWhen(true), MaybeNullWhen(false)] | ||
#endif | ||
out string error) | ||
{ | ||
error = _error; | ||
return IsFailure; | ||
} | ||
|
||
#if NETCORE || NETSTANDARD || NET45_OR_GREATER | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
#endif | ||
public bool TryGetValue( | ||
#if NET_5_0_OR_GREATER | ||
[NotNullWhen(true), MaybeNullWhen(false)] | ||
#endif | ||
out T value, | ||
#if NET_5_0_OR_GREATER | ||
[NotNullWhen(false), MaybeNullWhen(true)] | ||
#endif | ||
out string error | ||
) | ||
{ | ||
value = _value; | ||
error = _error; | ||
return IsSuccess; | ||
} | ||
|
||
#if NETCORE || NETSTANDARD || NET45_OR_GREATER | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
#endif | ||
public bool TryGetError( | ||
#if NET_5_0_OR_GREATER | ||
[NotNullWhen(true), MaybeNullWhen(false)] | ||
#endif | ||
out string error, | ||
#if NET_5_0_OR_GREATER | ||
[NotNullWhen(false), MaybeNullWhen(true)] | ||
#endif | ||
out T value | ||
) | ||
{ | ||
value = _value; | ||
error = _error; | ||
return IsFailure; | ||
} | ||
} | ||
|
||
partial struct Result<T, E> | ||
{ | ||
|
||
#if NETCORE || NETSTANDARD || NET45_OR_GREATER | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
#endif | ||
public bool TryGetValue( | ||
#if NET_5_0_OR_GREATER | ||
[NotNullWhen(true), MaybeNullWhen(false)] | ||
#endif | ||
out T value) | ||
{ | ||
value = _value; | ||
return IsSuccess; | ||
} | ||
|
||
#if NETCORE || NETSTANDARD || NET45_OR_GREATER | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
#endif | ||
public bool TryGetError( | ||
#if NET_5_0_OR_GREATER | ||
[NotNullWhen(true), MaybeNullWhen(false)] | ||
#endif | ||
out E error) | ||
{ | ||
error = _error; | ||
return IsFailure; | ||
} | ||
|
||
#if NETCORE || NETSTANDARD || NET45_OR_GREATER | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
#endif | ||
public bool TryGetValue( | ||
#if NET_5_0_OR_GREATER | ||
[NotNullWhen(true), MaybeNullWhen(false)] | ||
#endif | ||
out T value, | ||
#if NET_5_0_OR_GREATER | ||
[NotNullWhen(false), MaybeNullWhen(true)] | ||
#endif | ||
out E error | ||
) | ||
{ | ||
value = _value; | ||
error = _error; | ||
return IsSuccess; | ||
} | ||
|
||
#if NETCORE || NETSTANDARD || NET45_OR_GREATER | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
#endif | ||
public bool TryGetError( | ||
#if NET_5_0_OR_GREATER | ||
[NotNullWhen(true), MaybeNullWhen(false)] | ||
#endif | ||
out E error, | ||
#if NET_5_0_OR_GREATER | ||
[NotNullWhen(false), MaybeNullWhen(true)] | ||
#endif | ||
out T value | ||
) | ||
{ | ||
value = _value; | ||
error = _error; | ||
return IsFailure; | ||
} | ||
} | ||
|
||
partial struct UnitResult<E> | ||
{ | ||
#if NETCORE || NETSTANDARD || NET45_OR_GREATER | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
#endif | ||
public bool TryGetError( | ||
#if NET_5_0_OR_GREATER | ||
[NotNullWhen(true), MaybeNullWhen(false)] | ||
#endif | ||
out E error) | ||
{ | ||
error = _error; | ||
return IsFailure; | ||
} | ||
} | ||
} |