Path: redb-route/tests/redb.Route.Tests.XPath2/XPath2ExpressionTests.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.XPath2/XPath2ExpressionTests.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Expressions;
using redb.Route.XPath2;
using static redb.Route.XPath2.XPath2Dsl;
// Deliberately outside the redb.Route.* chain. Inside it, the simple name XPath2 resolves to the
// namespace redb.Route.XPath2 and shadows the statically imported XPath2(...) factory — the same
// trap a user hits only if they write routes in a namespace under redb.Route, where the answer is
// to spell it XPath2Dsl.XPath2(...). Documented in the package README.
namespace XPath2PackageTests;
///
/// XPath 2.0 as an expression: what it adds over 1.0, and where its boundary actually runs.
///
public class XPath2ExpressionTests
{
private const string Orders =
"""
10INV-2024-725INV-2025-37draft
""";
private static IExchange Exchange(string body = Orders, params (string Key, object? Value)[] headers)
{
var exchange = new Exchange(new Message(body));
foreach (var (key, value) in headers)
exchange.In.Headers[key] = value;
return exchange;
}
// ── What 1.0 could not do ──
[Fact]
public void Regular_expressions()
{
XPath2("matches(/orders/order[1]/ref, 'INV-\\d{4}-\\d+')").Evaluate(Exchange()).Should().BeTrue();
XPath2("replace(/orders/order[1]/ref, '[0-9]', '#')").Evaluate(Exchange()).Should().Be("INV-####-#");
XPath2("tokenize('a,b,c', ',')").Evaluate(Exchange()).Should().Equal("a", "b", "c");
}
[Fact]
public void Sequences_and_their_functions()
{
XPath2("distinct-values((1, 2, 2, 3))").Evaluate(Exchange()).Should().Equal("1", "2", "3");
XPath2("reverse((1, 2, 3))").Evaluate(Exchange()).Should().Equal("3", "2", "1");
XPath2("string-join(/orders/order/@id, '|')").Evaluate(Exchange()).Should().Be("A-1|B-2|C-3");
XPath2("empty(/orders/missing)").Evaluate(Exchange()).Should().BeTrue();
}
[Fact]
public void Aggregates_beyond_sum()
{
XPath2("avg(/orders/order/total)").Evaluate(Exchange()).Should().Be(14d);
XPath2("max(/orders/order/total)").Evaluate(Exchange()).Should().Be(25d);
}
[Fact]
public void A_conditional_inside_the_expression()
{
XPath2("if (count(/orders/order) > 2) then 'many' else 'few'")
.Evaluate(Exchange()).Should().Be("many");
}
[Fact]
public void For_return_over_nodes()
{
XPath2("for $o in /orders/order[number(total) > 8] return string($o/@id)")
.Evaluate(Exchange()).Should().Equal("A-1", "B-2");
}
[Fact]
public void Quantified_expressions()
{
XPath2("some $o in /orders/order satisfies number($o/total) > 20").Evaluate(Exchange()).Should().BeTrue();
XPath2("every $o in /orders/order satisfies number($o/total) > 20").Evaluate(Exchange()).Should().BeFalse();
}
[Fact]
public void Typed_comparison_and_dates()
{
XPath2("xs:date('2020-01-01') < xs:date('2021-01-01')").Evaluate(Exchange()).Should().BeTrue();
XPath2("/orders/order[number(total) gt 8]/@id").Evaluate(Exchange()).Should().Equal("A-1", "B-2");
}
// ── The boundary, stated as a test rather than as a hope ──
[Fact]
public void Let_where_and_order_by_are_XQuery_and_are_refused()
{
// These are FLWOR clauses of XQuery, not of XPath 2.0. The engine rejects them at compile
// time, which is the right moment — a route carrying one fails when it is built.
foreach (var xquery in new[]
{
"let $x := 5 return $x * 2",
"for $o in /orders/order where number($o/total) > 8 return $o",
"for $o in /orders/order order by number($o/total) return $o",
})
{
var act = () => XPath2(xquery);
act.Should().Throw()
.WithMessage("*Invalid XPath 2.0 expression*", $"'{xquery}' is XQuery, not XPath 2.0");
}
}
[Fact]
public void A_malformed_expression_fails_when_the_route_is_built()
{
var act = () => XPath2("for $x in");
act.Should().Throw().WithMessage("*Invalid XPath 2.0 expression*");
}
// ── The same surface as the 1.0 expression ──
[Fact]
public void A_condition_asks_whether_anything_matched()
{
((IPredicateExpression)XPath2("/orders/order[@id='B-2']")).Matches(Exchange()).Should().BeTrue();
((IPredicateExpression)XPath2("/orders/order[@id='Z-9']")).Matches(Exchange()).Should().BeFalse();
}
[Fact]
public void A_matched_node_holding_a_falsy_value_is_still_a_match()
{
var exchange = Exchange("0");
((IPredicateExpression)XPath2("/order/discount")).Matches(exchange).Should().BeTrue(
"the same rule the XPath 1.0 expression follows: a match is a match whatever it holds");
}
[Fact]
public void It_can_read_a_source_other_than_the_body()
{
var exchange = Exchange("", ("doc", Orders));
XPath2("string-join(/orders/order/@id, '|')").From(new HeaderExpression("doc"))
.Evaluate(exchange).Should().Be("A-1|B-2|C-3");
}
[Fact]
public void It_takes_bound_parameters()
{
var exchange = Exchange(Orders, ("wanted", "B-2"));
XPath2("/orders/order[@id=$id]/total")
.WithParameters(("id", new HeaderExpression("wanted")))
.Evaluate(exchange).Should().Be("25");
}
[Fact]
public void A_bound_value_carrying_XPath_syntax_stays_a_value()
{
var exchange = Exchange(Orders, ("wanted", "B-2' or '1'='1"));
XPath2("/orders/order[@id=$id]/total")
.WithParameters(("id", new HeaderExpression("wanted")))
.Evaluate(exchange).Should().BeNull();
}
[Fact]
public void It_binds_namespaces()
{
var exchange = Exchange("");
XPath2("string(/a:orders/a:order/@id)", ("a", "urn:acme"))
.Evaluate(exchange).Should().Be("X");
}
[Fact]
public void It_trims_when_asked()
{
var exchange = Exchange(" John ");
XPath2("/order/name").Evaluate(exchange).Should().Be(" John ");
XPath2("/order/name").Trimmed().Evaluate(exchange).Should().Be("John");
}
[Fact]
public void The_typed_form_converts_the_result()
{
XPath2("count(/orders/order)").Evaluate