rubytree 1.0.0 → 1.0.2
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.
- checksums.yaml +5 -5
- data/Gemfile +0 -7
- data/Gemfile.lock +69 -40
- data/LICENSE.md +1 -2
- data/README.md +1 -2
- data/Rakefile +18 -22
- data/examples/example_basic.rb +1 -1
- data/lib/rubytree.rb +1 -1
- data/lib/tree/binarytree.rb +7 -11
- data/lib/tree/utils/camel_case_method_handler.rb +7 -10
- data/lib/tree/utils/hash_converter.rb +60 -64
- data/lib/tree/utils/json_converter.rb +10 -15
- data/lib/tree/utils/metrics_methods.rb +8 -7
- data/lib/tree/utils/path_methods.rb +3 -7
- data/lib/tree/utils/tree_merge_handler.rb +5 -7
- data/lib/tree/utils/utils.rb +2 -4
- data/lib/tree/version.rb +2 -3
- data/lib/tree.rb +99 -85
- data/rubytree.gemspec +90 -0
- data/setup.rb +1565 -0
- data/spec/tree_spec.rb +0 -2
- data/test/test_binarytree.rb +21 -22
- data/test/test_rubytree_require.rb +0 -2
- data/test/test_subclassed_node.rb +0 -4
- data/test/test_thread_and_fiber.rb +7 -10
- data/test/test_tree.rb +201 -203
- metadata +86 -29
data/spec/tree_spec.rb
CHANGED
@@ -11,7 +11,6 @@ require 'rspec'
|
|
11
11
|
require 'spec_helper'
|
12
12
|
|
13
13
|
describe Tree do
|
14
|
-
|
15
14
|
shared_examples_for 'any detached node' do
|
16
15
|
it 'should not equal "Object.new"' do
|
17
16
|
expect(@tree).not_to eq(Object.new)
|
@@ -25,7 +24,6 @@ describe Tree do
|
|
25
24
|
it 'does not have a parent node' do
|
26
25
|
expect(@tree.parent).to eq(nil)
|
27
26
|
end
|
28
|
-
|
29
27
|
end
|
30
28
|
context '#initialize', 'with empty name and nil content' do
|
31
29
|
before(:each) do
|
data/test/test_binarytree.rb
CHANGED
@@ -39,7 +39,6 @@ require_relative '../lib/tree/binarytree'
|
|
39
39
|
module TestTree
|
40
40
|
# Test class for the binary tree node.
|
41
41
|
class TestBinaryTreeNode < Test::Unit::TestCase
|
42
|
-
|
43
42
|
# Setup the test data scaffolding.
|
44
43
|
def setup
|
45
44
|
@root = Tree::BinaryTreeNode.new('ROOT', 'Root Node')
|
@@ -65,15 +64,15 @@ module TestTree
|
|
65
64
|
|
66
65
|
def test_from_hash
|
67
66
|
# Can't make a root node without a name
|
68
|
-
assert_raise
|
67
|
+
assert_raise(ArgumentError) { Tree::BinaryTreeNode.from_hash({}) }
|
69
68
|
# Can't have multiple roots
|
70
|
-
assert_raise
|
69
|
+
assert_raise(ArgumentError) { Tree::BinaryTreeNode.from_hash({ A: {}, B: {} }) }
|
71
70
|
|
72
71
|
# Can't have more than 2 children
|
73
|
-
too_many_kids = {A: {B: {}, C: {}, D: {}}}
|
72
|
+
too_many_kids = { A: { B: {}, C: {}, D: {} } }
|
74
73
|
assert_raise(ArgumentError) { Tree::BinaryTreeNode.from_hash(too_many_kids) }
|
75
74
|
|
76
|
-
valid_hash = {A: {B: {}, C: {D: {}}}}
|
75
|
+
valid_hash = { A: { B: {}, C: { D: {} } } }
|
77
76
|
tree = Tree::BinaryTreeNode.from_hash(valid_hash)
|
78
77
|
# A
|
79
78
|
# / \
|
@@ -88,7 +87,7 @@ module TestTree
|
|
88
87
|
assert_equal(2, tree.children.count) # B, C, D
|
89
88
|
assert_equal(4, tree.size)
|
90
89
|
|
91
|
-
valid_hash_with_content = {[:A, 'Content!'] => {B: {}, C: {[:D, 'More content'] => {}}} }
|
90
|
+
valid_hash_with_content = { [:A, 'Content!'] => { B: {}, C: { [:D, 'More content'] => {} } } }
|
92
91
|
tree2 = Tree::BinaryTreeNode.from_hash(valid_hash_with_content)
|
93
92
|
|
94
93
|
assert_equal(Tree::BinaryTreeNode, tree2.class)
|
@@ -100,7 +99,7 @@ module TestTree
|
|
100
99
|
root = Tree::BinaryTreeNode.new('Root')
|
101
100
|
|
102
101
|
# Can't have too many children
|
103
|
-
too_many_kids = {child1: {}, child2: {}, child3: {}}
|
102
|
+
too_many_kids = { child1: {}, child2: {}, child3: {} }
|
104
103
|
assert_raise(ArgumentError) { root.add_from_hash(too_many_kids) }
|
105
104
|
assert_equal(0, root.children.count) # Nothing added
|
106
105
|
|
@@ -108,7 +107,7 @@ module TestTree
|
|
108
107
|
assert_equal([], root.add_from_hash({}))
|
109
108
|
assert_equal(1, root.size)
|
110
109
|
|
111
|
-
valid_hash = {A: {}, B: {C: {}, [:D, 'leaf'] => {}}}
|
110
|
+
valid_hash = { A: {}, B: { C: {}, [:D, 'leaf'] => {} } }
|
112
111
|
added = root.add_from_hash(valid_hash)
|
113
112
|
# root
|
114
113
|
# / \
|
@@ -123,9 +122,9 @@ module TestTree
|
|
123
122
|
assert_equal('leaf', root[:B][:D].content)
|
124
123
|
|
125
124
|
# Can't add more than two children
|
126
|
-
assert_raise(ArgumentError) { root.add_from_hash({X: {}}) }
|
125
|
+
assert_raise(ArgumentError) { root.add_from_hash({ X: {} }) }
|
127
126
|
node = Tree::BinaryTreeNode.new('Root 2')
|
128
|
-
assert_raise(ArgumentError) { node.add_from_hash({A: {}, B: {}, C: {}}) }
|
127
|
+
assert_raise(ArgumentError) { node.add_from_hash({ A: {}, B: {}, C: {} }) }
|
129
128
|
end
|
130
129
|
|
131
130
|
# Test the add method.
|
@@ -175,24 +174,26 @@ module TestTree
|
|
175
174
|
f << g
|
176
175
|
b << d << c
|
177
176
|
d << e
|
178
|
-
g.right_child = i
|
177
|
+
g.right_child = i # This needs to be explicit
|
179
178
|
i << h
|
180
179
|
|
181
180
|
# The expected order of response
|
182
181
|
expected_array = [a, b, c, d, e, f, g, h, i]
|
183
182
|
|
184
183
|
result_array = []
|
185
|
-
result = f.inordered_each { |node| result_array << node.detached_copy}
|
184
|
+
result = f.inordered_each { |node| result_array << node.detached_copy }
|
186
185
|
|
187
|
-
assert_equal(f, result)
|
186
|
+
assert_equal(f, result) # each should return the original object
|
188
187
|
|
189
188
|
expected_array.each_index do |idx|
|
190
189
|
# Match only the names.
|
191
190
|
assert_equal(expected_array[idx].name, result_array[idx].name)
|
192
191
|
end
|
193
192
|
|
194
|
-
assert_equal(Enumerator, f.inordered_each.class) if defined?(Enumerator.class
|
195
|
-
|
193
|
+
assert_equal(Enumerator, f.inordered_each.class) if defined?(Enumerator.class) # Without a block
|
194
|
+
if defined?(Enumerable::Enumerator.class)
|
195
|
+
assert_equal(Enumerable::Enumerator, f.inordered_each.class)
|
196
|
+
end # Without a block
|
196
197
|
end
|
197
198
|
|
198
199
|
# Test the left_child method.
|
@@ -304,19 +305,17 @@ module TestTree
|
|
304
305
|
|
305
306
|
require 'structured_warnings'
|
306
307
|
|
307
|
-
meth_names_for_test = %w
|
308
|
+
meth_names_for_test = %w[leftChild isLeftChild? rightChild isRightChild?]
|
308
309
|
|
309
310
|
meth_names_for_test.each do |meth_name|
|
310
|
-
assert_warn(StructuredWarnings::DeprecatedMethodWarning) {@root.send(meth_name)}
|
311
|
+
assert_warn(StructuredWarnings::DeprecatedMethodWarning) { @root.send(meth_name) }
|
311
312
|
end
|
312
313
|
|
313
314
|
# noinspection RubyResolve
|
314
|
-
assert_warn(StructuredWarnings::DeprecatedMethodWarning) {@root.leftChild = @left_child2}
|
315
|
+
assert_warn(StructuredWarnings::DeprecatedMethodWarning) { @root.leftChild = @left_child2 }
|
315
316
|
# noinspection RubyResolve
|
316
|
-
assert_warn(StructuredWarnings::DeprecatedMethodWarning) {@root.rightChild = @right_child2}
|
317
|
-
assert_raise(NoMethodError) {@root.DummyMethodDoesNotExist} # Make sure the right method is visible
|
318
|
-
|
317
|
+
assert_warn(StructuredWarnings::DeprecatedMethodWarning) { @root.rightChild = @right_child2 }
|
318
|
+
assert_raise(NoMethodError) { @root.DummyMethodDoesNotExist } # Make sure the right method is visible
|
319
319
|
end
|
320
|
-
|
321
320
|
end
|
322
321
|
end
|
@@ -36,10 +36,8 @@ require 'test/unit'
|
|
36
36
|
require_relative '../lib/rubytree'
|
37
37
|
|
38
38
|
module TestTree
|
39
|
-
|
40
39
|
# Test class for checking whether require 'rubytree' works.
|
41
40
|
class TestRequireRubytree < Test::Unit::TestCase
|
42
|
-
|
43
41
|
# A simple test. We are just checking whether the require worked.
|
44
42
|
def test_create_a_simple_node
|
45
43
|
assert_not_nil(Tree::TreeNode.new('Root', 'A Root node'))
|
@@ -37,10 +37,8 @@ require 'json'
|
|
37
37
|
require_relative '../lib/tree'
|
38
38
|
|
39
39
|
module TestTree
|
40
|
-
|
41
40
|
# Test class for the Tree node.
|
42
41
|
class TestSubclassedTreeNode < Test::Unit::TestCase
|
43
|
-
|
44
42
|
# A subclassed node to test various inheritance related features.
|
45
43
|
class MyNode < Tree::TreeNode
|
46
44
|
# A dummy method to test the camelCasedMethod resolution
|
@@ -62,14 +60,12 @@ module TestTree
|
|
62
60
|
# noinspection RubyResolve
|
63
61
|
assert_equal('Hello', root.myDummyMethod)
|
64
62
|
end
|
65
|
-
|
66
63
|
end
|
67
64
|
|
68
65
|
def test_detached_copy_same_clz
|
69
66
|
root = MyNode.new('Root')
|
70
67
|
assert_equal(MyNode, root.detached_copy.class)
|
71
68
|
end
|
72
|
-
|
73
69
|
end
|
74
70
|
end
|
75
71
|
|
@@ -39,13 +39,12 @@ require_relative '../lib/tree'
|
|
39
39
|
module TestTree
|
40
40
|
# Test class for the Tree node.
|
41
41
|
class TestFiberAndThreadOnNode < Test::Unit::TestCase
|
42
|
-
|
43
42
|
# Test long and unbalanced trees
|
44
|
-
def create_long_depth_trees(depth=100)
|
43
|
+
def create_long_depth_trees(depth = 100)
|
45
44
|
tree = Tree::TreeNode.new('/')
|
46
45
|
current = tree
|
47
46
|
depth.times do |i|
|
48
|
-
new_node = Tree::TreeNode.new(
|
47
|
+
new_node = Tree::TreeNode.new(i.to_s)
|
49
48
|
current << new_node
|
50
49
|
current = new_node
|
51
50
|
end
|
@@ -58,30 +57,28 @@ module TestTree
|
|
58
57
|
# failure for very large depths on unbalanced nodes.
|
59
58
|
def test_fiber_for_recursion
|
60
59
|
return unless defined?(Fiber.class) # Fibers exist only from Ruby 1.9 onwards.
|
60
|
+
|
61
61
|
assert_nothing_thrown do
|
62
62
|
Fiber.new do
|
63
|
-
depth = 1000
|
63
|
+
depth = 1000 # Use a reasonably large depth, which would trip a recursive stack
|
64
64
|
root = create_long_depth_trees(depth)
|
65
|
-
assert_equal(depth+1, root.size)
|
65
|
+
assert_equal(depth + 1, root.size)
|
66
66
|
end.resume
|
67
67
|
end
|
68
|
-
|
69
68
|
end # test_fiber
|
70
69
|
|
71
70
|
# Test the recursive methods with a thread. The stack usage is causing
|
72
71
|
# failure for very large depths on unbalanced nodes.
|
73
72
|
def test_thread_for_recursion
|
74
73
|
assert_nothing_thrown do
|
75
|
-
depth = 1000
|
74
|
+
depth = 1000 # Use a reasonably large depth, which would trip a recursive stack
|
76
75
|
Thread.abort_on_exception = true
|
77
76
|
Thread.new do
|
78
77
|
root = create_long_depth_trees(depth)
|
79
|
-
assert_equal(depth+1, root.size)
|
78
|
+
assert_equal(depth + 1, root.size)
|
80
79
|
end
|
81
80
|
end
|
82
|
-
|
83
81
|
end # test_thread
|
84
|
-
|
85
82
|
end
|
86
83
|
end
|
87
84
|
|