Path: redb-route/tests/redb.Route.Tests/Core/PredicateOverloadTests.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/Core/PredicateOverloadTests.cs ---- using AwesomeAssertions; using redb.Route.Abstractions; using redb.Route.Core; using redb.Route.Definitions; using redb.Route.Expressions; using redb.Route.Extensions; namespace redb.Route.Tests.Core; /// /// The nested-lambda When(predicate, branch) takes the same argument kinds as the one-argument /// When(...): a lambda or method group, an and an . /// OnWhen and RetryWhile of an exception handler take an too. /// public class PredicateOverloadTests { private static bool IsOrder(IExchange e) => e.In.GetHeader("type") == "order"; [Fact] public void NestedWhen_BindsEachArgumentKindToItsOwnOverload() { var choice = new ChoiceDefinition(); choice.When(e => e.In.GetHeader("type") == "lambda", _ => { }); // lambda choice.When(IsOrder, _ => { }); // method group IExpression expression = new HeaderExpression("flag"); choice.When(expression, _ => { }); // IExpression IPredicate predicate = new HeaderExpression("type").isEqualTo("order"); choice.When(predicate, _ => { }); // IPredicate choice.When(new HeaderExpression("type").isEqualTo("payment"), _ => { }); // IPredicate, inline choice.When(RouteBuilder.Method("#rules", "IsGold"), _ => { }); // IPredicateExpression: the IExpression overload var whens = choice.Whens; whens.Should().HaveCount(6); whens[0].SourcePredicate.Should().BeNull(); whens[0].SourceExpression.Should().BeNull(); whens[1].SourcePredicate.Should().BeNull(); whens[1].SourceExpression.Should().BeNull(); whens[2].SourceExpression.Should().BeSameAs(expression); whens[3].SourcePredicate.Should().BeSameAs(predicate); whens[4].SourcePredicate.Should().NotBeNull(); whens[5].SourceExpression.Should().NotBeNull(); whens[5].SourcePredicate.Should().BeNull(); } [Fact] public void NestedWhen_WithAPredicate_ReturnsTheChoiceAndRunsTheBranch() { var choice = new ChoiceDefinition(); WhenDefinition? seen = null; var returned = choice.When(new HeaderExpression("type").isEqualTo("order"), w => seen = w); returned.Should().BeSameAs(choice); seen.Should().BeSameAs(choice.Whens[0]); } [Fact] public void NestedWhen_WithANullBranch_IsRefused() { var choice = new ChoiceDefinition(); var act = () => choice.When(new HeaderExpression("type").isEqualTo("order"), null!); act.Should().Throw(); } [Fact] public async Task ARoute_ThroughTheNestedPredicateWhen_TakesTheMatchingBranch() { var taken = new List(); await using var context = new RouteContext(); context.AddRoutes(r => { r.From("direct://choice-predicate") .Choice(c => c .When(new HeaderExpression("type").isEqualTo("order"), b => b.Process(_ => taken.Add("order"))) .When(new HeaderExpression("type").isEqualTo("payment"), b => b.Process(_ => taken.Add("payment"))) .Otherwise(b => b.Process(_ => taken.Add("other")))) .Process(_ => taken.Add("tail")); }); await context.Start(); var producer = context.GetEndpoint("direct://choice-predicate").CreateProducer(); await producer.Start(); foreach (var type in new[] { "payment", "order", "refund" }) { var message = new Message("x"); message.Headers["type"] = type; await producer.Process(new Exchange(message)); } taken.Should().Equal("payment", "tail", "order", "tail", "other", "tail"); } [Fact] public async Task AnExceptionHandler_WithAPredicateOnWhen_IsPassedOverWhenItDoesNotHold() { var taken = new List(); await using var context = new RouteContext(); context.AddRoutes(r => { r.OnException().OnWhen(new HeaderExpression("urgent").isEqualTo(true)) .Handled().Process(_ => taken.Add("urgent")); r.OnException().Handled().Process(_ => taken.Add("any")); r.From("direct://onwhen-predicate").Process(_ => throw new IOException("io")); }); await context.Start(); var producer = context.GetEndpoint("direct://onwhen-predicate").CreateProducer(); await producer.Start(); await producer.Process(new Exchange(new Message("a"))); var urgent = new Message("b"); urgent.Headers["urgent"] = true; await producer.Process(new Exchange(urgent)); taken.Should().Equal("any", "urgent"); } [Fact] public async Task AnExceptionHandler_WithAPredicateRetryWhile_StopsRetryingWhenItNoLongerHolds() { var attempts = 0; await using var context = new RouteContext(); context.AddRoutes(r => { r.OnException() .MaximumRedeliveries(10) .RedeliveryDelay(TimeSpan.Zero) .RetryWhile(new HeaderExpression("retry").isEqualTo(true)) .Handled(); r.From("direct://retrywhile-predicate").Process(e => { attempts++; if (attempts == 3) e.In.Headers["retry"] = false; throw new IOException("io"); }); }); await context.Start(); var producer = context.GetEndpoint("direct://retrywhile-predicate").CreateProducer(); await producer.Start(); var message = new Message("x"); message.Headers["retry"] = true; await producer.Process(new Exchange(message)); attempts.Should().Be(3); } [Fact] public void PredicateOnWhenAndRetryWhile_RefuseNull() { var definition = new OnExceptionDefinition([typeof(IOException)]); ((Action)(() => definition.OnWhen((IPredicate)null!))).Should().Throw(); ((Action)(() => definition.RetryWhile((IPredicate)null!))).Should().Throw(); } }