rubytree 0.9.7 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -32,13 +32,13 @@
32
32
  # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
33
  #
34
34
 
35
- base_dir = File.expand_path(File.join(File.dirname(__FILE__), ".."))
36
- lib_dir = File.join(base_dir, "lib")
37
- test_dir = File.join(base_dir, "test")
35
+ base_dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
36
+ lib_dir = File.join(base_dir, 'lib')
37
+ test_dir = File.join(base_dir, 'test')
38
38
 
39
39
  $LOAD_PATH.unshift(lib_dir)
40
40
 
41
- if ENV["COVERAGE"]
41
+ if ENV['COVERAGE']
42
42
  begin
43
43
  require 'simplecov'
44
44
  require 'coveralls'
@@ -51,10 +51,10 @@ if ENV["COVERAGE"]
51
51
  add_group 'Internal Utilities', '/lib/tree/utils/.*.rb'
52
52
  end
53
53
  rescue LoadError => e
54
- puts "Could not load simplecov; continuing without code coverage"
54
+ puts 'Could not load simplecov; continuing without code coverage' + e.cause
55
55
  end
56
56
  end
57
57
 
58
58
  require 'test/unit'
59
59
 
60
- exit Test::Unit::AutoRunner.run(true, test_dir)
60
+ Test::Unit::AutoRunner.run(true, test_dir)
@@ -3,7 +3,7 @@
3
3
  # test_binarytree.rb - This file is part of the RubyTree package.
4
4
  #
5
5
  #
6
- # Copyright (c) 2006, 2007, 2008, 2009, 2010, 2012, 2013, 2015 Anupam Sengupta
6
+ # Copyright (c) 2006, 2007, 2008, 2009, 2010, 2012, 2013, 2015, 2017 Anupam Sengupta
7
7
  #
8
8
  # All rights reserved.
9
9
  #
@@ -34,7 +34,7 @@
34
34
  #
35
35
 
36
36
  require 'test/unit'
37
- require 'tree/binarytree'
37
+ require_relative '../lib/tree/binarytree'
38
38
 
39
39
  module TestTree
40
40
  # Test class for the binary tree node.
@@ -42,10 +42,10 @@ module TestTree
42
42
 
43
43
  # Setup the test data scaffolding.
44
44
  def setup
45
- @root = Tree::BinaryTreeNode.new("ROOT", "Root Node")
45
+ @root = Tree::BinaryTreeNode.new('ROOT', 'Root Node')
46
46
 
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")
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')
49
49
  end
50
50
 
51
51
  # Tear down the test data scaffolding.
@@ -58,22 +58,22 @@ module TestTree
58
58
  # Test initialization of the binary tree.
59
59
  def test_initialize
60
60
  assert_not_nil(@root, "Binary tree's Root should have been created")
61
- assert_nil(@root.left_child, "The initial left child of root should be nil")
62
- assert_nil(@root.right_child, "The initial right child of root should be nil")
63
- assert_equal(@root.children.size, 0, "Initially no children should be present")
61
+ assert_nil(@root.left_child, 'The initial left child of root should be nil')
62
+ assert_nil(@root.right_child, 'The initial right child of root should be nil')
63
+ assert_equal(@root.children.size, 0, 'Initially no children should be present')
64
64
  end
65
65
 
66
66
  def test_from_hash
67
67
  # Can't make a root node without a name
68
68
  assert_raise (ArgumentError) { Tree::BinaryTreeNode.from_hash({})}
69
69
  # Can't have multiple roots
70
- assert_raise (ArgumentError) { Tree::BinaryTreeNode.from_hash({:A => {}, :B => {}}) }
70
+ assert_raise (ArgumentError) { Tree::BinaryTreeNode.from_hash({A: {}, B: {}}) }
71
71
 
72
72
  # Can't have more than 2 children
73
- too_many_kids = {:A => {:B => {}, :C => {}, :D => {} } }
73
+ too_many_kids = {A: {B: {}, C: {}, D: {}}}
74
74
  assert_raise(ArgumentError) { Tree::BinaryTreeNode.from_hash(too_many_kids) }
75
75
 
