Permalink
Browse files

Make graph dup do a deep copy. Closes #6

  • Loading branch information...
1 parent fa99fbc commit eae33f92dbeb255dcd4c8bc3e748c7d43b2fd8e9 @D4L committed Jan 2, 2013
Showing with 52 additions and 0 deletions.
  1. +1 −0 lib/abstract_graph/graph.rb
  2. +14 −0 lib/abstract_graph/graph/dup.rb
  3. +37 −0 spec/abstract_graph/graph/dup_spec.rb
@@ -17,3 +17,4 @@ class Graph
require "abstract_graph/graph/initialize"
require "abstract_graph/graph/add_vertex"
require "abstract_graph/graph/has_vertex"
+require "abstract_graph/graph/dup"
@@ -0,0 +1,14 @@
+# required in "abstract_graph/graph"
+
+module AbstractGraph
+ class Graph
+
+ # does a deep copy of the graph
+ def dup
+ other = Graph.new
+ other.vertices = @vertices.dup
+ other
+ end
+
+ end
+end
@@ -0,0 +1,37 @@
+require 'spec_helper'
+
+module AbstractGraph
+ module Composition
+ describe Graph do
+
+ before :each do
+ @graph = Graph.new
+ @graph.add_vertex "MyVertex"
+ end
+
+ describe "#dup" do
+
+ it "returns a graph object" do
+ @graph.dup.should be_an_instance_of(Graph)
+ end
+
+ it "has a different id than the original graph" do
+ @graph.object_id.should_not == @graph.dup.object_id
+ end
+
+ it "copies over the existing vertices" do
+ graphdup = @graph.dup
+ graphdup.has_vertex?( "MyVertex" ).should be_true
+ end
+
+ it "will not modify the original if it is modified" do
+ graphdup = @graph.dup
+ graphdup.vertices.clear
+ @graph.vertices.size.should == 1
+ end
+
+ end
+
+ end
+ end
+end

0 comments on commit eae33f9

Please sign in to comment.