Core - Fix removing dynamic children and checking which properties are in use

- DataModel.RemoveDynamicChild compared the value of each child with the
  child that was passed in, so it never removed anything.
- IsPropertyInUse with includeChildren matched paths by their start, so
  a path to "Integers" made "Integer" count as in use. Children are now
  matched on what follows a dot.
This commit is contained in:
RobertBeekman committed 2026-10-20 09:26:46 +02:00
1 parent 504845c7d2
commit bbaf21c25f
2 files changed
+11 -12

No files matched your search

@@ -184,7 +184,7 @@ public class DataModelTests : IDisposable
}
[Fact]
public void RemovingADynamicChild_DoesNotRemoveIt()
public void RemovingADynamicChild_RemovesIt()
{
DynamicChild<int> child = DataModel.AddDynamicChild("Extra", 5);
int removed = 0;
@@ -192,11 +192,8 @@ public class DataModelTests : IDisposable
DataModel.RemoveDynamicChild(child);
// Pinned as-is: RemoveDynamicChild compares the value of each child (BaseValue) with the child that was passed in,
// rather than the child itself, so it only removes a child whose value is the child object. Looks like it should
// compare the children themselves; RemoveDynamicChildByKey works.
DataModel.DynamicChildren.ShouldContainKey("Extra");
removed.ShouldBe(0);
DataModel.DynamicChildren.ShouldNotContainKey("Extra");
removed.ShouldBe(1);
}
[Fact]
@@ -304,14 +301,14 @@ public class DataModelTests : IDisposable
}
[Fact]
public void IncludingChildren_AlsoMatchesPropertiesThatStartWithTheSameName()
public void IncludingChildren_DoesNotMatchPropertiesThatStartWithTheSameName()
{
using DataModelPath path = new(DataModel, "Integers");
// Pinned as-is: children are matched by checking whether a path starts with the given text, so "Integer" counts as
// in use because of a path to "Integers", a different property. Looks like it should check for "Integer."
_module.IsPropertyInUse("Integer", true).ShouldBeTrue();
// Integers is a different property than Integer, not a child of it
_module.IsPropertyInUse("Integer", true).ShouldBeFalse();
_module.IsPropertyInUse("Integer", false).ShouldBeFalse();
_module.IsPropertyInUse("Integers", true).ShouldBeTrue();
}
[Fact]
@@ -289,7 +289,7 @@ public abstract class DataModel
public void RemoveDynamicChild(DynamicChild dynamicChild)
{
if (dynamicChild == null) throw new ArgumentNullException(nameof(dynamicChild));
List<string> keys = _dynamicChildren.Where(kvp => kvp.Value.BaseValue == dynamicChild).Select(kvp => kvp.Key).ToList();
List<string> keys = _dynamicChildren.Where(kvp => kvp.Value == dynamicChild).Select(kvp => kvp.Key).ToList();
foreach (string key in keys)
{
_dynamicChildren.Remove(key);
@@ -335,8 +335,10 @@ public abstract class DataModel
{
lock (_activePaths)
{
// A child's path continues after a dot, matching on the text alone would also match a property that merely starts
// with the same name (Integer and Integers)
return includeChildren
? _activePathsHashSet.Any(p => p.StartsWith(path, PathsStringComparison))
? _activePathsHashSet.Any(p => string.Equals(p, path, PathsStringComparison) || p.StartsWith(path + ".", PathsStringComparison))
: _activePathsHashSet.Contains(path);
}
}