From 743a7efe46bc6d2b48f71e41e721d4cac36ae657 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Pavl=C3=ADk?= Date: Fri, 11 Aug 2017 22:50:33 +0200 Subject: [PATCH 1/3] describe #detached_copy --- spec/tree_spec.rb | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index 742c668..aae2f47 100755 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -71,4 +71,36 @@ it_behaves_like "any detached node" end + + shared_examples_for "any cloned node" do + it "is equal to the original" do + expect(@clone).to eq @tree + end + it "is not identical to the original" do + expect(clone).not_to be @tree + end + end + + context "#detached_copy", "without content" do + before(:each) do + @tree = Tree::TreeNode.new("A", nil) + @clone = @tree.detached_copy + end + + it_behaves_like "any cloned node" + end + + context "#detached_copy", "with clonable content" do + before(:each) do + @tree = Tree::TreeNode.new("A", "clonable content") + @clone = @tree.detached_copy + end + + it "makes a clone of the content" do + expect(@clone.content).to eq @tree.content + expect(@clone.content).not_to be @tree.content + end + + it_behaves_like "any cloned node" + end end From 564e26624e73f7830ce12359902a3b3c7dcff4fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Pavl=C3=ADk?= Date: Fri, 11 Aug 2017 22:58:02 +0200 Subject: [PATCH 2/3] handle unclonable content --- lib/tree.rb | 8 +++++++- spec/tree_spec.rb | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/tree.rb b/lib/tree.rb index 90b97af..caafea6 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -235,7 +235,13 @@ def initialize(name, content = nil) # # @return [Tree::TreeNode] A copy of this node. def detached_copy - self.class.new(@name, @content ? @content.clone : nil) + cloned_content = + begin + @content ? @content.clone : nil + rescue TypeError + @content + end + self.class.new(@name, cloned_content) end # Returns a copy of entire (sub-)tree from this node. diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index aae2f47..7beaa6d 100755 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -103,4 +103,17 @@ it_behaves_like "any cloned node" end + + context "#detached_copy", "with unclonable content" do + before(:each) do + @tree = Tree::TreeNode.new("A", :unclonable_content) + @clone = @tree.detached_copy + end + + it "keeps the content" do + expect(@clone.content).to be @tree.content + end + + it_behaves_like "any cloned node" + end end From d45517dd1bf3095eec167875a6d9ad10377e4cf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Pavl=C3=ADk?= Date: Fri, 11 Aug 2017 23:06:00 +0200 Subject: [PATCH 3/3] do not swap false for nil in #detached_copy --- lib/tree.rb | 2 +- spec/tree_spec.rb | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/tree.rb b/lib/tree.rb index caafea6..91212f2 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -237,7 +237,7 @@ def initialize(name, content = nil) def detached_copy cloned_content = begin - @content ? @content.clone : nil + @content && @content.clone rescue TypeError @content end diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index 7beaa6d..7ecdca7 100755 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -116,4 +116,17 @@ it_behaves_like "any cloned node" end + + context "#detached_copy", "with false as content" do + before(:each) do + @tree = Tree::TreeNode.new("A", false) + @clone = @tree.detached_copy + end + + it "keeps the content" do + expect(@clone.content).to be @tree.content + end + + it_behaves_like "any cloned node" + end end