Path: redb-route/tests/redb.Route.Tests.DataFormats/XmlJsonPluralTests.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.DataFormats/XmlJsonPluralTests.cs
----
using System.Text;
using AwesomeAssertions;
using redb.Route.DataFormats.XmlJson;
using redb.Route.DataFormats.XmlJson.Internal;
namespace redb.Route.Tests.DataFormats;
///
/// The plural-wrapper rule of xmljson (step S4): opt-in, regular and irregular English plurals, silent on every other
/// name, and a single child under a plural wrapper is an array.
///
public sealed class XmlJsonPluralTests
{
private static string Json(string xml, bool plural = true)
{
var converter = new XmlToJson(new XmlJsonOptions { Plural = plural });
return Encoding.UTF8.GetString(converter.Write(converter.Convert(Encoding.UTF8.GetBytes(xml))));
}
[Theory]
[InlineData("Incidents", "Incident")]
[InlineData("Categories", "Category")]
[InlineData("Boxes", "Box")]
[InlineData("Addresses", "Address")]
[InlineData("Statuses", "Status")]
[InlineData("Cases", "Case")]
[InlineData("Movies", "Movie")]
[InlineData("Children", "Child")]
[InlineData("people", "person")]
[InlineData("Indices", "Index")]
public void ThePairs(string parent, string child)
{
Plural.IsPair(parent, child).Should().BeTrue();
}
[Theory]
[InlineData("Items", "Line")]
[InlineData("Address", "Addres")]
[InlineData("Status", "Code")]
[InlineData("Data", "Datum")]
[InlineData("incidents", "Incident")]
[InlineData("Order", "Orde")]
public void NotPairs(string parent, string child)
{
Plural.IsPair(parent, child).Should().BeFalse();
}
[Fact]
public void ASingleChildOfAPluralWrapper_IsAnArray()
{
Json("1").Should().Be("""{"r":{"Incidents":{"Incident":["1"]}}}""");
}
[Fact]
public void TheWrappersAttributes_DoNotMatter()
{
Json("""1""").Should().Be("""{"Incidents":{"@count":"1","Incident":["1"]}}""");
}
[Fact]
public void AWrapperWithOtherChildren_IsNotAPluralWrapper()
{
Json("11").Should().Be("""{"Incidents":{"Incident":"1","Total":"1"}}""");
}
[Fact]
public void OffByDefault()
{
Json("1", plural: false).Should().Be("""{"Incidents":{"Incident":"1"}}""");
new XmlJsonOptions().Plural.Should().BeFalse();
}
[Fact]
public void SilentOnOtherNames()
{
Json("1").Should().Be("""{"Order":{"Line":"1"}}""");
}
}