-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from Meerownymous/i14-add-conditional-w2arp
feat: conditional
- Loading branch information
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -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 | ||
){ } | ||
} |
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 |
---|---|---|
@@ -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() | ||
); | ||
} | ||
} |