Path: redb-route/tests/redb.Route.Tests.Xml/DataFormatContextTests.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/DataFormatContextTests.cs
----
using System.Text;
using System.Xml.Linq;
using AwesomeAssertions;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Serialization;
using redb.Route.Xml;
namespace redb.Route.Tests.Xml;
/// A format with one option, as the redb.Route.DataFormats.* serializers are.
public sealed class ProbeDataFormat : IMessageSerializer
{
public string Prefix { get; set; } = "plain";
public string ContentType => "application/x-probe";
public byte[] Serialize(T value) => Encoding.UTF8.GetBytes($"{Prefix}:{value}");
public T? Deserialize(byte[] data) => (T?)Deserialize(data, typeof(T));
public object? Deserialize(byte[] data, Type type) => Encoding.UTF8.GetString(data);
}
/// The options of , as CsvDataFormatOptions is for CsvDataFormat.
public sealed class OptionsBuiltFormatOptions
{
public string Delimiter { get; set; } = ",";
}
/// Built from an options object only, the shape of the redb.Route.DataFormats.* serializers.
public sealed class OptionsBuiltFormat(OptionsBuiltFormatOptions? options = null) : IMessageSerializer
{
private readonly OptionsBuiltFormatOptions _options = options ?? new();
public string ContentType => "text/x-built";
public byte[] Serialize(T value) => Encoding.UTF8.GetBytes($"a{_options.Delimiter}{value}");
public T? Deserialize(byte[] data) => (T?)Deserialize(data, typeof(T));
public object? Deserialize(byte[] data, Type type) => Encoding.UTF8.GetString(data);
}
///
/// <dataFormat> in context.xml registers a format for <marshal format="…"/>, as
/// context.AddDataFormat(...) does in code. Before, a module in markup could not register one, and on the Tsak
/// worker <marshal format="csv"/> failed with "Data format 'csv' is not registered".
///
public sealed class DataFormatContextTests : IAsyncLifetime
{
private static readonly string ProbeType = $"{typeof(ProbeDataFormat).FullName}, {typeof(ProbeDataFormat).Assembly.GetName().Name}";
private readonly RouteContext _context = new();
public Task InitializeAsync() => Task.CompletedTask;
public async Task DisposeAsync() => await _context.DisposeAsync();
private void LoadContext(string inner)
=> new XmlContextLoader(_context).Load(XDocument.Parse($"""
{inner}
""", LoadOptions.SetLineInfo), "context.xml");
private async Task