rubytree 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,3 +1,17 @@
1
+ 2007-12-21 Anupam Sengupta <anupamsg@gmail.com>
2
+
3
+ * Rakefile: Added the rcov option to exclude rcov itself from
4
+ coverage reports.
5
+
6
+ * lib/tree.rb: Minor comment changes.
7
+
8
+ * test/test_tree.rb: Added the TestTree enclosing module, and
9
+ renamed tests to meet ZenTest requirements. Also added a few
10
+ missing test cases.
11
+
12
+ * test/test_binarytree.rb: Added the TestTree enclosing Module,
13
+ and renamed the tests to meet ZenTest requirements.
14
+
1
15
  2007-12-19 Anupam Sengupta <anupamsg@gmail.com>
2
16
 
3
17
  * README (Module): Modified the install instructions from source.
@@ -1,3 +1,12 @@
1
+ = 0.5.2 / 2007-12-21
2
+
3
+ * Added more test cases and enabled ZenTest compatibility for the test case
4
+ names.
5
+
6
+ = 0.5.1 / 2007-12-20
7
+
8
+ * Minor code refactoring.
9
+
1
10
  = 0.5.0 / 2007-12-18
2
11
 
3
12
  * Fixed the marshalling code to correctly handle non-string content.
@@ -3,8 +3,8 @@ ChangeLog
3
3
  History.txt
4
4
  Manifest.txt
5
5
  README
6
- TODO
7
6
  Rakefile
7
+ TODO
8
8
  lib/tree.rb
9
9
  lib/tree/binarytree.rb
10
10
  setup.rb
data/README CHANGED
@@ -144,4 +144,4 @@ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
144
144
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145
145
 
146
146
 
147
- (Document Revision: $Revision: 1.15 $ by $Author: anupamsg $)
147
+ (Document Revision: $Revision: 1.16 $ by $Author: anupamsg $)
data/Rakefile CHANGED
@@ -176,6 +176,7 @@ begin
176
176
  require 'rcov/rcovtask'
177
177
  Rcov::RcovTask.new do |t|
178
178
  t.test_files = FileList['test/test*.rb']
179
+ t.rcov_opts << "--exclude 'rcov.rb'" # rcov itself should not be profiled
179
180
  # t.verbose = true # uncomment to see the executed commands
180
181
  end
181
182
  rescue LoadError
@@ -1,6 +1,6 @@
1
1
  # tree.rb
2
2
  #
3
- # $Revision: 1.28 $ by $Author: anupamsg $
3
+ # $Revision: 1.29 $ by $Author: anupamsg $
4
4
  # $Name: $
5
5
  #
6
6
  # = tree.rb - Generic Multi-way Tree implementation
@@ -48,7 +48,7 @@
48
48
  module Tree
49
49
 
50
50
  # Rubytree Package Version
51
- VERSION = '0.5.1'
51
+ VERSION = '0.5.2'
52
52
 
53
53
  # == TreeNode Class Description
54
54
  #
@@ -132,13 +132,11 @@ module Tree
132
132
 
133
133
  # Print the string representation of this node.
134
134
  def to_s
135
-
136
135
  "Node Name: #{@name}" +
137
136
  " Content: " + (@content || "<Empty>") +
138
137
  " Parent: " + (isRoot?() ? "<None>" : @parent.name) +
139
138
  " Children: #{@children.length}" +
140
139
  " Total Nodes: #{size()}"
141
-
142
140
  end
143
141
 
144
142
  # Returns an array of ancestors in reversed order (the first element is the
@@ -452,7 +450,7 @@ module Tree
452
450
  # This is the root node, hence initialize self.
453
451
  initialize(name, content)
454
452
 
455
- nodes[name] = self # Add self to the
453
+ nodes[name] = self # Add self to the list of nodes
456
454
  end
457
455
  end
458
456
  end
@@ -477,6 +475,9 @@ module Tree
477
475
  end
478
476
 
479
477
  # $Log: tree.rb,v $
478
+ # Revision 1.29 2007/12/22 00:28:59 anupamsg
479
+ # Added more test cases, and enabled ZenTest compatibility.
480
+ #
480
481
  # Revision 1.28 2007/12/20 03:19:33 anupamsg
481
482
  # * README (Module): Modified the install instructions from source.
482
483
  # (Module): Updated the minor version number.
@@ -2,7 +2,7 @@
2
2
 
3
3
  # test_binarytree.rb
4
4
  #
5
- # $Revision: 1.4 $ by $Author: anupamsg $
5
+ # $Revision: 1.5 $ by $Author: anupamsg $
6
6
  # $Name: $
7
7
  #
8
8
  # Copyright (c) 2006, 2007 Anupam Sengupta
@@ -38,138 +38,143 @@
38
38
  require 'test/unit'
39
39
  require 'tree/binarytree'
40
40
 
41
- # Test class for the Tree node.
42
- class TC_BinaryTreeTest < Test::Unit::TestCase
41
+ module TestTree
42
+ # Test class for the Tree node.
43
+ class TestBinaryTreeNode < Test::Unit::TestCase
43
44
 
44
- def setup
45
- @root = Tree::BinaryTreeNode.new("ROOT", "Root Node")
45
+ def setup
46
+ @root = Tree::BinaryTreeNode.new("ROOT", "Root Node")
46
47
 
47
- @left_child1 = Tree::BinaryTreeNode.new("A Child at Left", "Child Node @ left")
48
- @right_child1 = Tree::BinaryTreeNode.new("B Child at Right", "Child Node @ right")
48
+ @left_child1 = Tree::BinaryTreeNode.new("A Child at Left", "Child Node @ left")
49
+ @right_child1 = Tree::BinaryTreeNode.new("B Child at Right", "Child Node @ right")
49
50
 
50
- end
51
+ end
51
52
 
52
- def teardown
53
- @root.remove!(@left_child1)
54
- @root.remove!(@right_child1)
55
- @root = nil
56
- end
53
+ def teardown
54
+ @root.remove!(@left_child1)
55
+ @root.remove!(@right_child1)
56
+ @root = nil
57
+ end
57
58
 
58
- def test_initialize
59
- assert_not_nil(@root, "Binary tree's Root should have been created")
60
- end
59
+ def test_initialize
60
+ assert_not_nil(@root, "Binary tree's Root should have been created")
61
+ end
61
62
 
62
- def test_add
63
- @root.add @left_child1
64
- assert_same(@left_child1, @root.leftChild, "The left node should be left_child1")
65
- assert_same(@left_child1, @root.firstChild, "The first node should be left_child1")
63
+ def test_add
64
+ @root.add @left_child1
65
+ assert_same(@left_child1, @root.leftChild, "The left node should be left_child1")
66
+ assert_same(@left_child1, @root.firstChild, "The first node should be left_child1")
66
67
 
67
- @root.add @right_child1
68
- assert_same(@right_child1, @root.rightChild, "The right node should be right_child1")
69
- assert_same(@right_child1, @root.lastChild, "The first node should be right_child1")
68
+ @root.add @right_child1
69
+ assert_same(@right_child1, @root.rightChild, "The right node should be right_child1")
70
+ assert_same(@right_child1, @root.lastChild, "The first node should be right_child1")
70
71
 
71
- assert_raise RuntimeError do
72
- @root.add Tree::BinaryTreeNode.new("The third child!")
73
- end
72
+ assert_raise RuntimeError do
73
+ @root.add Tree::BinaryTreeNode.new("The third child!")
74
+ end
74
75
 
75
- assert_raise RuntimeError do
76
- @root << Tree::BinaryTreeNode.new("The third child!")
76
+ assert_raise RuntimeError do
77
+ @root << Tree::BinaryTreeNode.new("The third child!")
78
+ end
77
79
  end
78
- end
79
80
 
80
- def test_left
81
- @root << @left_child1
82
- @root << @right_child1
83
- assert_same(@left_child1, @root.leftChild, "The left child should be 'left_child1")
84
- assert_not_same(@right_child1, @root.leftChild, "The right_child1 is not the left child")
85
- end
81
+ def test_leftChild
82
+ @root << @left_child1
83
+ @root << @right_child1
84
+ assert_same(@left_child1, @root.leftChild, "The left child should be 'left_child1")
85
+ assert_not_same(@right_child1, @root.leftChild, "The right_child1 is not the left child")
86
+ end
86
87
 
