Skip to content
Browse files

Make vertex names unique

  • Loading branch information...
1 parent b6b5ba6 commit ff05413d4b0dc2b729d074b8c94db048bfdc152b @D4L committed Aug 14, 2012
View
3 lib/abstract_graph/composition/vertex.rb
@@ -6,10 +6,13 @@ module Composition
# public Vertex class
class Vertex
attr_reader :name
+ @@names = []
def name=(name)
raise ArgumentError if name.class != String
+ @@names.delete @name
@name = name
+ @@names.push name
end
end
View
5 lib/abstract_graph/composition/vertex/initialize.rb
@@ -6,7 +6,10 @@ class Vertex
def initialize (name="", value="")
@name = name
-
+ if @@names.include? name
+ throw Exception
+ end
+ @@names.push name
end
end
View
5 spec/abstract_graph/composition/vertex/initialize_spec.rb
@@ -1,6 +1,11 @@
require 'spec_helper'
describe AbstractGraph::Composition::Vertex do
+
+ before :each do
+ AbstractGraph::Composition::Vertex.class_variable_set :@@names, []
+ end
+
describe "#new" do
before (:each) do
View
19 spec/abstract_graph/composition/vertex/name_spec.rb
@@ -1,6 +1,11 @@
require 'spec_helper'
describe AbstractGraph::Composition::Vertex do
+
+ before :each do
+ AbstractGraph::Composition::Vertex.class_variable_set :@@names, []
+ end
+
describe "#name" do
before (:each) do
@@ -33,5 +38,19 @@
expect { @vertex.name = 100 }.to raise_error
end
+ it "doesn't allow two objects to have the same name" do
+ @vertex.name = "v1"
+ expect { AbstractGraph::Composition::Vertex.new("v1") }.to raise_error
+ end
+
+ it "can change a name of a vertex to free up the name" do
+ @vertex.name = "v1"
+ begin
+ vertex2 = AbstractGraph::Composition::Vertex.new()
+ rescue Exception => e
+ fail
+ end
+ end
+
end
end

0 comments on commit ff05413

Please sign in to comment.
Something went wrong with that request. Please try again.