rubytree 0.7.0 → 0.8.1

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.
@@ -73,8 +73,8 @@ module Tree
73
73
  #
74
74
  # @return [Tree::BinaryTreeNode] The left most (or first) child.
75
75
  #
76
- # @see #rightChild
77
- def leftChild
76
+ # @see #right_child
77
+ def left_child
78
78
  children.first
79
79
  end
80
80
 
@@ -84,22 +84,39 @@ module Tree
84
84
  #
85
85
  # @return [Tree::BinaryTreeNode] The right child, or +nil+ if the right side child does not exist.
86
86
  #
87
- # @see #leftChild
88
- def rightChild
87
+ # @see #left_child
88
+ def right_child
89
89
  children[1]
90
90
  end
91
91
 
92
+ # A protected method to allow insertion of child nodes at the specified location.
93
+ # Note that this method allows insertion of +nil+ nodes.
94
+ #
95
+ # @param [Tree::BinaryTreeNode] child The child to add at the specified location.
96
+ # @param [Integer] at_index The location to add the entry at (0 or 1).
97
+ #
98
+ # @return [Tree::BinaryTreeNode] The added child.
99
+ #
100
+ # @raise [ArgumentError] If the index is out of limits.
101
+ def set_child_at(child, at_index)
102
+ raise ArgumentError "A binary tree cannot have more than two children." unless (0..1).include? at_index
103
+
104
+ @children[at_index] = child
105
+ @children_hash[child.name] = child if child # Assign the name mapping
106
+ child.parent = self if child
107
+ child
108
+ end
109
+
92
110
  # Sets the left child of the receiver node. If a previous child existed, it is replaced.
93
111
  #
94
112
  # @param [Tree::BinaryTreeNode] child The child to add as the left-side node.
95
113
  #
96
114
  # @return [Tree::BinaryTreeNode] The assigned child node.
97
115
  #
98
- # @see #leftChild
99
- # @see #rightChild=
100
- def leftChild=(child)
101
- @children[0] = child
102
- @childrenHash[child.name] = child if child # Assign the name mapping
116
+ # @see #left_child
117
+ # @see #right_child=
118
+ def left_child=(child)
119
+ set_child_at child, 0
103
120
  end
104
121
 
105
122
  # Sets the right child of the receiver node. If a previous child existed, it is replaced.
@@ -108,37 +125,39 @@ module Tree
108
125
  #
109
126
  # @return [Tree::BinaryTreeNode] The assigned child node.
110
127
  #
111
- # @see #rightChild
112
- # @see #leftChild=
113
- def rightChild=(child)
114
- @children[1] = child
115
- @childrenHash[child.name] = child if child # Assign the name mapping
128
+ # @see #right_child
129
+ # @see #left_child=
130
+ def right_child=(child)
131
+ set_child_at child, 1
116
132
  end
117
133
 
118
134
  # Returns +true+ if the receiver node is the left child of its parent.
119
135
  # Always returns +false+ if it is a root node.
120
136
  #
121
137
  # @return [Boolean] +true+ if this is the left child of its parent.
122
- def isLeftChild?
123
- return false if isRoot?
124
- self == parent.leftChild
138
+ def is_left_child?
139
+ return false if is_root?
140
+ self == parent.left_child
125
141
  end
126
142
 
127
143
  # Returns +true+ if the receiver node is the right child of its parent.
128
144
  # Always returns +false+ if it is a root node.
129
145
  #
130
146
  # @return [Boolean] +true+ if this is the right child of its parent.
131
- def isRightChild?
132
- return false if isRoot?
133
- self == parent.rightChild
147
+ def is_right_child?
148
+ return false if is_root?
149
+ self == parent.right_child
134
150
  end
135
151
 
136
152
  # Swaps the left and right child nodes of the receiver node with each other.
137
153
  #
138
154
  # @todo Define the return value.
139
155
  def swap_children
140
- self.leftChild, self.rightChild = self.rightChild, self.leftChild
156
+ self.left_child, self.right_child = self.right_child, self.left_child
141
157
  end
158
+
159
+ protected :set_child_at
160
+
142
161
  end
143
162
 
144
163
  end
@@ -47,7 +47,6 @@ module TestTree
47
47
 
48
48
  @left_child1 = Tree::BinaryTreeNode.new("A Child at Left", "Child Node @ left")
49
49
  @right_child1 = Tree::BinaryTreeNode.new("B Child at Right", "Child Node @ right")
50
-
51
50
  end
52
51
 
53
52
  # Tear down the test data scaffolding.
@@ -60,17 +59,24 @@ module TestTree
60
59
  # Test initialization of the binary tree.
61
60
  def test_initialize
62
61
  assert_not_nil(@root, "Binary tree's Root should have been created")
62
+ assert_nil(@root.left_child, "The initial left child of root should be nil")
63
+ assert_nil(@root.right_child, "The initial right child of root should be nil")
64
+ assert_equal(@root.children.size, 0, "Initially no children should be present")
63
65
  end
64
66
 
65
67
  # Test the add method.
66
68
  def test_add
67
- @root.add @left_child1
68
- assert_same(@left_child1, @root.leftChild, "The left node should be left_child1")
69
- assert_same(@left_child1, @root.firstChild, "The first node should be left_child1")
69
+ @root.add @left_child1
70
+ assert(!@left_child1.is_root?, "Left child1 cannot be a root after addition to the ROOT node")
71
+
72
+ assert_same(@left_child1, @root.left_child, "The left node should be left_child1")
73
+ assert_same(@left_child1, @root.first_child, "The first node should be left_child1")
70
74
 
71
75
  @root.add @right_child1
72
- assert_same(@right_child1, @root.rightChild, "The right node should be right_child1")
73
- assert_same(@right_child1, @root.lastChild, "The first node should be right_child1")
76
+ assert(!@right_child1.is_root?, "Right child1 cannot be a root after addition to the ROOT node")
77
+
78
+ assert_same(@right_child1, @root.right_child, "The right node should be right_child1")
79
+ assert_same(@right_child1, @root.last_child, "The first node should be right_child1")
74
80
 
75
81
  assert_raise ArgumentError do
76
82
  @root.add Tree::BinaryTreeNode.new("The third child!")
@@ -81,84 +87,88 @@ module TestTree
81
87
  end
82
88
  end
83
89
 
84
- # Test the leftChild method.
85
- def test_leftChild
90
+ # Test the left_child method.
91
+ def test_left_child
86
92
  @root << @left_child1
87
93
  @root << @right_child1
88
- assert_same(@left_child1, @root.leftChild, "The left child should be 'left_child1")
89
- assert_not_same(@right_child1, @root.leftChild, "The right_child1 is not the left child")
94
+ assert_same(@left_child1, @root.left_child, "The left child should be 'left_child1")
95
+ assert_not_same(@right_child1, @root.left_child, "The right_child1 is not the left child")
90
96
  end
91
97
 
92
- # Test the rightChild method.
93
- def test_rightChild
98
+ # Test the right_child method.
99
+ def test_right_child
94
100
  @root << @left_child1
95
101
  @root << @right_child1
96
- assert_same(@right_child1, @root.rightChild, "The right child should be 'right_child1")
97
- assert_not_same(@left_child1, @root.rightChild, "The left_child1 is not the left child")
102
+ assert_same(@right_child1, @root.right_child, "The right child should be 'right_child1")
103
+ assert_not_same(@left_child1, @root.right_child, "The left_child1 is not the left child")
98
104
  end
99
105
 
100
- # Test leftChild= method.
101
- def test_leftChild_equals
106
+ # Test left_child= method.
107
+ def test_left_child_equals
102
108
  @root << @left_child1
103
109
  @root << @right_child1
