Path: redb-route/tests/redb.Route.Tests/Expressions/PredicateBranchMatrixTests.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/Expressions/PredicateBranchMatrixTests.cs
----
using AwesomeAssertions;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Expressions;
using redb.Route.Predicates;
namespace redb.Route.Tests.Expressions;
///
/// Characterisation matrix for the ways a string becomes a boolean in this library.
/// It is a permanent part of the suite: it pins down what each path does, so a change to one of
/// them shows up here instead of surfacing as a silently mis-routed message.
///
/// Value position — plus the DSL truthiness rule.
/// This is what Expr(...) in SetBody / SetHeader does: an explicit
/// expression, evaluated and read for truthiness. Since 2026-08-28 it reads operators
/// whatever the spacing, like every other position.
/// Condition position — , what Filter,
/// When, LoopWhile and Validate use for a string. A ${...}
/// template stays a template; anything else is compiled by the AST parser.
///
///
[Collection("ExpressionResolver")]
public class PredicateBranchMatrixTests
{
/// What a path did with a condition.
public enum Outcome
{
/// The path produced false.
False,
/// The path produced true.
True,
/// The path refused to compile the condition.
Throws
}
private static IExchange CreateExchange()
{
var exchange = new Exchange(new Message("payload"));
exchange.In.Headers["a"] = 42;
exchange.In.Headers["b"] = 3;
exchange.In.Headers["flag"] = true;
exchange.In.Headers["s"] = "hello";
exchange.In.Headers["obj"] = new List { 1, 2, 3, 4, 5 };
exchange.Properties["count"] = 7;
exchange.Properties["flag"] = false;
exchange.Properties["items"] = new List { "p", "q", "r" };
exchange.Properties["name"] = "abcd";
return exchange;
}
///
/// condition, value position, condition position, the answer a reader of the condition would
/// expect. The hand-written logical branch used to be a fourth column; it was removed with
/// the branch itself on 2026-08-28 (one language, one parser).
///
public static TheoryData Matrix() => new()
{
// value condition expected
{ "header.a > 10", Outcome.True, Outcome.True, Outcome.True },
{ "header.a>10", Outcome.True, Outcome.True, Outcome.True },
{ "header.a >= 10", Outcome.True, Outcome.True, Outcome.True },
{ "header.a>=10", Outcome.True, Outcome.True, Outcome.True },
{ "header.a == 'x'", Outcome.False, Outcome.False, Outcome.False },
{ "header.a=='x'", Outcome.False, Outcome.False, Outcome.False },
{ "header.a != 'x'", Outcome.True, Outcome.True, Outcome.True },
{ "header.a>10 AND header.b<5", Outcome.True, Outcome.True, Outcome.True },
{ "header.a > 10 AND header.b < 5", Outcome.True, Outcome.True, Outcome.True },
{ "property.count > 5 OR property.flag", Outcome.True, Outcome.True, Outcome.True },
{ "count(property.items) > 2", Outcome.True, Outcome.True, Outcome.True },
{ "count(property.items)>2", Outcome.True, Outcome.True, Outcome.True },
{ "length(property.name) > 3 ? true : false", Outcome.True, Outcome.True, Outcome.True },
{ "NOT header.flag", Outcome.False, Outcome.False, Outcome.False },
{ "!header.flag", Outcome.False, Outcome.False, Outcome.False },
{ "header.flag", Outcome.True, Outcome.True, Outcome.True },
{ "header.missing", Outcome.False, Outcome.False, Outcome.False },
{ "${header.flag}", Outcome.True, Outcome.True, Outcome.True },
{ "header.a\t>\t10", Outcome.True, Outcome.True, Outcome.True },
{ "header.a > 10", Outcome.True, Outcome.True, Outcome.True },
{ "(header.a > 10) AND (header.b < 5)", Outcome.True, Outcome.True, Outcome.True },
{ "(header.a>10) AND (header.b<5)", Outcome.True, Outcome.True, Outcome.True },
{ "header.s == 'a > b'", Outcome.False, Outcome.False, Outcome.False },
{ "header.s == 'a AND b'", Outcome.False, Outcome.False, Outcome.False },
{ "contains(header.s, 'ell')", Outcome.True, Outcome.True, Outcome.True },
{ "header.a + header.b > 40", Outcome.True, Outcome.True, Outcome.True },
};
[Theory]
[MemberData(nameof(Matrix))]
public void ValuePosition_MatchesRecordedBehaviour(string condition, Outcome value, Outcome _, Outcome __)
=> Run(() => RouteTruthiness.ToBoolean(new StringExpression(condition).Evaluate