Skip to content

Commit ec6ab3b

Browse files
committed
Fix to_s empty content
Render '<Empty>' when a node has nil content and add tests for both nil and non-nil content values.
1 parent 085c3a4 commit ec6ab3b

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

lib/tree.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ def marshal_load(dumped_tree_array)
318318
#
319319
# @return [String] A string representation of the node.
320320
def to_s
321-
"Node Name: #{@name} Content: #{@content.to_s || '<Empty>'} " \
321+
content_str = @content.nil? ? '<Empty>' : @content.to_s
322+
"Node Name: #{@name} Content: #{content_str} " \
322323
"Parent: #{root? ? '<None>' : @parent.name.to_s} " \
323324
"Children: #{@children.length} Total Nodes: #{size}"
324325
end

test/test_tree.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def test_root_setup
9494
assert_not_nil(@root.name, 'Name should not be nil')
9595
assert_equal('ROOT', @root.name, "Name should be 'ROOT'")
9696
assert_equal('Root Node', @root.content, "Content should be 'Root Node'")
97+
assert(@root.to_s.include?('Content: Root Node'), 'to_s should include content value')
9798
assert(@root.root?, 'Should identify as root')
9899
assert(!@root.children?, 'Cannot have any children')
99100
assert(@root.content?, 'This root should have content')
@@ -117,6 +118,11 @@ def test_root
117118
assert_equal(2, @root.node_height, "Root's height after adding the children should be 2")
118119
end
119120

121+
def test_to_s_empty_content
122+
empty = Tree::TreeNode.new('EMPTY')
123+
assert_match(/Content: <Empty>/, empty.to_s)
124+
end
125+
120126
def test_from_hash
121127
# A
122128
# / | \

0 commit comments

Comments
 (0)