Decide element choices and integer narrowing in the inspection model

ElementChoices says what a new list element, dictionary value or union
pick may be (a union's declared subtypes, or the slot type itself), and
CreateElement makes one or returns a notice when a subtype has no
parameterless constructor. It replaces CreateUnionValue, whose throw the
IMGUI caught and sent to the console. A dictionary with a union value
type now offers the subtypes too instead of adding a null value.

NarrowInteger clamps an integer edit to its type's range; the IMGUI no
longer converts and swallows overflow itself. IMGUI renders the model's
answers and shows its notices.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-14 14:07:19 +02:00
commit d22f662f7d
3 changed files with 96 additions and 51 deletions

View file

@ -65,15 +65,34 @@ namespace GameCult.Caching.Tests
}
[Test]
public void UnionChoicesAreTheDeclaredSubtypesOnly()
public void ElementChoicesAreTheDeclaredUnionSubtypesOrTheTypeItself()
{
var model = Model();
var shape = model.ShapeOf(typeof(InspectShape));
Assert.That(shape.Kind, Is.EqualTo(CultInspectorValueKind.Union));
Assert.That(shape.UnionChoices, Is.EqualTo(new[] { typeof(InspectCircle) }));
Assert.That(model.CreateUnionValue(typeof(InspectShape), typeof(InspectCircle)), Is.InstanceOf<InspectCircle>());
Assert.That(() => model.CreateUnionValue(typeof(InspectShape), typeof(InspectSquare)), Throws.InvalidOperationException);
Assert.That(model.ShapeOf(typeof(InspectShape)).Kind, Is.EqualTo(CultInspectorValueKind.Union));
Assert.That(model.ElementChoices(typeof(InspectShape)), Is.EqualTo(new[] { typeof(InspectCircle) }));
Assert.That(model.CreateElement(typeof(InspectShape), typeof(InspectCircle), out var made), Is.InstanceOf<InspectCircle>());
Assert.That(made, Is.Null);
Assert.That(() => model.CreateElement(typeof(InspectShape), typeof(InspectSquare), out _), Throws.ArgumentException,
"an undeclared subtype is not a choice");
Assert.That(model.ElementChoices(typeof(List<int>)), Is.EqualTo(new[] { typeof(List<int>) }));
Assert.That(model.CreateElement(typeof(List<int>), typeof(List<int>), out _), Is.EqualTo(new List<int>()));
Assert.That(model.CreateElement(typeof(InspectPicky), typeof(InspectNeedsArgs), out var refused), Is.Null);
Assert.That(refused, Does.Contain("parameterless"));
}
[Test]
public void IntegerEditsClampToTheirType()
{
var model = Model();
Assert.That(model.NarrowInteger(typeof(byte), 300), Is.TypeOf<byte>().And.EqualTo(byte.MaxValue));
Assert.That(model.NarrowInteger(typeof(sbyte), -300), Is.TypeOf<sbyte>().And.EqualTo(sbyte.MinValue));
Assert.That(model.NarrowInteger(typeof(uint), -1), Is.TypeOf<uint>().And.EqualTo(0u));
Assert.That(model.NarrowInteger(typeof(int), 42), Is.TypeOf<int>().And.EqualTo(42));
Assert.That(() => model.NarrowInteger(typeof(float), 1), Throws.ArgumentException);
}
[Test]
@ -300,6 +319,18 @@ namespace GameCult.Caching.Tests
public new string Value = string.Empty;
}
[Union(0, typeof(InspectNeedsArgs))]
public abstract class InspectPicky
{
}
public sealed class InspectNeedsArgs : InspectPicky
{
public InspectNeedsArgs(int side)
{
}
}
public struct InspectOpenStruct
{
public float x;