104
- assert_same(@left_child1, @root.leftChild, "The left child should be 'left_child1")
110
+ assert_same(@left_child1, @root.left_child, "The left child should be 'left_child1")
111
+ assert(!@left_child1.is_root?, "The left child now cannot be a root.")
105
112
 
106
- @root.leftChild = Tree::BinaryTreeNode.new("New Left Child")
107
- assert_equal("New Left Child", @root.leftChild.name, "The left child should now be the new child")
108
- assert_equal("B Child at Right", @root.lastChild.name, "The last child should now be the right child")
113
+ @root.left_child = Tree::BinaryTreeNode.new("New Left Child")
114
+ assert(!@root.left_child.is_root?, "The left child now cannot be a root.")
115
+ assert_equal("New Left Child", @root.left_child.name, "The left child should now be the new child")
116
+ assert_equal("B Child at Right", @root.last_child.name, "The last child should now be the right child")
109
117
 
110
118
  # Now set the left child as nil, and retest
111
- @root.leftChild = nil
112
- assert_nil(@root.leftChild, "The left child should now be nil")
113
- assert_nil(@root.firstChild, "The first child is now nil")
114
- assert_equal("B Child at Right", @root.lastChild.name, "The last child should now be the right child")
119
+ @root.left_child = nil
120
+ assert_nil(@root.left_child, "The left child should now be nil")
121
+ assert_nil(@root.first_child, "The first child is now nil")
122
+ assert_equal("B Child at Right", @root.last_child.name, "The last child should now be the right child")
115
123
  end
116
124
 
117
- # Test rightChild= method.
118
- def test_rightChild_equals
125
+ # Test right_child= method.
126
+ def test_right_child_equals
119
127
  @root << @left_child1
120
128
  @root << @right_child1
121
- assert_same(@right_child1, @root.rightChild, "The right child should be 'right_child1")
129
+ assert_same(@right_child1, @root.right_child, "The right child should be 'right_child1")
130
+ assert(!@right_child1.is_root?, "The right child now cannot be a root.")
122
131
 
123
- @root.rightChild = Tree::BinaryTreeNode.new("New Right Child")
124
- assert_equal("New Right Child", @root.rightChild.name, "The right child should now be the new child")
125
- assert_equal("A Child at Left", @root.firstChild.name, "The first child should now be the left child")
126
- assert_equal("New Right Child", @root.lastChild.name, "The last child should now be the right child")
132
+ @root.right_child = Tree::BinaryTreeNode.new("New Right Child")
133
+ assert(!@root.right_child.is_root?, "The right child now cannot be a root.")
134
+ assert_equal("New Right Child", @root.right_child.name, "The right child should now be the new child")
135
+ assert_equal("A Child at Left", @root.first_child.name, "The first child should now be the left child")
136
+ assert_equal("New Right Child", @root.last_child.name, "The last child should now be the right child")
127
137
 
128
138
  # Now set the right child as nil, and retest
129
- @root.rightChild = nil
130
- assert_nil(@root.rightChild, "The right child should now be nil")
131
- assert_equal("A Child at Left", @root.firstChild.name, "The first child should now be the left child")
132
- assert_nil(@root.lastChild, "The first child is now nil")
139
+ @root.right_child = nil
140
+ assert_nil(@root.right_child, "The right child should now be nil")
141
+ assert_equal("A Child at Left", @root.first_child.name, "The first child should now be the left child")
142
+ assert_nil(@root.last_child, "The first child is now nil")
133
143
  end
134
144
 
135
- # Test isLeftChild? method.
136
- def test_isLeftChild_eh
145
+ # Test isLeft_child? method.
146
+ def test_is_left_child_eh
137
147
  @root << @left_child1
138
148
  @root << @right_child1
139
149
 
140
- assert(@left_child1.isLeftChild?, "left_child1 should be the left child")
141
- assert(!@right_child1.isLeftChild?, "left_child1 should be the left child")
150
+ assert(@left_child1.is_left_child?, "left_child1 should be the left child")
151
+ assert(!@right_child1.is_left_child?, "left_child1 should be the left child")
142
152
 
143
153
  # Now set the right child as nil, and retest
144
- @root.rightChild = nil
145
- assert(@left_child1.isLeftChild?, "left_child1 should be the left child")
154
+ @root.right_child = nil
155
+ assert(@left_child1.is_left_child?, "left_child1 should be the left child")
146
156
 
147
- assert(!@root.isLeftChild?, "Root is neither left child nor right")
157
+ assert(!@root.is_left_child?, "Root is neither left child nor right")
148
158
  end
149
159
 
150
- # Test isRightChild? method.
151
- def test_isRightChild_eh
160
+ # Test is_right_child? method.
161
+ def test_is_right_child_eh
152
162
  @root << @left_child1
153
163
  @root << @right_child1
154
164
 
155
- assert(@right_child1.isRightChild?, "right_child1 should be the right child")
156
- assert(!@left_child1.isRightChild?, "right_child1 should be the right child")
165
+ assert(@right_child1.is_right_child?, "right_child1 should be the right child")
166
+ assert(!@left_child1.is_right_child?, "right_child1 should be the right child")
157
167
 
158
168
  # Now set the left child as nil, and retest
159
- @root.leftChild = nil
160
- assert(@right_child1.isRightChild?, "right_child1 should be the right child")
161
- assert(!@root.isRightChild?, "Root is neither left child nor right")
169
+ @root.left_child = nil
170
+ assert(@right_child1.is_right_child?, "right_child1 should be the right child")
171
+ assert(!@root.is_right_child?, "Root is neither left child nor right")
162
172
  end
163
173
 
164
174
  # Test swap_children method.
@@ -166,17 +176,36 @@ module TestTree
166
176
  @root << @left_child1
167
177
  @root << @right_child1
168
178
 
169
- assert(@right_child1.isRightChild?, "right_child1 should be the right child")
170
- assert(!@left_child1.isRightChild?, "right_child1 should be the right child")
179
+ assert(@right_child1.is_right_child?, "right_child1 should be the right child")
180
+ assert(!@left_child1.is_right_child?, "right_child1 should be the right child")
171
181
 
172
182
  @root.swap_children
173
183
 
174
- assert(@right_child1.isLeftChild?, "right_child1 should now be the left child")
175
- assert(@left_child1.isRightChild?, "left_child1 should now be the right child")
176
- assert_equal(@right_child1, @root.firstChild, "right_child1 should now be the first child")
177
- assert_equal(@left_child1, @root.lastChild, "left_child1 should now be the last child")
184
+ assert(@right_child1.is_left_child?, "right_child1 should now be the left child")
185
+ assert(@left_child1.is_right_child?, "left_child1 should now be the right child")
186
+ assert_equal(@right_child1, @root.first_child, "right_child1 should now be the first child")
187
+ assert_equal(@left_child1, @root.last_child, "left_child1 should now be the last child")
178
188
  assert_equal(@right_child1, @root[0], "right_child1 should now be the first child")
179
189
  assert_equal(@left_child1, @root[1], "left_child1 should now be the last child")
180
190
  end
191
+
192
+ # Test the old CamelCase method names
193
+ def test_old_camelCase_method_names
194
+ @left_child2 = Tree::BinaryTreeNode.new("A Child at Left", "Child Node @ left")
195
+ @right_child2 = Tree::BinaryTreeNode.new("B Child at Right", "Child Node @ right")
196
+
197
+ require 'structured_warnings'
198
+
199
+ meth_names_for_test = %w{leftChild isLeftChild? rightChild isRightChild?}
200
+
201
+ meth_names_for_test.each do |meth_name|
202
+ assert_warn(DeprecatedMethodWarning) {@root.send(meth_name)}
203
+ end
204
+
205
+ assert_warn(DeprecatedMethodWarning) {@root.leftChild = @left_child2}
206
+ assert_warn(DeprecatedMethodWarning) {@root.rightChild = @right_child2}
207
+
208
+ end
209
+
181
210
  end