76
- valid_hash = {:A => {:B => {}, :C => { :D => {} } } }
76
+ valid_hash = {A: {B: {}, C: {D: {}}}}
77
77
  tree = Tree::BinaryTreeNode.from_hash(valid_hash)
78
78
  # A
79
79
  # / \
@@ -88,19 +88,19 @@ module TestTree
88
88
  assert_equal(2, tree.children.count) # B, C, D
89
89
  assert_equal(4, tree.size)
90
90
 
91
- valid_hash_with_content = {[:A, "Content!"] => {:B => {}, :C => { [:D, "More content"] => {} } } }
91
+ valid_hash_with_content = {[:A, 'Content!'] => {B: {}, C: {[:D, 'More content'] => {}}} }
92
92
  tree2 = Tree::BinaryTreeNode.from_hash(valid_hash_with_content)
93
93
 
94
94
  assert_equal(Tree::BinaryTreeNode, tree2.class)
95
- assert_equal("Content!" , tree2.content)
96
- assert_equal("More content", tree2[:C][:D].content)
95
+ assert_equal('Content!', tree2.content)
96
+ assert_equal('More content', tree2[:C][:D].content)
97
97
  end
98
98
 
99
99
  def test_add_from_hash
100
- root = Tree::BinaryTreeNode.new("Root")
100
+ root = Tree::BinaryTreeNode.new('Root')
101
101
 
102
102
  # Can't have too many children
103
- too_many_kids = {:child1 => {}, :child2 => {}, :child3 => {}}
103
+ too_many_kids = {child1: {}, child2: {}, child3: {}}
104
104
  assert_raise(ArgumentError) { root.add_from_hash(too_many_kids) }
105
105
  assert_equal(0, root.children.count) # Nothing added
106
106
 
@@ -108,7 +108,7 @@ module TestTree
108
108
  assert_equal([], root.add_from_hash({}))
109
109
  assert_equal(1, root.size)
110
110
 
111
- valid_hash = {:A => {}, :B => { :C => {}, [:D, "leaf"] => {} } }
111
+ valid_hash = {A: {}, B: {C: {}, [:D, 'leaf'] => {}}}
112
112
  added = root.add_from_hash(valid_hash)
113
113
  # root
114
114
  # / \
@@ -120,48 +120,48 @@ module TestTree
120
120
  assert_equal(2, added.count)
121
121
  assert_equal(5, root.size)
122
122
  assert_equal(root.children.count, 2)
123
- assert_equal("leaf", root[:B][:D].content)
123
+ assert_equal('leaf', root[:B][:D].content)
124
124
 
125
125
  # Can't add more than two children
126
- assert_raise(ArgumentError) { root.add_from_hash({:X => {}}) }
127
- node = Tree::BinaryTreeNode.new("Root 2")
128
- assert_raise(ArgumentError) { node.add_from_hash({:A => {}, :B => {}, :C => {}}) }
126
+ assert_raise(ArgumentError) { root.add_from_hash({X: {}}) }
127
+ node = Tree::BinaryTreeNode.new('Root 2')
128
+ assert_raise(ArgumentError) { node.add_from_hash({A: {}, B: {}, C: {}}) }
129
129
  end
130
130
 
131
131
  # Test the add method.
132
132
  def test_add
133
133
  @root.add @left_child1
134
- assert(!@left_child1.is_root?, "Left child1 cannot be a root after addition to the ROOT node")
134
+ assert(!@left_child1.is_root?, 'Left child1 cannot be a root after addition to the ROOT node')
135
135
 
136
- assert_same(@left_child1, @root.left_child, "The left node should be left_child1")
137
- assert_same(@left_child1, @root.first_child, "The first node should be left_child1")
136
+ assert_same(@left_child1, @root.left_child, 'The left node should be left_child1')
137
+ assert_same(@left_child1, @root.first_child, 'The first node should be left_child1')
138
138
 
139
139
  @root.add @right_child1
140
- assert(!@right_child1.is_root?, "Right child1 cannot be a root after addition to the ROOT node")
140
+ assert(!@right_child1.is_root?, 'Right child1 cannot be a root after addition to the ROOT node')
141
141
 
