Caching.Tests fixtures: every list-reachable record and union arm carries an element id

LeafInner, the Node arms (Wrap new), the Outcome arms, and the GuardNode arms
(Branch, Unit8, StubArm new) take [Key] [CultElementId] string Id, with fixed
literal ids and hand-written emitters updated to the new slots. Goldens are
regenerated in the next commits.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-14 19:40:37 +02:00
commit f84428b9b9
3 changed files with 37 additions and 32 deletions

View file

@ -36,7 +36,7 @@ namespace GameCult.Caching.Tests
[MessagePackObject] public sealed class ArrayStringSlot { [Key(0)] public string[]? V; }
[MessagePackObject] public sealed class NullableInt32Slot { [Key(0)] public int? V; }
[MessagePackObject] public sealed class ListInt32Slot { [Key(0)] public List<int>? V; }
[MessagePackObject] public sealed class LeafInner { [Key(0)] public string N = string.Empty; [Key(1)] public float W; }
[MessagePackObject] public sealed class LeafInner { [Key(0)] public string N = string.Empty; [Key(1)] public float W; [Key(2)] [CultElementId] public string Id = string.Empty; }
[MessagePackObject] public sealed class RecordSlot { [Key(0)] public LeafInner? V; }
[MessagePackObject] public sealed class ListRecordSlot { [Key(0)] public List<LeafInner>? V; }
[Union(0, typeof(LeafCircle))] public interface ILeafShape { }
@ -308,7 +308,7 @@ namespace GameCult.Caching.Tests
{
Id = "leaves", Flag = true, Blob = new byte[] { 1, 2, 3 }, I = -7, L = 5_000_000_000, F = 0.1f, D = 0.1, MaybeI = 9,
Tags = new List<string> { "a", "b" }, Names = new[] { "x", "y" }, Any = "text",
Nested = new LeafInner { N = "inner", W = 0.1f }, Items = new List<LeafInner> { new() { N = "one", W = 0.1f }, new() { N = "two", W = 1.5f } },
Nested = new LeafInner { N = "inner", W = 0.1f, Id = "n" }, Items = new List<LeafInner> { new() { N = "one", W = 0.1f, Id = "i1" }, new() { N = "two", W = 1.5f, Id = "i2" } },
Shape = new LeafCircle { R = 0.1f }, Floats = new[] { 0.1f, 1.5f }, MaybeF = 0.1f,
};

View file