182
211
  end
data/test/test_tree.rb CHANGED
@@ -71,7 +71,8 @@ module TestTree
71
71
  @child1 = Tree::TreeNode.new("Child1", "Child Node 1")
72
72
  @child2 = Tree::TreeNode.new("Child2", "Child Node 2")
73
73
  @child3 = Tree::TreeNode.new("Child3", "Child Node 3")
74
- @child4 = Tree::TreeNode.new("Child31", "Grand Child 1")
74
+ @child4 = Tree::TreeNode.new("Child4", "Grand Child 1")
75
+ @child5 = Tree::TreeNode.new("Child5", "Child Node 4")
75
76
 
76
77
  end
77
78
 
@@ -89,18 +90,18 @@ module TestTree
89
90
 
90
91
  # This test is for the root alone - without any children being linked
91
92
  def test_root_setup
92
- assert_not_nil(@root , "Root cannot be nil")
93
- assert_nil(@root.parent , "Parent of root node should be nil")
94
- assert_not_nil(@root.name , "Name should not be nil")
95
- assert_equal("ROOT" , @root.name, "Name should be 'ROOT'")
96
- assert_equal("Root Node" , @root.content, "Content should be 'Root Node'")
97
- assert(@root.isRoot? , "Should identify as root")
98
- assert(!@root.hasChildren? , "Cannot have any children")
99
- assert(@root.hasContent? , "This root should have content")
100
- assert_equal(1 , @root.size, "Number of nodes should be one")
101
- assert_nil(@root.siblings , "This root does not have any children")
93
+ assert_not_nil(@root , "Root cannot be nil")
94
+ assert_nil(@root.parent , "Parent of root node should be nil")
95
+ assert_not_nil(@root.name , "Name should not be nil")
96
+ assert_equal("ROOT" , @root.name, "Name should be 'ROOT'")
97
+ assert_equal("Root Node" , @root.content, "Content should be 'Root Node'")
98
+ assert(@root.is_root? , "Should identify as root")
99
+ assert(!@root.has_children? , "Cannot have any children")
100
+ assert(@root.has_content? , "This root should have content")
101
+ assert_equal(1 , @root.size, "Number of nodes should be one")
102
+ assert_nil(@root.siblings , "This root does not have any children")
102
103
  assert_equal(0, @root.in_degree, "Root should have an in-degree of 0")
103
- assert_equal(0, @root.nodeHeight, "Root's height before adding any children is 0")
104
+ assert_equal(0, @root.node_height, "Root's height before adding any children is 0")
104
105
  assert_raise(ArgumentError) { Tree::TreeNode.new(nil) }
105
106
  end
106
107
 
@@ -114,18 +115,18 @@ module TestTree
114
115
  assert_same(@root , @root.root, "Root's root is self")
115
116
  assert_same(@root , @child1.root, "Root should be ROOT")
116
117
  assert_same(@root , @child4.root, "Root should be ROOT")
117
- assert_equal(2 , @root.nodeHeight, "Root's height after adding the children should be 2")
118
+ assert_equal(2 , @root.node_height, "Root's height after adding the children should be 2")
118
119
  end
119
120
 
120
121
  # Test the presence of content in the nodes.
121
- def test_hasContent_eh
122
- aNode = Tree::TreeNode.new("A Node")
123
- assert_nil(aNode.content , "The node should not have content")
124
- assert(!aNode.hasContent? , "The node should not have content")
125
-
126
- aNode.content = "Something"
127
- assert_not_nil(aNode.content, "The node should now have content")
128
- assert(aNode.hasContent?, "The node should now have content")
122
+ def test_has_content_eh
123
+ a_node = Tree::TreeNode.new("A Node")
124
+ assert_nil(a_node.content , "The node should not have content")
125
+ assert(!a_node.has_content? , "The node should not have content")
126
+
127
+ a_node.content = "Something"
128
+ assert_not_nil(a_node.content, "The node should now have content")
129
+ assert(a_node.has_content?, "The node should now have content")
129
130
  end
130
131
 
131
132
  # Test the equivalence of size and length methods.
@@ -136,80 +137,81 @@ module TestTree
136
137
 
137
138
  # Test the <=> operator.
138
139
  def test_spaceship
139
- firstNode = Tree::TreeNode.new(1)
140
- secondNode = Tree::TreeNode.new(2)
140
+ first_node = Tree::TreeNode.new(1)
141
+ second_node = Tree::TreeNode.new(2)
142
+
141
143
 
142
- assert_equal(firstNode <=> nil, +1)
143
- assert_equal(firstNode <=> secondNode, -1)
144
+ assert_equal(first_node <=> nil, +1)
145
+ assert_equal(first_node <=> second_node, -1)
144
146
 
145
- secondNode = Tree::TreeNode.new(1)
146
- assert_equal(firstNode <=> secondNode, 0)
147
+ second_node = Tree::TreeNode.new(1)
148
+ assert_equal(first_node <=> second_node, 0)
147
149
 
148
- firstNode = Tree::TreeNode.new("ABC")
149
- secondNode = Tree::TreeNode.new("XYZ")
150
+ first_node = Tree::TreeNode.new("ABC")
151
+ second_node = Tree::TreeNode.new("XYZ")
150
152
 
151
- assert_equal(firstNode <=> nil, +1)
152
- assert_equal(firstNode <=> secondNode, -1)
153
+ assert_equal(first_node <=> nil, +1)
154
+ assert_equal(first_node <=> second_node, -1)
153
155
 
154
- secondNode = Tree::TreeNode.new("ABC")
155
- assert_equal(firstNode <=> secondNode, 0)
156
+ second_node = Tree::TreeNode.new("ABC")
157
+ assert_equal(first_node <=> second_node, 0)
156
158
  end
157
159
 
158
160
  # Test the to_s method. This is probably a little fragile right now.
159
161
  def test_to_s
160
- aNode = Tree::TreeNode.new("A Node", "Some Content")
162
+ a_node = Tree::TreeNode.new("A Node", "Some Content")
161
163
 
162
- expectedString = "Node Name: A Node Content: Some Content Parent: <None> Children: 0 Total Nodes: 1"
164
+ expected_string = "Node Name: A Node Content: Some Content Parent: <None> Children: 0 Total Nodes: 1"
163
165
 
164
- assert_equal(expectedString, aNode.to_s, "The string representation should be same")
166
+ assert_equal(expected_string, a_node.to_s, "The string representation should be same")
165
167
  end
166
168
 
167
- # Test the firstSibling method.
168
- def test_firstSibling
169
+ # Test the first_sibling method.
170
+ def test_first_sibling
169
171
  setup_test_tree
170
172
 
171
- # TODO: Need to fix the firstSibling method to return nil for nodes with no siblings.
172
- assert_same(@root, @root.firstSibling, "Root's first sibling is itself")
173
- assert_same(@child1, @child1.firstSibling, "Child1's first sibling is itself")
174
- assert_same(@child1, @child2.firstSibling, "Child2's first sibling should be child1")
175
- assert_same(@child1, @child3.firstSibling, "Child3's first sibling should be child1")
176
- assert_not_same(@child1, @child4.firstSibling, "Child4's first sibling is itself")
173
+ # TODO: Need to fix the first_sibling method to return nil for nodes with no siblings.
174
+ assert_same(@root, @root.first_sibling, "Root's first sibling is itself")
175
+ assert_same(@child1, @child1.first_sibling, "Child1's first sibling is itself")
176
+ assert_same(@child1, @child2.first_sibling, "Child2's first sibling should be child1")
177
+ assert_same(@child1, @child3.first_sibling, "Child3's first sibling should be child1")
178
+ assert_not_same(@child1, @child4.first_sibling, "Child4's first sibling is itself")
177
179
  end