142
- assert_same(@right_child1, @root.right_child, "The right node should be right_child1")
143
- assert_same(@right_child1, @root.last_child, "The first node should be right_child1")
142
+ assert_same(@right_child1, @root.right_child, 'The right node should be right_child1')
143
+ assert_same(@right_child1, @root.last_child, 'The first node should be right_child1')
144
144
 
145
145
  assert_raise ArgumentError do
146
- @root.add Tree::BinaryTreeNode.new("The third child!")
146
+ @root.add Tree::BinaryTreeNode.new('The third child!')
147
147
  end
148
148
 
149
149
  assert_raise ArgumentError do
150
- @root << Tree::BinaryTreeNode.new("The third child!")
150
+ @root << Tree::BinaryTreeNode.new('The third child!')
151
151
  end
152
152
  end
153
153
 
154
154
  # Test the inordered_each method.
155
155
  def test_inordered_each
156
- a = Tree::BinaryTreeNode.new("a")
157
- b = Tree::BinaryTreeNode.new("b")
158
- c = Tree::BinaryTreeNode.new("c")
159
- d = Tree::BinaryTreeNode.new("d")
160
- e = Tree::BinaryTreeNode.new("e")
161
- f = Tree::BinaryTreeNode.new("f")
162
- g = Tree::BinaryTreeNode.new("g")
163
- h = Tree::BinaryTreeNode.new("h")
164
- i = Tree::BinaryTreeNode.new("i")
156
+ a = Tree::BinaryTreeNode.new('a')
157
+ b = Tree::BinaryTreeNode.new('b')
158
+ c = Tree::BinaryTreeNode.new('c')
159
+ d = Tree::BinaryTreeNode.new('d')
160
+ e = Tree::BinaryTreeNode.new('e')
161
+ f = Tree::BinaryTreeNode.new('f')
162
+ g = Tree::BinaryTreeNode.new('g')
163
+ h = Tree::BinaryTreeNode.new('h')
164
+ i = Tree::BinaryTreeNode.new('i')
165
165
 
166
166
  # Create the following Tree
167
167
  # f <-- level 0 (Root)
@@ -186,9 +186,9 @@ module TestTree
186
186
 
187
187
  assert_equal(f, result) # each should return the original object
188
188
 
189
- expected_array.each_index do |i|
189
+ expected_array.each_index do |idx|
190
190
  # Match only the names.
191
- assert_equal(expected_array[i].name, result_array[i].name)
191
+ assert_equal(expected_array[idx].name, result_array[idx].name)
192
192
  end
193
193
 
194
194
  assert_equal(Enumerator, f.inordered_each.class) if defined?(Enumerator.class )# Without a block
@@ -200,7 +200,7 @@ module TestTree
200
200
  @root << @left_child1
201
201
  @root << @right_child1
202
202
  assert_same(@left_child1, @root.left_child, "The left child should be 'left_child1")
203
- assert_not_same(@right_child1, @root.left_child, "The right_child1 is not the left child")
203
+ assert_not_same(@right_child1, @root.left_child, 'The right_child1 is not the left child')
204
204
  end
205
205
 
206
206
  # Test the right_child method.
@@ -208,7 +208,7 @@ module TestTree
208
208
  @root << @left_child1
209
209
  @root << @right_child1
210
210
  assert_same(@right_child1, @root.right_child, "The right child should be 'right_child1")
211
- assert_not_same(@left_child1, @root.right_child, "The left_child1 is not the left child")
211
+ assert_not_same(@left_child1, @root.right_child, 'The left_child1 is not the left child')
212
212
  end
213
213
 
214
214
  # Test left_child= method.
@@ -216,18 +216,18 @@ module TestTree
216
216
  @root << @left_child1
217
217
  @root << @right_child1
218
218
  assert_same(@left_child1, @root.left_child, "The left child should be 'left_child1")
219
- assert(!@left_child1.is_root?, "The left child now cannot be a root.")
219
+ assert(!@left_child1.is_root?, 'The left child now cannot be a root.')
220
220
 
