Path: redb-route/tests/redb.Route.Tests/Expressions/ExpressionsGuideExamplesTests.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/ExpressionsGuideExamplesTests.cs
----
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using AwesomeAssertions;
using redb.Route.Core;
using redb.Route.Expressions;
using redb.Route.Predicates;
namespace redb.Route.Tests.Expressions;
/// A person in the guide's fixture exchange.
public sealed class GuidePerson
{
public string Name { get; set; } = "";
public int Age { get; set; }
public bool Active { get; set; }
public GuidePerson? Manager { get; set; }
}
/// The bean the guide's bean: examples call.
public sealed class GuidePricing
{
public double Gross(int qty, double price) => qty * price;
public bool IsGold(string? name) => name == "Ann";
public string Badge(string name, int points) => $"{name}:{points}";
public void Touch(string _) { }
public string Rate => "standard";
}
///
/// Runs every example of redb.Route/EXPRESSIONS.md that the guide marks for running: each table preceded by
/// <!-- run: value -->, <!-- run: template --> or <!-- run: condition --> is
/// evaluated row by row on the exchange of the guide's section 0, and the result column must be what the engine
/// gives. The guide cannot drift from the code: a changed result fails here.
///
public sealed class ExpressionsGuideExamplesTests : IAsyncLifetime
{
private readonly RouteContext _context = new();
public Task InitializeAsync()
{
_context.AddToRegistry("pricing", new GuidePricing());
_context.AddToRegistry("pricing-rules", new GuidePricing());
_context.AddToRegistry("limits.max", 100);
return Task.CompletedTask;
}
public async Task DisposeAsync() => await _context.DisposeAsync();
private static string Guide([CallerFilePath] string file = "")
=> Path.GetFullPath(Path.Combine(Path.GetDirectoryName(file)!, "..", "..", "..", "EXPRESSIONS.md"));
/// The exchange of the guide's section 0, fresh for every row (increments change it).
private Exchange Fixture(string? body)
{
var exchange = new Exchange(new Message(body switch
{
"json" => """{"order":{"id":7,"total":150.5,"tags":["a","b"]}}""",
"xml" => "7150.5",
_ => "payload",
})) { Context = _context };
var headers = exchange.In.Headers;
headers["a"] = 42;
headers["b"] = 3;
headers["neg"] = -7;
headers["zero"] = 0;
headers["price"] = 2.5;
headers["s"] = "Hello World";
headers["flag"] = true;
headers["off"] = false;
headers["Content-Type"] = "text/xml";
headers["empty"] = "";
headers["user"] = new GuidePerson { Name = "Ann", Age = 30, Active = true, Manager = new GuidePerson { Name = "Bob", Age = 51 } };
headers["list"] = new List { 5, 1, 9 };
headers["when"] = new DateTime(2026, 8, 28, 10, 30, 0, DateTimeKind.Utc);
headers["O'Brien"] = "obrien";
headers["redbFile.Name"] = "orders/a.xml";
headers["redbFile.NameOnly"] = "a";
exchange.Properties["count"] = 7;
exchange.Properties["items"] = new List { "p", "q", "r" };
exchange.Properties["nums"] = new List { 1, 2, 3, 4 };
exchange.Properties["cfg"] = new Dictionary { ["enabled"] = true, ["limit"] = 5 };
exchange.Properties["counter"] = 10;
exchange.Exception = new InvalidOperationException("boom");
return exchange;
}
private static readonly Regex Marker = new(@"^$", RegexOptions.Compiled);
private static readonly Regex Code = new(@"`(?[^`]+)`", RegexOptions.Compiled);
public sealed record Example(string Mode, string? Body, string Expression, string Expected, int Line)
{
public override string ToString() => $"line {Line}: {Expression}";
}
/// Every marked row of the guide.
public static IEnumerable