178
180
 
179
- # Test the isFirstSibling? method.
180
- def test_isFirstSibling_eh
181
+ # Test the is_first_sibling? method.
182
+ def test_is_first_sibling_eh
181
183
  setup_test_tree
182
184
 
183
- # TODO: Need to fix the firstSibling method to return nil for nodes with no siblings.
184
- assert(@root.isFirstSibling?, "Root's first sibling is itself")
185
- assert( @child1.isFirstSibling?, "Child1's first sibling is itself")
186
- assert(!@child2.isFirstSibling?, "Child2 is not the first sibling")
187
- assert(!@child3.isFirstSibling?, "Child3 is not the first sibling")
188
- assert( @child4.isFirstSibling?, "Child4's first sibling is itself")
185
+ # TODO: Need to fix the first_sibling method to return nil for nodes with no siblings.
186
+ assert(@root.is_first_sibling?, "Root's first sibling is itself")
187
+ assert( @child1.is_first_sibling?, "Child1's first sibling is itself")
188
+ assert(!@child2.is_first_sibling?, "Child2 is not the first sibling")
189
+ assert(!@child3.is_first_sibling?, "Child3 is not the first sibling")
190
+ assert( @child4.is_first_sibling?, "Child4's first sibling is itself")
189
191
  end
190
192
 
191
- # Test the isLastSibling? method.
192
- def test_isLastSibling_eh
193
+ # Test the is_last_sibling? method.
194
+ def test_is_last_sibling_eh
193
195
  setup_test_tree
194
196
 
195
- # TODO: Need to fix the lastSibling method to return nil for nodes with no siblings.
196
- assert(@root.isLastSibling?, "Root's last sibling is itself")
197
- assert(!@child1.isLastSibling?, "Child1 is not the last sibling")
198
- assert(!@child2.isLastSibling?, "Child2 is not the last sibling")
199
- assert( @child3.isLastSibling?, "Child3's last sibling is itself")
200
- assert( @child4.isLastSibling?, "Child4's last sibling is itself")
197
+ # TODO: Need to fix the last_sibling method to return nil for nodes with no siblings.
198
+ assert(@root.is_last_sibling?, "Root's last sibling is itself")
199
+ assert(!@child1.is_last_sibling?, "Child1 is not the last sibling")
200
+ assert(!@child2.is_last_sibling?, "Child2 is not the last sibling")
201
+ assert( @child3.is_last_sibling?, "Child3's last sibling is itself")
202
+ assert( @child4.is_last_sibling?, "Child4's last sibling is itself")
201
203
  end
202
204
 
203
- # Test the lastSibling method.
204
- def test_lastSibling
205
+ # Test the last_sibling method.
206
+ def test_last_sibling
205
207
  setup_test_tree
206
208
 
207
- # TODO: Need to fix the lastSibling method to return nil for nodes with no siblings.
208
- assert_same(@root, @root.lastSibling, "Root's last sibling is itself")
209
- assert_same(@child3, @child1.lastSibling, "Child1's last sibling should be child3")
210
- assert_same(@child3, @child2.lastSibling, "Child2's last sibling should be child3")
211
- assert_same(@child3, @child3.lastSibling, "Child3's last sibling should be itself")
212
- assert_not_same(@child3, @child4.lastSibling, "Child4's last sibling is itself")
209
+ # TODO: Need to fix the last_sibling method to return nil for nodes with no siblings.
210
+ assert_same(@root, @root.last_sibling, "Root's last sibling is itself")
211
+ assert_same(@child3, @child1.last_sibling, "Child1's last sibling should be child3")
212
+ assert_same(@child3, @child2.last_sibling, "Child2's last sibling should be child3")
213
+ assert_same(@child3, @child3.last_sibling, "Child3's last sibling should be itself")
214
+ assert_not_same(@child3, @child4.last_sibling, "Child4's last sibling is itself")
213
215
  end
214
216
 
215
217
  # Test the siblings method, which is essentially an iterator.
@@ -237,49 +239,49 @@ module TestTree
237
239
  assert_nil(siblings, "Root should not have any siblings")
238
240
  end
239
241
 
240
- # Test the isOnlyChild? method.
241
- def test_isOnlyChild_eh
242
+ # Test the is_only_child? method.
243
+ def test_is_only_child_eh
242
244
  setup_test_tree
243
245
 
244
- assert( @root.isOnlyChild? , "Root is an only child")
245
- assert(!@child1.isOnlyChild?, "Child1 is not the only child")
246
- assert(!@child2.isOnlyChild?, "Child2 is not the only child")
247
- assert(!@child3.isOnlyChild?, "Child3 is not the only child")
248
- assert( @child4.isOnlyChild?, "Child4 is an only child")
246
+ assert( @root.is_only_child? , "Root is an only child")
247
+ assert(!@child1.is_only_child?, "Child1 is not the only child")
248
+ assert(!@child2.is_only_child?, "Child2 is not the only child")
249
+ assert(!@child3.is_only_child?, "Child3 is not the only child")
250
+ assert( @child4.is_only_child?, "Child4 is an only child")
249
251
  end
250
252
 
251
- # Test the nextSibling method.
252
- def test_nextSibling
253
+ # Test the next_sibling method.
254
+ def test_next_sibling
253
255
  setup_test_tree
254
256
 
255
- assert_nil(@root.nextSibling, "Root does not have any next sibling")
256
- assert_equal(@child2, @child1.nextSibling, "Child1's next sibling is Child2")
257
- assert_equal(@child3, @child2.nextSibling, "Child2's next sibling is Child3")
258
- assert_nil(@child3.nextSibling, "Child3 does not have a next sibling")
259
- assert_nil(@child4.nextSibling, "Child4 does not have a next sibling")
257
+ assert_nil(@root.next_sibling, "Root does not have any next sibling")
258
+ assert_equal(@child2, @child1.next_sibling, "Child1's next sibling is Child2")
259
+ assert_equal(@child3, @child2.next_sibling, "Child2's next sibling is Child3")
260
+ assert_nil(@child3.next_sibling, "Child3 does not have a next sibling")
261
+ assert_nil(@child4.next_sibling, "Child4 does not have a next sibling")
260
262
  end
261
263
 
262
- # Test the previousSibling method.
263
- def test_previousSibling
264
+ # Test the previous_sibling method.
265
+ def test_previous_sibling
264
266
  setup_test_tree
265
267
 
266
- assert_nil(@root.previousSibling, "Root does not have any previous sibling")
267
- assert_nil(@child1.previousSibling, "Child1 does not have previous sibling")
268
- assert_equal(@child1, @child2.previousSibling, "Child2's previous sibling is Child1")
269
- assert_equal(@child2, @child3.previousSibling, "Child3's previous sibling is Child2")
270
- assert_nil(@child4.previousSibling, "Child4 does not have a previous sibling")
268
+ assert_nil(@root.previous_sibling, "Root does not have any previous sibling")
269
+ assert_nil(@child1.previous_sibling, "Child1 does not have previous sibling")
270
+ assert_equal(@child1, @child2.previous_sibling, "Child2's previous sibling is Child1")
271
+ assert_equal(@child2, @child3.previous_sibling, "Child3's previous sibling is Child2")
272
+ assert_nil(@child4.previous_sibling, "Child4 does not have a previous sibling")
271
273
  end
272
274
 
273
275
  # Test the add method.
274
276
  def test_add
275
- assert(!@root.hasChildren?, "Should not have any children")
277
+ assert(!@root.has_children?, "Should not have any children")
276
278
 
