Path: redb-route/tests/redb.Route.Tests.Xml/TypedReferenceGateTests.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/TypedReferenceGateTests.cs
----
using AwesomeAssertions;
using redb.Route.Xml;
using redb.Route.Xml.Packaging;
namespace redb.Route.Tests.Xml;
///
/// Row 7 part 3: with the built assemblies at hand, a name in an option marked [RegistryReference(typeof(T))],
/// bare or with #, URI form or structured form, is held against T per connector: a bean the package
/// declares with a type that is not a T is an error, worded as for headerFilterStrategy. An option marked
/// without a type is never checked; a bean whose type does not load, or that the package does not declare, gives no
/// type error; values from configuration or expressions are left alone.
///
public sealed class TypedReferenceGateTests : IDisposable
{
private readonly string _root = Path.Combine(Path.GetTempPath(), "redb-xml-typed-" + Guid.NewGuid().ToString("N"));
public TypedReferenceGateTests() => Directory.CreateDirectory(_root);
public void Dispose() => Directory.Delete(_root, recursive: true);
private static readonly IReadOnlyList Catalog = ComponentCatalog.Build([new ProbeRefComponent()]);
private static Type? Resolve(string typeName) => Type.GetType(typeName);
private static string T() => $"{typeof(TType).FullName}, {typeof(TType).Assembly.GetName().Name}";
private PackageResult Check(string step, string beans, IReadOnlyList? catalog = null)
{
var dir = RouteProjectScaffold.Create(_root, "Typ" + Guid.NewGuid().ToString("N")[..6]);
File.WriteAllText(Path.Combine(dir, "routes", "main.route.xml"), $"""
{beans}
{step}
""");
return RoutePackage.Check(dir, "p", "1.0.0", Resolve, catalog: catalog ?? Catalog);
}
private static IEnumerable TypeErrors(PackageResult result)
=> result.Errors.Select(e => e.Message).Where(m => m.Contains("is registered as", StringComparison.Ordinal));
private static string WrongBean => $"""""";
[Theory]
[InlineData("""""", "")]
[InlineData("""""", "")]
[InlineData("""""", "")]
public void ABeanOfTheWrongType_IsAnError_InEveryForm(string step, string at)
{
TypeErrors(Check(step, WrongBean)).Should().ContainSingle().Which.Should().Be(
$"{at}: 'main' is registered as {typeof(DeepBeanTarget).FullName}, which is not an {nameof(ProbeRefFactory)}.");
}
[Fact]
public void ABeanOfTheExpectedType_Passes()
{
TypeErrors(Check("""""", $""""""))
.Should().BeEmpty();
}
[Fact]
public void AnUntypedMark_IsNeverTypeChecked()
{
TypeErrors(Check("""""", WrongBean)).Should().BeEmpty();
}
[Fact]
public void ABeanWhoseTypeDoesNotLoad_GivesNoTypeError()
{
TypeErrors(Check("""""", """"""))
.Should().BeEmpty();
}
[Fact]
public void AnUndeclaredBean_IsOnlyTheWarning()
{
var result = Check("""""", "");
TypeErrors(result).Should().BeEmpty();
result.Warnings.Should().Contain(w => w.Message.StartsWith("'#main' is not declared", StringComparison.Ordinal));
}
[Theory]
[InlineData("""""")]
[InlineData("""""")]
public void AValueFromConfigurationOrAnExpression_IsLeftAlone(string step)
{
TypeErrors(Check(step, WrongBean)).Should().BeEmpty();
}
private static string WrongStrategy => $"""""";
[Theory]
[InlineData("""""", "")]
[InlineData("""""", "")]
public void AUriInAnyOtherAttribute_IsHeldAgainstTheType_AsBefore(string step, string at)
{
TypeErrors(Check(step, WrongStrategy)).Should().ContainSingle().Which.Should().Be(
$"{at}: 'x' is registered as {typeof(DeepBeanTarget).FullName}, which is not an IHeaderFilterStrategy.");
}
[Fact]
public void ABareNameInAnyOtherAttribute_IsAReference()
{
Check("""""", "").Warnings
.Should().ContainSingle(w => w.Message.StartsWith("'#x' is not declared", StringComparison.Ordinal));
}
[Fact]
public void AnEndpointPosition_IsReadOnce()
{
TypeErrors(Check("""""", WrongStrategy)).Should().ContainSingle();
}
[Fact]
public void TheCheckIsPerConnector_AnOptionOfTheSameNameElsewhereIsNotHeldAgainstThisType()
{
// vm has no connectionFactory option; the name means nothing there and is not checked against ProbeRefFactory.
TypeErrors(Check("""""", WrongBean)).Should().BeEmpty();
}
// Row 57: ftp and sftp share RemoteFileEndpointOptions, whose connectionFactory was marked without a type, so a bean
// of any class passed the gate. The mark now names the factories' common base.
private static readonly IReadOnlyList RemoteFileCatalog =
ComponentCatalog.Build([new redb.Route.Ftp.FtpComponent(), new redb.Route.Sftp.SftpComponent()]);
[Theory]
[InlineData("ftp")]
[InlineData("sftp")]
public void ARemoteFileConnector_HoldsItsConnectionFactoryAgainstTheFactoryType(string scheme)
{
TypeErrors(Check($"""""", WrongBean, RemoteFileCatalog))
.Should().ContainSingle().Which.Should().EndWith(
$"'main' is registered as {typeof(DeepBeanTarget).FullName}, which is not an {nameof(redb.Route.GenericFile.RemoteFileConnectionFactory)}.");
}
[Theory]
[InlineData("ftp", typeof(redb.Route.Ftp.FtpConnectionFactory))]
[InlineData("sftp", typeof(redb.Route.Sftp.SftpConnectionFactory))]
public void ARemoteFileConnector_AcceptsItsOwnFactory(string scheme, Type factory)
{
var bean = $"""""";
TypeErrors(Check($"""""", bean, RemoteFileCatalog)).Should().BeEmpty();
}
}