87
- def test_right
88
- @root << @left_child1
89
- @root << @right_child1
90
- assert_same(@right_child1, @root.rightChild, "The right child should be 'right_child1")
91
- assert_not_same(@left_child1, @root.rightChild, "The left_child1 is not the left child")
92
- end
88
+ def test_rightChild
89
+ @root << @left_child1
90
+ @root << @right_child1
91
+ assert_same(@right_child1, @root.rightChild, "The right child should be 'right_child1")
92
+ assert_not_same(@left_child1, @root.rightChild, "The left_child1 is not the left child")
93
+ end
93
94
 
94
- def test_left_assignment
95
- @root << @left_child1
96
- @root << @right_child1
97
- assert_same(@left_child1, @root.leftChild, "The left child should be 'left_child1")
95
+ def test_leftChild_equals
96
+ @root << @left_child1
97
+ @root << @right_child1
98
+ assert_same(@left_child1, @root.leftChild, "The left child should be 'left_child1")
98
99
 
99
- @root.leftChild = Tree::BinaryTreeNode.new("New Left Child")
100
- assert_equal("New Left Child", @root.leftChild.name, "The left child should now be the new child")
101
- assert_equal("B Child at Right", @root.lastChild.name, "The last child should now be the right child")
100
+ @root.leftChild = Tree::BinaryTreeNode.new("New Left Child")
101
+ assert_equal("New Left Child", @root.leftChild.name, "The left child should now be the new child")
102
+ assert_equal("B Child at Right", @root.lastChild.name, "The last child should now be the right child")
102
103
 
103
- # Now set the left child as nil, and retest
104
- @root.leftChild = nil
105
- assert_nil(@root.leftChild, "The left child should now be nil")
106
- assert_nil(@root.firstChild, "The first child is now nil")
107
- assert_equal("B Child at Right", @root.lastChild.name, "The last child should now be the right child")
108
- end
104
+ # Now set the left child as nil, and retest
105
+ @root.leftChild = nil
106
+ assert_nil(@root.leftChild, "The left child should now be nil")
107
+ assert_nil(@root.firstChild, "The first child is now nil")
108
+ assert_equal("B Child at Right", @root.lastChild.name, "The last child should now be the right child")
109
+ end
109
110
 
110
- def test_right_assignment
111
- @root << @left_child1
112
- @root << @right_child1
113
- assert_same(@right_child1, @root.rightChild, "The right child should be 'right_child1")
114
-
115
- @root.rightChild = Tree::BinaryTreeNode.new("New Right Child")
116
- assert_equal("New Right Child", @root.rightChild.name, "The right child should now be the new child")
117
- assert_equal("A Child at Left", @root.firstChild.name, "The first child should now be the left child")
118
- assert_equal("New Right Child", @root.lastChild.name, "The last child should now be the right child")
119
-
120
- # Now set the right child as nil, and retest
121
- @root.rightChild = nil
122
- assert_nil(@root.rightChild, "The right child should now be nil")
123
- assert_equal("A Child at Left", @root.firstChild.name, "The first child should now be the left child")
124
- assert_nil(@root.lastChild, "The first child is now nil")
125
- end
111
+ def test_rightChild_equals
112
+ @root << @left_child1
113
+ @root << @right_child1
114
+ assert_same(@right_child1, @root.rightChild, "The right child should be 'right_child1")
115
+
116
+ @root.rightChild = Tree::BinaryTreeNode.new("New Right Child")
117
+ assert_equal("New Right Child", @root.rightChild.name, "The right child should now be the new child")
118
+ assert_equal("A Child at Left", @root.firstChild.name, "The first child should now be the left child")
119
+ assert_equal("New Right Child", @root.lastChild.name, "The last child should now be the right child")
120
+
121
+ # Now set the right child as nil, and retest
122
+ @root.rightChild = nil
123
+ assert_nil(@root.rightChild, "The right child should now be nil")
124
+ assert_equal("A Child at Left", @root.firstChild.name, "The first child should now be the left child")
125
+ assert_nil(@root.lastChild, "The first child is now nil")
126
+ end
126
127
 
127
- def test_isLeftChild?
128
- @root << @left_child1
129
- @root << @right_child1
128
+ def test_isLeftChild_eh
129
+ @root << @left_child1
130
+ @root << @right_child1
130
131
 
131
- assert(@left_child1.isLeftChild?, "left_child1 should be the left child")
132
- assert(!@right_child1.isLeftChild?, "left_child1 should be the left child")
132
+ assert(@left_child1.isLeftChild?, "left_child1 should be the left child")
133
+ assert(!@right_child1.isLeftChild?, "left_child1 should be the left child")
133
134
 
134
- # Now set the right child as nil, and retest
135
- @root.rightChild = nil
136
- assert(@left_child1.isLeftChild?, "left_child1 should be the left child")
135
+ # Now set the right child as nil, and retest
136
+ @root.rightChild = nil
137
+ assert(@left_child1.isLeftChild?, "left_child1 should be the left child")
137
138
 
138
- assert(!@root.isLeftChild?, "Root is neither left child nor right")
139
- end
139
+ assert(!@root.isLeftChild?, "Root is neither left child nor right")
140
+ end
140
141
 
141
- def test_isRightChild?
142
- @root << @left_child1
143
- @root << @right_child1
142
+ def test_isRightChild_eh
143
+ @root << @left_child1
144
+ @root << @right_child1
144
145
 
145
- assert(@right_child1.isRightChild?, "right_child1 should be the right child")
146
- assert(!@left_child1.isRightChild?, "right_child1 should be the right child")
146
+ assert(@right_child1.isRightChild?, "right_child1 should be the right child")
147
+ assert(!@left_child1.isRightChild?, "right_child1 should be the right child")
147
148
 
148
- # Now set the left child as nil, and retest
149
- @root.leftChild = nil
150
- assert(@right_child1.isRightChild?, "right_child1 should be the right child")
151
- assert(!@root.isRightChild?, "Root is neither left child nor right")
152
- end
149
+ # Now set the left child as nil, and retest
150
+ @root.leftChild = nil
151
+ assert(@right_child1.isRightChild?, "right_child1 should be the right child")
152
+ assert(!@root.isRightChild?, "Root is neither left child nor right")
153
+ end
153
154
 
154
- def test_swap_children
155
- @root << @left_child1
156
- @root << @right_child1
155
+ def test_swap_children
156
+ @root << @left_child1
157
+ @root << @right_child1
157
158
 
158
- assert(@right_child1.isRightChild?, "right_child1 should be the right child")
159
- assert(!@left_child1.isRightChild?, "right_child1 should be the right child")
159
+ assert(@right_child1.isRightChild?, "right_child1 should be the right child")
160
+ assert(!@left_child1.isRightChild?, "right_child1 should be the right child")
160
161
 
161
- @root.swap_children
162
- assert(@right_child1.isLeftChild?, "right_child1 should now be the left child")
163
- assert(@left_child1.isRightChild?, "left_child1 should now be the right child")
164
- assert_equal(@right_child1, @root.firstChild, "right_child1 should now be the first child")
165
- assert_equal(@left_child1, @root.lastChild, "left_child1 should now be the last child")
166
- assert_equal(@right_child1, @root[0], "right_child1 should now be the first child")
167
- assert_equal(@left_child1, @root[1], "left_child1 should now be the last child")
162
+ @root.swap_children
168
163
 
164
+ assert(@right_child1.isLeftChild?, "right_child1 should now be the left child")
165
+ assert(@left_child1.isRightChild?, "left_child1 should now be the right child")
166
+ assert_equal(@right_child1, @root.firstChild, "right_child1 should now be the first child")
167
+ assert_equal(@left_child1, @root.lastChild, "left_child1 should now be the last child")
168
+ assert_equal(@right_child1, @root[0], "right_child1 should now be the first child")
169
+ assert_equal(@left_child1, @root[1], "left_child1 should now be the last child")
170
+ end
169
171
  end
170
172
  end
171
173
 
172
174
  # $Log: test_binarytree.rb,v $
175
+ # Revision 1.5 2007/12/22 00:28:59 anupamsg
176
+ # Added more test cases, and enabled ZenTest compatibility.
177
+ #
173
178
  # Revision 1.4 2007/12/18 23:11:29 anupamsg
174
179
  # Minor documentation changes in the binarytree class.
175
180
  #
