|
1 | | -using System; |
2 | | -using System.Threading.Tasks; |
3 | | -using FluentAssertions; |
4 | | - |
5 | | -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions |
6 | | -{ |
7 | | - public abstract class MapIfTestsBase : TestBase |
8 | | - { |
9 | | - protected bool actionExecuted; |
10 | | - protected bool predicateExecuted; |
11 | | - |
12 | | - protected MapIfTestsBase() |
13 | | - { |
14 | | - actionExecuted = false; |
15 | | - predicateExecuted = false; |
16 | | - } |
17 | | - |
18 | | - protected Func<T, T> GetAction() => |
19 | | - value => |
20 | | - { |
21 | | - actionExecuted.Should().BeFalse(); |
22 | | - value.Should().Be(T.Value); |
23 | | - |
24 | | - actionExecuted = true; |
25 | | - return T.Value2; |
26 | | - }; |
27 | | - |
28 | | - protected Func<T, Task<T>> GetTaskAction() |
29 | | - { |
30 | | - return t => Task.FromResult(GetAction()(t)); |
31 | | - } |
32 | | - |
33 | | - protected Func<T, ValueTask<T>> GetValueTaskAction() |
34 | | - { |
35 | | - return t => ValueTask.FromResult(GetAction()(t)); |
36 | | - } |
37 | | - |
38 | | - protected Func<T, bool> GetValuePredicate(bool value) |
39 | | - { |
40 | | - return t => |
41 | | - { |
42 | | - predicateExecuted.Should().BeFalse(); |
43 | | - t.Should().Be(T.Value); |
44 | | - |
45 | | - predicateExecuted = true; |
46 | | - return value; |
47 | | - }; |
48 | | - } |
49 | | - |
50 | | - protected static Result<T> GetExpectedValueResult(bool isSuccess, bool condition) |
51 | | - { |
52 | | - var resultChanged = isSuccess && condition; |
53 | | - |
54 | | - if (!resultChanged) |
55 | | - { |
56 | | - return Result.SuccessIf(isSuccess, T.Value, ErrorMessage); |
57 | | - } |
58 | | - |
59 | | - return Result.SuccessIf(isSuccess, T.Value2, ErrorMessage2); |
60 | | - } |
61 | | - |
62 | | - protected static Result<T, E> GetExpectedValueErrorResult(bool isSuccess, bool condition) |
63 | | - { |
64 | | - var resultChanged = isSuccess && condition; |
65 | | - |
66 | | - if (!resultChanged) |
67 | | - { |
68 | | - return Result.SuccessIf(isSuccess, T.Value, E.Value); |
69 | | - } |
70 | | - |
71 | | - return Result.SuccessIf(isSuccess, T.Value2, E.Value2); |
72 | | - } |
73 | | - |
74 | | - protected readonly string ContextMessage = "Context data"; |
75 | | - } |
76 | | -} |
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using FluentAssertions; |
| 4 | + |
| 5 | +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions |
| 6 | +{ |
| 7 | + public abstract class MapIfTestsBase : TestBase |
| 8 | + { |
| 9 | + protected bool actionExecuted; |
| 10 | + protected bool predicateExecuted; |
| 11 | + |
| 12 | + protected MapIfTestsBase() |
| 13 | + { |
| 14 | + actionExecuted = false; |
| 15 | + predicateExecuted = false; |
| 16 | + } |
| 17 | + |
| 18 | + protected Func<T, T> GetAction() => |
| 19 | + value => |
| 20 | + { |
| 21 | + actionExecuted.Should().BeFalse(); |
| 22 | + value.Should().Be(T.Value); |
| 23 | + |
| 24 | + actionExecuted = true; |
| 25 | + return T.Value2; |
| 26 | + }; |
| 27 | + |
| 28 | + protected Func<T, Task<T>> GetTaskAction() |
| 29 | + { |
| 30 | + return t => Task.FromResult(GetAction()(t)); |
| 31 | + } |
| 32 | + |
| 33 | + protected Func<T, ValueTask<T>> GetValueTaskAction() |
| 34 | + { |
| 35 | + return t => ValueTask.FromResult(GetAction()(t)); |
| 36 | + } |
| 37 | + |
| 38 | + protected Func<T, bool> GetValuePredicate(bool value) |
| 39 | + { |
| 40 | + return t => |
| 41 | + { |
| 42 | + predicateExecuted.Should().BeFalse(); |
| 43 | + t.Should().Be(T.Value); |
| 44 | + |
| 45 | + predicateExecuted = true; |
| 46 | + return value; |
| 47 | + }; |
| 48 | + } |
| 49 | + |
| 50 | + protected static Result<T> GetExpectedValueResult(bool isSuccess, bool condition) |
| 51 | + { |
| 52 | + var resultChanged = isSuccess && condition; |
| 53 | + |
| 54 | + if (!resultChanged) |
| 55 | + { |
| 56 | + return Result.SuccessIf(isSuccess, T.Value, ErrorMessage); |
| 57 | + } |
| 58 | + |
| 59 | + return Result.SuccessIf(isSuccess, T.Value2, ErrorMessage2); |
| 60 | + } |
| 61 | + |
| 62 | + protected static Result<T, E> GetExpectedValueErrorResult(bool isSuccess, bool condition) |
| 63 | + { |
| 64 | + var resultChanged = isSuccess && condition; |
| 65 | + |
| 66 | + if (!resultChanged) |
| 67 | + { |
| 68 | + return Result.SuccessIf(isSuccess, T.Value, E.Value); |
| 69 | + } |
| 70 | + |
| 71 | + return Result.SuccessIf(isSuccess, T.Value2, E.Value2); |
| 72 | + } |
| 73 | + |
| 74 | + protected readonly string ContextMessage = "Context data"; |
| 75 | + } |
| 76 | +} |
0 commit comments