221
- @root.left_child = Tree::BinaryTreeNode.new("New Left Child")
222
- assert(!@root.left_child.is_root?, "The left child now cannot be a root.")
223
- assert_equal("New Left Child", @root.left_child.name, "The left child should now be the new child")
224
- assert_equal("B Child at Right", @root.last_child.name, "The last child should now be the right child")
221
+ @root.left_child = Tree::BinaryTreeNode.new('New Left Child')
222
+ assert(!@root.left_child.is_root?, 'The left child now cannot be a root.')
223
+ assert_equal('New Left Child', @root.left_child.name, 'The left child should now be the new child')
224
+ assert_equal('B Child at Right', @root.last_child.name, 'The last child should now be the right child')
225
225
 
226
226
  # Now set the left child as nil, and retest
227
227
  @root.left_child = nil
228
- assert_nil(@root.left_child, "The left child should now be nil")
229
- assert_nil(@root.first_child, "The first child is now nil")
230
- assert_equal("B Child at Right", @root.last_child.name, "The last child should now be the right child")
228
+ assert_nil(@root.left_child, 'The left child should now be nil')
229
+ assert_nil(@root.first_child, 'The first child is now nil')
230
+ assert_equal('B Child at Right', @root.last_child.name, 'The last child should now be the right child')
231
231
  end
232
232
 
233
233
  # Test right_child= method.
@@ -235,19 +235,19 @@ module TestTree
235
235
  @root << @left_child1
236
236
  @root << @right_child1
237
237
  assert_same(@right_child1, @root.right_child, "The right child should be 'right_child1")
238
- assert(!@right_child1.is_root?, "The right child now cannot be a root.")
238
+ assert(!@right_child1.is_root?, 'The right child now cannot be a root.')
239
239
 
240
- @root.right_child = Tree::BinaryTreeNode.new("New Right Child")
241
- assert(!@root.right_child.is_root?, "The right child now cannot be a root.")
242
- assert_equal("New Right Child", @root.right_child.name, "The right child should now be the new child")
243
- assert_equal("A Child at Left", @root.first_child.name, "The first child should now be the left child")
244
- assert_equal("New Right Child", @root.last_child.name, "The last child should now be the right child")
240
+ @root.right_child = Tree::BinaryTreeNode.new('New Right Child')
241
+ assert(!@root.right_child.is_root?, 'The right child now cannot be a root.')
242
+ assert_equal('New Right Child', @root.right_child.name, 'The right child should now be the new child')
243
+ assert_equal('A Child at Left', @root.first_child.name, 'The first child should now be the left child')
244
+ assert_equal('New Right Child', @root.last_child.name, 'The last child should now be the right child')
245
245
 
246
246
  # Now set the right child as nil, and retest
247
247
  @root.right_child = nil
248
- assert_nil(@root.right_child, "The right child should now be nil")
249
- assert_equal("A Child at Left", @root.first_child.name, "The first child should now be the left child")
250
- assert_nil(@root.last_child, "The first child is now nil")
248
+ assert_nil(@root.right_child, 'The right child should now be nil')
249
+ assert_equal('A Child at Left', @root.first_child.name, 'The first child should now be the left child')
250
+ assert_nil(@root.last_child, 'The first child is now nil')
251
251
  end
252
252
 
253
253
  # Test isLeft_child? method.
@@ -255,14 +255,14 @@ module TestTree
255
255
  @root << @left_child1
256
256
  @root << @right_child1
257
257
 
258
- assert(@left_child1.is_left_child?, "left_child1 should be the left child")
259
- assert(!@right_child1.is_left_child?, "left_child1 should be the left child")
258
+ assert(@left_child1.is_left_child?, 'left_child1 should be the left child')
259
+ assert(!@right_child1.is_left_child?, 'left_child1 should be the left child')
260
260
 
261
261
  # Now set the right child as nil, and retest
262
262
  @root.right_child = nil
263
- assert(@left_child1.is_left_child?, "left_child1 should be the left child")
263
+ assert(@left_child1.is_left_child?, 'left_child1 should be the left child')
264
264
 
265
- assert(!@root.is_left_child?, "Root is neither left child nor right")
265
+ assert(!@root.is_left_child?, 'Root is neither left child nor right')
266
266
  end
267
267
 
268
268
  # Test is_right_child? method.
