-
Notifications
You must be signed in to change notification settings - Fork 107
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
Add Map Extension Method to Support Mapping from Result<T> to Result #213
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f75be4f
Add Map method to map Generic Result to Result
jmrtnz94 29429f2
Add unit tests for Generic Result to Result map method
jmrtnz94 f2e611a
Merge branch 'main' into 212_MapGenericResultToResult
jmrtnz94 ceb7df8
Update ResultGenericToVoidMap.cs and add test cases
KyleMcMaster 4c21d81
Update ResultGenericToVoidMap.cs
KyleMcMaster a4e6251
Update ResultGenericToVoidMap.cs
KyleMcMaster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
125 changes: 125 additions & 0 deletions
125
tests/Ardalis.Result.UnitTests/ResultGenericToVoidMap.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,125 @@ | ||
using FluentAssertions; | ||
using FluentAssertions.Execution; | ||
using System; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace Ardalis.Result.UnitTests; | ||
|
||
public class ResultGenericToVoidMap | ||
{ | ||
[Fact] | ||
public void ShouldProduceSuccess() | ||
{ | ||
var result = Result<int>.Success(1); | ||
|
||
var actual = result.Map(); | ||
|
||
actual.Status.Should().Be(ResultStatus.Ok); | ||
} | ||
|
||
[Fact] | ||
public void ShouldProduceNotFound() | ||
{ | ||
var result = Result<int>.NotFound(); | ||
|
||
var actual = result.Map(); | ||
|
||
actual.Status.Should().Be(ResultStatus.NotFound); | ||
} | ||
|
||
[Fact] | ||
public void ShouldProduceUnauthorized() | ||
{ | ||
var result = Result<int>.Unauthorized(); | ||
|
||
var actual = result.Map(); | ||
|
||
actual.Status.Should().Be(ResultStatus.Unauthorized); | ||
} | ||
|
||
[Fact] | ||
public void ShouldProduceForbidden() | ||
{ | ||
var result = Result<int>.Forbidden(); | ||
|
||
var actual = result.Map(); | ||
|
||
actual.Status.Should().Be(ResultStatus.Forbidden); | ||
} | ||
|
||
[Fact] | ||
public void ShouldProduceInvalid() | ||
{ | ||
var result = Result<int>.Invalid(new ValidationError()); | ||
|
||
var actual = result.Map(); | ||
|
||
actual.Status.Should().Be(ResultStatus.Invalid); | ||
} | ||
|
||
[Fact] | ||
public void ShouldProduceError() | ||
{ | ||
var result = Result<int>.Error(); | ||
|
||
var actual = result.Map(); | ||
|
||
actual.Status.Should().Be(ResultStatus.Error); | ||
} | ||
|
||
[Fact] | ||
public void ShouldProduceErrorWhenConstructedWithErrorList() | ||
{ | ||
string correlationId = Guid.NewGuid().ToString(); | ||
string errorMessage = "Error occured 💥"; | ||
var result = Result<int>.Error(new ErrorList([errorMessage], correlationId)); | ||
|
||
var actual = result.Map(); | ||
|
||
using var assertionScope = new AssertionScope(); | ||
actual.Status.Should().Be(ResultStatus.Error); | ||
actual.CorrelationId.Should().Be(correlationId); | ||
actual.Errors.Single().Should().Be(errorMessage); | ||
} | ||
|
||
[Fact] | ||
public void ShouldProduceConflict() | ||
{ | ||
var result = Result<int>.Conflict(); | ||
|
||
var actual = result.Map(); | ||
|
||
actual.Status.Should().Be(ResultStatus.Conflict); | ||
} | ||
|
||
[Fact] | ||
public void ShouldProduceCriticalError() | ||
{ | ||
var result = Result<int>.CriticalError(); | ||
|
||
var actual = result.Map(); | ||
|
||
actual.Status.Should().Be(ResultStatus.CriticalError); | ||
} | ||
|
||
[Fact] | ||
public void ShouldProduceUnavailable() | ||
{ | ||
var result = Result<int>.Unavailable(); | ||
|
||
var actual = result.Map(); | ||
|
||
actual.Status.Should().Be(ResultStatus.Unavailable); | ||
} | ||
|
||
[Fact] | ||
public void ShouldProduceNoContent() | ||
{ | ||
var result = Result<int>.NoContent(); | ||
|
||
var actual = result.Map(); | ||
|
||
actual.Status.Should().Be(ResultStatus.NoContent); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need the correlation id as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CriticalError accepts a string[], so we cannot pass in the correlation id. See https://github.com/ardalis/Result/blob/main/src/Ardalis.Result/Result.cs#L223.
While Error accepts an ErrorList object that contains correlation id. See https://github.com/ardalis/Result/blob/main/src/Ardalis.Result/Result.cs#L123-L127
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we're in a bit of a transition phase to using the
ErrorList
class in more places but ideally we'd use the constructors that accept aCorrelationId
wherever possible.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have verified the CorrelationId property is propagated through the constructors where it is available. We can open another issue to add CorrelationId to the remaining constructors and then update these helper methods as needed.