Path: redb-route/tests/redb.Route.Tests/Expressions/XPathPredicateSemanticsTests.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/XPathPredicateSemanticsTests.cs ---- using AwesomeAssertions; using redb.Route.Core; using redb.Route.Expressions; using XPathExpr = redb.Route.Expressions.XPathExpression; namespace redb.Route.Tests.Expressions; /// /// XPath in a condition position asks whether the path matched, not what the matched node says. /// /// This is XPath 1.0, not a house rule: a node-set is true when it is non-empty, and the node's /// content never enters the question. The distinction is invisible until the content happens to /// look falsy — <discount>0</discount>, an empty <vip/>, a /// <flag>false</flag> — and then a matched node reads as "no match". /// /// /// The give-away that the old reading was a defect rather than a choice is that it disagreed with /// itself: one <discount>0</discount> was false while two of them were true, /// because a multi-node result never got flattened to a scalar. /// /// /// Asking for a CLR type is a different question and keeps its own answer: /// XPath<bool>("/order/flag") reads the node's text, so it is false. So does an XPath /// expression that already produces a scalar — string(...), count(...) > 0 — which /// is read by the one DSL truthiness rule like any other value. /// /// public class XPathPredicateSemanticsTests { private const string Order = """ 0 false """; private const string TwoDiscounts = """ 0 0 """; // ── Harness ── // // One context per run: a route added after Start() is not started, so sharing a context // between two runs inside one test silently answers "did not pass" for the second one. private static async Task PassesFilter(string id, redb.Route.Abstractions.IExpression expression, string xml) { await using var context = new RouteContext(); var passed = false; context.AddRoutes(r => r.From($"direct://xp-{id}") .Filter(expression) .Process(_ => passed = true) .EndFilter()); await Send(context, $"xp-{id}", xml); return passed; } private static async Task MatchesWhen(string id, redb.Route.Abstractions.IExpression expression, string xml) { await using var context = new RouteContext(); var matched = false; context.AddRoutes(r => r.From($"direct://xp-{id}") .Choice() .When(expression) .Process(_ => matched = true) .EndChoice()); await Send(context, $"xp-{id}", xml); return matched; } private static async Task Send(RouteContext context, string id, string xml) { await context.Start(); var producer = context.GetEndpoint($"direct://{id}").CreateProducer(); await producer.Start(); await producer.Process(new Exchange(new Message(xml))); } // ── The spelling the semantics exist for ── [Fact] public async Task An_expression_goes_straight_into_a_condition_verb() { // A compile-time guard, not a runtime one. Condition verbs are overloaded on IExpression // and IPredicate both, so an expression that implemented IPredicate would make this call // ambiguous and cost the language the very spelling the semantics were added for. The rest // of this class passes expressions through an IExpression-typed parameter, which would have // hidden that. var passed = false; await using var context = new RouteContext(); context.AddRoutes(r => r.From("direct://xp-direct") .Filter(new XPathExpr("/order/discount")) .Process(_ => passed = true) .EndFilter()); await context.Start(); var producer = context.GetEndpoint("direct://xp-direct").CreateProducer(); await producer.Start(); await producer.Process(new Exchange(new Message(Order))); passed.Should().BeTrue(); } // ── The node-set question ── [Fact] public async Task A_path_that_matches_nothing_is_false() { var passed = await PassesFilter("miss", new XPathExpr("/order/missing"), Order); passed.Should().BeFalse(); } [Fact] public async Task A_matched_node_holding_zero_is_still_a_match() { var passed = await PassesFilter("zero", new XPathExpr("/order/discount"), Order); passed.Should().BeTrue("the element exists; XPath 1.0 reads a non-empty node-set as true"); } [Fact] public async Task A_matched_empty_element_is_still_a_match() { var passed = await PassesFilter("vip", new XPathExpr("/order/vip"), Order); passed.Should().BeTrue("an element with no content is an element that matched"); } [Fact] public async Task A_matched_node_holding_the_word_false_is_still_a_match() { var passed = await PassesFilter("flag", new XPathExpr("/order/flag"), Order); passed.Should().BeTrue("the node's text is not the question a bare path asks"); } [Fact] public async Task Two_matched_nodes_are_a_match() { var passed = await PassesFilter("two", new XPathExpr("/order/discount"), TwoDiscounts); passed.Should().BeTrue(); } [Fact] public async Task One_match_and_two_matches_answer_the_same_way() { var one = await PassesFilter("cons1", new XPathExpr("/order/discount"), Order); var two = await PassesFilter("cons2", new XPathExpr("/order/discount"), TwoDiscounts); one.Should().Be(two, "the same path over the same element name cannot flip on how many matched"); } [Fact] public async Task When_reads_a_path_the_same_way_Filter_does() { var matched = await MatchesWhen("when", new XPathExpr("/order/discount"), Order); matched.Should().BeTrue(); } // ── The other two questions, unchanged ── [Fact] public async Task Asking_for_a_CLR_bool_still_reads_the_node_text() { var passed = await PassesFilter("typed", new TypedXPathExpression("/order/flag"), Order); passed.Should().BeFalse("XPath asks to convert the node's text, and the text is 'false'"); } [Fact] public async Task An_XPath_boolean_function_keeps_XPath_semantics() { var present = await PassesFilter("fn1", new XPathExpr("boolean(/order/discount)"), Order); var absent = await PassesFilter("fn2", new XPathExpr("boolean(/order/missing)"), Order); present.Should().BeTrue(); absent.Should().BeFalse(); } [Fact] public async Task An_XPath_scalar_result_is_read_by_the_one_DSL_truthiness_rule() { // string() produces a scalar, so the node-set question never arises and the DSL rule // applies exactly as it would to a header holding "0". var zeroText = await PassesFilter("str", new XPathExpr("string(/order/discount)"), Order); var counted = await PassesFilter("cnt", new XPathExpr("count(/order/discount) > 0"), Order); zeroText.Should().BeFalse(); counted.Should().BeTrue(); } }