@@ -270,13 +270,13 @@ module TestTree
270
270
  @root << @left_child1
271
271
  @root << @right_child1
272
272
 
273
- assert(@right_child1.is_right_child?, "right_child1 should be the right child")
274
- assert(!@left_child1.is_right_child?, "right_child1 should be the right child")
273
+ assert(@right_child1.is_right_child?, 'right_child1 should be the right child')
274
+ assert(!@left_child1.is_right_child?, 'right_child1 should be the right child')
275
275
 
276
276
  # Now set the left child as nil, and retest
277
277
  @root.left_child = nil
278
- assert(@right_child1.is_right_child?, "right_child1 should be the right child")
279
- assert(!@root.is_right_child?, "Root is neither left child nor right")
278
+ assert(@right_child1.is_right_child?, 'right_child1 should be the right child')
279
+ assert(!@root.is_right_child?, 'Root is neither left child nor right')
280
280
  end
281
281
 
282
282
  # Test swap_children method.
@@ -284,35 +284,37 @@ module TestTree
284
284
  @root << @left_child1
285
285
  @root << @right_child1
286
286
 
287
- assert(@right_child1.is_right_child?, "right_child1 should be the right child")
288
- assert(!@left_child1.is_right_child?, "right_child1 should be the right child")
287
+ assert(@right_child1.is_right_child?, 'right_child1 should be the right child')
288
+ assert(!@left_child1.is_right_child?, 'right_child1 should be the right child')
289
289
 
290
290
  @root.swap_children
291
291
 
292
- assert(@right_child1.is_left_child?, "right_child1 should now be the left child")
293
- assert(@left_child1.is_right_child?, "left_child1 should now be the right child")
294
- assert_equal(@right_child1, @root.first_child, "right_child1 should now be the first child")
295
- assert_equal(@left_child1, @root.last_child, "left_child1 should now be the last child")
296
- assert_equal(@right_child1, @root[0], "right_child1 should now be the first child")
297
- assert_equal(@left_child1, @root[1], "left_child1 should now be the last child")
292
+ assert(@right_child1.is_left_child?, 'right_child1 should now be the left child')
293
+ assert(@left_child1.is_right_child?, 'left_child1 should now be the right child')
294
+ assert_equal(@right_child1, @root.first_child, 'right_child1 should now be the first child')
295
+ assert_equal(@left_child1, @root.last_child, 'left_child1 should now be the last child')
296
+ assert_equal(@right_child1, @root[0], 'right_child1 should now be the first child')
297
+ assert_equal(@left_child1, @root[1], 'left_child1 should now be the last child')
298
298
  end
299
299
 
300
300
  # Test the old CamelCase method names
301
- def test_old_camelCase_method_names
302
- @left_child2 = Tree::BinaryTreeNode.new("A Child at Left", "Child Node @ left")
303
- @right_child2 = Tree::BinaryTreeNode.new("B Child at Right", "Child Node @ right")
301
+ def test_old_camel_case_names
302
+ @left_child2 = Tree::BinaryTreeNode.new('A Child at Left', 'Child Node @ left')
303
+ @right_child2 = Tree::BinaryTreeNode.new('B Child at Right', 'Child Node @ right')
304
304
 
305
305
  require 'structured_warnings'
306
306
 
307
307
  meth_names_for_test = %w{leftChild isLeftChild? rightChild isRightChild?}
308
308
 
309
309
  meth_names_for_test.each do |meth_name|
310
- assert_warn(DeprecatedMethodWarning) {@root.send(meth_name)}
310
+ assert_warn(StructuredWarnings::DeprecatedMethodWarning) {@root.send(meth_name)}
311
311
  end
312
312
 
313
- assert_warn(DeprecatedMethodWarning) {@root.leftChild = @left_child2}
314
- assert_warn(DeprecatedMethodWarning) {@root.rightChild = @right_child2}
315
- assert_raise(NoMethodError) {@root.to_snake_case("ABCD")} # Make sure the right method is visible
313
+ # noinspection RubyResolve
314
+ assert_warn(StructuredWarnings::DeprecatedMethodWarning) {@root.leftChild = @left_child2}
315
+ # noinspection RubyResolve
316
+ assert_warn(StructuredWarnings::DeprecatedMethodWarning) {@root.rightChild = @right_child2}
317
+ assert_raise(NoMethodError) {@root.DummyMethodDoesNotExist} # Make sure the right method is visible
316
318
 
