using System.Collections.Generic; using System.Linq; using Chernobyl.Collections.Generic.Event; using NUnit.Framework; namespace Chernobyl.Collections.Generic.Hierarchy { [TestFixture, Description("")] public class TreeExtensionsTests { [Test, Description("A test to ensure that TreeExtensions.Parenter " + "properly parents children when told to by an event.")] public void ParenterTest() { Hierarchy parent = new Hierarchy(); parent.Children.ItemsAdded += new TreeExtensions.Parenter(parent, (newParent, changeableChild) => changeableChild.Parent = newParent); Hierarchy child = new Hierarchy(); parent.Children.Add(child); Assert.AreEqual(parent, child.Parent, "The child's parent was not set to the expected value."); } [Test, Description("A test to ensure that TreeExtensions.Unparenter " + "properly un-parents children when told to by an event.")] public void UnparenterTest() { Hierarchy parent = new Hierarchy(); Hierarchy child = new Hierarchy(parent); parent.Children.Add(child); parent.Children.ItemsRemoved += new TreeExtensions.Unparenter(parent, (newParent, changeableChild) => changeableChild.Parent = newParent, childRequested => childRequested.Parent); parent.Children.Remove(child); Assert.AreEqual(null, child.Parent, "The child's parent was not set to the expected value."); } [Test, Description("A test to ensure that TreeExtensions.Unparenter " + "does not un-parents children when those children are already parented " + "to another parent.")] public void UnparenterDoesNotUnparentTest() { Hierarchy parent = new Hierarchy(); Hierarchy parent2 = new Hierarchy(); Hierarchy child = new Hierarchy(parent2); parent.Children.Add(child); parent.Children.ItemsRemoved += new TreeExtensions.Unparenter(parent, (newParent, changeableChild) => changeableChild.Parent = newParent, childRequested => childRequested.Parent); parent.Children.Remove(child); Assert.AreEqual(parent2, child.Parent, "The child's parent was not set to the expected value."); } [Test, Description("A test that ensures that GetParent(TChild, ref TParent, " + "TParent, Func>) properly removes child from old parent " + "and adds child to new parent.")] public void GetTerritoryParentTest() { Hierarchy parent = new Hierarchy(); Hierarchy child = new Hierarchy(parent); parent.Children.Add(child); Hierarchy currentParent = parent; Hierarchy newParent = new Hierarchy(); TreeExtensions.GetParent(child, ref currentParent, newParent, parentRequested => parentRequested.Children); Assert.AreEqual(newParent, currentParent, "The current parent value is not the expected value."); Assert.False(parent.Children.Contains(child), "The parent should no longer hold the child but it does."); Assert.True(newParent.Children.Contains(child), "The new parent should hold the child but it does not."); } [Test, Description("A test that ensures that GetParent(TChild, ref TParent, " + "TParent, Func>) does not add a child to a parent twice. ")] public void GetTerritoryParentChildIsNotAddedTwiceTest() { Hierarchy parent = new Hierarchy(); Hierarchy child = new Hierarchy(parent); parent.Children.Add(child); Hierarchy currentParent = parent; TreeExtensions.GetParent(child, ref currentParent, parent, parentRequested => parentRequested.Children); Assert.AreEqual(parent, currentParent, "The current parent value is not the expected value."); Assert.True(parent.Children.Contains(child), "The parent should hold the child but it does not."); Assert.AreEqual(1, parent.Children.Count, "The new parent should only hold once instance of the child."); } [Test, Description("A test that ensures that GetChildren(" + "TParent, ref ICollection, ICollection, " + "Func, Action) sets " + "children's parents properly.")] public void GetTerritoryChildrenTest() { Hierarchy parent = new Hierarchy(); ICollection children = new List(); children.Add(new Hierarchy(parent)); children.Add(new Hierarchy(parent)); ICollection newChildren = new List(); newChildren.Add(new Hierarchy()); newChildren.Add(new Hierarchy()); ICollection currentChildren = children; TreeExtensions.GetChildren(parent, ref currentChildren, newChildren, childRequested => childRequested.Parent, (childToModify, newParent) => childToModify.Parent = newParent); Assert.AreEqual(newChildren, currentChildren, "Current child list was not set to the expected value."); foreach (Hierarchy child in children) Assert.AreEqual(null, child.Parent, "The parent value of the old children is not the expected value."); foreach (Hierarchy child in newChildren) Assert.AreEqual(parent, child.Parent, "The parent value of the old children is not the expected value."); } } /// /// A class that is used to test parent/child relationships. /// public class Hierarchy { public Hierarchy() : this(null) {} public Hierarchy(Hierarchy parent) : this(parent, System.Linq.Enumerable.Empty()) {} public Hierarchy(Hierarchy parent, IEnumerable children) { Parent = parent; Children = new DecoratingEventList(new List(children)); } public Hierarchy Parent { get; set; } public IEventCollection Children { get; private set; } } }