Path: redb-route/tests/redb.Route.Tests.Xml/XmlBeanListAndRefTests.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.Xml/XmlBeanListAndRefTests.cs
----
using System.Xml.Linq;
using AwesomeAssertions;
using redb.Route.Core;
using redb.Route.Xml;
namespace redb.Route.Tests.Xml;
/// One partner of a node: registered on its own, listed by the node.
public class ProbePartner
{
public string? Id { get; set; }
}
/// A node holding its partners in every collection shape a list builds into.
public class ProbeNode
{
public List Partners { get; set; } = [];
public ProbePartner[]? PartnerArray { get; set; }
public IReadOnlyList? ReadOnlyPartners { get; set; }
public IEnumerable? Ports { get; set; }
public List>? Groups { get; set; }
public ProbePartner? Signing { get; set; }
public string? Title { get; set; }
}
/// Partners through the constructor, and through a static creator taking a List.
public class ProbeRoster(IReadOnlyList partners)
{
public IReadOnlyList Partners => partners;
public static ProbeRoster Of(List partners) => new(partners);
}
///
/// <bean> lists and references: a node that lists partners registered on their own (the AS4
/// shape: each partner its own bean, rotation a second slot) is built from XML, into the declared
/// collection type, and a reference may only look back — never forward, never at nothing.
///
public class XmlBeanListAndRefTests : IAsyncLifetime
{
private readonly RouteContext _context = new();
public Task InitializeAsync() => Task.CompletedTask;
public async Task DisposeAsync()
{
await _context.DisposeAsync();
GC.SuppressFinalize(this);
}
private static string T() => $"{typeof(TType).FullName}, {typeof(TType).Assembly.GetName().Name}";
private static string Partners => $"""
""";
private void LoadContext(string beans)
=> _context.AddXmlContextFromContent($"""
{beans}
""");
// ── building ─────────────────────────────────────────────────────────────
[Fact]
public void Property_List_OfRefsAndAnInlineBean_BuildsEveryCollectionShape()
{
_context.SetProperty("as4.partner3.id", "initech");
_context.SetProperty("as4.port", "8443");
LoadContext(Partners + $"""
{"{{"}as4.port{"}}"}8080
ab
""");
var node = _context.GetFromRegistry("node")!;
var acme = _context.GetFromRegistry("acme");
var globex = _context.GetFromRegistry("globex");
node.Partners.Should().HaveCount(3);
node.Partners[0].Should().BeSameAs(acme, "a reference is the registered instance, not a copy");
node.Partners[1].Should().BeSameAs(globex);
node.Partners[2].Id.Should().Be("initech", "an inline bean resolves its placeholders like any other");
node.PartnerArray.Should().ContainSingle().Which.Should().BeSameAs(globex);
node.ReadOnlyPartners.Should().Equal(acme!, globex!);
node.Ports.Should().Equal(8443, 8080);
node.Groups.Should().HaveCount(2);
node.Groups![0].Should().Equal("a", "b");
node.Groups[1].Should().BeEmpty();
}
[Fact]
public void Property_RefAttribute_AssignsTheRegisteredBean()
{
LoadContext(Partners + $"""
""");
_context.GetFromRegistry("node")!.Signing
.Should().BeSameAs(_context.GetFromRegistry("acme"));
}
[Fact]
public void ConstructorArg_ListWithOf_BuildsTheArrayTheConstructorTakes()
{
LoadContext(Partners + $"""
""");
_context.GetFromRegistry("roster")!.Partners.Select(p => p.Id).Should().Equal("acme", "globex");
}
[Fact]
public void FactoryMethod_ListArgument_IsTypedByTheParameter()
{
LoadContext(Partners + $"""
""");
_context.GetFromRegistry("roster")!.Partners.Should().ContainSingle().Which.Id.Should().Be("globex");
}
[Fact]
public void ReferenceFromTheContextFile_ReachesABeanOfARoutesFile()
{
// The context file loads first, so a routes-file bean may point back at it.
LoadContext(Partners);
_context.AddXmlRoutesFromContent($"""
""");
_context.GetFromRegistry("node")!.Signing!.Id.Should().Be("globex");
}
// ── refusals ─────────────────────────────────────────────────────────────
[Fact]
public void ForwardReference_IsAPositionedError_NotANull()
{
var act = () => LoadContext($"""
""");
act.Should().Throw()
.WithMessage("*(3,*bean reference 'late' names nothing registered so far*");
}
[Fact]
public void UnknownReferenceInsideAList_IsAPositionedError()
{
var act = () => LoadContext($"""
""");
act.Should().Throw().WithMessage("*bean reference 'nobody' names nothing registered so far*");
}
[Fact]
public void ListItemOfTheWrongType_IsRefused_NamingTheItem()
{
var act = () => LoadContext($"""
""");
act.Should().Throw()
.WithMessage($"*'{typeof(ProbeOptions).FullName}' is not assignable to ProbePartner for 'Partners[0]'*");
}
[Fact]
public void ListIntoAPropertyThatIsNoCollection_IsRefused()
{
var act = () => LoadContext($"""
x
""");
act.Should().Throw().WithMessage("*'Title' is String, which a does not build into*");
}
[Fact]
public void ListOf_DifferingFromThePropertyElementType_IsRefused()
{
var act = () => LoadContext($"""
""");
act.Should().Throw().WithMessage("*differs from the element type*of 'Partners'*");
}
[Fact]
public void ConstructorList_WithoutOf_IsRefused()
{
var act = () => LoadContext(Partners + $"""
""");
act.Should().Throw().WithMessage("*'constructorArg 1': a passed to a constructor names its element type with of=*");
}
[Fact]
public void RefWithAHash_IsRefused_OneSpellingOnly()
{
var act = () => LoadContext(Partners + $"""
""");
act.Should().Throw().WithMessage("*a bean reference is the bare name: 'acme', not '#acme'*");
}
[Fact]
public void SlotWithTwoForms_IsRefused()
{
var act = () => LoadContext(Partners + $"""
""");
act.Should().Throw().WithMessage("* takes exactly one of: value=, ref=*");
}
[Fact]
public void ValueOutsideAList_IsRefused()
{
var act = () => LoadContext($"""
x
""");
act.Should().Throw().WithMessage("* belongs inside a *");
}
[Fact]
public void Schema_AcceptsListsAndReferences_AndRejectsAForeignListItem()
{
var valid = XDocument.Parse($"""
1
""", LoadOptions.SetLineInfo);
XmlRouteSchema.Validate(valid, ElementRegistry.CreateDefault()).Should().BeEmpty();
var invalid = XDocument.Parse("""
""", LoadOptions.SetLineInfo);
XmlRouteSchema.Validate(invalid, ElementRegistry.CreateDefault()).Should().NotBeEmpty();
}
// ── generated C# ─────────────────────────────────────────────────────────
[Fact]
public void Generator_PrintsReferencesAndLists()
{
var document = XDocument.Parse($"""
{Partners}
{"{{"}x{"}}"}
""");
var code = XmlCodeGenerator.Generate(document, "ListGenerated", "Tests.Generated");
code.Should().Contain("(\"Partners\", XmlBeans.List(Context!, null, [XmlBeans.Ref(Context!, \"acme\"), \"{{x}}\"]))")
.And.Contain("(\"Signing\", XmlBeans.Ref(Context!, \"globex\"))")
.And.Contain($"XmlBeans.List(Context!, \"{T()}\", [XmlBeans.Ref(Context!, \"acme\")])");
}
[Fact]
public void GeneratedCalls_BuildWhatTheXmlSectionBuilds()
{
// Exactly the calls the generator prints above, run against a context.
_context.AddToRegistry("acme", XmlBeans.Create(_context, T(), [], [("Id", "acme")]));
_context.AddToRegistry("node", XmlBeans.Create(_context, T(), [],
[
("Partners", XmlBeans.List(_context, null, [XmlBeans.Ref(_context, "acme")])),
("Signing", XmlBeans.Ref(_context, "acme")),
]));
var roster = (ProbeRoster)XmlBeans.Create(_context, T(),
[XmlBeans.List(_context, T(), [XmlBeans.Ref(_context, "acme")])], []);
var acme = _context.GetFromRegistry("acme");
_context.GetFromRegistry("node")!.Partners.Should().ContainSingle().Which.Should().BeSameAs(acme);
_context.GetFromRegistry("node")!.Signing.Should().BeSameAs(acme);
roster.Partners.Should().ContainSingle().Which.Should().BeSameAs(acme);
var forward = () => XmlBeans.Ref(_context, "late");
forward.Should().Throw().WithMessage("*'late' names nothing registered so far*");
}
}