-
Notifications
You must be signed in to change notification settings - Fork 316
/
TreeTest.cs
executable file
·185 lines (128 loc) · 5.79 KB
/
TreeTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using wojilu.Web.Mvc;
using NUnit.Framework;
namespace wojilu.Test.Common {
[TestFixture]
public class TreeTest {
[Test]
public void testTreeData() {
List<Node> nodes = getNodesAll();
Tree<Node> tree = new Tree<Node>( nodes );
// GetById
INode node = tree.GetById( 13 );
Assert.IsNotNull( node );
Assert.AreEqual( "node13", node.Name );
INode nullNode = tree.GetById( 9999 );
Assert.IsNull( nullNode );
// GetParent
INode parent = tree.GetParent( node.Id );
Assert.IsNotNull( parent );
Assert.AreEqual( 5, parent.Id );
// GetDepth
Assert.AreEqual( 0, tree.GetDepth( 2 ) );
Assert.AreEqual( 1, tree.GetDepth( 7 ) );
Assert.AreEqual( 2, tree.GetDepth( 13 ) );
Assert.AreEqual( 2, tree.GetDepth( 14 ) );
// GetPath
List<Node> path = tree.GetPath( 13 );
Assert.AreEqual( 3, path.Count );
Assert.AreEqual( 1, path[0].Id );
Assert.AreEqual( 5, path[1].Id );
Assert.AreEqual( 13, path[2].Id );
// GetChildren
Assert.AreEqual( 0, tree.GetChildren( 4 ).Count );
Assert.AreEqual( 0, tree.GetChildren( 7 ).Count );
List<Node> children = tree.GetChildren( 5 );
Assert.AreEqual( 3, children.Count );
Assert.AreEqual( 13, children[0].Id );
Assert.AreEqual( 14, children[1].Id );
Assert.AreEqual( 15, children[2].Id );
// GetRoots
List<Node> roots = tree.GetRoots();
Assert.AreEqual( 3, roots.Count );
Assert.AreEqual( 1, roots[0].Id );
Assert.AreEqual( 2, roots[1].Id );
Assert.AreEqual( 3, roots[2].Id );
}
[Test]
public void testDroplist() {
List<Node> nodes = getNodesAll();
Tree<Node> tree = new Tree<Node>( nodes );
String rthml = tree.DropList( "myDrop", 0 );
Console.WriteLine( rthml );
}
[Test]
public void testTreeHtml() {
List<Node> nodes = getNodesAll();
Tree<Node> tree = new Tree<Node>( nodes );
string html = @"<ul class=""wTree"" id=""myTree""><li class=""parentNode expandNode"">node1</li><ul class=""hide""><li>node4</li><li class=""parentNode expandNode"">node5</li><ul class=""hide""><li>node13</li><li>node14</li><li>node15</li></ul><li>node6</li></ul><li class=""parentNode expandNode"">node2</li><ul class=""hide""><li>node7</li><li>node8</li><li>node9</li></ul><li class=""parentNode expandNode"">node3</li><ul class=""hide""><li>node10</li><li>node11</li><li>node12</li></ul></ul>";
string rthml = tree.RenderList( "myTree", false, null, 0 );
Assert.AreEqual( html, rthml );
Console.WriteLine( rthml );
}
[Test]
public void testFormatLoop() {
List<Node> nodes = getNodesAll();
string results = Html.Tree( nodes, getItemHtml );
Console.WriteLine( results );
}
private string getItemString( INode node, int depth ) {
string tabPrefix = "";
for (int i = 0; i < depth; i++) {
tabPrefix += "\t";
}
return tabPrefix + node.Name;
}
private string getItemHtml( INode node, int depth ) {
return string.Format( "<div style='margin-left:{0}px;'>{1}</div>", (depth + 1) * 15, node.Name );
}
//------------------------------------------------------------------------
private List<Node> getNodesAll() {
List<Node> nodes = new List<Node>();
nodes.Add( new Node { Id = 1, ParentId = 0, Name = "node1" } );
nodes.Add( new Node { Id = 2, ParentId = 0, Name = "node2" } );
nodes.Add( new Node { Id = 3, ParentId = 0, Name = "node3" } );
nodes.Add( new Node { Id = 4, ParentId = 1, Name = "node4" } );
nodes.Add( new Node { Id = 5, ParentId = 1, Name = "node5" } );
nodes.Add( new Node { Id = 6, ParentId = 1, Name = "node6" } );
nodes.Add( new Node { Id = 7, ParentId = 2, Name = "node7" } );
nodes.Add( new Node { Id = 8, ParentId = 2, Name = "node8" } );
nodes.Add( new Node { Id = 9, ParentId = 2, Name = "node9" } );
nodes.Add( new Node { Id = 10, ParentId = 3, Name = "node10" } );
nodes.Add( new Node { Id = 11, ParentId = 3, Name = "node11" } );
nodes.Add( new Node { Id = 12, ParentId = 3, Name = "node12" } );
nodes.Add( new Node { Id = 13, ParentId = 5, Name = "node13" } );
nodes.Add( new Node { Id = 14, ParentId = 5, Name = "node14" } );
nodes.Add( new Node { Id = 15, ParentId = 5, Name = "node15" } );
return nodes;
}
}
public class Node : INode {
public long Id { get; set; }
public string Name { get; set; }
public long ParentId { get; set; }
public string Title {
get { return this.Name + this.Id + "....."; }
}
}
public class SelectBinder : INodeBinder {
public string Bind( INode node ) {
return string.Format( "<option value=\"{0}\">{1}</option>" + Environment.NewLine, node.Id, node.Name );
}
public bool IsOpen( INode node ) {
return true;
}
public string GetTarget( INode node ) {
return "";
}
public string GetUrl( INode node ) {
return "";
}
public string GetName( INode node ) {
return node.Name;
}
}
}