@@ -2,7 +2,7 @@
2
2
 
3
3
  # testtree.rb
4
4
  #
5
- # $Revision: 1.5 $ by $Author: anupamsg $
5
+ # $Revision: 1.6 $ by $Author: anupamsg $
6
6
  # $Name: $
7
7
  #
8
8
  # Copyright (c) 2006, 2007 Anupam Sengupta
@@ -38,591 +38,652 @@
38
38
  require 'test/unit'
39
39
  require 'tree'
40
40
 
41
- # Test class for the Tree node.
42
- class TC_TreeTest < Test::Unit::TestCase
41
+ module TestTree
42
+ # Test class for the Tree node.
43
+ class TestTreeNode < Test::Unit::TestCase
43
44
 
44
- Person = Struct::new(:First, :last)
45
+ Person = Struct::new(:First, :last)
45
46
 
46
- def setup
47
- @root = Tree::TreeNode.new("ROOT", "Root Node")
47
+ def setup
48
+ @root = Tree::TreeNode.new("ROOT", "Root Node")
48
49
 
49
- @child1 = Tree::TreeNode.new("Child1", "Child Node 1")
50
- @child2 = Tree::TreeNode.new("Child2", "Child Node 2")
51
- @child3 = Tree::TreeNode.new("Child3", "Child Node 3")
52
- @child4 = Tree::TreeNode.new("Child31", "Grand Child 1")
50
+ @child1 = Tree::TreeNode.new("Child1", "Child Node 1")
51
+ @child2 = Tree::TreeNode.new("Child2", "Child Node 2")
52
+ @child3 = Tree::TreeNode.new("Child3", "Child Node 3")
53
+ @child4 = Tree::TreeNode.new("Child31", "Grand Child 1")
53
54
 
54
- end
55
+ end
55
56
 
56
- # Create this structure for the tests
57
- #
58
- # +----------+
59
- # | ROOT |
60
- # +-+--------+
61
- # |
62
- # | +---------------+
63
- # +----+ CHILD1 |
64
- # | +---------------+
65
- # |
66
- # | +---------------+
67
- # +----+ CHILD2 |
68
- # | +---------------+
69
- # |
70
- # | +---------------+ +------------------+
71
- # +----+ CHILD3 +---+ CHILD4 |
72
- # +---------------+ +------------------+
73
- #
74
- def loadChildren
75
- @root << @child1
76
- @root << @child2
77
- @root << @child3 << @child4
78
- end
57
+ # Create this structure for the tests
58
+ #
59
+ # +----------+
60
+ # | ROOT |
61
+ # +-+--------+
62
+ # |
63
+ # | +---------------+
64
+ # +----+ CHILD1 |
65
+ # | +---------------+
66
+ # |
67
+ # | +---------------+
68
+ # +----+ CHILD2 |
69
+ # | +---------------+
70
+ # |
71
+ # | +---------------+ +------------------+
72
+ # +----+ CHILD3 +---+ CHILD4 |
73
+ # +---------------+ +------------------+
74
+ #
75
+ def loadChildren
76
+ @root << @child1
77
+ @root << @child2
78
+ @root << @child3 << @child4
79
+ end
79
80
 
80
- def teardown
81
- @root = nil
82
- end
81
+ def teardown
82
+ @root = nil
83
+ end
83
84
 
84
- def test_root_setup
85
- assert_not_nil(@root, "Root cannot be nil")
86
- assert_nil(@root.parent, "Parent of root node should be nil")
87
- assert_not_nil(@root.name, "Name should not be nil")
88
- assert_equal("ROOT", @root.name, "Name should be 'ROOT'")
89
- assert_equal("Root Node", @root.content, "Content should be 'Root Node'")
90
- assert(@root.isRoot?, "Should identify as root")
91
- assert(!@root.hasChildren?, "Cannot have any children")
92
- assert(@root.hasContent?, "This root should have content")
93
- assert_equal(1, @root.size, "Number of nodes should be one")
94
- assert_nil(@root.siblings, "Root cannot have any children")
95
-
96
- assert_raise(RuntimeError) { Tree::TreeNode.new(nil) }
97
- end
85
+ def test_root_setup
86
+ assert_not_nil(@root, "Root cannot be nil")
87
+ assert_nil(@root.parent, "Parent of root node should be nil")
88
+ assert_not_nil(@root.name, "Name should not be nil")
89
+ assert_equal("ROOT", @root.name, "Name should be 'ROOT'")
90
+ assert_equal("Root Node", @root.content, "Content should be 'Root Node'")
91
+ assert(@root.isRoot?, "Should identify as root")
92
+ assert(!@root.hasChildren?, "Cannot have any children")
93
+ assert(@root.hasContent?, "This root should have content")
94
+ assert_equal(1, @root.size, "Number of nodes should be one")
95
+ assert_nil(@root.siblings, "Root cannot have any children")
96
+
97
+ assert_raise(RuntimeError) { Tree::TreeNode.new(nil) }
98
+ end
98
99
 
99
- def test_root
100
- loadChildren
100
+ def test_root
101
+ loadChildren
101
102
 
102
- assert_same(@root, @root.root, "Root's root is self")
103
- assert_same(@root, @child1.root, "Root should be ROOT")
104
- assert_same(@root, @child4.root, "Root should be ROOT")
105
- end
103
+ assert_same(@root, @root.root, "Root's root is self")
104
+ assert_same(@root, @child1.root, "Root should be ROOT")
105
+ assert_same(@root, @child4.root, "Root should be ROOT")
106
+ end
106
107
 
107
- def test_hasContent
108
- aNode = Tree::TreeNode.new("A Node")
109
- assert_nil(aNode.content, "The node should not have content")
110
- assert(!aNode.hasContent?, "The node should not have content")
108
+ def test_hasContent_eh
109
+ aNode = Tree::TreeNode.new("A Node")
110
+ assert_nil(aNode.content, "The node should not have content")
111
+ assert(!aNode.hasContent?, "The node should not have content")
111
112
 
112
- aNode.content = "Something"
113
- assert_not_nil(aNode.content, "The node should now have content")
114
- assert(aNode.hasContent?, "The node should now have content")
115
- end
113
+ aNode.content = "Something"
114
+ assert_not_nil(aNode.content, "The node should now have content")
115
+ assert(aNode.hasContent?, "The node should now have content")
116
+ end
116
117
 
117
- def test_length
118
- loadChildren
118
+ def test_length
119
+ loadChildren
120
+ assert_equal(@root.size, @root.length, "Length and size methods should return the same result")
121
+ end
119
122
 
120
- assert_equal(@root.size, @root.length, "Length and size methods should return the same result")
121
- end
123
+ def test_spaceship # Test the <=> operator.
124
+ firstNode = Tree::TreeNode.new(1)
125
+ secondNode = Tree::TreeNode.new(2)
122
126
 
123
- def test_comparator
124
- firstNode = Tree::TreeNode.new(1)
125
- secondNode = Tree::TreeNode.new(2)
127
+ assert_equal(firstNode <=> nil, +1)
128
+ assert_equal(firstNode <=> secondNode, -1)
126
129
 
127
- assert_equal(firstNode <=> nil, +1)
128
- assert_equal(firstNode <=> secondNode, -1)
130
+ secondNode = Tree::TreeNode.new(1)
131
+ assert_equal(firstNode <=> secondNode, 0)
129
132
 
130
- secondNode = Tree::TreeNode.new(1)
131
- assert_equal(firstNode <=> secondNode, 0)
133
+ firstNode = Tree::TreeNode.new("ABC")
134
+ secondNode = Tree::TreeNode.new("XYZ")
132
135
 
133
- firstNode = Tree::TreeNode.new("ABC")
134
- secondNode = Tree::TreeNode.new("XYZ")
136
+ assert_equal(firstNode <=> nil, +1)
137
+ assert_equal(firstNode <=> secondNode, -1)
135
138
 
136
- assert_equal(firstNode <=> nil, +1)
137
- assert_equal(firstNode <=> secondNode, -1)
139
+ secondNode = Tree::TreeNode.new("ABC")
140
+ assert_equal(firstNode <=> secondNode, 0)
141
+ end
138
142
 
139
- secondNode = Tree::TreeNode.new("ABC")
140
- assert_equal(firstNode <=> secondNode, 0)
141
- end
143
+ def test_to_s
144
+ aNode = Tree::TreeNode.new("A Node", "Some Content")
142
145
 