277
279
  assert_equal(1, @root.size, "Should have 1 node (the root)")
278
280
  @root.add(@child1)
279
281
 
280
282
  @root << @child2
281
283
 
282
- assert(@root.hasChildren?, "Should have children")
284
+ assert(@root.has_children?, "Should have children")
283
285
  assert_equal(3, @root.size, "Should have three nodes")
284
286
 
285
287
  @root << @child3 << @child4
@@ -294,64 +296,128 @@ module TestTree
294
296
  assert_raise(ArgumentError) { @root.add(nil) }
295
297
  end
296
298
 
297
- # Test the remove! and removeAll! methods.
299
+ # Test Addition at a specific position
300
+ def test_add_at_specific_position
301
+ assert(!@root.has_children?, "Should not have any children")
302
+
303
+ assert_equal(1, @root.size, "Should have 1 node (the root)")
304
+ @root.add(@child1) # First Child added at position 0
305
+ # Validate that children = [@child1]
306
+ assert_equal(@child1, @root[0])
307
+
308
+ @root << @child2 # Second child appended at position 1.
309
+ # Validate that children = [@child1, @child2]
310
+ assert_equal(@child1, @root[0])
311
+ assert_equal(@child2, @root[1])
312
+ assert_equal(2, @root.children.size, "Should have two child nodes")
313
+
314
+ @root.add(@child3, 1) # Third child inserted at position 1 (before @child2)
315
+ # Validate that children = [@child1, @child3, @child2]
316
+ assert_equal(@child1, @root[0])
317
+ assert_equal(@child3, @root[1])
318
+ assert_equal(@child2, @root[2])
319
+ assert_equal(3, @root.children.size, "Should have three child nodes")
320
+
321
+ @root.add(@child4, @root.children.size) # Fourth child inserted at the end (equivalent to plain #add(child4)
322
+ # Validate that children = [@child1, @child3, @child2, @child4]
323
+ assert_equal(@child1, @root[0])
324
+ assert_equal(@child3, @root[1])
325
+ assert_equal(@child2, @root[2])
326
+ assert_equal(@child4, @root[3])
327
+ assert_equal(4, @root.children.size, "Should have four child nodes")
328
+
329
+ # Now, a negative test. We are preventing addition to a position that does not exist.
330
+ assert_raise(RuntimeError) {
331
+ @root.add(@child5, @root.children.size + 1) # Fifth child inserted beyond the last position that is valid (at 5th pos).
332
+ }
333
+ # Validate that we still have children = [@child1, @child3, @child2, @child4]
334
+ assert_equal(@child1, @root[0])
335
+ assert_equal(@child3, @root[1])
336
+ assert_equal(@child2, @root[2])
337
+ assert_equal(@child4, @root[3])
338
+ assert_nil(@root[4])
339
+ assert_equal(4, @root.children.size, "Should have four child nodes")
340
+
341
+ # Another negative test. Lets attempt to add from the end at a position that is not available
342
+ assert_raise(RuntimeError) {
343
+ @root.add(@child5, -(@root.children.size+2)) # Fifth child inserted beyond the first position that is valid; i.e. at -6
344
+ }
345
+ assert_nil(@root[-5])
346
+ assert_equal(@child1, @root[-4])
347
+ assert_equal(@child3, @root[-3])
348
+ assert_equal(@child2, @root[-2])
349
+ assert_equal(@child4, @root[-1])
350
+ assert_equal(4, @root.children.size, "Should have four child nodes")
351
+
352
+ # Lets correctly add the fifth child from the end to effectively prepend the node.
353
+ @root.add(@child5, -(@root.children.size+1)) # Fifth child inserted beyond the first position; i.e. at -5
354
+ assert_nil(@root[-6])
355
+ assert_equal(@child5, @root[-5])
356
+ assert_equal(@child1, @root[-4])
357
+ assert_equal(@child3, @root[-3])
358
+ assert_equal(@child2, @root[-2])
359
+ assert_equal(@child4, @root[-1])
360
+ assert_equal(5, @root.children.size, "Should have five child nodes")
361
+ end
362
+
363
+ # Test the remove! and remove_all! methods.
298
364
  def test_remove_bang
299
365
  @root << @child1
300
366
  @root << @child2
301
367
 
302
- assert(@root.hasChildren?, "Should have children")
368
+ assert(@root.has_children?, "Should have children")
303
369
  assert_equal(3, @root.size, "Should have three nodes")
304
370
 
305
371
  @root.remove!(@child1)
306
372
  assert_equal(2, @root.size, "Should have two nodes")
307
373
  @root.remove!(@child2)
308
374
 
309
- assert(!@root.hasChildren?, "Should have no children")
375
+ assert(!@root.has_children?, "Should have no children")
310
376
  assert_equal(1, @root.size, "Should have one node")
311
377
 
312
378
  @root << @child1
313
379
  @root << @child2
314
380
 
315
- assert(@root.hasChildren?, "Should have children")
381
+ assert(@root.has_children?, "Should have children")
316
382
  assert_equal(3, @root.size, "Should have three nodes")
317
383
 
318
- @root.removeAll!
384
+ @root.remove_all!
319
385
 
320
- assert(!@root.hasChildren?, "Should have no children")
386
+ assert(!@root.has_children?, "Should have no children")
321
387
  assert_equal(1, @root.size, "Should have one node")
322
388
 
323
389
  # Some negative testing
324
390
  @root.remove!(nil)
325
- assert(!@root.hasChildren?, "Should have no children")
391
+ assert(!@root.has_children?, "Should have no children")
326
392
  assert_equal(1, @root.size, "Should have one node")
327
393
  end
328
394
 
329
- # Test the removeAll! method.
330
- def test_removeAll_bang
395
+ # Test the remove_all! method.
396
+ def test_remove_all_bang
331
397
  setup_test_tree
398
+ assert(@root.has_children?, "Should have children")
399
+ @root.remove_all!
332
400
 
333
- assert(@root.hasChildren?, "Should have children")
334
- @root.removeAll!
335
-
336
- assert(!@root.hasChildren?, "Should have no children")
401
+ assert(!@root.has_children?, "Should have no children")
337
402
  assert_equal(1, @root.size, "Should have one node")
338
403
  end
339
404
 
340
- # Test the removeFromParent! method.
341
- def test_removeFromParent_bang
405
+ # Test the remove_from_parent! method.
406
+ def test_remove_from_parent_bang
342
407
  setup_test_tree
343
- assert(@root.hasChildren?, "Should have children")
344
- assert(!@root.isLeaf?, "Root is not a leaf here")
408
+
409
+ assert(@root.has_children?, "Should have children")
410
+ assert(!@root.is_leaf?, "Root is not a leaf here")
345
411
 
346
412
  child1 = @root[0]
347
413
  assert_not_nil(child1, "Child 1 should exist")
348
414
  assert_same(@root, child1.root, "Child 1's root should be ROOT")
349
415
  assert(@root.include?(child1), "root should have child1")
350
- child1.removeFromParent!
416
+ child1.remove_from_parent!
351
417
  assert_same(child1, child1.root, "Child 1's root should be self")
352
418
  assert(!@root.include?(child1), "root should not have child1")
353
419
 
354
- child1.removeFromParent!
420
+ child1.remove_from_parent!
355
421
  assert_same(child1, child1.root, "Child 1's root should still be self")
356
422
  end
357
423
 
@@ -359,14 +425,14 @@ module TestTree
359
425
  def test_children
360
426
  setup_test_tree
361
427
 
362
- assert(@root.hasChildren?, "Should have children")
428
+ assert(@root.has_children?, "Should have children")
363
429
  assert_equal(5, @root.size, "Should have five nodes")
