Skip to content

Commit 2d66fef

Browse files
committed
Validate options type for TreeNode (#45)
Raise ArgumentError when a non-hash options value is provided to Tree::TreeNode.new, and add coverage in the core test suite.
1 parent 633f6cf commit 2d66fef

2 files changed

Lines changed: 4 additions & 1 deletion

File tree

lib/tree.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,9 @@ def validate_acyclic!
271271
#
272272
# @see #[]
273273
def initialize(name, content = nil, options = nil)
274-
options = {} unless options.is_a?(Hash)
274+
raise ArgumentError, 'Options must be a hash' if options && !options.is_a?(Hash)
275+
276+
options ||= {}
275277
@checks_enabled = options.fetch(:checks, true)
276278

277279
raise ArgumentError, 'Node name HAS to be provided!' if checks_enabled? && name.nil?

test/test_tree.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def test_root_setup
8080
assert_equal(0, @root.in_degree, 'Root should have an in-degree of 0')
8181
assert_equal(0, @root.node_height, "Root's height before adding any children is 0")
8282
assert_raise(ArgumentError) { Tree::TreeNode.new(nil) }
83+
assert_raise(ArgumentError) { Tree::TreeNode.new('ROOT', nil, :bad) }
8384
end
8485

8586
def test_root

0 commit comments

Comments
 (0)