File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -394,6 +394,10 @@ def add(child, at_index = -1)
394394
395395 raise ArgumentError , 'Attempting add root as a child' if child . equal? ( root )
396396
397+ if ( ancestors = parentage ) && ancestors . include? ( child )
398+ raise ArgumentError , 'Attempting add ancestor as a child'
399+ end
400+
397401 # Lazy man's unique test, won't test if children of child are unique in
398402 # this tree too.
399403 raise "Child #{ child . name } already added!" \
Original file line number Diff line number Diff line change @@ -491,6 +491,10 @@ def test_add
491491
492492 # Test the addition of a nil node.
493493 assert_raise ( ArgumentError ) { @root . add ( nil ) }
494+
495+ # Test adding an ancestor as a child (cycle prevention).
496+ error = assert_raise ( ArgumentError ) { @child4 . add ( @child3 ) }
497+ assert_match ( /Attempting add ancestor as a child/ , error . message )
494498 end
495499
496500 # Test the addition of a duplicate node (duplicate being defined as a node with the same name).
You can’t perform that action at this time.
0 commit comments