@ -42,16 +42,18 @@ namespace GameCult.Caching.Tests
w.WriteArrayHeader(4); w.Write(id); w.Write(content); w.WriteNil(); w.Write(weight);
};
private static Emit UnitArm(int key) => (ref MessagePackWriter w) => { w.WriteArrayHeader(2); w.Write(key); w.WriteArrayHeader(0); };
// Unit8: slots [id].
private static Emit UnitArm(int key) => (ref MessagePackWriter w) => { w.WriteArrayHeader(2); w.Write(key); w.WriteArrayHeader(1); w.Write("u"); };
// Branch: slots [children, next].
// Branch: slots [children, next, id].
private static Emit BranchArm(int key, Emit next, params Emit[] children) => (ref MessagePackWriter w) =>
{
w.WriteArrayHeader(2); w.Write(key);
w.WriteArrayHeader(2);
w.WriteArrayHeader(3);
w.WriteArrayHeader(children.Length);
foreach (var child in children) child(ref w);
next(ref w);
w.Write("b");
};
private static Emit List(params Emit[] items) => (ref MessagePackWriter w) =>
@ -173,9 +175,9 @@ namespace GameCult.Caching.Tests
foreach (var doc in new[]
{
new GuardDoc { Root = new Rogue() },
new GuardDoc { Items = { new Unit8(), new Rogue() } },
new GuardDoc { Root = new Branch { Children = { new Rogue() } } },
new GuardDoc { Root = new Branch { Next = new Rogue() } },
new GuardDoc { Items = { new Unit8 { Id = "u" }, new Rogue() } },
new GuardDoc { Root = new Branch { Id = "b", Children = { new Rogue() } } },
new GuardDoc { Root = new Branch { Id = "b", Next = new Rogue() } },
new GuardDoc { Root = new DerivedLeaf() },
new GuardDoc { Crafted = new Unlisted() },
})
@ -204,15 +206,16 @@ namespace GameCult.Caching.Tests
Id = "d1",
Root = new Branch
{
Id = "b",
Children =
{
new Leaf { Id = "a", Content = "x", Weight = 1 },
new Unit8(),
new Branch { Children = { new Branch { Children = { new Leaf { Id = "deep", Content = "y", Weight = 2 } } } } }
new Unit8 { Id = "u" },
new Branch { Id = "b", Children = { new Branch { Id = "b", Children = { new Leaf { Id = "deep", Content = "y", Weight = 2 } } } } }
},
Next = new Leaf { Id = "n", Content = "z", Weight = 3 }
},
Items = { new Unit8(), new Leaf { Id = "i", Content = "w", Weight = 4 }, null! },
Items = { new Unit8 { Id = "u" }, new Leaf { Id = "i", Content = "w", Weight = 4 }, null! },
Crafted = new InnerB { N = 9 },
Any = new InnerA { N = 8 }
};
@ -567,7 +570,7 @@ namespace GameCult.Caching.Tests
[MessagePackObject]
public class Leaf : GuardNode
{
[Key(0)] public string Id = string.Empty;
[Key(0)] [CultElementId] public string Id = string.Empty;
[Key(1)] public string Content = string.Empty;
[Key(3)] public int Weight;
}
@ -580,15 +583,16 @@ namespace GameCult.Caching.Tests
{
[Key(0)] public List<GuardNode> Children = new();
[Key(1)] public GuardNode? Next;
[Key(2)] [CultElementId] public string Id = string.Empty;
}
[MessagePackObject] public sealed class Unit8 : GuardNode { }
[MessagePackObject] public sealed class Unit8 : GuardNode { [Key(0)] [CultElementId] public string Id = string.Empty; }
// A subtype the union does not name.
[MessagePackObject] public sealed class Rogue : GuardNode { [Key(0)] public string Note = string.Empty; }
// Written and read by a consumer resolver, not by MessagePack.
public sealed class StubArm : GuardNode { }
public sealed class StubArm : GuardNode { [Key(0)] [CultElementId] public string Id = string.Empty; }
public sealed class StubArmResolver : IFormatterResolver
{

View file

@ -76,26 +76,27 @@ namespace GameCult.Caching.Tests
public sealed class Wrap : Node
{
[Key(0)] public Node? Inner;
[Key(1)] [CultElementId] public string Id = string.Empty;
}
[MessagePackObject]
public sealed class Box : Node
{
[Key(0)] public string Id = string.Empty;
[Key(0)] [CultElementId] public string Id = string.Empty;
[Key(1)] public List<Node> Children = new();
}
[MessagePackObject]
public sealed class Text : Node
{
[Key(0)] public string Id = string.Empty;
[Key(0)] [CultElementId] public string Id = string.Empty;
[Key(1)] public string Content = string.Empty;
}
[MessagePackObject]
public sealed class Use : Node
{
[Key(0)] public string Id = string.Empty;
[Key(0)] [CultElementId] public string Id = string.Empty;
[Key(1)] public Composition Composition = new();
[Key(2)] public List<Node> Slots = new();
}
@ -104,7 +105,7 @@ namespace GameCult.Caching.Tests
[MessagePackObject]
public sealed class Image : Node
{
[Key(0)] public string Id = string.Empty;
[Key(0)] [CultElementId] public string Id = string.Empty;
[Key(1)] public string Uri = string.Empty;
[Key(3)] public string Alt = string.Empty;
}
@ -122,12 +123,12 @@ namespace GameCult.Caching.Tests
Union(int.MinValue, typeof(Floor)), Union(int.MaxValue, typeof(Ceiling))]
public abstract class Outcome { }
[MessagePackObject] public sealed class Pending : Outcome { [Key(0)] public string Message = string.Empty; }
[MessagePackObject] public sealed class Completed : Outcome { [Key(0)] public int SourceVersion; }
// A unit arm: written [2, []].
[MessagePackObject] public sealed class Denied : Outcome { }
[MessagePackObject] public sealed class Floor : Outcome { }
[MessagePackObject] public sealed class Ceiling : Outcome { }
[MessagePackObject] public sealed class Pending : Outcome { [Key(0)] public string Message = string.Empty; [Key(1)] [CultElementId] public string Id = string.Empty; }
[MessagePackObject] public sealed class Completed : Outcome { [Key(0)] public int SourceVersion; [Key(1)] [CultElementId] public string Id = string.Empty; }
// The arms of a list carry an id, so a unit arm is written [2, [id]].
[MessagePackObject] public sealed class Denied : Outcome { [Key(0)] [CultElementId] public string Id = string.Empty; }
[MessagePackObject] public sealed class Floor : Outcome { [Key(0)] [CultElementId] public string Id = string.Empty; }
[MessagePackObject] public sealed class Ceiling : Outcome { [Key(0)] [CultElementId] public string Id = string.Empty; }
// Item names Tool and Part under keys 1 and 2; CraftedItem, the inner union both derive from, names them under 0 and 1.
[Union(0, typeof(Gem)), Union(1, typeof(Tool)), Union(2, typeof(Part))]
@ -178,7 +179,7 @@ namespace GameCult.Caching.Tests
new Image { Id = "i2", Uri = "uri://b", Alt = "" }
}
},
Outcomes = { new Pending { Message = "wait" }, new Completed { SourceVersion = 7 }, new Denied() },
Outcomes = { new Pending { Message = "wait", Id = "p" }, new Completed { SourceVersion = 7, Id = "c" }, new Denied { Id = "d" } },
Part = new Tool { Name = "hammer", Uses = 12 },
Crafted = new Part { Name = "gear", Count = 4 },
Missing = null
@ -260,7 +261,7 @@ namespace GameCult.Caching.Tests
Plus(extra.Composition, Str("comp"), BoxArm("inner", Slots(TextArm("deep", "x"), ImageArm("img", "uri://a", "alt")))),
Slots(TextArm("s1", "slot", extra.ListElement), Nil, BoxArm("b2", Slots()))),
ImageArm("i2", "uri://b", "")), extra.RootArm);
outcomes ??= Slots(ArmOf(0, Str("wait")), ArmOf(1, Num(7)), ArmOf(2));
outcomes ??= Slots(ArmOf(0, Str("wait"), Str("p")), ArmOf(1, Num(7), Str("c")), ArmOf(2, Str("d")));
crafted ??= ArmOf(1, Str("gear"), Num(4));
var slots = new[] { Str("surface"), Num(3), root, outcomes, ArmOf(1, Str("hammer"), Num(12)), crafted, Nil };
return Raw(Slots(trailing != null ? slots.Append(trailing).ToArray() : extra.Record ? slots.Append(ExtraSlot).ToArray() : slots));
@ -460,13 +461,13 @@ namespace GameCult.Caching.Tests
Slots(slots)(ref w);
};
private static Emit WithOutcome(Emit element) => Slots(ArmOf(0, Str("wait")), ArmOf(1, Num(7)), ArmOf(2), element);
private static Emit WithOutcome(Emit element) => Slots(ArmOf(0, Str("wait"), Str("p")), ArmOf(1, Num(7), Str("c")), ArmOf(2, Str("d")), element);
private static Emit WrapChain(int wraps, Emit leaf) => wraps == 0 ? leaf : ArmOf(6, WrapChain(wraps - 1, leaf));
private static Emit WrapChain(int wraps, Emit leaf) => wraps == 0 ? leaf : ArmOf(6, WrapChain(wraps - 1, leaf), Str("w"));
private static Node WrapChainValue(int wraps, Node leaf)
{
for (var i = 0; i < wraps; i++) leaf = new Wrap { Inner = leaf };
for (var i = 0; i < wraps; i++) leaf = new Wrap { Inner = leaf, Id = "w" };
return leaf;
}
@ -479,11 +480,11 @@ namespace GameCult.Caching.Tests
private static IReadOnlyList<Case> BoundsCases() => new[]
{
new Case("arm-key-int32-min.msgpack", "accepted", "Outcome", int.MinValue, "arm-key-int32-min.msgpack",
Surface(outcomes: WithOutcome(ArmOf(int.MinValue)))),
Surface(outcomes: WithOutcome(ArmOf(int.MinValue, Str("f"))))),
new Case("arm-key-int32-max.msgpack", "accepted", "Outcome", int.MaxValue, "arm-key-int32-max.msgpack",
Surface(outcomes: WithOutcome(ArmOf(int.MaxValue)))),
Surface(outcomes: WithOutcome(ArmOf(int.MaxValue, Str("x"))))),
new Case("arm-key-wide-coding.msgpack", "accepted", "Outcome", int.MaxValue, "arm-key-int32-max.msgpack",
Surface(outcomes: WithOutcome(RawKey((ref MessagePackWriter w) => w.WriteUInt64(int.MaxValue))))),
Surface(outcomes: WithOutcome(RawKey((ref MessagePackWriter w) => w.WriteUInt64(int.MaxValue), Str("x"))))),
new Case("arm-key-above-int32.msgpack", "refused", "Outcome", null, "key-out-of-range",
Surface(outcomes: WithOutcome(RawKey((ref MessagePackWriter w) => w.Write(0x80000000u))))),
new Case("arm-key-below-int32.msgpack", "refused", "Outcome", null, "key-out-of-range",