Skip to content

Commit 665b783

Browse files
committed
Merge branch 'pr-65-unclonable-content'
2 parents eabd410 + d45517d commit 665b783

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

lib/tree.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,13 @@ def initialize(name, content = nil)
229229
#
230230
# @return [Tree::TreeNode] A copy of this node.
231231
def detached_copy
232-
self.class.new(@name, @content ? @content.clone : nil)
232+
cloned_content =
233+
begin
234+
@content && @content.clone
235+
rescue TypeError
236+
@content
237+
end
238+
self.class.new(@name, cloned_content)
233239
end
234240

235241
# Returns a copy of entire (sub-)tree from this node.

spec/tree_spec.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,62 @@ class SpecializedTreeNode < Tree::TreeNode; end
115115
expect(Marshal.load(serialized_node2)).to be_a(Tree::TreeNode)
116116
end
117117
end
118+
119+
shared_examples_for "any cloned node" do
120+
it "is equal to the original" do
121+
expect(@clone).to eq @tree
122+
end
123+
it "is not identical to the original" do
124+
expect(clone).not_to be @tree
125+
end
126+
end
127+
128+
context "#detached_copy", "without content" do
129+
before(:each) do
130+
@tree = Tree::TreeNode.new("A", nil)
131+
@clone = @tree.detached_copy
132+
end
133+
134+
it_behaves_like "any cloned node"
135+
end
136+
137+
context "#detached_copy", "with clonable content" do
138+
before(:each) do
139+
@tree = Tree::TreeNode.new("A", "clonable content")
140+
@clone = @tree.detached_copy
141+
end
142+
143+
it "makes a clone of the content" do
144+
expect(@clone.content).to eq @tree.content
145+
expect(@clone.content).not_to be @tree.content
146+
end
147+
148+
it_behaves_like "any cloned node"
149+
end
150+
151+
context "#detached_copy", "with unclonable content" do
152+
before(:each) do
153+
@tree = Tree::TreeNode.new("A", :unclonable_content)
154+
@clone = @tree.detached_copy
155+
end
156+
157+
it "keeps the content" do
158+
expect(@clone.content).to be @tree.content
159+
end
160+
161+
it_behaves_like "any cloned node"
162+
end
163+
164+
context "#detached_copy", "with false as content" do
165+
before(:each) do
166+
@tree = Tree::TreeNode.new("A", false)
167+
@clone = @tree.detached_copy
168+
end
169+
170+
it "keeps the content" do
171+
expect(@clone.content).to be @tree.content
172+
end
173+
174+
it_behaves_like "any cloned node"
175+
end
118176
end

0 commit comments

Comments
 (0)