143
- def test_to_s
144
- aNode = Tree::TreeNode.new("A Node", "Some Content")
146
+ expectedString = "Node Name: A Node Content: Some Content Parent: <None> Children: 0 Total Nodes: 1"
145
147
 
146
- expectedString = "Node Name: A Node Content: Some Content Parent: <None> Children: 0 Total Nodes: 1"
148
+ assert_equal(expectedString, aNode.to_s, "The string representation should be same")
149
+ end
147
150
 
148
- assert_equal(expectedString, aNode.to_s, "The string representation should be same")
149
- end
151
+ def test_firstSibling
152
+ loadChildren
150
153
 
151
- def test_firstSibling
152
- loadChildren
154
+ assert_same(@root, @root.firstSibling, "Root's first sibling is itself")
155
+ assert_same(@child1, @child1.firstSibling, "Child1's first sibling is itself")
156
+ assert_same(@child1, @child2.firstSibling, "Child2's first sibling should be child1")
157
+ assert_same(@child1, @child3.firstSibling, "Child3's first sibling should be child1")
158
+ assert_not_same(@child1, @child4.firstSibling, "Child4's first sibling is itself")
159
+ end
153
160
 
154
- assert_same(@root, @root.firstSibling, "Root's first sibling is itself")
155
- assert_same(@child1, @child1.firstSibling, "Child1's first sibling is itself")
156
- assert_same(@child1, @child2.firstSibling, "Child2's first sibling should be child1")
157
- assert_same(@child1, @child3.firstSibling, "Child3's first sibling should be child1")
158
- assert_not_same(@child1, @child4.firstSibling, "Child4's first sibling is itself")
159
- end
161
+ def test_isFirstSibling_eh
162
+ loadChildren
160
163
 
161
- def test_isFirstSibling
162
- loadChildren
164
+ assert(@root.isFirstSibling?, "Root's first sibling is itself")
165
+ assert( @child1.isFirstSibling?, "Child1's first sibling is itself")
166
+ assert(!@child2.isFirstSibling?, "Child2 is not the first sibling")
167
+ assert(!@child3.isFirstSibling?, "Child3 is not the first sibling")
168
+ assert( @child4.isFirstSibling?, "Child4's first sibling is itself")
169
+ end
163
170
 
164
- assert(@root.isFirstSibling?, "Root's first sibling is itself")
165
- assert( @child1.isFirstSibling?, "Child1's first sibling is itself")
166
- assert(!@child2.isFirstSibling?, "Child2 is not the first sibling")
167
- assert(!@child3.isFirstSibling?, "Child3 is not the first sibling")
168
- assert( @child4.isFirstSibling?, "Child4's first sibling is itself")
169
- end
171
+ def test_isLastSibling_eh
172
+ loadChildren
173
+
174
+ assert(@root.isLastSibling?, "Root's last sibling is itself")
175
+ assert(!@child1.isLastSibling?, "Child1 is not the last sibling")
176
+ assert(!@child2.isLastSibling?, "Child2 is not the last sibling")
177
+ assert( @child3.isLastSibling?, "Child3's last sibling is itself")
178
+ assert( @child4.isLastSibling?, "Child4's last sibling is itself")
179
+ end
170
180
 
171
- def test_isLastSibling
172
- loadChildren
181
+ def test_lastSibling
182
+ loadChildren
173
183
 
174
- assert(@root.isLastSibling?, "Root's last sibling is itself")
175
- assert(!@child1.isLastSibling?, "Child1 is not the last sibling")
176
- assert(!@child2.isLastSibling?, "Child2 is not the last sibling")
177
- assert( @child3.isLastSibling?, "Child3's last sibling is itself")
178
- assert( @child4.isLastSibling?, "Child4's last sibling is itself")
179
- end
184
+ assert_same(@root, @root.lastSibling, "Root's last sibling is itself")
185
+ assert_same(@child3, @child1.lastSibling, "Child1's last sibling should be child3")
186
+ assert_same(@child3, @child2.lastSibling, "Child2's last sibling should be child3")
187
+ assert_same(@child3, @child3.lastSibling, "Child3's last sibling should be itself")
188
+ assert_not_same(@child3, @child4.lastSibling, "Child4's last sibling is itself")
189
+ end
180
190
 
181
- def test_lastSibling
182
- loadChildren
191
+ def test_siblings
192
+ loadChildren
183
193
 
184
- assert_same(@root, @root.lastSibling, "Root's last sibling is itself")
185
- assert_same(@child3, @child1.lastSibling, "Child1's last sibling should be child3")
186
- assert_same(@child3, @child2.lastSibling, "Child2's last sibling should be child3")
187
- assert_same(@child3, @child3.lastSibling, "Child3's last sibling should be itself")
188
- assert_not_same(@child3, @child4.lastSibling, "Child4's last sibling is itself")
189
- end
194
+ siblings = []
195
+ @child1.siblings { |sibling| siblings << sibling}
196
+ assert_equal(2, siblings.length, "Should have two siblings")
197
+ assert(siblings.include?(@child2), "Should have 2nd child as sibling")
198
+ assert(siblings.include?(@child3), "Should have 3rd child as sibling")
190
199
 
191
- def test_siblings
192
- loadChildren
200
+ siblings.clear
201
+ siblings = @child1.siblings
202
+ assert_equal(2, siblings.length, "Should have two siblings")
193
203
 
194
- siblings = []
195
- @child1.siblings { |sibling| siblings << sibling}
196
- assert_equal(2, siblings.length, "Should have two siblings")
197
- assert(siblings.include?(@child2), "Should have 2nd child as sibling")
198
- assert(siblings.include?(@child3), "Should have 3rd child as sibling")
204
+ siblings.clear
205
+ @child4.siblings {|sibling| siblings << sibling}
206
+ assert(siblings.empty?, "Should not have any children")
199
207
 
200
- siblings.clear
201
- siblings = @child1.siblings
202
- assert_equal(2, siblings.length, "Should have two siblings")
208
+ end
203
209
 
204
- siblings.clear
205
- @child4.siblings {|sibling| siblings << sibling}
206
- assert(siblings.empty?, "Should not have any children")
210
+ def test_isOnlyChild_eh
211
+ loadChildren
207
212
 
208
- end
213
+ assert(!@child1.isOnlyChild?, "Child1 is not the only child")
214
+ assert(!@child2.isOnlyChild?, "Child2 is not the only child")
215
+ assert(!@child3.isOnlyChild?, "Child3 is not the only child")
216
+ assert( @child4.isOnlyChild?, "Child4 is not the only child")
217
+ end
209
218
 
210
- def test_isOnlyChild?
211
- loadChildren
219
+ def test_nextSibling
220
+ loadChildren
212
221
 
213
- assert(!@child1.isOnlyChild?, "Child1 is not the only child")
214
- assert(!@child2.isOnlyChild?, "Child2 is not the only child")
215
- assert(!@child3.isOnlyChild?, "Child3 is not the only child")
216
- assert( @child4.isOnlyChild?, "Child4 is not the only child")
217
- end
222
+ assert_equal(@child2, @child1.nextSibling, "Child1's next sibling is Child2")
223
+ assert_equal(@child3, @child2.nextSibling, "Child2's next sibling is Child3")
224
+ assert_nil(@child3.nextSibling, "Child3 does not have a next sibling")
225
+ assert_nil(@child4.nextSibling, "Child4 does not have a next sibling")
226
+ end
218
227
 
219
- def test_nextSibling
220
- loadChildren
228
+ def test_previousSibling
229
+ loadChildren
221
230
 
222
- assert_equal(@child2, @child1.nextSibling, "Child1's next sibling is Child2")
223
- assert_equal(@child3, @child2.nextSibling, "Child2's next sibling is Child3")
224
- assert_nil(@child3.nextSibling, "Child3 does not have a next sibling")
225
- assert_nil(@child4.nextSibling, "Child4 does not have a next sibling")
226
- end
231
+ assert_nil(@child1.previousSibling, "Child1 does not have previous sibling")
232
+ assert_equal(@child1, @child2.previousSibling, "Child2's previous sibling is Child1")
233
+ assert_equal(@child2, @child3.previousSibling, "Child3's previous sibling is Child2")
234
+ assert_nil(@child4.previousSibling, "Child4 does not have a previous sibling")
235
+ end
227
236
 
