Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
No longer require globally unique node names.
This fixes issue#24. The node names need to be unique only among sibling nodes.

The node comparison is still based on node names in this release.
  • Loading branch information
evolve75 committed Feb 1, 2014
1 parent f7f6377 commit 0432c2d
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 21 deletions.
6 changes: 6 additions & 0 deletions API-CHANGES.rdoc
Expand Up @@ -9,6 +9,12 @@ Note: API level changes are expected to reduce dramatically after the 1.x
release. In most cases, an alternative will be provided to ensure relatively
smooth transition to the new APIs.

== Release 0.9.3 Changes

- The validation for unique node names has changed in the {Tree::TreeNode#add}
method. RubyTree no longer enforces globally unique names. The node-names need
to be unique only between the sibling nodes.

== Release 0.9.0 Changes

- New post-ordered traversal via the {Tree::TreeNode#postordered_each} method.
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
rubytree (0.9.3pre)
rubytree (0.9.3)
json (~> 1.8)
structured_warnings (~> 0.1)

Expand Down
4 changes: 4 additions & 0 deletions History.rdoc
Expand Up @@ -2,6 +2,10 @@

= History of Changes

=== 0.9.3 / 2014-02-01

* Fixed the issue with globally unique node names. See Github issue #24.

=== 0.9.2 / 2014-01-03

* Yanked R0.9.1 as the History file was not updated.
Expand Down
8 changes: 4 additions & 4 deletions lib/tree.rb
Expand Up @@ -354,11 +354,11 @@ def <<(child)
# @see #<<
def add(child, at_index = -1)
raise ArgumentError, "Attempting to add a nil node" unless child # Only handles the immediate child scenario
raise ArgumentError, "Attempting add node to itself" if self == child
raise ArgumentError, "Attempting to add root as a child" if self.root == child
raise ArgumentError, "Attempting add node to itself" if self.equal?(child)
raise ArgumentError, "Attempting add root as a child" if self.equal?(root) unless self.is_root?

# Lazy mans unique test, won't test if children of child are unique in this tree too.
#self.root.each { |node| raise "Child #{child.name} already added!" if node.name == child.name }
self.children.each { |node| raise "Child #{child.name} already added!" if node.name == child.name }
raise "Child #{child.name} already added!" if @children_hash.include?(child.name)

if insertion_range.include?(at_index)
@children.insert(at_index, child)
Expand Down
2 changes: 1 addition & 1 deletion lib/tree/version.rb
Expand Up @@ -37,5 +37,5 @@
#
module Tree
# Rubytree Package Version
VERSION = '0.9.3pre'
VERSION = '0.9.3'
end
14 changes: 2 additions & 12 deletions rubytree.gemspec
Expand Up @@ -75,19 +75,9 @@ Gem::Specification.new do |s|

s.post_install_message = <<-EOF
========================================================================
Thank you for installing rubytree.
Thank you for installing RubyTree.
Note that the TreeNode#siblings method has changed in 0.8.3.
It now returns an empty array for the root node.
WARNING: SIGNIFICANT API CHANGE in 0.8.0 !
------------------------------------------
Please note that as of 0.8.0 the CamelCase method names are DEPRECATED.
The new method names follow the ruby_convention (separated by '_').
The old CamelCase methods still work (a warning will be displayed),
but may go away in the future.
Note:: As of 0.9.3, node names do not need to be globally unique.
Details of the API changes are documented in the API-CHANGES file.
========================================================================
Expand Down
20 changes: 17 additions & 3 deletions test/test_tree.rb
Expand Up @@ -359,9 +359,9 @@ def test_add_duplicate
root = Tree::TreeNode.new("root")
one = Tree::TreeNode.new("one")
two = Tree::TreeNode.new("two")
three= Tree::TreeNode.new("three")
deep = Tree::TreeNode.new("deep")


root << one << deep
# The same child cannot be added under any circumstance
assert_raise(RuntimeError) { root.add(Tree::TreeNode.new(one.name)) }
Expand All @@ -370,7 +370,17 @@ def test_add_duplicate
begin
root << two << deep
rescue RuntimeError => e
fail("Error! The RuntimeError should not have been thrown.")
fail("Error! The RuntimeError should not have been thrown. The same node can be added to different branches.")
end

assert_raise(ArgumentError) {root << three << three }

root.remove_all! # Because the first child 'three' whould have been added.
begin
three_dup = Tree::TreeNode.new("three")
root << three << three_dup
rescue RuntimeError => e
fail("Error! The RuntimeError should not have been thrown. The same node name can be used in the branch.")
end
end

Expand Down Expand Up @@ -1229,7 +1239,11 @@ def test_add_node_to_self_as_child

# And now a scenario where the node addition is done down the hierarchy
child = Tree::TreeNode.new("child")
assert_raise(ArgumentError) { root << child << root }
begin
root << child << root
rescue ArgumentError => e
fail("The ArgumentError should not have been raised.")
end
end

# Test whether the tree_leaf method works correctly
Expand Down

0 comments on commit 0432c2d

Please sign in to comment.