Path: redb-route/tests/redb.Route.Tests/Expressions/NamespacedCallGrammarTests.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/NamespacedCallGrammarTests.cs
----
using AwesomeAssertions;
using redb.Route.Core;
using redb.Route.Expressions;
using redb.Route.Expressions.Ast;
using redb.Route.Predicates;
namespace redb.Route.Tests.Expressions;
///
/// The grammar of a namespaced call, bean:name.Method(args) (stage 2 step c): the namespace token, the target
/// with dashed registry names, the required method, ${...} in the arguments only, the ternary edge, both
/// dispatchers reaching the AST.
///
public sealed class NamespacedCallGrammarTests
{
private static (TokenType, string)[] Tokens(string expression)
=> [.. new Tokenizer(expression).GetAllTokens().Where(t => t.Type != TokenType.Eof).Select(t => (t.Type, t.Value))];
private static AstNode Parse(string expression) => new Parser(new Tokenizer(expression).GetAllTokens()).Parse();
[Fact]
public void Namespace_IsOneToken_AndTheTargetFollows()
{
var tokens = Tokens("bean:svc.Check(body)");
tokens.Select(t => t.Item1).Should().Equal(
TokenType.Namespace, TokenType.Function, TokenType.LeftParen, TokenType.Identifier, TokenType.RightParen);
tokens[0].Item2.Should().Be("bean");
tokens[1].Item2.Should().Be("svc.Check");
}
[Fact]
public void DashedRegistryName_IsPartOfTheTarget()
{
var call = Parse("bean:seen-orders.Check()").Should().BeOfType().Subject;
call.Reference.Should().Be("seen-orders");
call.Method.Should().Be("Check");
call.Arguments.Should().BeEmpty();
}
[Fact]
public void MinusAfterTheCall_IsTheOperator()
{
Tokens("bean:svc.Count()-1").Select(t => t.Item1).Should().Equal(
TokenType.Namespace, TokenType.Function, TokenType.LeftParen, TokenType.RightParen,
TokenType.Operator, TokenType.Number);
// The one shape a reader may misread: without parentheses the method ends at the dash.
Tokens("bean:svc.Count-1").Should().Equal(
(TokenType.Namespace, "bean"), (TokenType.Identifier, "svc.Count"), (TokenType.Operator, "-"), (TokenType.Number, "1"));
}
[Fact]
public void MethodWithoutParentheses_HasNoArguments()
{
var call = Parse("bean:svc.Total").Should().BeOfType().Subject;
call.Reference.Should().Be("svc");
call.Method.Should().Be("Total");
call.Arguments.Should().BeEmpty();
}
[Theory]
[InlineData("bean:svc")]
[InlineData("bean:svc(body)")]
[InlineData("bean:svc.(body)")]
public void TheMethod_IsRequired(string expression)
{
var act = () => Parse(expression);
act.Should().Throw()
.WithMessage("a bean: call names its method: bean:svc.Method(...) (position 1 within the expression).");
}
[Fact]
public void PlaceholderInTheArguments_IsTheTypedValueOfItsExpression()
{
var call = Parse("bean:svc.Check(${header.a}, 'x', ${body})").Should().BeOfType().Subject;
call.Arguments.Should().HaveCount(3);
call.Arguments[0].Should().BeOfType().Which.Name.Should().Be("header.a");
call.Arguments[2].Should().BeOfType().Which.Name.Should().Be("body");
}
[Fact]
public void PlaceholderOutsideBeanArguments_IsRefused()
{
var act = () => Parse("header.a + ${header.b}");
act.Should().Throw()
.WithMessage("${...} inside an expression is supported only in the arguments of a bean: call (position 12 within the expression).");
}
[Fact]
public void Ternary_WithSpaces_ReadsAsBefore()
{
Tokens("flag ? bean : other").Select(t => t.Item1).Should().Equal(
TokenType.Identifier, TokenType.Operator, TokenType.Identifier, TokenType.Colon, TokenType.Identifier);
}
[Fact]
public void Ternary_WithoutSpaces_BeforeAName_IsANamespacedCall()
{
// Documented edge: "bean" written against a colon and a name is the namespace. Write spaces in a ternary
// whose branch is a variable named bean.
var act = () => Parse("flag?bean:other");
act.Should().Throw().WithMessage("a bean: call names its method: bean:other.Method(...)*");
}
[Theory]
[InlineData("${bean:svc.Check(body)}")]
[InlineData("${bean:svc.Check(${body})}")]
[InlineData("total: ${bean:pricing-rules.Gross(header.qty, header.price)}")]
public void TemplateHole_ReachesTheAst(string template)
{
// Only the AST knows the namespace: another path would read the hole as a property and give null. The call
// site's own error, raised on an exchange without a context, proves the AST compiled it.
var expression = new StringExpression(template);
expression.Invoking(e => e.Evaluate