228
- def test_previousSibling
229
- loadChildren
237
+ def test_add
238
+ assert(!@root.hasChildren?, "Should not have any children")
230
239
 
231
- assert_nil(@child1.previousSibling, "Child1 does not have previous sibling")
232
- assert_equal(@child1, @child2.previousSibling, "Child2's previous sibling is Child1")
233
- assert_equal(@child2, @child3.previousSibling, "Child3's previous sibling is Child2")
234
- assert_nil(@child4.previousSibling, "Child4 does not have a previous sibling")
235
- end
240
+ @root.add(@child1)
236
241
 
237
- def test_add
238
- assert(!@root.hasChildren?, "Should not have any children")
242
+ @root << @child2
239
243
 
240
- @root.add(@child1)
244
+ assert(@root.hasChildren?, "Should have children")
245
+ assert_equal(3, @root.size, "Should have three nodes")
241
246
 
242
- @root << @child2
247
+ @root << @child3 << @child4
243
248
 
244
- assert(@root.hasChildren?, "Should have children")
245
- assert_equal(3, @root.size, "Should have three nodes")
249
+ assert_equal(5, @root.size, "Should have five nodes")
250
+ assert_equal(2, @child3.size, "Should have two nodes")
246
251
 
247
- @root << @child3 << @child4
252
+ assert_raise(RuntimeError) { @root.add(Tree::TreeNode.new(@child1.name)) }
248
253
 
249
- assert_equal(5, @root.size, "Should have five nodes")
250
- assert_equal(2, @child3.size, "Should have two nodes")
254
+ end
251
255
 
252
- assert_raise(RuntimeError) { @root.add(Tree::TreeNode.new(@child1.name)) }
256
+ def test_remove_bang
257
+ @root << @child1
258
+ @root << @child2
253
259
 
254
- end
260
+ assert(@root.hasChildren?, "Should have children")
261
+ assert_equal(3, @root.size, "Should have three nodes")
255
262
 
256
- def test_remove
257
- @root << @child1
258
- @root << @child2
263
+ @root.remove!(@child1)
264
+ assert_equal(2, @root.size, "Should have two nodes")
265
+ @root.remove!(@child2)
259
266
 
260
- assert(@root.hasChildren?, "Should have children")
261
- assert_equal(3, @root.size, "Should have three nodes")
267
+ assert(!@root.hasChildren?, "Should have no children")
268
+ assert_equal(1, @root.size, "Should have one node")
262
269
 
263
- @root.remove!(@child1)
264
- assert_equal(2, @root.size, "Should have two nodes")
265
- @root.remove!(@child2)
270
+ @root << @child1
271
+ @root << @child2
266
272
 
267
- assert(!@root.hasChildren?, "Should have no children")
268
- assert_equal(1, @root.size, "Should have one node")
273
+ assert(@root.hasChildren?, "Should have children")
274
+ assert_equal(3, @root.size, "Should have three nodes")
269
275
 
270
- @root << @child1
271
- @root << @child2
276
+ @root.removeAll!
272
277
 
273
- assert(@root.hasChildren?, "Should have children")
274
- assert_equal(3, @root.size, "Should have three nodes")
278
+ assert(!@root.hasChildren?, "Should have no children")
279
+ assert_equal(1, @root.size, "Should have one node")
275
280
 
276
- @root.removeAll!
281
+ end
277
282
 
278
- assert(!@root.hasChildren?, "Should have no children")
279
- assert_equal(1, @root.size, "Should have one node")
283
+ def test_removeAll_bang
284
+ loadChildren
285
+ assert(@root.hasChildren?, "Should have children")
286
+ @root.removeAll!
280
287
 
281
- end
288
+ assert(!@root.hasChildren?, "Should have no children")
289
+ assert_equal(1, @root.size, "Should have one node")
290
+ end
282
291
 
283
- def test_removeAll
284
- loadChildren
285
- assert(@root.hasChildren?, "Should have children")
286
- @root.removeAll!
292
+ def test_removeFromParent_bang
293
+ loadChildren
294
+ assert(@root.hasChildren?, "Should have children")
295
+ assert(!@root.isLeaf?, "Root is not a leaf here")
296
+
297
+ child1 = @root[0]
298
+ assert_not_nil(child1, "Child 1 should exist")
299
+ assert_same(@root, child1.root, "Child 1's root should be ROOT")
300
+ assert(@root.include?(child1), "root should have child1")
301
+ child1.removeFromParent!
302
+ assert_same(child1, child1.root, "Child 1's root should be self")
303
+ assert(!@root.include?(child1), "root should not have child1")
304
+
305
+ child1.removeFromParent!
306
+ assert_same(child1, child1.root, "Child 1's root should still be self")
307
+ end
287
308
 
288
- assert(!@root.hasChildren?, "Should have no children")
289
- assert_equal(1, @root.size, "Should have one node")
290
- end
309
+ def test_children
310
+ loadChildren
291
311
 
292
- def test_removeFromParent
293
- loadChildren
294
- assert(@root.hasChildren?, "Should have children")
295
- assert(!@root.isLeaf?, "Root is not a leaf here")
296
-
297
- child1 = @root[0]
298
- assert_not_nil(child1, "Child 1 should exist")
299
- assert_same(@root, child1.root, "Child 1's root should be ROOT")
300
- assert(@root.include?(child1), "root should have child1")
301
- child1.removeFromParent!
302
- assert_same(child1, child1.root, "Child 1's root should be self")
303
- assert(!@root.include?(child1), "root should not have child1")
304
-
305
- child1.removeFromParent!
306
- assert_same(child1, child1.root, "Child 1's root should still be self")
307
- end
312
+ assert(@root.hasChildren?, "Should have children")
313
+ assert_equal(5, @root.size, "Should have four nodes")
314
+ assert(@child3.hasChildren?, "Should have children")
315
+ assert(!@child3.isLeaf?, "Should not be a leaf")
316
+
317
+ children = []
318
+ for child in @root.children
319
+ children << child
320
+ end
308
321
 
309
- def test_children
310
- loadChildren
322
+ assert_equal(3, children.length, "Should have three direct children")
323
+ assert(!children.include?(@root), "Should not have root")
324
+ assert(children.include?(@child1), "Should have child 1")
325
+ assert(children.include?(@child2), "Should have child 2")
326
+ assert(children.include?(@child3), "Should have child 3")
327
+ assert(!children.include?(@child4), "Should not have child 4")
311
328
 
312
- assert(@root.hasChildren?, "Should have children")
313
- assert_equal(5, @root.size, "Should have four nodes")
314
- assert(@child3.hasChildren?, "Should have children")
315
- assert(!@child3.isLeaf?, "Should not be a leaf")
329
+ children.clear
330
+ children = @root.children
331
+ assert_equal(3, children.length, "Should have three children")
316
332
 
317
- children = []
318
- for child in @root.children
319
- children << child
320
333
  end
321
334
 
322
- assert_equal(3, children.length, "Should have three direct children")
323
- assert(!children.include?(@root), "Should not have root")
324
- assert(children.include?(@child1), "Should have child 1")
325
- assert(children.include?(@child2), "Should have child 2")
326
- assert(children.include?(@child3), "Should have child 3")
327
- assert(!children.include?(@child4), "Should not have child 4")
335
+ def test_firstChild
336
+ loadChildren
328
337
 
329
- children.clear
330
- children = @root.children
331
- assert_equal(3, children.length, "Should have three children")
338
+ assert_equal(@child1, @root.firstChild, "Root's first child is Child1")
339
+ assert_nil(@child1.firstChild, "Child1 does not have any children")
340
+ assert_equal(@child4, @child3.firstChild, "Child3's first child is Child4")
332
341
 
333
- end
342
+ end
334
343
 
335
- def test_firstChild
336
- loadChildren
344
+ def test_lastChild
345
+ loadChildren
337
346
 
338
- assert_equal(@child1, @root.firstChild, "Root's first child is Child1")
339
- assert_nil(@child1.firstChild, "Child1 does not have any children")
340
- assert_equal(@child4, @child3.firstChild, "Child3's first child is Child4")
347
+ assert_equal(@child3, @root.lastChild, "Root's last child is Child3")
348
+ assert_nil(@child1.lastChild, "Child1 does not have any children")
349
+ assert_equal(@child4, @child3.lastChild, "Child3's last child is Child4")
341
350
 
