public void AttachNode_Node() { var tree = getTree(); var node = new MyMenu() { Id = 4, ParentId = 4 }; Assert.Throws <TreeNodeException <MyMenu, int> >(() => { tree.AttachNode(node); }); node.Id = 5; //这里测试配置正确的下级节点能否在树中生存良好(在程序集以外理论上不能创建这样的节点,必须经过算法整理才能创建层级节点) var subNode = new MyMenu { Id = 6, ParentId = 5, Name = "待测试", Parent = node }; ((List <MyMenu>)node.Children).Add(subNode); tree.AttachNode(node); Assert.NotNull(node.Parent); Assert.True(node.Parent == tree.Find(node.ParentId)); Assert.True(subNode.Parent.Children[0] == subNode); Assert.NotNull(tree.Find(node.Id)); //尝试重复添加 Assert.Throws <TreeNodeException <MyMenu, int> >(() => { tree.AttachNode(node); }); //尝试错误添加,id重复 Assert.Throws <TreeNodeException <MyMenu, int> >(() => { tree.AttachNode(new MyMenu() { Id = 3, ParentId = 2, Name = "id重复" }); }); //尝试错误添加,父节点不存在重复 Assert.Throws <TreeNodeException <MyMenu, int> >(() => { tree.AttachNode(new MyMenu() { Id = 8, ParentId = 7, Name = "不存在父节点" }); }); //尝试错误添加,混合以上两点 Assert.Throws <TreeNodeException <MyMenu, int> >(() => { tree.AttachNode(new MyMenu() { Id = 3, ParentId = 7, Name = "id重复的不存在父节点" }); }); //附加另一颗树的节点 var tree2 = getTree(); Assert.Throws <TreeNodeException <MyMenu, int> >(() => { tree.AttachNode(tree2.Root); }); }
public void AttachNode_NodeIndex() { var tree = getTree(); var node = new MyMenu() { Id = 5, ParentId = 1 }; const int index = 1; tree.AttachNode(node, index); Assert.True(node.Index == index); Assert.True(node.Parent == tree.Find(node.ParentId)); Assert.True(node.Parent.Children[index] == node); }