364
- assert(@child3.hasChildren?, "Should have children")
365
- assert(!@child3.isLeaf?, "Should not be a leaf")
430
+ assert(@child3.has_children?, "Should have children")
431
+ assert(!@child3.is_leaf?, "Should not be a leaf")
366
432
 
367
- assert_equal(1, @child3.nodeHeight, "The subtree at Child 3 should have a height of 1")
433
+ assert_equal(1, @child3.node_height, "The subtree at Child 3 should have a height of 1")
368
434
  for child in [@child1, @child2, @child4]
369
- assert_equal(0, child.nodeHeight, "The subtree at #{child.name} should have a height of 0")
435
+ assert_equal(0, child.node_height, "The subtree at #{child.name} should have a height of 0")
370
436
  end
371
437
 
372
438
  children = []
@@ -384,42 +450,39 @@ module TestTree
384
450
  children.clear
385
451
  children = @root.children
386
452
  assert_equal(3, children.length, "Should have three children")
387
-
388
453
  end
389
454
 
390
- # Test the firstChild method.
391
- def test_firstChild
455
+ # Test the first_child method.
456
+ def test_first_child
392
457
  setup_test_tree
393
458
 
394
- assert_equal(@child1, @root.firstChild, "Root's first child is Child1")
395
- assert_nil(@child1.firstChild, "Child1 does not have any children")
396
- assert_equal(@child4, @child3.firstChild, "Child3's first child is Child4")
397
-
459
+ assert_equal(@child1, @root.first_child, "Root's first child is Child1")
460
+ assert_nil(@child1.first_child, "Child1 does not have any children")
461
+ assert_equal(@child4, @child3.first_child, "Child3's first child is Child4")
398
462
  end
399
463
 
400
- # Test the lastChild method.
401
- def test_lastChild
464
+ # Test the last_child method.
465
+ def test_last_child
402
466
  setup_test_tree
403
467
 
404
- assert_equal(@child3, @root.lastChild, "Root's last child is Child3")
405
- assert_nil(@child1.lastChild, "Child1 does not have any children")
406
- assert_equal(@child4, @child3.lastChild, "Child3's last child is Child4")
407
-
468
+ assert_equal(@child3, @root.last_child, "Root's last child is Child3")
469
+ assert_nil(@child1.last_child, "Child1 does not have any children")
470
+ assert_equal(@child4, @child3.last_child, "Child3's last child is Child4")
408
471
  end
409
472
 
410
473
  # Test the find method.
411
474
  def test_find
412
475
  setup_test_tree
413
- foundNode = @root.find { |node| node == @child2}
414
- assert_same(@child2, foundNode, "The node should be Child 2")
476
+ found_node = @root.find { |node| node == @child2}
477
+ assert_same(@child2, found_node, "The node should be Child 2")
415
478
 
416
- foundNode = @root.find { |node| node == @child4}
417
- assert_same(@child4, foundNode, "The node should be Child 4")
479
+ found_node = @root.find { |node| node == @child4}
480
+ assert_same(@child4, found_node, "The node should be Child 4")
418
481
 
419
- foundNode = @root.find { |node| node.name == "Child31" }
420
- assert_same(@child4, foundNode, "The node should be Child 4")
421
- foundNode = @root.find { |node| node.name == "NOT PRESENT" }
422
- assert_nil(foundNode, "The node should not be found")
482
+ found_node = @root.find { |node| node.name == "Child4" }
483
+ assert_same(@child4, found_node, "The node should be Child 4")
484
+ found_node = @root.find { |node| node.name == "NOT PRESENT" }
485
+ assert_nil(found_node, "The node should not be found")
423
486
  end
424
487
 
425
488
  # Test the parentage method.
@@ -434,9 +497,10 @@ module TestTree
434
497
  # Test the each method.
435
498
  def test_each
436
499
  setup_test_tree
437
- assert(@root.hasChildren?, "Should have children")
500
+
501
+ assert(@root.has_children?, "Should have children")
438
502
  assert_equal(5, @root.size, "Should have five nodes")
439
- assert(@child3.hasChildren?, "Should have children")
503
+ assert(@child3.has_children?, "Should have children")
440
504
 
441
505
  nodes = []
442
506
  @root.each { |node| nodes << node }
@@ -467,6 +531,7 @@ module TestTree
467
531
  # Test the parent method.
468
532
  def test_parent
469
533
  setup_test_tree
534
+
470
535
  assert_nil(@root.parent, "Root's parent should be nil")
471
536
  assert_equal(@root, @child1.parent, "Parent should be root")
472
537
  assert_equal(@root, @child3.parent, "Parent should be root")
@@ -477,6 +542,7 @@ module TestTree
477
542
  # Test the [] method.
478
543
  def test_indexed_access
479
544
  setup_test_tree
545
+
480
546
  assert_equal(@child1, @root[0], "Should be the first child")
481
547
  assert_equal(@child4, @root[2][0], "Should be the grandchild")
482
548
  assert_nil(@root["TEST"], "Should be nil")
@@ -484,11 +550,11 @@ module TestTree
484
550
  assert_raise(ArgumentError) { @root[nil] }
485
551
  end
486
552
 
487
- # Test the printTree method.
488
- def test_printTree
553
+ # Test the print_tree method.
554
+ def test_print_tree
489
555
  setup_test_tree
490
556
  #puts
491
- #@root.printTree
557
+ #@root.print_tree
492
558
  end
493
559
 
494
560
  # Tests the binary dumping mechanism with an Object content node
@@ -508,14 +574,14 @@ module TestTree
508
574
  # Test the root node
509
575
  assert_equal(test_root.name, new_root.name, "Must identify as ROOT")
510
576
  assert_equal(test_root.content, new_root.content, "Must have root's content")
511
- assert(new_root.isRoot?, "Must be the ROOT node")
512
- assert(new_root.hasChildren?, "Must have a child node")
577
+ assert(new_root.is_root?, "Must be the ROOT node")
578
+ assert(new_root.has_children?, "Must have a child node")
513
579
 
514
580
  # Test the child node
515
581
  new_child = new_root[test_child.name]
516
582
  assert_equal(test_child.name, new_child.name, "Must have child 1")
517
- assert(new_child.hasContent?, "Child must have content")
518
- assert(new_child.isOnlyChild?, "Child must be the only child")
583
+ assert(new_child.has_content?, "Child must have content")
584
+ assert(new_child.is_only_child?, "Child must be the only child")
519
585
 
520
586
  new_child_content = new_child.content
521
587
  assert_equal(Hash, new_child_content.class, "Class of child's content should be a hash")
@@ -524,8 +590,8 @@ module TestTree
524
590
  # Test the grand-child node
525
591
  new_grand_child = new_child[test_grand_child.name]
526
592
  assert_equal(test_grand_child.name, new_grand_child.name, "Must have grand child")
527
- assert(new_grand_child.hasContent?, "Grand-child must have content")
528
- assert(new_grand_child.isOnlyChild?, "Grand-child must be the only child")
593
+ assert(new_grand_child.has_content?, "Grand-child must have content")
594
+ assert(new_grand_child.is_only_child?, "Grand-child must be the only child")
529
595
 
530
596
  new_grand_child_content = new_grand_child.content
531
597
  assert_equal(Array, new_grand_child_content.class, "Class of grand-child's content should be an Array")
@@ -539,19 +605,20 @@ module TestTree
539
605
  # Test the collect method from the mixed-in Enumerable functionality.
540
606
  def test_collect
541
607
  setup_test_tree
542
- collectArray = @root.collect do |node|
608
+ collect_array = @root.collect do |node|
543
609
  node.content = "abc"
544
610
  node
545
611
  end
546
- collectArray.each {|node| assert_equal("abc", node.content, "Should be 'abc'")}
612
+ collect_array.each {|node| assert_equal("abc", node.content, "Should be 'abc'")}
547
613
  end