342
- end
351
+ end
343
352
 
344
- def test_lastChild
345
- loadChildren
353
+ def test_find
354
+ loadChildren
355
+ foundNode = @root.find { |node| node == @child2}
356
+ assert_same(@child2, foundNode, "The node should be Child 2")
346
357
 
347
- assert_equal(@child3, @root.lastChild, "Root's last child is Child3")
348
- assert_nil(@child1.lastChild, "Child1 does not have any children")
349
- assert_equal(@child4, @child3.lastChild, "Child3's last child is Child4")
358
+ foundNode = @root.find { |node| node == @child4}
359
+ assert_same(@child4, foundNode, "The node should be Child 4")
350
360
 
351
- end
361
+ foundNode = @root.find { |node| node.name == "Child31" }
362
+ assert_same(@child4, foundNode, "The node should be Child 4")
363
+ foundNode = @root.find { |node| node.name == "NOT PRESENT" }
364
+ assert_nil(foundNode, "The node should not be found")
365
+ end
352
366
 
353
- def test_find
354
- loadChildren
355
- foundNode = @root.find { |node| node == @child2}
356
- assert_same(@child2, foundNode, "The node should be Child 2")
367
+ def test_parentage
368
+ loadChildren
357
369
 
358
- foundNode = @root.find { |node| node == @child4}
359
- assert_same(@child4, foundNode, "The node should be Child 4")
370
+ assert_nil(@root.parentage, "Root does not have any parentage")
371
+ assert_equal([@root], @child1.parentage, "Child1 has Root as its parent")
372
+ assert_equal([@child3, @root], @child4.parentage, "Child4 has Child3 and Root as ancestors")
373
+ end
360
374
 
361
- foundNode = @root.find { |node| node.name == "Child31" }
362
- assert_same(@child4, foundNode, "The node should be Child 4")
363
- foundNode = @root.find { |node| node.name == "NOT PRESENT" }
364
- assert_nil(foundNode, "The node should not be found")
365
- end
375
+ def test_each
376
+ loadChildren
377
+ assert(@root.hasChildren?, "Should have children")
378
+ assert_equal(5, @root.size, "Should have five nodes")
379
+ assert(@child3.hasChildren?, "Should have children")
380
+
381
+ nodes = []
382
+ @root.each { |node| nodes << node }
383
+
384
+ assert_equal(5, nodes.length, "Should have FIVE NODES")
385
+ assert(nodes.include?(@root), "Should have root")
386
+ assert(nodes.include?(@child1), "Should have child 1")
387
+ assert(nodes.include?(@child2), "Should have child 2")
388
+ assert(nodes.include?(@child3), "Should have child 3")
389
+ assert(nodes.include?(@child4), "Should have child 4")
390
+ end
366
391
 
367
- def test_parentage
368
- loadChildren
392
+ def test_each_leaf
393
+ loadChildren
369
394
 
370
- assert_nil(@root.parentage, "Root does not have any parentage")
371
- assert_equal([@root], @child1.parentage, "Child1 has Root as its parent")
372
- assert_equal([@child3, @root], @child4.parentage, "Child4 has Child3 and Root as ancestors")
373
- end
395
+ nodes = []
396
+ @root.each_leaf { |node| nodes << node }
374
397
 
375
- def test_each
376
- loadChildren
377
- assert(@root.hasChildren?, "Should have children")
378
- assert_equal(5, @root.size, "Should have five nodes")
379
- assert(@child3.hasChildren?, "Should have children")
380
-
381
- nodes = []
382
- @root.each { |node| nodes << node }
383
-
384
- assert_equal(5, nodes.length, "Should have FIVE NODES")
385
- assert(nodes.include?(@root), "Should have root")
386
- assert(nodes.include?(@child1), "Should have child 1")
387
- assert(nodes.include?(@child2), "Should have child 2")
388
- assert(nodes.include?(@child3), "Should have child 3")
389
- assert(nodes.include?(@child4), "Should have child 4")
390
- end
398
+ assert_equal(3, nodes.length, "Should have THREE LEAF NODES")
399
+ assert(!nodes.include?(@root), "Should not have root")
400
+ assert(nodes.include?(@child1), "Should have child 1")
401
+ assert(nodes.include?(@child2), "Should have child 2")
402
+ assert(!nodes.include?(@child3), "Should not have child 3")
403
+ assert(nodes.include?(@child4), "Should have child 4")
404
+ end
391
405
 
392
- def test_each_leaf
393
- loadChildren
406
+ def test_parent
407
+ loadChildren
408
+ assert_nil(@root.parent, "Root's parent should be nil")
409
+ assert_equal(@root, @child1.parent, "Parent should be root")
410
+ assert_equal(@root, @child3.parent, "Parent should be root")
411
+ assert_equal(@child3, @child4.parent, "Parent should be child3")
412
+ assert_equal(@root, @child4.parent.parent, "Parent should be root")
413
+ end
394
414
 
395
- nodes = []
396
- @root.each_leaf { |node| nodes << node }
415
+ def test_indexed_access
416
+ loadChildren
417
+ assert_equal(@child1, @root[0], "Should be the first child")
418
+ assert_equal(@child4, @root[2][0], "Should be the grandchild")
419
+ assert_nil(@root["TEST"], "Should be nil")
420
+ assert_raise(RuntimeError) { @root[nil] }
421
+ end
397
422
 
398
- assert_equal(3, nodes.length, "Should have THREE LEAF NODES")
399
- assert(!nodes.include?(@root), "Should not have root")
400
- assert(nodes.include?(@child1), "Should have child 1")
401
- assert(nodes.include?(@child2), "Should have child 2")
402
- assert(!nodes.include?(@child3), "Should not have child 3")
403
- assert(nodes.include?(@child4), "Should have child 4")
404
- end
423
+ def test_printTree
424
+ loadChildren
425
+ #puts
426
+ #@root.printTree
427
+ end
405
428
 
406
- def test_parent
407
- loadChildren
408
- assert_nil(@root.parent, "Root's parent should be nil")
409
- assert_equal(@root, @child1.parent, "Parent should be root")
410
- assert_equal(@root, @child3.parent, "Parent should be root")
411
- assert_equal(@child3, @child4.parent, "Parent should be child3")
412
- assert_equal(@root, @child4.parent.parent, "Parent should be root")
413
- end
429
+ # Tests the binary dumping mechanism with an Object content node
430
+ def test_marshal_dump
431
+ # Setup Test Data
432
+ test_root = Tree::TreeNode.new("ROOT", "Root Node")
433
+ test_content = {"KEY1" => "Value1", "KEY2" => "Value2" }
434
+ test_child = Tree::TreeNode.new("Child", test_content)
435
+ test_content2 = ["AValue1", "AValue2", "AValue3"]
436
+ test_grand_child = Tree::TreeNode.new("Grand Child 1", test_content2)
437
+ test_root << test_child << test_grand_child
438
+
439
+ # Perform the test operation
440
+ data = Marshal.dump(test_root) # Marshal
441
+ new_root = Marshal.load(data) # And unmarshal
442
+
443
+ # Test the root node
444
+ assert_equal(test_root.name, new_root.name, "Must identify as ROOT")
445
+ assert_equal(test_root.content, new_root.content, "Must have root's content")
446
+ assert(new_root.isRoot?, "Must be the ROOT node")
447
+ assert(new_root.hasChildren?, "Must have a child node")
448
+
449
+ # Test the child node
450
+ new_child = new_root[test_child.name]
451
+ assert_equal(test_child.name, new_child.name, "Must have child 1")
452
+ assert(new_child.hasContent?, "Child must have content")
453
+ assert(new_child.isOnlyChild?, "Child must be the only child")
454
+
455
+ new_child_content = new_child.content
456
+ assert_equal(Hash, new_child_content.class, "Class of child's content should be a hash")
457
+ assert_equal(test_child.content.size, new_child_content.size, "The content should have same size")
458
+
459
+ # Test the grand-child node
460
+ new_grand_child = new_child[test_grand_child.name]
461
+ assert_equal(test_grand_child.name, new_grand_child.name, "Must have grand child")
462
+ assert(new_grand_child.hasContent?, "Grand-child must have content")
463
+ assert(new_grand_child.isOnlyChild?, "Grand-child must be the only child")
464
+
465
+ new_grand_child_content = new_grand_child.content
466
+ assert_equal(Array, new_grand_child_content.class, "Class of grand-child's content should be an Array")
467
+ assert_equal(test_grand_child.content.size, new_grand_child_content.size, "The content should have same size")
468
+ end
414
469
 