317
319
  end
318
320
 
@@ -33,7 +33,7 @@
33
33
  #
34
34
 
35
35
  require 'test/unit'
36
- require 'rubytree'
36
+ require_relative '../lib/rubytree'
37
37
 
38
38
  module TestTree
39
39
 
@@ -42,7 +42,7 @@ module TestTree
42
42
 
43
43
  # A simple test. We are just checking whether the require worked.
44
44
  def test_create_a_simple_node
45
- assert_not_nil(Tree::TreeNode.new("Root", "A Root node"))
45
+ assert_not_nil(Tree::TreeNode.new('Root', 'A Root node'))
46
46
  end
47
47
  end
48
48
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  # test_subclassed_node.rb - This file is part of the RubyTree package.
4
4
  #
5
- # Copyright (c) 2012 Anupam Sengupta
5
+ # Copyright (c) 2012, 2017 Anupam Sengupta
6
6
  #
7
7
  # All rights reserved.
8
8
  #
@@ -34,7 +34,7 @@
34
34
 
35
35
  require 'test/unit'
36
36
  require 'json'
37
- require 'tree'
37
+ require_relative '../lib/tree'
38
38
 
39
39
  module TestTree
40
40
 
@@ -45,27 +45,28 @@ module TestTree
45
45
  class MyNode < Tree::TreeNode
46
46
  # A dummy method to test the camelCasedMethod resolution
47
47
  def my_dummy_method
48
- "Hello"
48
+ 'Hello'
49
49
  end
50
50
  end
51
51
 
52
- def test_subclassed_camelcase_methods
53
- root = MyNode.new("Root")
52
+ def test_camelcase_methods
53
+ root = MyNode.new('Root')
54
54
 
55
- assert_equal("Hello", root.my_dummy_method)
55
+ assert_equal('Hello', root.my_dummy_method)
56
56
 
57
57
  # We should get a warning as we are invoking the camelCase version of the dummy method.
58
- assert_warn(DeprecatedMethodWarning) { root.send('MyDummyMethod') }
58
+ assert_warn(StructuredWarnings::DeprecatedMethodWarning) { root.send('MyDummyMethod') }
59
59
 
60
- # Test if the structured_warnings can be disabled to call the CamelCa
61
- DeprecatedMethodWarning.disable do
62
- assert_equal("Hello", root.myDummyMethod)
60
+ # Test if the structured_warnings can be disabled to call the CamelCase methods.
61
+ StructuredWarnings::DeprecatedMethodWarning.disable do
62
+ # noinspection RubyResolve
63
+ assert_equal('Hello', root.myDummyMethod)
63
64
  end
64
65
 
65
66
  end
66
67
 
67
- def test_subclassed_detached_copy_is_same_class
68
- root = MyNode.new("Root")
68
+ def test_detached_copy_same_clz
69
+ root = MyNode.new('Root')
69
70
  assert_equal(MyNode, root.detached_copy.class)
70
71
  end
71
72
 
@@ -34,7 +34,7 @@
34
34
 
35
35
  require 'test/unit'
36
36
  require 'json'
37
- require 'tree'
37
+ require_relative '../lib/tree'
38
38
 
39
39
  module TestTree
40
40
  # Test class for the Tree node.
@@ -42,7 +42,7 @@ module TestTree
42
42
 
43
43
  # Test long and unbalanced trees
44
44
  def create_long_depth_trees(depth=100)
45
- tree = Tree::TreeNode.new("/")
45
+ tree = Tree::TreeNode.new('/')
46
46
  current = tree
47
47
  depth.times do |i|
48
48
  new_node = Tree::TreeNode.new("#{i}")
@@ -50,7 +50,7 @@ module TestTree
50
50
  current = new_node
51
51
  end
52
52
 
53
- tree.each { |n| nil }
53
+ tree.each { |_| nil }
54
54
  tree
55
55
  end
56
56