Path: redb-route/tests/redb.Route.Tests/Processors/ChoiceProcessorTests.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests/Processors/ChoiceProcessorTests.cs
----
using AwesomeAssertions;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Processors;
namespace redb.Route.Tests.Processors;
/// Tests for and .
public class ChoiceProcessorTests
{
/// First matching when-clause executes.
[Fact]
public async Task Process_FirstMatchingWhen_Executes()
{
var result = "";
var choice = new ChoiceProcessor()
.When(ex => ex.In.Body is "A", new DelegateProcessor(_ => result = "branch-A"))
.When(ex => ex.In.Body is "B", new DelegateProcessor(_ => result = "branch-B"));
await choice.Process(new Exchange(new Message("A")));
result.Should().Be("branch-A");
}
/// Second match is used when first doesn't match.
[Fact]
public async Task Process_SecondMatch_WhenFirstFails()
{
var result = "";
var choice = new ChoiceProcessor()
.When(ex => ex.In.Body is "A", new DelegateProcessor(_ => result = "branch-A"))
.When(ex => ex.In.Body is "B", new DelegateProcessor(_ => result = "branch-B"));
await choice.Process(new Exchange(new Message("B")));
result.Should().Be("branch-B");
}
/// Only first matching executes (short-circuit).
[Fact]
public async Task Process_OnlyFirstMatch_ShortCircuits()
{
var count = 0;
var choice = new ChoiceProcessor()
.When(_ => true, new DelegateProcessor(_ => count++))
.When(_ => true, new DelegateProcessor(_ => count++));
await choice.Process(new Exchange());
count.Should().Be(1);
}
/// Otherwise executes when no when-clause matches.
[Fact]
public async Task Process_NoMatch_Otherwise_Executes()
{
var result = "";
var choice = new ChoiceProcessor()
.When(ex => ex.In.Body is "A", new DelegateProcessor(_ => result = "A"))
.SetOtherwise(new DelegateProcessor(_ => result = "otherwise"));
await choice.Process(new Exchange(new Message("X")));
result.Should().Be("otherwise");
}
/// No match and no otherwise — nothing happens.
[Fact]
public async Task Process_NoMatch_NoOtherwise_DoesNothing()
{
var executed = false;
var choice = new ChoiceProcessor()
.When(ex => ex.In.Body is "A", new DelegateProcessor(_ => executed = true));
await choice.Process(new Exchange(new Message("X")));
executed.Should().BeFalse();
}
/// WhenClauses property returns added clauses.
[Fact]
public void WhenClauses_ReturnsAll()
{
var choice = new ChoiceProcessor()
.When(_ => true, new DelegateProcessor(_ => { }))
.When(_ => false, new DelegateProcessor(_ => { }));
choice.WhenClauses.Should().HaveCount(2);
}
/// WhenClause validates arguments.
[Fact]
public void WhenClause_NullPredicate_Throws()
{
// The cast only picks the overload: both the predicate and the delegate constructors reject null.
var act = () => new WhenClause((redb.Route.Abstractions.IPredicate)null!, new DelegateProcessor(_ => { }));
act.Should().Throw();
}
}