415
- def test_indexed_access
416
- loadChildren
417
- assert_equal(@child1, @root[0], "Should be the first child")
418
- assert_equal(@child4, @root[2][0], "Should be the grandchild")
419
- assert_nil(@root["TEST"], "Should be nil")
420
- assert_raise(RuntimeError) { @root[nil] }
421
- end
470
+ # marshal_load and marshal_dump are symmetric methods
471
+ # This alias is for satisfying ZenTest
472
+ alias test_marshal_load test_marshal_dump
473
+
474
+ # Test the collect method from the mixed-in Enumerable functionality.
475
+ def test_collect
476
+ loadChildren
477
+ collectArray = @root.collect do |node|
478
+ node.content = "abc"
479
+ node
480
+ end
481
+ collectArray.each {|node| assert_equal("abc", node.content, "Should be 'abc'")}
482
+ end
422
483
 
423
- def test_printTree
424
- loadChildren
425
- #puts
426
- #@root.printTree
427
- end
484
+ # Test freezing the tree
485
+ def test_freezeTree_bang
486
+ loadChildren
487
+ @root.content = "ABC"
488
+ assert_equal("ABC", @root.content, "Content should be 'ABC'")
489
+ @root.freezeTree!
490
+ assert_raise(TypeError) {@root.content = "123"}
491
+ assert_raise(TypeError) {@root[0].content = "123"}
492
+ end
428
493
 
429
- # Tests the binary dumping mechanism with an Object content node
430
- def test_marshalling
431
- # Setup Test Data
432
- test_root = Tree::TreeNode.new("ROOT", "Root Node")
433
- test_content = {"KEY1" => "Value1", "KEY2" => "Value2" }
434
- test_child = Tree::TreeNode.new("Child", test_content)
435
- test_content2 = ["AValue1", "AValue2", "AValue3"]
436
- test_grand_child = Tree::TreeNode.new("Grand Child 1", test_content2)
437
- test_root << test_child << test_grand_child
438
-
439
- # Perform the test operation
440
- data = Marshal.dump(test_root) # Marshal
441
- new_root = Marshal.load(data) # And unmarshal
442
-
443
- # Test the root node
444
- assert_equal(test_root.name, new_root.name, "Must identify as ROOT")
445
- assert_equal(test_root.content, new_root.content, "Must have root's content")
446
- assert(new_root.isRoot?, "Must be the ROOT node")
447
- assert(new_root.hasChildren?, "Must have a child node")
448
-
449
- # Test the child node
450
- new_child = new_root[test_child.name]
451
- assert_equal(test_child.name, new_child.name, "Must have child 1")
452
- assert(new_child.hasContent?, "Child must have content")
453
- assert(new_child.isOnlyChild?, "Child must be the only child")
454
-
455
- new_child_content = new_child.content
456
- assert_equal(Hash, new_child_content.class, "Class of child's content should be a hash")
457
- assert_equal(test_child.content.size, new_child_content.size, "The content should have same size")
458
-
459
- # Test the grand-child node
460
- new_grand_child = new_child[test_grand_child.name]
461
- assert_equal(test_grand_child.name, new_grand_child.name, "Must have grand child")
462
- assert(new_grand_child.hasContent?, "Grand-child must have content")
463
- assert(new_grand_child.isOnlyChild?, "Grand-child must be the only child")
464
-
465
- new_grand_child_content = new_grand_child.content
466
- assert_equal(Array, new_grand_child_content.class, "Class of grand-child's content should be an Array")
467
- assert_equal(test_grand_child.content.size, new_grand_child_content.size, "The content should have same size")
468
- end
494
+ # Test whether the content is accesible
495
+ def test_content
496
+ pers = Person::new("John", "Doe")
497
+ @root.content = pers
498
+ assert_same(pers, @root.content, "Content should be the same")
499
+ end
500
+
501
+ # Test the depth computation algorithm
502
+ def test_depth
503
+ assert_equal(1, @root.depth, "A single node's depth is 1")
469
504
 
470
- # Test the collect method from the mixed-in Enumerable functionality.
471
- def test_collect
472
- loadChildren
473
- collectArray = @root.collect do |node|
474
- node.content = "abc"
475
- node
505
+ @root << @child1
506
+ assert_equal(2, @root.depth, "This should be of depth 2")
507
+
508
+ @root << @child2
509
+ assert_equal(2, @root.depth, "This should be of depth 2")
510
+
511
+ @child2 << @child3
512
+ assert_equal(3, @root.depth, "This should be of depth 3")
513
+ assert_equal(2, @child2.depth, "This should be of depth 2")
514
+
515
+ @child3 << @child4
516
+ assert_equal(4, @root.depth, "This should be of depth 4")
476
517
  end
477
- collectArray.each {|node| assert_equal("abc", node.content, "Should be 'abc'")}
478
- end
479
518
 
480
- # Test freezing the tree
481
- def test_freezeTree
482
- loadChildren
483
- @root.content = "ABC"
484
- assert_equal("ABC", @root.content, "Content should be 'ABC'")
485
- @root.freezeTree!
486
- assert_raise(TypeError) {@root.content = "123"}
487
- assert_raise(TypeError) {@root[0].content = "123"}
488
- end
519
+ # Test the breadth computation algorithm
520
+ def test_breadth
521
+ assert_equal(1, @root.breadth, "A single node's breadth is 1")
489
522
 
490
- # Test whether the content is accesible
491
- def test_content
492
- pers = Person::new("John", "Doe")
493
- @root.content = pers
494
- assert_same(pers, @root.content, "Content should be the same")
495
- end
523
+ @root << @child1
524
+ assert_equal(1, @root.breadth, "This should be of breadth 1")
496
525
 
497
- # Test the depth computation algorithm
498
- def test_depth
499
- assert_equal(1, @root.depth, "A single node's depth is 1")
526
+ @root << @child2
527
+ assert_equal(2, @child1.breadth, "This should be of breadth 2")
528
+ assert_equal(2, @child2.breadth, "This should be of breadth 2")
500
529
 
501
- @root << @child1
502
- assert_equal(2, @root.depth, "This should be of depth 2")
530
+ @root << @child3
531
+ assert_equal(3, @child1.breadth, "This should be of breadth 3")
532
+ assert_equal(3, @child2.breadth, "This should be of breadth 3")
503
533
 
504
- @root << @child2
505
- assert_equal(2, @root.depth, "This should be of depth 2")
534
+ @child3 << @child4
535
+ assert_equal(1, @child4.breadth, "This should be of breadth 1")
536
+ end
506
537
 
507
- @child2 << @child3
508
- assert_equal(3, @root.depth, "This should be of depth 3")
509
- assert_equal(2, @child2.depth, "This should be of depth 2")
538
+ # Test the breadth for each
539
+ def test_breadth_each
540
+ j = Tree::TreeNode.new("j")
541
+ f = Tree::TreeNode.new("f")
542
+ k = Tree::TreeNode.new("k")
543
+ a = Tree::TreeNode.new("a")
544
+ d = Tree::TreeNode.new("d")
545
+ h = Tree::TreeNode.new("h")
546
+ z = Tree::TreeNode.new("z")
547
+
548
+ # The expected order of response
549
+ expected_array = [j,
550
+ f, k,
551
+ a, h, z,
552
+ d]
553
+
554
+ # Create the following Tree
555
+ # j <-- level 0 (Root)
556
+ # / \
557
+ # f k <-- level 1
558
+ # / \ \
559
+ # a h z <-- level 2
560
+ # \
561
+ # d <-- level 3
562
+ j << f << a << d
563
+ f << h
564
+ j << k << z
565
+
566
+ # Create the response
567
+ result_array = Array.new
568
+ j.breadth_each { |node| result_array << node.detached_copy }
569
+
570
+ expected_array.each_index do |i|
571
+ assert_equal(expected_array[i].name, result_array[i].name) # Match only the names.
572
+ end
573
+ end
510
574
 
511
- @child3 << @child4
512
- assert_equal(4, @root.depth, "This should be of depth 4")
513
- end
514
575
 
