Skip to content

Commit

Permalink
Merge pull request #16 from Meerownymous/i14-add-conditional-w2arp
Browse files Browse the repository at this point in the history
feat: conditional
  • Loading branch information
Meerownymous authored Dec 16, 2024
2 parents 4423558 + 6d544ec commit dd4186f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions WHyLL.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
appveyor.yml = appveyor.yml
build.sh = build.sh
.github\workflows\ci-cd.yml = .github\workflows\ci-cd.yml
.gitignore = .gitignore
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WHyLL", "src\WHyLL\WHyLL.csproj", "{EAEA2EE1-691C-43B9-B171-8430D5ED6813}"
Expand Down
29 changes: 29 additions & 0 deletions src/WHyLL/Warp/Conditional.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace WHyLL.Warp;

/// <summary>
/// Warp depending on a condition.
/// </summary>
public sealed class Conditional<TOutcome>(
Func<IMessage, Task<bool>> condition,
IWarp<TOutcome> yes,
IWarp<TOutcome> no)
: WarpEnvelope<TOutcome>(
new MessageAs<TOutcome>(async msg =>
await condition(msg)
? await msg.To(yes)
: await msg.To(no)
)
)
{
/// <summary>
/// Warp depending on a condition.
/// </summary>
public Conditional(
Func<IMessage, bool> condition,
IWarp<TOutcome> yes,
IWarp<TOutcome> no
) : this(message => Task.FromResult(condition(message)),
yes,
no
){ }
}
33 changes: 33 additions & 0 deletions tests/Test.WHyLL/Warp/ConditionalTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using WHyLL.Warp;
using Xunit;

namespace Test.WHyLL.Warp;

public sealed class ConditionalTests
{
[Fact]
public async Task DelegatesToYes()
{
Assert.Equal(100,
await
new Conditional<int>(
_ => true,
new BodyAs<int>(_ => 100),
new BodyAs<int>(_ => 0)
).Render()
);
}

[Fact]
public async Task DelegatesToNo()
{
Assert.Equal(50,
await
new Conditional<int>(
_ => false,
new BodyAs<int>(_ => 100),
new BodyAs<int>(_ => 50)
).Render()
);
}
}

0 comments on commit dd4186f

Please sign in to comment.