public static int GetNodeCount(TestNode root) { int nodeCount = 0; foreach (TestNode item in TestNode.AllChildren(root)) { nodeCount++; } return(nodeCount); }
public static void TestTreeIntegrity(TestNode root) { foreach (TestNode parent in TestNode.AllChildren(root)) { foreach (var child in parent.Children) { Debug.WriteLine($"Testing:{parent.Name} against {child.Name}"); string result = VerifyRelationship(parent, child); Assert.AreEqual(String.Empty, result, $"Item relationship is not correct for Parent:{parent}, Child:{child}, reason:{result}"); } } }
public void TerribleUnitTest(Action <TestNode, TestNode> method0, Action <TestNode, TestNode> method1) { var root = new TestNode("Root"); Arrange.BuildTestTree(root, 4, 4); // Used to test the tree is correct after we undo every move. foreach (TestNode item in TestNode.AllChildren(root)) { item.OriginalParent = item.Parent; } // Ensure the tree parent/child relationships are correct. Arrange.TestTreeIntegrity(root); Debug.WriteLine(root.PrintTree()); // Used to store moves, so we can undo them later. Stack <Tuple <TestNode, TestNode> > moves = new Stack <Tuple <TestNode, TestNode> >(); int nodeCount = Arrange.GetNodeCount(root); for (int c = 0; c < nodeCount; c++) { TestNode child, newParent; GetTwoSuitableNodes(root, nodeCount, out child, out newParent); Debug.WriteLine($"Moving {child.Name} from {child.Parent.Name} to {newParent.Name}"); // Store the move, so we can undo it later. moves.Push(new Tuple <TestNode, TestNode>(child, child.Parent)); // Move the child to the new parent. method0(child, newParent); Assert.AreEqual(newParent, child.Parent, "Wrong parent"); Assert.AreEqual(nodeCount, Arrange.GetNodeCount(root), "Node has gone missing"); } Arrange.TestTreeIntegrity(root); //Debug.WriteLine(root.ToString()); Assert.AreEqual(nodeCount, Arrange.GetNodeCount(root)); // Undo every move. // NOTE: The tree will be back to its starting shape, but the order of node Children may be different. while (moves.TryPop(out var move)) { method1(move.Item1, move.Item2); } // Ensure the tree has the same shape we started with, allowing for node Children to be in a different order. foreach (TestNode item in TestNode.AllChildren(root)) { Assert.AreEqual(item.OriginalParent, item.Parent); } //Debug.WriteLine(root.PrintTree()); }
private TestNode GetItemAtIndex(TestNode root, int index) { int nodeCount = 0; foreach (TestNode item in TestNode.AllChildren(root)) { if (nodeCount == index) { return(item); } nodeCount++; } throw new InvalidOperationException(); }