-
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 of Maybe.Map
- Loading branch information
1 parent
b421543
commit 0d26cdd
Showing
14 changed files
with
548 additions
and
321 deletions.
There are no files selected for viewing
80 changes: 50 additions & 30 deletions
80
CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Left.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,30 +1,50 @@ | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions | ||
{ | ||
public class MapTests_Task_Left : MaybeTestBase | ||
{ | ||
[Fact] | ||
public async Task Map_Task_Left_returns_mapped_value() | ||
{ | ||
Maybe<T> maybe = T.Value; | ||
|
||
var maybe2 = await maybe.AsTask().Map(ExpectAndReturn(T.Value, T.Value2)); | ||
|
||
maybe2.HasValue.Should().BeTrue(); | ||
maybe2.Value.Should().Be(T.Value2); | ||
} | ||
|
||
[Fact] | ||
public async Task Map_Task_Left_returns_no_value_if_initial_maybe_is_null() | ||
{ | ||
Maybe<T> maybe = null; | ||
|
||
var maybe2 = await maybe.AsTask().Map(ExpectAndReturn(null, T.Value2)); | ||
|
||
maybe2.HasValue.Should().BeFalse(); | ||
} | ||
} | ||
} | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions | ||
{ | ||
public class MapTests_Task_Left : MaybeTestBase | ||
{ | ||
[Fact] | ||
public async Task Map_Task_Left_returns_mapped_value() | ||
{ | ||
Maybe<T> maybe = T.Value; | ||
|
||
var maybe2 = await maybe.AsTask().Map(ExpectAndReturn(T.Value, T.Value2)); | ||
|
||
maybe2.HasValue.Should().BeTrue(); | ||
maybe2.Value.Should().Be(T.Value2); | ||
} | ||
|
||
[Fact] | ||
public async Task Map_Task_Left_returns_no_value_if_initial_maybe_is_null() | ||
{ | ||
Maybe<T> maybe = null; | ||
|
||
var maybe2 = await maybe.AsTask().Map(ExpectAndReturn(null, T.Value2)); | ||
|
||
maybe2.HasValue.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public async Task Map_Task_Left_provides_context_to_selector() | ||
{ | ||
Maybe<T> maybe = T.Value; | ||
var context = 5; | ||
|
||
var maybe2 = await maybe | ||
.AsTask() | ||
.Map( | ||
(value, ctx) => | ||
{ | ||
ctx.Should().Be(context); | ||
return value; | ||
}, | ||
context | ||
); | ||
|
||
maybe2.HasValue.Should().BeTrue(); | ||
} | ||
} | ||
} |
78 changes: 48 additions & 30 deletions
78
CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Right.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,30 +1,48 @@ | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions | ||
{ | ||
public class MapTests_Task_Right : MaybeTestBase | ||
{ | ||
[Fact] | ||
public async Task Map_Task_Right_returns_mapped_value() | ||
{ | ||
Maybe<T> maybe = T.Value; | ||
|
||
var maybe2 = await maybe.Map(ExpectAndReturn_Task(T.Value, T.Value2)); | ||
|
||
maybe2.HasValue.Should().BeTrue(); | ||
maybe2.Value.Should().Be(T.Value2); | ||
} | ||
|
||
[Fact] | ||
public async Task Map_Task_Right_returns_no_value_if_initial_maybe_is_null() | ||
{ | ||
Maybe<T> maybe = null; | ||
|
||
var maybe2 = await maybe.Map(ExpectAndReturn_Task(null, T.Value2)); | ||
|
||
maybe2.HasValue.Should().BeFalse(); | ||
} | ||
} | ||
} | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions | ||
{ | ||
public class MapTests_Task_Right : MaybeTestBase | ||
{ | ||
[Fact] | ||
public async Task Map_Task_Right_returns_mapped_value() | ||
{ | ||
Maybe<T> maybe = T.Value; | ||
|
||
var maybe2 = await maybe.Map(ExpectAndReturn_Task(T.Value, T.Value2)); | ||
|
||
maybe2.HasValue.Should().BeTrue(); | ||
maybe2.Value.Should().Be(T.Value2); | ||
} | ||
|
||
[Fact] | ||
public async Task Map_Task_Right_returns_no_value_if_initial_maybe_is_null() | ||
{ | ||
Maybe<T> maybe = null; | ||
|
||
var maybe2 = await maybe.Map(ExpectAndReturn_Task(null, T.Value2)); | ||
|
||
maybe2.HasValue.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public async Task Map_Task_Right_provides_context_to_selector() | ||
{ | ||
Maybe<T> maybe = T.Value; | ||
var context = 5; | ||
|
||
var maybe2 = await maybe.Map( | ||
(value, ctx) => | ||
{ | ||
ctx.Should().Be(context); | ||
return value.AsTask(); | ||
}, | ||
context | ||
); | ||
|
||
maybe2.HasValue.Should().BeTrue(); | ||
} | ||
} | ||
} |
80 changes: 50 additions & 30 deletions
80
CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.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,30 +1,50 @@ | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions | ||
{ | ||
public class MapTests_Task : MaybeTestBase | ||
{ | ||
[Fact] | ||
public async Task Map_Task_returns_mapped_value() | ||
{ | ||
Maybe<T> maybe = T.Value; | ||
|
||
var maybe2 = await maybe.AsTask().Map(ExpectAndReturn_Task(T.Value, T.Value2)); | ||
|
||
maybe2.HasValue.Should().BeTrue(); | ||
maybe2.Value.Should().Be(T.Value2); | ||
} | ||
|
||
[Fact] | ||
public async Task Map_Task_returns_no_value_if_initial_maybe_is_null() | ||
{ | ||
Maybe<T> maybe = null; | ||
|
||
var maybe2 = await maybe.AsTask().Map(ExpectAndReturn_Task(null, T.Value2)); | ||
|
||
maybe2.HasValue.Should().BeFalse(); | ||
} | ||
} | ||
} | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions | ||
{ | ||
public class MapTests_Task : MaybeTestBase | ||
{ | ||
[Fact] | ||
public async Task Map_Task_returns_mapped_value() | ||
{ | ||
Maybe<T> maybe = T.Value; | ||
|
||
var maybe2 = await maybe.AsTask().Map(ExpectAndReturn_Task(T.Value, T.Value2)); | ||
|
||
maybe2.HasValue.Should().BeTrue(); | ||
maybe2.Value.Should().Be(T.Value2); | ||
} | ||
|
||
[Fact] | ||
public async Task Map_Task_returns_no_value_if_initial_maybe_is_null() | ||
{ | ||
Maybe<T> maybe = null; | ||
|
||
var maybe2 = await maybe.AsTask().Map(ExpectAndReturn_Task(null, T.Value2)); | ||
|
||
maybe2.HasValue.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public async Task Map_Task_provides_context_to_selector() | ||
{ | ||
Maybe<T> maybe = T.Value; | ||
var context = 5; | ||
|
||
var maybe2 = await maybe | ||
.AsTask() | ||
.Map( | ||
(value, ctx) => | ||
{ | ||
ctx.Should().Be(context); | ||
return value.AsTask(); | ||
}, | ||
context | ||
); | ||
|
||
maybe2.HasValue.Should().BeTrue(); | ||
} | ||
} | ||
} |
82 changes: 51 additions & 31 deletions
82
CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Left.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,31 +1,51 @@ | ||
using System.Threading.Tasks; | ||
using CSharpFunctionalExtensions.ValueTasks; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions | ||
{ | ||
public class MapTests_ValueTask_Left : MaybeTestBase | ||
{ | ||
[Fact] | ||
public async Task Map_ValueTask_Left_returns_mapped_value() | ||
{ | ||
Maybe<T> maybe = T.Value; | ||
|
||
var maybe2 = await maybe.AsValueTask().Map(ExpectAndReturn(T.Value, T.Value2)); | ||
|
||
maybe2.HasValue.Should().BeTrue(); | ||
maybe2.Value.Should().Be(T.Value2); | ||
} | ||
|
||
[Fact] | ||
public async Task Map_ValueTask_Left_returns_no_value_if_initial_maybe_is_null() | ||
{ | ||
Maybe<T> maybe = null; | ||
|
||
var maybe2 = await maybe.AsValueTask().Map(ExpectAndReturn(null, T.Value2)); | ||
|
||
maybe2.HasValue.Should().BeFalse(); | ||
} | ||
} | ||
} | ||
using System.Threading.Tasks; | ||
using CSharpFunctionalExtensions.ValueTasks; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions | ||
{ | ||
public class MapTests_ValueTask_Left : MaybeTestBase | ||
{ | ||
[Fact] | ||
public async Task Map_ValueTask_Left_returns_mapped_value() | ||
{ | ||
Maybe<T> maybe = T.Value; | ||
|
||
var maybe2 = await maybe.AsValueTask().Map(ExpectAndReturn(T.Value, T.Value2)); | ||
|
||
maybe2.HasValue.Should().BeTrue(); | ||
maybe2.Value.Should().Be(T.Value2); | ||
} | ||
|
||
[Fact] | ||
public async Task Map_ValueTask_Left_returns_no_value_if_initial_maybe_is_null() | ||
{ | ||
Maybe<T> maybe = null; | ||
|
||
var maybe2 = await maybe.AsValueTask().Map(ExpectAndReturn(null, T.Value2)); | ||
|
||
maybe2.HasValue.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public async Task Map_ValueTask_Left_provides_context_to_selector() | ||
{ | ||
Maybe<T> maybe = T.Value; | ||
var context = 5; | ||
|
||
var maybe2 = await maybe | ||
.AsValueTask() | ||
.Map( | ||
(value, ctx) => | ||
{ | ||
ctx.Should().Be(context); | ||
return value; | ||
}, | ||
context | ||
); | ||
|
||
maybe2.HasValue.Should().BeTrue(); | ||
} | ||
} | ||
} |
Oops, something went wrong.