rubytree 0.2.4 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (8) hide show
  1. data/{LICENSE → COPYING} +1 -1
  2. data/ChangeLog +24 -1
  3. data/README +15 -11
  4. data/Rakefile +101 -89
  5. data/lib/tree.rb +380 -244
  6. data/test/person.rb +54 -47
  7. data/test/testtree.rb +355 -219
  8. metadata +4 -4
@@ -1,47 +1,54 @@
1
- # person.rb
2
- #
3
- # Revision: $Revision: 1.4 $
4
- #
5
- # Copyright (c) 2006, 2007 Anupam Sengupta
6
- #
7
- # All rights reserved.
8
- #
9
- # Redistribution and use in source and binary forms, with or without modification,
10
- # are permitted provided that the following conditions are met:
11
- #
12
- # - Redistributions of source code must retain the above copyright notice, this
13
- # list of conditions and the following disclaimer.
14
- #
15
- # - Redistributions in binary form must reproduce the above copyright notice, this
16
- # list of conditions and the following disclaimer in the documentation and/or
17
- # other materials provided with the distribution.
18
- #
19
- # - Neither the name of the organization nor the names of its contributors may
20
- # be used to endorse or promote products derived from this software without
21
- # specific prior written permission.
22
- #
23
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26
- # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
27
- # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28
- # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29
- # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30
- # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31
- # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32
- # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
-
34
- # Define a simple class for testing the RubyTree implementation
35
- class Person
36
- attr_reader :first, :last
37
- attr_writer :first, :last
38
- def initialize(first, last)
39
- @first = first
40
- @last = last
41
- end
42
-
43
- def to_s
44
- "#@first, #@last"
45
- end
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
+ #
@@ -2,7 +2,8 @@
2
2
 
3
3
  # testtree.rb
4
4
  #
5
- # Revision: $Revision: 1.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
- def setup
44
- @root = Tree::TreeNode.new("ROOT", "Root Node")
45
-
46
- @child1 = Tree::TreeNode.new("Child1", "Child Node 1")
47
- @child2 = Tree::TreeNode.new("Child2", "Child Node 2")
48
- @child3 = Tree::TreeNode.new("Child3", "Child Node 3")
49
- @child4 = Tree::TreeNode.new("Child31", "Grand Child 1")
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
- end
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
- def loadChildren
54
- @root << @child1
55
- @root << @child2
56
- @root << @child3 << @child4
57
- end
182
+ def test_previousSibling
183
+ loadChildren
58
184
 
59
- def teardown
60
- @root = nil
61
- end
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
- def test_root_setup
64
- assert_not_nil(@root, "Root cannot be nil")
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
- def test_root
78
- loadChildren
194
+ @root.add(@child1)
79
195
 
80
- assert_same(@root, @root.root, "Root's root is self")
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
- def test_siblings
86
- loadChildren
198
+ assert(@root.hasChildren?, "Should have children")
199
+ assert_equal(3, @root.size, "Should have three nodes")
87
200
 
88
- siblings = []
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
- siblings.clear
95
- siblings = @child1.siblings
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
- siblings.clear
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
- end
208
+ end
103
209
 
104
- def test_add
105
- assert(!@root.hasChildren?, "Should not have any children")
210
+ def test_remove
211
+ @root << @child1
212
+ @root << @child2
106
213
 
107
- @root.add(@child1)
214
+ assert(@root.hasChildren?, "Should have children")
215
+ assert_equal(3, @root.size, "Should have three nodes")
108
216
 
109
- @root << @child2
217
+ @root.remove!(@child1)
218
+ assert_equal(2, @root.size, "Should have two nodes")
219
+ @root.remove!(@child2)
110
220
 
111
- assert(@root.hasChildren?, "Should have children")
112
- assert_equal(3, @root.size, "Should have three nodes")
221
+ assert(!@root.hasChildren?, "Should have no children")
222
+ assert_equal(1, @root.size, "Should have one node")
113
223
 
114
- @root << @child3 << @child4
224
+ @root << @child1
225
+ @root << @child2
115
226
 
116
- assert_equal(5, @root.size, "Should have five nodes")
117
- assert_equal(2, @child3.size, "Should have two nodes")
227
+ assert(@root.hasChildren?, "Should have children")
228
+ assert_equal(3, @root.size, "Should have three nodes")
118
229
 
119
- assert_raise(RuntimeError) { @root.add(Tree::TreeNode.new(@child1.name)) }
230
+ @root.removeAll!
120
231
 
121
- end
232
+ assert(!@root.hasChildren?, "Should have no children")
233
+ assert_equal(1, @root.size, "Should have one node")
122
234
 
123
- def test_remove
124
- @root << @child1
125
- @root << @child2
235
+ end
126
236
 
127
- assert(@root.hasChildren?, "Should have children")
128
- assert_equal(3, @root.size, "Should have three nodes")
237
+ def test_removeAll
238
+ loadChildren
239
+ assert(@root.hasChildren?, "Should have children")
240
+ @root.removeAll!
129
241
 
130
- @root.remove!(@child1)
131
- assert_equal(2, @root.size, "Should have two nodes")
132
- @root.remove!(@child2)
242
+ assert(!@root.hasChildren?, "Should have no children")
243
+ assert_equal(1, @root.size, "Should have one node")
244
+ end
133
245
 
134
- assert(!@root.hasChildren?, "Should have no children")
135
- assert_equal(1, @root.size, "Should have one node")
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
- @root << @child1
138
- @root << @child2
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
- assert(@root.hasChildren?, "Should have children")
141
- assert_equal(3, @root.size, "Should have three nodes")
259
+ child1.removeFromParent!
260
+ assert_same(child1, child1.root, "Child 1's root should still be self")
261
+ end
142
262
 