548
614
 
549
615
  # Test freezing the tree
550
- def test_freezeTree_bang
616
+ def test_freeze_tree_bang
551
617
  setup_test_tree
618
+
552
619
  @root.content = "ABC"
553
620
  assert_equal("ABC", @root.content, "Content should be 'ABC'")
554
- @root.freezeTree!
621
+ @root.freeze_tree!
555
622
  # Note: The error raised here depends on the Ruby version.
556
623
  # For Ruby > 1.9, RuntimeError is raised
557
624
  # For Ruby ~ 1.8, TypeError is raised
@@ -597,52 +664,53 @@ module TestTree
597
664
  end
598
665
 
599
666
  # Test the height computation algorithm
600
- def test_nodeHeight
601
- assert_equal(0, @root.nodeHeight, "A single node's height is 0")
667
+ def test_node_height
668
+ assert_equal(0, @root.node_height, "A single node's height is 0")
602
669
 
603
670
  @root << @child1
604
- assert_equal(1, @root.nodeHeight, "This should be of height 1")
605
- assert_equal(0, @child1.nodeHeight, "This should be of height 0")
671
+ assert_equal(1, @root.node_height, "This should be of height 1")
672
+ assert_equal(0, @child1.node_height, "This should be of height 0")
606
673
 
607
674
  @root << @child2
608
- assert_equal(1, @root.nodeHeight, "This should be of height 1")
609
- assert_equal(0, @child2.nodeHeight, "This should be of height 0")
675
+ assert_equal(1, @root.node_height, "This should be of height 1")
676
+ assert_equal(0, @child2.node_height, "This should be of height 0")
610
677
 
611
678
  @child2 << @child3
612
- assert_equal(2, @root.nodeHeight, "This should be of height 2")
613
- assert_equal(1, @child2.nodeHeight, "This should be of height 1")
614
- assert_equal(0, @child3.nodeHeight, "This should be of height 0")
679
+ assert_equal(2, @root.node_height, "This should be of height 2")
680
+ assert_equal(1, @child2.node_height, "This should be of height 1")
681
+ assert_equal(0, @child3.node_height, "This should be of height 0")
615
682
 
616
683
  @child3 << @child4
617
- assert_equal(3, @root.nodeHeight, "This should be of height 3")
618
- assert_equal(2, @child2.nodeHeight, "This should be of height 2")
619
- assert_equal(1, @child3.nodeHeight, "This should be of height 1")
620
- assert_equal(0, @child4.nodeHeight, "This should be of height 0")
684
+ assert_equal(3, @root.node_height, "This should be of height 3")
685
+ assert_equal(2, @child2.node_height, "This should be of height 2")
686
+ assert_equal(1, @child3.node_height, "This should be of height 1")
687
+ assert_equal(0, @child4.node_height, "This should be of height 0")
621
688
  end
622
689
 
623
690
  # Test the depth computation algorithm. Note that this is the correct depth computation. The original
624
691
  # Tree::TreeNode#depth was incorrectly computing the height of the node - instead of its depth.
625
- def test_nodeDepth
626
- assert_equal(0, @root.nodeDepth, "A root node's depth is 0")
692
+ def test_node_depth
693
+ assert_equal(0, @root.node_depth, "A root node's depth is 0")
627
694
 
628
695
  setup_test_tree
696
+
629
697
  for child in [@child1, @child2, @child3]
630
- assert_equal(1, child.nodeDepth, "Node #{child.name} should have depth 1")
698
+ assert_equal(1, child.node_depth, "Node #{child.name} should have depth 1")
631
699
  end
632
700
 
633
- assert_equal(2, @child4.nodeDepth, "Child 4 should have depth 2")
701
+ assert_equal(2, @child4.node_depth, "Child 4 should have depth 2")
634
702
  end
635
703
 
636
- # Test the level method. Since this is an alias of nodeDepth, we just test for equivalence
704
+ # Test the level method. Since this is an alias of node_depth, we just test for equivalence
637
705
  def test_level
638
706
  assert_equal(0, @root.level, "A root node's level is 0")
639
707
 
640
- assert_equal(@root.nodeDepth, @root.level, "Level and depth should be the same")
708
+ assert_equal(@root.node_depth, @root.level, "Level and depth should be the same")
641
709
 
642
710
  setup_test_tree
643
711
  for child in [@child1, @child2, @child3]
644
712
  assert_equal(1, child.level, "Node #{child.name} should have level 1")
645
- assert_equal(@root.nodeDepth, @root.level, "Level and depth should be the same")
713
+ assert_equal(@root.node_depth, @root.level, "Level and depth should be the same")
646
714
  end
647
715
 
648
716
  assert_equal(2, @child4.level, "Child 4 should have level 2")
@@ -742,36 +810,70 @@ module TestTree
742
810
  def test_detached_copy
743
811
  setup_test_tree
744
812
 
745
- assert(@root.hasChildren?, "The root should have children")
813
+ assert(@root.has_children?, "The root should have children")
746
814
  copy_of_root = @root.detached_copy
747
- assert(!copy_of_root.hasChildren?, "The copy should not have children")
815
+ assert(!copy_of_root.has_children?, "The copy should not have children")
748
816
  assert_equal(@root.name, copy_of_root.name, "The names should be equal")
749
817
 
750
818
  # Try the same test with a child node
751
- assert(!@child3.isRoot?, "Child 3 is not a root")
752
- assert(@child3.hasChildren?, "Child 3 has children")
819
+ assert(!@child3.is_root?, "Child 3 is not a root")
820
+ assert(@child3.has_children?, "Child 3 has children")
753
821
  copy_of_child3 = @child3.detached_copy
754
- assert(copy_of_child3.isRoot?, "Child 3's copy is a root")
755
- assert(!copy_of_child3.hasChildren?, "Child 3's copy does not have children")
822
+ assert(copy_of_child3.is_root?, "Child 3's copy is a root")
823
+ assert(!copy_of_child3.has_children?, "Child 3's copy does not have children")
756
824
  end
757
825
 
758
- # Test the hasChildren? method.
759
- def test_hasChildren_eh
826
+ # Test the detached_subtree_copy method.
827
+ def test_detached_subtree_copy
760
828
  setup_test_tree
761
- assert(@root.hasChildren?, "The Root node MUST have children")
829
+
830
+ assert(@root.has_children?, "The root should have children.")
831
+ tree_copy = @root.detached_subtree_copy
832
+
833
+ assert_equal(@root.name, tree_copy.name, "The names should be equal.")
834
+ assert_not_equal(@root.object_id, tree_copy.object_id, "Object_ids should differ.")
835
+ assert(tree_copy.is_root?, "Copied root should be a root node.")
836
+ assert(tree_copy.has_children?, "Copied tree should have children.")
837
+ assert_equal(tree_copy.children.count, @root.children.count, "Copied tree and the original tree should have same number of children.")
838
+
839
+ assert_equal(tree_copy[0].name, @child1.name, "The names of Child1 (original and copy) should be same.")
840
+ assert_not_equal(tree_copy[0].object_id, @child1.object_id, "Child1 Object_ids (original and copy) should differ.")
841
+ assert(!tree_copy[0].is_root?, "Child1 copied should not be root.")
842
+ assert(!tree_copy[0].has_children?, "Child1 copied should not have children.")
843
+
844
+ assert_equal(tree_copy[1].name, @child2.name, "The names of Child2 (original and copy) should be same.")
845
+ assert_not_equal(tree_copy[1].object_id, @child2.object_id, "Child2 Object_ids (original and copy) should differ.")
846
+ assert(!tree_copy[1].is_root?, "Child2 copied should not be root.")
847
+ assert(!tree_copy[1].has_children?, "Child2 copied should not have children.")
848
+
849
+ assert_equal(tree_copy[2].name, @child3.name, "The names of Child3 (original and copy) should be same.")
850
+ assert_not_equal(tree_copy[2].object_id, @child3.object_id, "Child3 Object_ids (original and copy) should differ.")
851
+ assert(!tree_copy[2].is_root?, "Child3 copied should not be root.")
852
+ assert(tree_copy[2].has_children?, "Child3 copied should have children.")
853
+
854
+ assert_equal(tree_copy[2][0].name, @child4.name, "The names of Child4 (original and copy) should be same.")
855
+ assert_not_equal(tree_copy[2][0].object_id, @child4.object_id, "Child4 Object_ids (original and copy) should differ.")
856
+ assert(!tree_copy[2][0].is_root?, "Child4 copied should not be root.")
857
+ assert(!tree_copy[2][0].has_children?, "Child4 copied should not have children.")
762
858
  end
