From 87217337b7fe82c9896b5a24217f35808d37e13a Mon Sep 17 00:00:00 2001 From: Misaki Date: Sat, 28 Mar 2026 12:45:55 +0900 Subject: [PATCH] fix(dock): encapsulate Children collection and polish tests --- .../Controls/Internal/Docking/DockGroupNode.cs | 12 +++++++----- src/Test/Ghost.UnitTest/DockingModelTest.cs | 3 +-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Editor/Ghost.Editor.Core/Controls/Internal/Docking/DockGroupNode.cs b/src/Editor/Ghost.Editor.Core/Controls/Internal/Docking/DockGroupNode.cs index 909cbf3..f916fee 100644 --- a/src/Editor/Ghost.Editor.Core/Controls/Internal/Docking/DockGroupNode.cs +++ b/src/Editor/Ghost.Editor.Core/Controls/Internal/Docking/DockGroupNode.cs @@ -15,16 +15,19 @@ public partial class DockGroupNode : DockNode [ObservableProperty] public partial Orientation Orientation { get; set; } + private readonly ObservableCollection _children = new(); + /// /// Gets the collection of child nodes. /// - public ObservableCollection Children { get; } = new(); + public ReadOnlyObservableCollection Children { get; } /// /// Initializes a new instance of the class. /// public DockGroupNode() { + Children = new ReadOnlyObservableCollection(_children); Orientation = Orientation.Horizontal; } @@ -35,7 +38,6 @@ public partial class DockGroupNode : DockNode /// Thrown if node is null. /// Thrown if adding the node would create a cycle or if adding self. public void AddChild(DockNode node) - { ArgumentNullException.ThrowIfNull(node); @@ -44,7 +46,7 @@ public partial class DockGroupNode : DockNode throw new InvalidOperationException("Cannot add a node to itself."); } - if (Children.Contains(node)) + if (_children.Contains(node)) { return; } @@ -66,7 +68,7 @@ public partial class DockGroupNode : DockNode } node.Parent = this; - Children.Add(node); + _children.Add(node); } /// @@ -75,7 +77,7 @@ public partial class DockGroupNode : DockNode /// The node to remove. public void RemoveChild(DockNode node) { - if (Children.Remove(node)) + if (_children.Remove(node)) { node.Parent = null; } diff --git a/src/Test/Ghost.UnitTest/DockingModelTest.cs b/src/Test/Ghost.UnitTest/DockingModelTest.cs index b324619..3ce0018 100644 --- a/src/Test/Ghost.UnitTest/DockingModelTest.cs +++ b/src/Test/Ghost.UnitTest/DockingModelTest.cs @@ -1,5 +1,4 @@ using Ghost.Editor.Core.Controls.Internal.Docking; -using Microsoft.UI.Xaml.Controls; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Ghost.UnitTest; @@ -50,7 +49,7 @@ public class DockingModelTest { thrown = true; } - Assert.IsTrue(thrown); + Assert.IsTrue(thrown, "Should have thrown InvalidOperationException due to cycle."); } [TestMethod]