Skip to content

Commit

Permalink
Make graph dup do a deep copy. Closes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
D4L committed Jan 2, 2013
1 parent fa99fbc commit eae33f9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/abstract_graph/graph.rb
Expand Up @@ -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"
14 changes: 14 additions & 0 deletions lib/abstract_graph/graph/dup.rb
@@ -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
37 changes: 37 additions & 0 deletions spec/abstract_graph/graph/dup_spec.rb
@@ -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.