763
859
 
764
- # test the isLeaf? method.
765
- def test_isLeaf_eh
860
+ # Test the has_children? method.
861
+ def test_has_children_eh
766
862
  setup_test_tree
767
- assert(!@child3.isLeaf?, "Child 3 is not a leaf node")
768
- assert(@child4.isLeaf?, "Child 4 is a leaf node")
863
+ assert(@root.has_children?, "The Root node MUST have children")
769
864
  end
770
865
 
771
- # Test the isRoot? method.
772
- def test_isRoot_eh
866
+ # test the is_leaf? method.
867
+ def test_is_leaf_eh
773
868
  setup_test_tree
774
- assert(@root.isRoot?, "The ROOT node must respond as the root node")
869
+ assert(!@child3.is_leaf?, "Child 3 is not a leaf node")
870
+ assert(@child4.is_leaf?, "Child 4 is a leaf node")
871
+ end
872
+
873
+ # Test the is_root? method.
874
+ def test_is_root_eh
875
+ setup_test_tree
876
+ assert(@root.is_root?, "The ROOT node must respond as the root node")
775
877
  end
776
878
 
777
879
  # Test the content= method.
@@ -786,6 +888,7 @@ module TestTree
786
888
  def test_size
787
889
  assert_equal(1, @root.size, "Root's size should be 1")
788
890
  setup_test_tree
891
+
789
892
  assert_equal(5, @root.size, "Root's size should be 5")
790
893
  assert_equal(2, @child3.size, "Child 3's size should be 2")
791
894
  end
@@ -798,7 +901,7 @@ module TestTree
798
901
  assert_not_nil(@root['Child1'], "Child 1 should have been added to Root")
799
902
  assert_not_nil(@root['Child2'], "Child 2 should have been added to Root")
800
903
  assert_not_nil(@root['Child3'], "Child 3 should have been added to Root")
801
- assert_not_nil(@child3['Child31'], "Child 31 should have been added to Child3")
904
+ assert_not_nil(@child3['Child4'], "Child 4 should have been added to Child3")
802
905
  end
803
906
 
804
907
  # Test the [] method.
@@ -809,11 +912,17 @@ module TestTree
809
912
  @root << @child2
810
913
  assert_equal(@child1.name, @root['Child1'].name, "Child 1 should be returned")
811
914
  assert_equal(@child1.name, @root[0].name, "Child 1 should be returned")
915
+ assert_equal(@child1.name, @root[-2].name, "Child 1 should be returned") # Negative access also works
916
+ assert_equal(@child1.name, @root[-(@root.children.size)].name, "Child 1 should be returned") # Negative access also works
917
+
812
918
  assert_equal(@child2.name, @root['Child2'].name, "Child 2 should be returned")
813
919
  assert_equal(@child2.name, @root[1].name, "Child 2 should be returned")
920
+ assert_equal(@child2.name, @root[-1].name, "Child 2 should be returned") # Negative access also works
814
921
 
815
922
  assert_nil(@root['Some Random Name'], "Should return nil")
816
923
  assert_nil(@root[99], "Should return nil")
924
+ assert_nil(@root[-(@root.children.size+1)], "Should return nil")
925
+ assert_nil(@root[-3], "Should return nil")
817
926
  end
818
927
 
819
928
  # Test the in_degree method.
@@ -854,7 +963,7 @@ module TestTree
854
963
  "content" => "Child Node 3",
855
964
  JSON.create_id => "Tree::TreeNode",
856
965
  "children" => [
857
- {"name" => "Child31", "content" => "Grand Child 1", JSON.create_id => "Tree::TreeNode"}
966
+ {"name" => "Child4", "content" => "Grand Child 1", JSON.create_id => "Tree::TreeNode"}
858
967
  ]
859
968
  }
860
969
  ]
@@ -876,7 +985,7 @@ module TestTree
876
985
  "content" => "Child Node 3",
877
986
  JSON.create_id => "Tree::TreeNode",
878
987
  "children" => [
879
- {"name" => "Child31", "content" => "Grand Child 1", JSON.create_id => "Tree::TreeNode"}
988
+ {"name" => "Child4", "content" => "Grand Child 1", JSON.create_id => "Tree::TreeNode"}
880
989
  ]
881
990
  }
882
991
  ]
@@ -890,6 +999,53 @@ module TestTree
890
999
  assert_equal(@child3.name, tree[2].name, "Child 3 should be returned")
891
1000
  assert_equal(@child4.name, tree[2][0].name, "Grand Child 1 should be returned")
892
1001
  end
1002
+
1003
+ def test_json_roundtrip
1004
+ root_node = Tree::TreeNode.new("RTROOT", "Root Content")
1005
+ root_node << Tree::TreeNode.new("RTCHILD1", "Child1 Content") << Tree::TreeNode.new("RTGRANDCHILD1", "GrandChild1 Content")
1006
+ root_node << Tree::TreeNode.new("RTCHILD2", "Child2 Content")
1007
+
1008
+ j = root_node.to_json
1009
+
1010
+ k = JSON.parse(j)
1011
+
1012
+ assert_equal(k.name, root_node.name, "Root should be returned")
1013
+ assert_equal(k[0].name, root_node[0].name, "Child 1 should be returned")
1014
+ assert_equal(k[0][0].name, root_node[0][0].name, "Grand Child 1 should be returned")
1015
+ assert_equal(k[1].name, root_node[1].name, "Child 2 should be returned")
1016
+ end
1017
+
1018
+ # Test the old CamelCase method names
1019
+ def test_old_camelCase_method_names
1020
+ setup_test_tree
1021
+
1022
+ meth_names_to_test = %w{isRoot? isLeaf? hasContent?
1023
+ hasChildren? setAsRoot! firstChild lastChild
1024
+ firstSibling isFirstSibling? lastSibling isLastSibling?
1025
+ isOnlyChild? nextSibling previousSibling nodeHeight nodeDepth
1026
+ createDumpRep removeFromParent! removeAll! freezeTree! }
1027
+
1028
+ require 'structured_warnings'
1029
+
1030
+ DeprecatedMethodWarning.disable do
1031
+ assert(@root.isRoot?) # Test if the original method is really called
1032
+ end
1033
+
1034
+ meth_names_to_test.each do |meth_name|
1035
+ assert_warn(DeprecatedMethodWarning) {@root.send(meth_name)}
1036
+ end
1037
+
1038
+ # Special Case for printTree to avoid putting stuff on the STDOUT during the unit test.
1039
+ begin
1040
+ require 'stringio'
1041
+ $stdout = StringIO.new
1042
+ assert_warn(DeprecatedMethodWarning) { @root.send('printTree') }
1043
+ ensure
1044
+ $stdout = STDOUT
1045
+ end
1046
+
1047
+ end
1048
+
893
1049
  end
894
1050
  end
895
1051