rubytree 0.2.4 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/{LICENSE → COPYING} +1 -1
- data/ChangeLog +24 -1
- data/README +15 -11
- data/Rakefile +101 -89
- data/lib/tree.rb +380 -244
- data/test/person.rb +54 -47
- data/test/testtree.rb +355 -219
- metadata +4 -4
data/test/person.rb
CHANGED
@@ -1,47 +1,54 @@
|
|
1
|
-
# person.rb
|
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
|
-
end
|
1
|
+
# person.rb
|
2
|
+
#
|
3
|
+
# $Revision: 1.8 $ by $Author: anupamsg $
|
4
|
+
# $Name: $
|
5
|
+
#
|
6
|
+
# Copyright (c) 2006, 2007 Anupam Sengupta
|
7
|
+
#
|
8
|
+
# All rights reserved.
|
9
|
+
#
|
10
|
+
# Redistribution and use in source and binary forms, with or without modification,
|
11
|
+
# are permitted provided that the following conditions are met:
|
12
|
+
#
|
13
|
+
# - Redistributions of source code must retain the above copyright notice, this
|
14
|
+
# list of conditions and the following disclaimer.
|
15
|
+
#
|
16
|
+
# - Redistributions in binary form must reproduce the above copyright notice, this
|
17
|
+
# list of conditions and the following disclaimer in the documentation and/or
|
18
|
+
# other materials provided with the distribution.
|
19
|
+
#
|
20
|
+
# - Neither the name of the organization nor the names of its contributors may
|
21
|
+
# be used to endorse or promote products derived from this software without
|
22
|
+
# specific prior written permission.
|
23
|
+
#
|
24
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
25
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
26
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
27
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
28
|
+
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
29
|
+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
30
|
+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
31
|
+
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
32
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
33
|
+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
34
|
+
#
|
35
|
+
|
36
|
+
# Define a simple class for testing the RubyTree implementation
|
37
|
+
class Person
|
38
|
+
attr_reader :first, :last
|
39
|
+
attr_writer :first, :last
|
40
|
+
def initialize(first, last)
|
41
|
+
@first = first
|
42
|
+
@last = last
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_s
|
46
|
+
"#@first, #@last"
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
# $Log: person.rb,v $
|
52
|
+
# Revision 1.8 2007/07/17 03:39:28 anupamsg
|
53
|
+
# Moved the CVS Log keyword to end of the files.
|
54
|
+
#
|
data/test/testtree.rb
CHANGED
@@ -2,7 +2,8 @@
|
|
2
2
|
|
3
3
|
# testtree.rb
|
4
4
|
#
|
5
|
-
#
|
5
|
+
# $Revision: 1.11 $ by $Author: anupamsg $
|
6
|
+
# $Name: $
|
6
7
|
#
|
7
8
|
# Copyright (c) 2006, 2007 Anupam Sengupta
|
8
9
|
#
|
@@ -32,6 +33,7 @@
|
|
32
33
|
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
33
34
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
34
35
|
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
36
|
+
#
|
35
37
|
|
36
38
|
require 'test/unit'
|
37
39
|
require 'tree'
|
@@ -40,254 +42,388 @@ require 'person'
|
|
40
42
|
# Test class for the Tree node.
|
41
43
|
class TC_TreeTest < Test::Unit::TestCase
|
42
44
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
45
|
+
def setup
|
46
|
+
@root = Tree::TreeNode.new("ROOT", "Root Node")
|
47
|
+
|
48
|
+
@child1 = Tree::TreeNode.new("Child1", "Child Node 1")
|
49
|
+
@child2 = Tree::TreeNode.new("Child2", "Child Node 2")
|
50
|
+
@child3 = Tree::TreeNode.new("Child3", "Child Node 3")
|
51
|
+
@child4 = Tree::TreeNode.new("Child31", "Grand Child 1")
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
# Create this structure for the tests
|
56
|
+
#
|
57
|
+
# +----------+
|
58
|
+
# | ROOT |
|
59
|
+
# +-+--------+
|
60
|
+
# |
|
61
|
+
# | +---------------+
|
62
|
+
# +----+ CHILD1 |
|
63
|
+
# | +---------------+
|
64
|
+
# |
|
65
|
+
# | +---------------+
|
66
|
+
# +----+ CHILD2 |
|
67
|
+
# | +---------------+
|
68
|
+
# |
|
69
|
+
# | +---------------+ +------------------+
|
70
|
+
# +----+ CHILD3 +---+ CHILD4 |
|
71
|
+
# +---------------+ +------------------+
|
72
|
+
#
|
73
|
+
def loadChildren
|
74
|
+
@root << @child1
|
75
|
+
@root << @child2
|
76
|
+
@root << @child3 << @child4
|
77
|
+
end
|
78
|
+
|
79
|
+
def teardown
|
80
|
+
@root = nil
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_root_setup
|
84
|
+
assert_not_nil(@root, "Root cannot be nil")
|
85
|
+
assert_nil(@root.parent, "Parent of root node should be nil")
|
86
|
+
assert_not_nil(@root.name, "Name should not be nil")
|
87
|
+
assert_equal("ROOT", @root.name, "Name should be 'ROOT'")
|
88
|
+
assert_equal("Root Node", @root.content, "Content should be 'Root Node'")
|
89
|
+
assert(@root.isRoot?, "Should identify as root")
|
90
|
+
assert(!@root.hasChildren?, "Cannot have any children")
|
91
|
+
assert_equal(1, @root.size, "Number of nodes should be one")
|
92
|
+
assert_nil(@root.siblings, "Root cannot have any children")
|
93
|
+
|
94
|
+
assert_raise(RuntimeError) { Tree::TreeNode.new(nil) }
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_root
|
98
|
+
loadChildren
|
99
|
+
|
100
|
+
assert_same(@root, @root.root, "Root's root is self")
|
101
|
+
assert_same(@root, @child1.root, "Root should be ROOT")
|
102
|
+
assert_same(@root, @child4.root, "Root should be ROOT")
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_firstSibling
|
106
|
+
loadChildren
|
107
|
+
|
108
|
+
assert_same(@root, @root.firstSibling, "Root's first sibling is itself")
|
109
|
+
assert_same(@child1, @child1.firstSibling, "Child1's first sibling is itself")
|
110
|
+
assert_same(@child1, @child2.firstSibling, "Child2's first sibling should be child1")
|
111
|
+
assert_same(@child1, @child3.firstSibling, "Child3's first sibling should be child1")
|
112
|
+
assert_not_same(@child1, @child4.firstSibling, "Child4's first sibling is itself")
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_isFirstSibling
|
116
|
+
loadChildren
|
117
|
+
|
118
|
+
assert(@root.isFirstSibling?, "Root's first sibling is itself")
|
119
|
+
assert( @child1.isFirstSibling?, "Child1's first sibling is itself")
|
120
|
+
assert(!@child2.isFirstSibling?, "Child2 is not the first sibling")
|
121
|
+
assert(!@child3.isFirstSibling?, "Child3 is not the first sibling")
|
122
|
+
assert( @child4.isFirstSibling?, "Child4's first sibling is itself")
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_isLastSibling
|
126
|
+
loadChildren
|
127
|
+
|
128
|
+
assert(@root.isLastSibling?, "Root's last sibling is itself")
|
129
|
+
assert(!@child1.isLastSibling?, "Child1 is not the last sibling")
|
130
|
+
assert(!@child2.isLastSibling?, "Child2 is not the last sibling")
|
131
|
+
assert( @child3.isLastSibling?, "Child3's last sibling is itself")
|
132
|
+
assert( @child4.isLastSibling?, "Child4's last sibling is itself")
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_lastSibling
|
136
|
+
loadChildren
|
137
|
+
|
138
|
+
assert_same(@root, @root.lastSibling, "Root's last sibling is itself")
|
139
|
+
assert_same(@child3, @child1.lastSibling, "Child1's last sibling should be child3")
|
140
|
+
assert_same(@child3, @child2.lastSibling, "Child2's last sibling should be child3")
|
141
|
+
assert_same(@child3, @child3.lastSibling, "Child3's last sibling should be itself")
|
142
|
+
assert_not_same(@child3, @child4.lastSibling, "Child4's last sibling is itself")
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_siblings
|
146
|
+
loadChildren
|
147
|
+
|
148
|
+
siblings = []
|
149
|
+
@child1.siblings { |sibling| siblings << sibling}
|
150
|
+
assert_equal(2, siblings.length, "Should have two siblings")
|
151
|
+
assert(siblings.include?(@child2), "Should have 2nd child as sibling")
|
152
|
+
assert(siblings.include?(@child3), "Should have 3rd child as sibling")
|
153
|
+
|
154
|
+
siblings.clear
|
155
|
+
siblings = @child1.siblings
|
156
|
+
assert_equal(2, siblings.length, "Should have two siblings")
|
157
|
+
|
158
|
+
siblings.clear
|
159
|
+
@child4.siblings {|sibling| siblings << sibling}
|
160
|
+
assert(siblings.empty?, "Should not have any children")
|
161
|
+
|
162
|
+
end
|
163
|
+
|
164
|
+
def test_isOnlyChild?
|
165
|
+
loadChildren
|
166
|
+
|
167
|
+
assert(!@child1.isOnlyChild?, "Child1 is not the only child")
|
168
|
+
assert(!@child2.isOnlyChild?, "Child2 is not the only child")
|
169
|
+
assert(!@child3.isOnlyChild?, "Child3 is not the only child")
|
170
|
+
assert( @child4.isOnlyChild?, "Child4 is not the only child")
|
171
|
+
end
|
172
|
+
|
173
|
+
def test_nextSibling
|
174
|
+
loadChildren
|
50
175
|
|
51
|
-
|
176
|
+
assert_equal(@child2, @child1.nextSibling, "Child1's next sibling is Child2")
|
177
|
+
assert_equal(@child3, @child2.nextSibling, "Child2's next sibling is Child3")
|
178
|
+
assert_nil(@child3.nextSibling, "Child3 does not have a next sibling")
|
179
|
+
assert_nil(@child4.nextSibling, "Child4 does not have a next sibling")
|
180
|
+
end
|
52
181
|
|
53
|
-
|
54
|
-
|
55
|
-
@root << @child2
|
56
|
-
@root << @child3 << @child4
|
57
|
-
end
|
182
|
+
def test_previousSibling
|
183
|
+
loadChildren
|
58
184
|
|
59
|
-
|
60
|
-
|
61
|
-
|
185
|
+
assert_nil(@child1.previousSibling, "Child1 does not have previous sibling")
|
186
|
+
assert_equal(@child1, @child2.previousSibling, "Child2's previous sibling is Child1")
|
187
|
+
assert_equal(@child2, @child3.previousSibling, "Child3's previous sibling is Child2")
|
188
|
+
assert_nil(@child4.previousSibling, "Child4 does not have a previous sibling")
|
189
|
+
end
|
62
190
|
|
63
|
-
|
64
|
-
|
65
|
-
assert_nil(@root.parent, "Parent of root node should be nil")
|
66
|
-
assert_not_nil(@root.name, "Name should not be nil")
|
67
|
-
assert_equal("ROOT", @root.name, "Name should be 'ROOT'")
|
68
|
-
assert_equal("Root Node", @root.content, "Content should be 'Root Node'")
|
69
|
-
assert(@root.isRoot?, "Should identify as root")
|
70
|
-
assert(!@root.hasChildren?, "Cannot have any children")
|
71
|
-
assert_equal(1, @root.size, "Number of nodes should be one")
|
72
|
-
assert_nil(@root.siblings, "Root cannot have any children")
|
73
|
-
|
74
|
-
assert_raise(RuntimeError) { Tree::TreeNode.new(nil) }
|
75
|
-
end
|
191
|
+
def test_add
|
192
|
+
assert(!@root.hasChildren?, "Should not have any children")
|
76
193
|
|
77
|
-
|
78
|
-
loadChildren
|
194
|
+
@root.add(@child1)
|
79
195
|
|
80
|
-
|
81
|
-
assert_same(@root, @child1.root, "Root should be ROOT")
|
82
|
-
assert_same(@root, @child4.root, "Root should be ROOT")
|
83
|
-
end
|
196
|
+
@root << @child2
|
84
197
|
|
85
|
-
|
86
|
-
|
198
|
+
assert(@root.hasChildren?, "Should have children")
|
199
|
+
assert_equal(3, @root.size, "Should have three nodes")
|
87
200
|
|
88
|
-
|
89
|
-
@child1.siblings { |sibling| siblings << sibling}
|
90
|
-
assert_equal(2, siblings.length, "Should have two siblings")
|
91
|
-
assert(siblings.include?(@child2), "Should have 2nd child as sibling")
|
92
|
-
assert(siblings.include?(@child3), "Should have 3rd child as sibling")
|
201
|
+
@root << @child3 << @child4
|
93
202
|
|
94
|
-
|
95
|
-
|
96
|
-
assert_equal(2, siblings.length, "Should have two siblings")
|
203
|
+
assert_equal(5, @root.size, "Should have five nodes")
|
204
|
+
assert_equal(2, @child3.size, "Should have two nodes")
|
97
205
|
|
98
|
-
|
99
|
-
@child4.siblings {|sibling| siblings << sibling}
|
100
|
-
assert(siblings.empty?, "Should not have any children")
|
206
|
+
assert_raise(RuntimeError) { @root.add(Tree::TreeNode.new(@child1.name)) }
|
101
207
|
|
102
|
-
|
208
|
+
end
|
103
209
|
|
104
|
-
|
105
|
-
|
210
|
+
def test_remove
|
211
|
+
@root << @child1
|
212
|
+
@root << @child2
|
106
213
|
|
107
|
-
|
214
|
+
assert(@root.hasChildren?, "Should have children")
|
215
|
+
assert_equal(3, @root.size, "Should have three nodes")
|
108
216
|
|
109
|
-
|
217
|
+
@root.remove!(@child1)
|
218
|
+
assert_equal(2, @root.size, "Should have two nodes")
|
219
|
+
@root.remove!(@child2)
|
110
220
|
|
111
|
-
|
112
|
-
|
221
|
+
assert(!@root.hasChildren?, "Should have no children")
|
222
|
+
assert_equal(1, @root.size, "Should have one node")
|
113
223
|
|
114
|
-
|
224
|
+
@root << @child1
|
225
|
+
@root << @child2
|
115
226
|
|
116
|
-
|
117
|
-
|
227
|
+
assert(@root.hasChildren?, "Should have children")
|
228
|
+
assert_equal(3, @root.size, "Should have three nodes")
|
118
229
|
|
119
|
-
|
230
|
+
@root.removeAll!
|
120
231
|
|
121
|
-
|
232
|
+
assert(!@root.hasChildren?, "Should have no children")
|
233
|
+
assert_equal(1, @root.size, "Should have one node")
|
122
234
|
|
123
|
-
|
124
|
-
@root << @child1
|
125
|
-
@root << @child2
|
235
|
+
end
|
126
236
|
|
127
|
-
|
128
|
-
|
237
|
+
def test_removeAll
|
238
|
+
loadChildren
|
239
|
+
assert(@root.hasChildren?, "Should have children")
|
240
|
+
@root.removeAll!
|
129
241
|
|
130
|
-
|
131
|
-
|
132
|
-
|
242
|
+
assert(!@root.hasChildren?, "Should have no children")
|
243
|
+
assert_equal(1, @root.size, "Should have one node")
|
244
|
+
end
|
133
245
|
|
134
|
-
|
135
|
-
|
246
|
+
def test_removeFromParent
|
247
|
+
loadChildren
|
248
|
+
assert(@root.hasChildren?, "Should have children")
|
249
|
+
assert(!@root.isLeaf?, "Root is not a leaf here")
|
136
250
|
|
137
|
-
|
138
|
-
|
251
|
+
child1 = @root[0]
|
252
|
+
assert_not_nil(child1, "Child 1 should exist")
|
253
|
+
assert_same(@root, child1.root, "Child 1's root should be ROOT")
|
254
|
+
assert(@root.include?(child1), "root should have child1")
|
255
|
+
child1.removeFromParent!
|
256
|
+
assert_same(child1, child1.root, "Child 1's root should be self")
|
257
|
+
assert(!@root.include?(child1), "root should not have child1")
|
139
258
|
|
140
|
-
|
141
|
-
|
259
|
+
child1.removeFromParent!
|
260
|
+
assert_same(child1, child1.root, "Child 1's root should still be self")
|
261
|
+
end
|
142
262
|
|
143
|
-
|
263
|
+
def test_children
|
264
|
+
loadChildren
|
144
265
|
|
145
|
-
|
146
|
-
|
266
|
+
assert(@root.hasChildren?, "Should have children")
|
267
|
+
assert_equal(5, @root.size, "Should have four nodes")
|
268
|
+
assert(@child3.hasChildren?, "Should have children")
|
269
|
+
assert(!@child3.isLeaf?, "Should not be a leaf")
|
147
270
|
|
271
|
+
children = []
|
272
|
+
for child in @root.children
|
273
|
+
children << child
|
148
274
|
end
|
149
275
|
|
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
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
276
|
+
assert_equal(3, children.length, "Should have three direct children")
|
277
|
+
assert(!children.include?(@root), "Should not have root")
|
278
|
+
assert(children.include?(@child1), "Should have child 1")
|
279
|
+
assert(children.include?(@child2), "Should have child 2")
|
280
|
+
assert(children.include?(@child3), "Should have child 3")
|
281
|
+
assert(!children.include?(@child4), "Should not have child 4")
|
282
|
+
|
283
|
+
children.clear
|
284
|
+
children = @root.children
|
285
|
+
assert_equal(3, children.length, "Should have three children")
|
286
|
+
|
287
|
+
end
|
288
|
+
|
289
|
+
def test_firstChild
|
290
|
+
loadChildren
|
291
|
+
|
292
|
+
assert_equal(@child1, @root.firstChild, "Root's first child is Child1")
|
293
|
+
assert_nil(@child1.firstChild, "Child1 does not have any children")
|
294
|
+
assert_equal(@child4, @child3.firstChild, "Child3's first child is Child4")
|
295
|
+
|
296
|
+
end
|
297
|
+
|
298
|
+
def test_lastChild
|
299
|
+
loadChildren
|
300
|
+
|
301
|
+
assert_equal(@child3, @root.lastChild, "Root's last child is Child3")
|
302
|
+
assert_nil(@child1.lastChild, "Child1 does not have any children")
|
303
|
+
assert_equal(@child4, @child3.lastChild, "Child3's last child is Child4")
|
304
|
+
|
305
|
+
end
|
306
|
+
|
307
|
+
def test_find
|
308
|
+
loadChildren
|
309
|
+
foundNode = @root.find { |node| node == @child2}
|
310
|
+
assert_same(@child2, foundNode, "The node should be Child 2")
|
311
|
+
|
312
|
+
foundNode = @root.find { |node| node == @child4}
|
313
|
+
assert_same(@child4, foundNode, "The node should be Child 4")
|
314
|
+
|
315
|
+
foundNode = @root.find { |node| node.name == "Child31" }
|
316
|
+
assert_same(@child4, foundNode, "The node should be Child 4")
|
317
|
+
foundNode = @root.find { |node| node.name == "NOT PRESENT" }
|
318
|
+
assert_nil(foundNode, "The node should not be found")
|
319
|
+
end
|
320
|
+
|
321
|
+
def test_ancestors
|
322
|
+
loadChildren
|
323
|
+
|
324
|
+
assert_nil(@root.ancestors, "Root does not have any ancestors")
|
325
|
+
assert_equal([@root], @child1.ancestors, "Child1 has Root as its parent")
|
326
|
+
assert_equal([@child3, @root], @child4.ancestors, "Child4 has Child3 and Root as ancestors")
|
327
|
+
end
|
328
|
+
|
329
|
+
def test_each
|
330
|
+
loadChildren
|
331
|
+
assert(@root.hasChildren?, "Should have children")
|
332
|
+
assert_equal(5, @root.size, "Should have five nodes")
|
333
|
+
assert(@child3.hasChildren?, "Should have children")
|
334
|
+
|
335
|
+
nodes = []
|
336
|
+
@root.each { |node| nodes << node }
|
337
|
+
|
338
|
+
assert_equal(5, nodes.length, "Should have FIVE NODES")
|
339
|
+
assert(nodes.include?(@root), "Should have root")
|
340
|
+
assert(nodes.include?(@child1), "Should have child 1")
|
341
|
+
assert(nodes.include?(@child2), "Should have child 2")
|
342
|
+
assert(nodes.include?(@child3), "Should have child 3")
|
343
|
+
assert(nodes.include?(@child4), "Should have child 4")
|
344
|
+
end
|
345
|
+
|
346
|
+
def test_each_leaf
|
347
|
+
loadChildren
|
348
|
+
|
349
|
+
nodes = []
|
350
|
+
@root.each_leaf { |node| nodes << node }
|
351
|
+
|
352
|
+
assert_equal(3, nodes.length, "Should have THREE LEAF NODES")
|
353
|
+
assert(!nodes.include?(@root), "Should not have root")
|
354
|
+
assert(nodes.include?(@child1), "Should have child 1")
|
355
|
+
assert(nodes.include?(@child2), "Should have child 2")
|
356
|
+
assert(!nodes.include?(@child3), "Should not have child 3")
|
357
|
+
assert(nodes.include?(@child4), "Should have child 4")
|
358
|
+
end
|
359
|
+
|
360
|
+
def test_parent
|
361
|
+
loadChildren
|
362
|
+
assert_nil(@root.parent, "Root's parent should be nil")
|
363
|
+
assert_equal(@root, @child1.parent, "Parent should be root")
|
364
|
+
assert_equal(@root, @child3.parent, "Parent should be root")
|
365
|
+
assert_equal(@child3, @child4.parent, "Parent should be child3")
|
366
|
+
assert_equal(@root, @child4.parent.parent, "Parent should be root")
|
367
|
+
end
|
368
|
+
|
369
|
+
def test_indexed_access
|
370
|
+
loadChildren
|
371
|
+
assert_equal(@child1, @root[0], "Should be the first child")
|
372
|
+
assert_equal(@child4, @root[2][0], "Should be the grandchild")
|
373
|
+
assert_nil(@root["TEST"], "Should be nil")
|
374
|
+
assert_raise(RuntimeError) { @root[nil] }
|
375
|
+
end
|
376
|
+
|
377
|
+
def test_printTree
|
378
|
+
loadChildren
|
379
|
+
#puts
|
380
|
+
#@root.printTree
|
381
|
+
end
|
382
|
+
|
383
|
+
def test_dump
|
384
|
+
loadChildren
|
385
|
+
|
386
|
+
pers = Person.new("John", "Doe")
|
387
|
+
#@root.content = pers
|
388
|
+
|
389
|
+
data = Marshal.dump(@root)
|
390
|
+
|
391
|
+
newRoot = Marshal.load(data)
|
392
|
+
assert(newRoot.isRoot?, "Must be a root node")
|
393
|
+
assert_equal("ROOT", newRoot.name, "Must identify as ROOT")
|
394
|
+
assert_equal("Root Node", newRoot.content, "Must have root's content")
|
395
|
+
#assert_equal(pers.first, newRoot.content.first, "Must be the same content")
|
396
|
+
assert_equal(@child4.name, newRoot['Child3']['Child31'].name, "Must be the grand child")
|
397
|
+
end
|
398
|
+
|
399
|
+
def test_collect
|
400
|
+
loadChildren
|
401
|
+
collectArray = @root.collect do |node|
|
402
|
+
node.content = "abc"
|
403
|
+
node
|
277
404
|
end
|
405
|
+
collectArray.each {|node| assert_equal("abc", node.content, "Should be 'abc'")}
|
406
|
+
end
|
407
|
+
|
408
|
+
def test_freezeTree
|
409
|
+
loadChildren
|
410
|
+
@root.content = "ABC"
|
411
|
+
assert_equal("ABC", @root.content, "Content should be 'ABC'")
|
412
|
+
@root.freezeTree!
|
413
|
+
assert_raise(TypeError) {@root.content = "123"}
|
414
|
+
assert_raise(TypeError) {@root[0].content = "123"}
|
415
|
+
end
|
416
|
+
|
417
|
+
def test_content
|
418
|
+
pers = Person.new("John", "Doe")
|
419
|
+
@root.content = pers
|
420
|
+
assert_same(pers, @root.content, "Content should be the same")
|
421
|
+
end
|
422
|
+
end
|
278
423
|
|
279
|
-
|
280
|
-
loadChildren
|
281
|
-
@root.content = "ABC"
|
282
|
-
assert_equal("ABC", @root.content, "Content should be 'ABC'")
|
283
|
-
@root.freezeTree!
|
284
|
-
assert_raise(TypeError) {@root.content = "123"}
|
285
|
-
assert_raise(TypeError) {@root[0].content = "123"}
|
286
|
-
end
|
424
|
+
__END__
|
287
425
|
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
end
|
293
|
-
end
|
426
|
+
# $Log: testtree.rb,v $
|
427
|
+
# Revision 1.11 2007/07/17 03:39:29 anupamsg
|
428
|
+
# Moved the CVS Log keyword to end of the files.
|
429
|
+
#
|