public void RemoveNodeHierachyWriter_does_nothing_for_wrong_path() { // ARRANGE var childNodeMock = new Mock <NodeType>(); var childNode = childNodeMock.Object; var startNodeMock = new Mock <NodeType>(); startNodeMock .Setup(n => n.TryGetChildNode("a")) .Returns((false, null)); var writer = new RemoveNodeHierarchyWriter <string, NodeType>(); // ACT var result = writer.RemoveNode(startNodeMock.Object, HierarchyPath.Create("a"), false, out var nodeRemoved); // ASSERT Assert.Same(result, startNodeMock.Object); Assert.False(nodeRemoved); startNodeMock.Verify(n => n.RemoveChild(childNode), Times.Never()); startNodeMock.VerifyAll(); }
public void RemoveNodeHierachyWriter_removes_child_node() { // ARRANGE var childNodeMock = new Mock <NodeType>(); var childNode = childNodeMock.Object; var startNodeMock = new Mock <NodeType>(); startNodeMock .Setup(n => n.RemoveChild(childNode)) .Returns(startNodeMock.Object); startNodeMock .Setup(n => n.TryGetChildNode("a")) .Returns((true, childNode)); var writer = new RemoveNodeHierarchyWriter <string, NodeType>(); // ACT var result = writer.RemoveNode(startNodeMock.Object, HierarchyPath.Create("a"), false, out var nodeRemoved); // ASSERT Assert.Same(result, startNodeMock.Object); Assert.True(nodeRemoved); startNodeMock.Verify(n => n.RemoveChild(childNode), Times.Once()); startNodeMock.VerifyAll(); }
public void RemoveNodeHierachyWriter_doesnt_removes_child_node_if_it_has_children() { // ARRANGE var grandChildMock = new Mock <NodeType>(); var grandChild = grandChildMock.Object; var childNodeMock = new Mock <NodeType>(); childNodeMock .Setup(n => n.HasChildNodes) .Returns(true); var startNodeMock = new Mock <NodeType>(); startNodeMock .Setup(n => n.TryGetChildNode("a")) .Returns((true, childNodeMock.Object)); var writer = new RemoveNodeHierarchyWriter <string, NodeType>(); // ACT var result = writer.RemoveNode(startNodeMock.Object, HierarchyPath.Create("a"), false, out var nodeRemoved); // ASSERT Assert.Same(result, startNodeMock.Object); Assert.False(nodeRemoved); startNodeMock.Verify(n => n.RemoveChild(childNodeMock.Object), Times.Never()); startNodeMock.VerifyAll(); childNodeMock.Verify(n => n.RemoveChild(grandChild), Times.Never()); childNodeMock.VerifyAll(); }
public void RemoveNodeHierarchyWriter_removes_startNode() { // ARRANGE var startNodeMock = new Mock <NodeType>(); var writer = new RemoveNodeHierarchyWriter <string, NodeType>(); // ACT var result = writer.RemoveNode(startNodeMock.Object, HierarchyPath.Create <string>(), false, out var nodeRemoved); // ASSERT Assert.Null(result); Assert.True(nodeRemoved); }
/// <summary> /// Removes the specifed node fro the hierarchy. If the node has child nodes and /// <paramref name="recurse"/> is true, the complete subnode is removed. /// If recurse is specified removal fails if teh node has subnodes. /// </summary> /// <param name="path"></param> /// <param name="recurse">Indicats if the removal contains ths subnodes</param> /// <returns>true, if nod (and subnodes) has been removed, false otherwise</returns> public bool RemoveNode(HierarchyPath <TKey> path, bool recurse) { bool isLocked = false; try { this.writeLock.Enter(ref isLocked); if (path.IsRoot) { if (!recurse && this.rootNode.HasChildNodes) { // is recurse is not set, the root node can be exhanged if the root has no child nodes return(false); } this.rootNode = new ImmutableNode <TKey, TValue>(); return(true); } else { // this isn't a special case. // use the hierachy writer for inner nodes var writer = new RemoveNodeHierarchyWriter <TKey, ImmutableNode <TKey, TValue> >(); var resultRootNode = writer.RemoveNode(this.rootNode, path, recurse, out var nodeWasRemoved); if (!object.ReferenceEquals(resultRootNode, rootNode)) { this.rootNode = resultRootNode; } return(nodeWasRemoved); } } finally { if (isLocked) { this.writeLock.Exit(); } } }
public void RemoveNodeHierarchyWriter_doesnt_remove_startNode_if_it_has_children() { // ARRANGE var startNodeMock = new Mock <NodeType>(); startNodeMock .Setup(n => n.HasChildNodes) .Returns(true); var writer = new RemoveNodeHierarchyWriter <string, NodeType>(); // ACT var result = writer.RemoveNode(startNodeMock.Object, HierarchyPath.Create <string>(), false, out var nodeRemoved); // ASSERT Assert.Equal(startNodeMock.Object, result); Assert.False(nodeRemoved); startNodeMock.VerifyAll(); }
public bool RemoveNode(HierarchyPath <TKey> path, bool recurse) { if (path.IsRoot) { if (!recurse && this.rootNode.HasChildNodes) { // is recurse is not set, the root node can be exhanged if the root has no child nodes return(false); } this.rootNode = MutableNode <TKey, TValue> .CreateRoot(); return(true); } else { // this isn't a special case. // use the hierachy writer for inner nodes var writer = new RemoveNodeHierarchyWriter <TKey, MutableNode <TKey, TValue> >(); var result = writer.RemoveNode(this.rootNode, path, recurse, out var nodeWasRemoved); return(nodeWasRemoved); } }