143
- @root.removeAll!
263
+ def test_children
264
+ loadChildren
144
265
 
145
- assert(!@root.hasChildren?, "Should have no children")
146
- assert_equal(1, @root.size, "Should have one node")
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
- def test_removeAll
151
- loadChildren
152
- assert(@root.hasChildren?, "Should have children")
153
- @root.removeAll!
154
-
155
- assert(!@root.hasChildren?, "Should have no children")
156
- assert_equal(1, @root.size, "Should have one node")
157
- end
158
-
159
- def test_removeFromParent
160
- loadChildren
161
- assert(@root.hasChildren?, "Should have children")
162
-
163
- child1 = @root[0]
164
- assert_not_nil(child1, "Child 1 should exist")
165
- assert_same(@root, child1.root, "Child 1's root should be ROOT")
166
- assert(@root.include?(child1), "root should have child1")
167
- child1.removeFromParent!
168
- assert_same(child1, child1.root, "Child 1's root should be self")
169
- assert(!@root.include?(child1), "root should not have child1")
170
-
171
- child1.removeFromParent!
172
- assert_same(child1, child1.root, "Child 1's root should still be self")
173
- end
174
-
175
- def test_children
176
- loadChildren
177
-
178
- assert(@root.hasChildren?, "Should have children")
179
- assert_equal(5, @root.size, "Should have four nodes")
180
- assert(@child3.hasChildren?, "Should have children")
181
-
182
- children = []
183
- for child in @root.children
184
- children << child
185
- end
186
-
187
- assert_equal(3, children.length, "Should have three direct children")
188
- assert(!children.include?(@root), "Should not have root")
189
- assert(children.include?(@child1), "Should have child 1")
190
- assert(children.include?(@child2), "Should have child 2")
191
- assert(children.include?(@child3), "Should have child 3")
192
- assert(!children.include?(@child4), "Should not have child 4")
193
-
194
- children.clear
195
- children = @root.children
196
- assert_equal(3, children.length, "Should have three children")
197
-
198
- end
199
-
200
- def test_find
201
- loadChildren
202
- foundNode = @root.find { |node| node == @child2}
203
- assert_same(@child2, foundNode, "The node should be Child 2")
204
-
205
- foundNode = @root.find { |node| node == @child4}
206
- assert_same(@child4, foundNode, "The node should be Child 4")
207
-
208
- foundNode = @root.find { |node| node.name == "Child31" }
209
- assert_same(@child4, foundNode, "The node should be Child 4")
210
- foundNode = @root.find { |node| node.name == "NOT PRESENT" }
211
- assert_nil(foundNode, "The node should not be found")
212
- end
213
-
214
- def test_each
215
- loadChildren
216
- assert(@root.hasChildren?, "Should have children")
217
- assert_equal(5, @root.size, "Should have five nodes")
218
- assert(@child3.hasChildren?, "Should have children")
219
-
220
- nodes = []
221
- @root.each { |node| nodes << node }
222
-
223
- assert_equal(5, nodes.length, "Should have FIVE NODES")
224
- assert(nodes.include?(@root), "Should have root")
225
- assert(nodes.include?(@child1), "Should have child 1")
226
- assert(nodes.include?(@child2), "Should have child 2")
227
- assert(nodes.include?(@child3), "Should have child 3")
228
- assert(nodes.include?(@child4), "Should have child 4")
229
- end
230
-
231
- def test_parent
232
- loadChildren
233
- assert_nil(@root.parent, "Root's parent should be nil")
234
- assert_equal(@root, @child1.parent, "Parent should be root")
235
- assert_equal(@root, @child3.parent, "Parent should be root")
236
- assert_equal(@child3, @child4.parent, "Parent should be child3")
237
- assert_equal(@root, @child4.parent.parent, "Parent should be root")
238
- end
239
-
240
- def test_indexed_access
241
- loadChildren
242
- assert_equal(@child1, @root[0], "Should be the first child")
243
- assert_equal(@child4, @root[2][0], "Should be the grandchild")
244
- assert_nil(@root["TEST"], "Should be nil")
245
- assert_raise(RuntimeError) { @root[nil] }
246
- end
247
-
248
- def test_printTree
249
- loadChildren
250
- #puts
251
- #@root.printTree
252
- end
253
-
254
- def test_dump
255
- loadChildren
256
-
257
- pers = Person.new("John", "Doe")
258
- #@root.content = pers
259
-
260
- data = Marshal.dump(@root)
261
-
262
- newRoot = Marshal.load(data)
263
- assert(newRoot.isRoot?, "Must be a root node")
264
- assert_equal("ROOT", newRoot.name, "Must identify as ROOT")
265
- assert_equal("Root Node", newRoot.content, "Must have root's content")
266
- #assert_equal(pers.first, newRoot.content.first, "Must be the same content")
267
- assert_equal(@child4.name, newRoot['Child3']['Child31'].name, "Must be the grand child")
268
- end
269
-
270
- def test_collect
271
- loadChildren
272
- collectArray = @root.collect do |node|
273
- node.content = "abc"
274
- node
275
- end
276
- collectArray.each {|node| assert_equal("abc", node.content, "Should be 'abc'")}
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
- def test_freezeTree
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
- def test_content
289
- pers = Person.new("John", "Doe")
290
- @root.content = pers
291
- assert_same(pers, @root.content, "Content should be the same")
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
+ #