Skip to content

Commit 7d87186

Browse files
committed
feat: add allocation-free overloads for Result.MapIf
1 parent e2089b9 commit 7d87186

File tree

15 files changed

+2221
-861
lines changed

15 files changed

+2221
-861
lines changed
Lines changed: 76 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,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() => value =>
19-
{
20-
actionExecuted.Should().BeFalse();
21-
value.Should().Be(T.Value);
22-
23-
actionExecuted = true;
24-
return T.Value2;
25-
};
26-
27-
protected Func<T, Task<T>> GetTaskAction()
28-
{
29-
return t => Task.FromResult(GetAction()(t));
30-
}
31-
32-
protected Func<T, ValueTask<T>> GetValueTaskAction()
33-
{
34-
return t => ValueTask.FromResult(GetAction()(t));
35-
}
36-
37-
protected Func<T, bool> GetValuePredicate(bool value)
38-
{
39-
return t =>
40-
{
41-
predicateExecuted.Should().BeFalse();
42-
t.Should().Be(T.Value);
43-
44-
predicateExecuted = true;
45-
return value;
46-
};
47-
}
48-
49-
protected static Result<T> GetExpectedValueResult(bool isSuccess, bool condition)
50-
{
51-
var resultChanged = isSuccess && condition;
52-
53-
if (!resultChanged)
54-
{
55-
return Result.SuccessIf(isSuccess, T.Value, ErrorMessage);
56-
}
57-
58-
return Result.SuccessIf(isSuccess, T.Value2, ErrorMessage2);
59-
}
60-
61-
protected static Result<T, E> GetExpectedValueErrorResult(bool isSuccess, bool condition)
62-
{
63-
var resultChanged = isSuccess && condition;
64-
65-
if (!resultChanged)
66-
{
67-
return Result.SuccessIf(isSuccess, T.Value, E.Value);
68-
}
69-
70-
return Result.SuccessIf(isSuccess, T.Value2, E.Value2);
71-
}
72-
}
73-
}
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

Comments
 (0)