Path: redb-route/tests/redb.Route.Tests/Expressions/BeanFunctionTests.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/BeanFunctionTests.cs
----
using AwesomeAssertions;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Expressions;
using redb.Route.Predicates;
namespace redb.Route.Tests.Expressions;
/// A bean the expression function calls.
public class PricingRules
{
public int Calls;
public double Gross(int qty, double price) { Calls++; return qty * price; }
public bool IsGold(string? name) => name == "ann";
public string Stamp(IExchange exchange, string mark) => $"{mark}:{exchange.In.Body}";
public string Name { get; set; } = "rules";
public Task CheckAsync(string name) => Task.FromResult(name == "ann");
public ValueTask CountAsync() => ValueTask.FromResult(1);
}
///
/// The bean: function of the expression language (stage 2 step d): the bean from the exchange's context
/// registry, the method bound by the bean: URI rules, synchronous only, refused under the sandbox, resolved once
/// per context. Used the same way in templates, bare conditions, C# routes and XML routes.
///
public sealed class BeanFunctionTests : IAsyncLifetime
{
private readonly RouteContext _context = new();
private readonly PricingRules _rules = new();
public Task InitializeAsync()
{
_context.AddToRegistry("rules", _rules);
_context.AddToRegistry("pricing-rules", _rules);
return Task.CompletedTask;
}
public async Task DisposeAsync() => await _context.DisposeAsync();
private Exchange Message(object? body = null, params (string Key, object? Value)[] headers)
{
var exchange = new Exchange(new Message(body ?? "payload")) { Context = _context };
foreach (var (key, value) in headers)
exchange.In.Headers[key] = value;
return exchange;
}
[Theory]
[InlineData("${bean:rules.Gross(header.qty, header.price)}")]
[InlineData("${bean:rules.Gross(${header.qty}, ${header.price})}")]
[InlineData("${bean:pricing-rules.Gross(header.qty, 2.5)}")]
public void ATemplateHole_CallsTheBean_AndKeepsTheType(string template)
{
new StringExpression(template).Evaluate