Skip to content

Commit

Permalink
Introduce async variant of Option.Map (#45)
Browse files Browse the repository at this point in the history
* upgrade target framework

* introduce async option for map

* upgrade workflow to use .net 8
  • Loading branch information
lfcyja authored Nov 29, 2024
1 parent 5d731ff commit aea9389
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 7.0.*
dotnet-version: 8.0.*
- name: Install dependencies
run: dotnet restore
- name: Build
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion Galaxus.Functional.Tests/Galaxus.Functional.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Threading.Tasks;
using NUnit.Framework;

namespace Galaxus.Functional.Tests.Option.Async;

[TestFixture]
internal class AsyncOptionExtensions_MapTest
{
public sealed class MapAsyncTest
{
[Test]
public async Task TestMapAsync_WhenSelfIsSome()
{
var mapInvoked = false;

var subject = "value".ToOption();
var value = await subject.MapAsync(
s => Task.Run(() =>
{
mapInvoked = true;
return s.Length.ToOption();
}));

Assert.AreEqual(5.ToOption(), value);
Assert.IsTrue(mapInvoked);
}

[Test]
public async Task TestMapAsync_WhenSelfIsNone()
{
var mapInvoked = false;

var subject = Option<string>.None;
var value = await subject.MapAsync(
s => Task.Run(() =>
{
mapInvoked = true;
return s.Length.ToOption();
}));

Assert.AreEqual(Option<int>.None, value);
Assert.IsFalse(mapInvoked);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,8 @@ public static async Task<TTo> MapOrElseAsync<T, TTo>(this Option<T> self, Func<T
{
return await self.MapOrElse(map, fallback).ConfigureAwait(false);
}

/// <inheritdoc cref="Option{T}.Map{TTo}"/>
public static Task<Option<TTo>> MapAsync<T, TTo>(this Option<T> self, Func<T, Task<Option<TTo>>> map)
=> self.MapOrElseAsync(map, () => Option<TTo>.None);
}

0 comments on commit aea9389

Please sign in to comment.