-
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.
feat: add allocation-free overloads for Result.MapIf
- Loading branch information
1 parent
e2089b9
commit 7d87186
Showing
15 changed files
with
2,221 additions
and
861 deletions.
There are no files selected for viewing
149 changes: 76 additions & 73 deletions
149
CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.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 |
---|---|---|
@@ -1,73 +1,76 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
|
||
namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions | ||
{ | ||
public abstract class MapIfTestsBase : TestBase | ||
{ | ||
protected bool actionExecuted; | ||
protected bool predicateExecuted; | ||
|
||
protected MapIfTestsBase() | ||
{ | ||
actionExecuted = false; | ||
predicateExecuted = false; | ||
} | ||
|
||
protected Func<T, T> GetAction() => value => | ||
{ | ||
actionExecuted.Should().BeFalse(); | ||
value.Should().Be(T.Value); | ||
|
||
actionExecuted = true; | ||
return T.Value2; | ||
}; | ||
|
||
protected Func<T, Task<T>> GetTaskAction() | ||
{ | ||
return t => Task.FromResult(GetAction()(t)); | ||
} | ||
|
||
protected Func<T, ValueTask<T>> GetValueTaskAction() | ||
{ | ||
return t => ValueTask.FromResult(GetAction()(t)); | ||
} | ||
|
||
protected Func<T, bool> GetValuePredicate(bool value) | ||
{ | ||
return t => | ||
{ | ||
predicateExecuted.Should().BeFalse(); | ||
t.Should().Be(T.Value); | ||
|
||
predicateExecuted = true; | ||
return value; | ||
}; | ||
} | ||
|
||
protected static Result<T> GetExpectedValueResult(bool isSuccess, bool condition) | ||
{ | ||
var resultChanged = isSuccess && condition; | ||
|
||
if (!resultChanged) | ||
{ | ||
return Result.SuccessIf(isSuccess, T.Value, ErrorMessage); | ||
} | ||
|
||
return Result.SuccessIf(isSuccess, T.Value2, ErrorMessage2); | ||
} | ||
|
||
protected static Result<T, E> GetExpectedValueErrorResult(bool isSuccess, bool condition) | ||
{ | ||
var resultChanged = isSuccess && condition; | ||
|
||
if (!resultChanged) | ||
{ | ||
return Result.SuccessIf(isSuccess, T.Value, E.Value); | ||
} | ||
|
||
return Result.SuccessIf(isSuccess, T.Value2, E.Value2); | ||
} | ||
} | ||
} | ||
using System; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
|
||
namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions | ||
{ | ||
public abstract class MapIfTestsBase : TestBase | ||
{ | ||
protected bool actionExecuted; | ||
protected bool predicateExecuted; | ||
|
||
protected MapIfTestsBase() | ||
{ | ||
actionExecuted = false; | ||
predicateExecuted = false; | ||
} | ||
|
||
protected Func<T, T> GetAction() => | ||
value => | ||
{ | ||
actionExecuted.Should().BeFalse(); | ||
value.Should().Be(T.Value); | ||
|
||
actionExecuted = true; | ||
return T.Value2; | ||
}; | ||
|
||
protected Func<T, Task<T>> GetTaskAction() | ||
{ | ||
return t => Task.FromResult(GetAction()(t)); | ||
} | ||
|
||
protected Func<T, ValueTask<T>> GetValueTaskAction() | ||
{ | ||
return t => ValueTask.FromResult(GetAction()(t)); | ||
} | ||
|
||
protected Func<T, bool> GetValuePredicate(bool value) | ||
{ | ||
return t => | ||
{ | ||
predicateExecuted.Should().BeFalse(); | ||
t.Should().Be(T.Value); | ||
|
||
predicateExecuted = true; | ||
return value; | ||
}; | ||
} | ||
|
||
protected static Result<T> GetExpectedValueResult(bool isSuccess, bool condition) | ||
{ | ||
var resultChanged = isSuccess && condition; | ||
|
||
if (!resultChanged) | ||
{ | ||
return Result.SuccessIf(isSuccess, T.Value, ErrorMessage); | ||
} | ||
|
||
return Result.SuccessIf(isSuccess, T.Value2, ErrorMessage2); | ||
} | ||
|
||
protected static Result<T, E> GetExpectedValueErrorResult(bool isSuccess, bool condition) | ||
{ | ||
var resultChanged = isSuccess && condition; | ||
|
||
if (!resultChanged) | ||
{ | ||
return Result.SuccessIf(isSuccess, T.Value, E.Value); | ||
} | ||
|
||
return Result.SuccessIf(isSuccess, T.Value2, E.Value2); | ||
} | ||
|
||
protected readonly string ContextMessage = "Context data"; | ||
} | ||
} |
Oops, something went wrong.