515
- # Test the breadth computation algorithm
516
- def test_breadth
517
- assert_equal(1, @root.breadth, "A single node's breadth is 1")
576
+ def test_preordered_each
577
+ j = Tree::TreeNode.new("j")
578
+ f = Tree::TreeNode.new("f")
579
+ k = Tree::TreeNode.new("k")
580
+ a = Tree::TreeNode.new("a")
581
+ d = Tree::TreeNode.new("d")
582
+ h = Tree::TreeNode.new("h")
583
+ z = Tree::TreeNode.new("z")
584
+
585
+ # The expected order of response
586
+ expected_array = [j, f, a, d, h, k, z]
587
+
588
+ # Create the following Tree
589
+ # j <-- level 0 (Root)
590
+ # / \
591
+ # f k <-- level 1
592
+ # / \ \
593
+ # a h z <-- level 2
594
+ # \
595
+ # d <-- level 3
596
+ j << f << a << d
597
+ f << h
598
+ j << k << z
599
+
600
+ result_array = []
601
+ j.preordered_each { |node| result_array << node.detached_copy}
602
+
603
+ expected_array.each_index do |i|
604
+ # Match only the names.
605
+ assert_equal(expected_array[i].name, result_array[i].name)
606
+ end
607
+ end
518
608
 
519
- @root << @child1
520
- assert_equal(1, @root.breadth, "This should be of breadth 1")
609
+ def test_detached_copy
610
+ loadChildren
521
611
 
522
- @root << @child2
523
- assert_equal(2, @child1.breadth, "This should be of breadth 2")
524
- assert_equal(2, @child2.breadth, "This should be of breadth 2")
612
+ assert(@root.hasChildren?, "The root should have children")
613
+ copy_of_root = @root.detached_copy
614
+ assert(!copy_of_root.hasChildren?, "The copy should not have children")
615
+ assert_equal(@root.name, copy_of_root.name, "The names should be equal")
525
616
 
526
- @root << @child3
527
- assert_equal(3, @child1.breadth, "This should be of breadth 3")
528
- assert_equal(3, @child2.breadth, "This should be of breadth 3")
617
+ # Try the same test with a child node
618
+ assert(!@child3.isRoot?, "Child 3 is not a root")
619
+ assert(@child3.hasChildren?, "Child 3 has children")
620
+ copy_of_child3 = @child3.detached_copy
621
+ assert(copy_of_child3.isRoot?, "Child 3's copy is a root")
622
+ assert(!copy_of_child3.hasChildren?, "Child 3's copy does not have children")
623
+ end
529
624
 
530
- @child3 << @child4
531
- assert_equal(1, @child4.breadth, "This should be of breadth 1")
532
- end
625
+ def test_hasChildren_eh
626
+ loadChildren
627
+ assert(@root.hasChildren?, "The Root node MUST have children")
628
+ end
533
629
 
534
- # Test the breadth for each
535
- def test_breadth_each
536
- j = Tree::TreeNode.new("j")
537
- f = Tree::TreeNode.new("f")
538
- k = Tree::TreeNode.new("k")
539
- a = Tree::TreeNode.new("a")
540
- d = Tree::TreeNode.new("d")
541
- h = Tree::TreeNode.new("h")
542
- z = Tree::TreeNode.new("z")
543
-
544
- # The expected order of response
545
- expected_array = [j,
546
- f, k,
547
- a, h, z,
548
- d]
549
-
550
- # Create the following Tree
551
- # j <-- level 0 (Root)
552
- # / \
553
- # f k <-- level 1
554
- # / \ \
555
- # a h z <-- level 2
556
- # \
557
- # d <-- level 3
558
- j << f << a << d
559
- f << h
560
- j << k << z
561
-
562
- # Create the response
563
- result_array = Array.new
564
- j.breadth_each { |node| result_array << node.detached_copy }
565
-
566
- expected_array.each_index do |i|
567
- assert_equal(expected_array[i].name, result_array[i].name) # Match only the names.
630
+ def test_isLeaf_eh
631
+ loadChildren
632
+ assert(!@child3.isLeaf?, "Child 3 is not a leaf node")
633
+ assert(@child4.isLeaf?, "Child 4 is a leaf node")
568
634
  end
569
- end
570
635
 
636
+ def test_isRoot_eh
637
+ loadChildren
638
+ assert(@root.isRoot?, "The ROOT node must respond as the root node")
639
+ end
571
640
 
572
- def test_preordered_each
573
- j = Tree::TreeNode.new("j")
574
- f = Tree::TreeNode.new("f")
575
- k = Tree::TreeNode.new("k")
576
- a = Tree::TreeNode.new("a")
577
- d = Tree::TreeNode.new("d")
578
- h = Tree::TreeNode.new("h")
579
- z = Tree::TreeNode.new("z")
580
-
581
- # The expected order of response
582
- expected_array = [j, f, a, d, h, k, z]
583
-
584
- # Create the following Tree
585
- # j <-- level 0 (Root)
586
- # / \
587
- # f k <-- level 1
588
- # / \ \
589
- # a h z <-- level 2
590
- # \
591
- # d <-- level 3
592
- j << f << a << d
593
- f << h
594
- j << k << z
595
-
596
- result_array = []
597
- j.preordered_each { |node| result_array << node.detached_copy}
598
-
599
- expected_array.each_index do |i|
600
- # Match only the names.
601
- assert_equal(expected_array[i].name, result_array[i].name)
641
+ def test_content_equals
642
+ @root.content = nil
643
+ assert_nil(@root.content, "Root's content should be nil")
644
+ @root.content = "ABCD"
645
+ assert_equal("ABCD", @root.content, "Root's content should now be 'ABCD'")
602
646
  end
603
- end
604
647
 
605
- def test_detached_copy
606
- loadChildren
648
+ def test_size
649
+ assert_equal(1, @root.size, "Root's size should be 1")
650
+ loadChildren
651
+ assert_equal(5, @root.size, "Root's size should be 5")
652
+ assert_equal(2, @child3.size, "Child 3's size should be 2")
653
+ end
607
654
 
608
- assert(@root.hasChildren?, "The root should have children")
609
- copy_of_root = @root.detached_copy
610
- assert(!copy_of_root.hasChildren?, "The copy should not have children")
611
- assert_equal(@root.name, copy_of_root.name, "The names should be equal")
655
+ def test_lt2 # Test the << method
656
+ @root << @child1
657
+ @root << @child2
658
+ @root << @child3 << @child4
659
+ assert_not_nil(@root['Child1'], "Child 1 should have been added to Root")
660
+ assert_not_nil(@root['Child2'], "Child 2 should have been added to Root")
661
+ assert_not_nil(@root['Child3'], "Child 3 should have been added to Root")
662
+ assert_not_nil(@child3['Child31'], "Child 31 should have been added to Child3")
663
+ end
612
664
 
613
- # Try the same test with a child node
614
- assert(!@child3.isRoot?, "Child 3 is not a root")
615
- assert(@child3.hasChildren?, "Child 3 has children")
616
- copy_of_child3 = @child3.detached_copy
617
- assert(copy_of_child3.isRoot?, "Child 3's copy is a root")
618
- assert(!copy_of_child3.hasChildren?, "Child 3's copy does not have children")
619
- end
665
+ def test_index # Test the [] method
666
+ assert_raise(RuntimeError) {@root[nil]}
667
+
668
+ @root << @child1
669
+ @root << @child2
670
+ assert_equal(@child1.name, @root['Child1'].name, "Child 1 should be returned")
671
+ assert_equal(@child1.name, @root[0].name, "Child 1 should be returned")
672
+ assert_equal(@child2.name, @root['Child2'].name, "Child 2 should be returned")
673
+ assert_equal(@child2.name, @root[1].name, "Child 2 should be returned")
620
674
 
675
+ assert_nil(@root['Some Random Name'], "Should return nil")
676
+ assert_nil(@root[99], "Should return nil")
677
+ end
678
+ end
621
679
  end
622
680
 
623
681
  __END__
624
682
 
625
683
  # $Log: test_tree.rb,v $
684
+ # Revision 1.6 2007/12/22 00:28:59 anupamsg
685
+ # Added more test cases, and enabled ZenTest compatibility.
686
+ #
626
687
  # Revision 1.5 2007/12/19 02:24:18 anupamsg
627
688
  # Updated the marshalling logic to handle non-string contents on the nodes.
628
689
  #