using Microsoft.Xna.Framework; using MrGameEng.Inspector; using Xunit; namespace MrGameEng.Inspector.Tests; public class ComponentReflectorTests { private enum Mode { Idle, Run, } private struct Probe { public int Count; public float Speed; public bool Active; public Mode State; public Vector2 Offset; } [Fact] public void Read_ClassifiesFieldKinds() { var rows = ComponentReflector.Read( new Probe { Count = 3, Speed = 1.5f, Active = true, State = Mode.Run, Offset = new Vector2(2f, 4f), } ); Assert.Equal(FieldKind.Number, Find(rows, "Count").Kind); Assert.Equal(FieldKind.Bool, Find(rows, "Active").Kind); Assert.Equal(FieldKind.Enum, Find(rows, "State").Kind); Assert.Equal(FieldKind.Text, Find(rows, "Offset").Kind); Assert.False(Find(rows, "Offset").Editable); Assert.Equal("Run", Find(rows, "State").Value); Assert.Equal("(2, 4)", Find(rows, "Offset").Value); } [Fact] public void TrySet_UpdatesScalarFields() { object boxed = new Probe(); Assert.True(ComponentReflector.TrySet(boxed, "Count", "42")); Assert.True(ComponentReflector.TrySet(boxed, "Active", "true")); Assert.True(ComponentReflector.TrySet(boxed, "State", "Run")); var probe = (Probe)boxed; Assert.Equal(42, probe.Count); Assert.True(probe.Active); Assert.Equal(Mode.Run, probe.State); } [Fact] public void TrySet_RejectsUnparseableValue() { object boxed = new Probe(); Assert.False(ComponentReflector.TrySet(boxed, "Count", "not-a-number")); } [Fact] public void TrySet_RejectsNonScalarOrMissingField() { object boxed = new Probe(); Assert.False(ComponentReflector.TrySet(boxed, "Offset", "1,2")); Assert.False(ComponentReflector.TrySet(boxed, "Missing", "1")); } private static FieldRow Find(IReadOnlyList rows, string name) { foreach (var row in rows) { if (row.Name == name) { return row; } } throw new Xunit.Sdk.XunitException($"field {name} not found"); } }