Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow graphs to add vertices. Part of vertex operations for #3 and cl…
…oses #1
- Loading branch information
Showing
13 changed files
with
181 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# required in "abstract_graph/composition" | ||
|
||
module AbstractGraph | ||
module Composition | ||
|
||
# public UniqueNameCollection class | ||
# Note that the collection of this | ||
# class must implement #name | ||
class UniqueNameCollection | ||
attr_accessor :collection | ||
|
||
end | ||
end | ||
end | ||
|
||
require "abstract_graph/composition/uniquenamecollection/initialize" | ||
require "abstract_graph/composition/uniquenamecollection/add" |
19 changes: 19 additions & 0 deletions
19
lib/abstract_graph/composition/uniquenamecollection/add.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# required in "abstract_graph/composition/uniquenamecollection" | ||
|
||
module AbstractGraph | ||
module Composition | ||
class UniqueNameCollection | ||
|
||
# adds an object o into the collection | ||
# p: Object o that implements #name | ||
def add( o ) | ||
if @collection[o.name] | ||
raise IndexError | ||
else | ||
@collection[o.name] = o | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |
13 changes: 13 additions & 0 deletions
13
lib/abstract_graph/composition/uniquenamecollection/initialize.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# required in "abstract_graph/composition/uniquenamecollection" | ||
|
||
module AbstractGraph | ||
module Composition | ||
class UniqueNameCollection | ||
|
||
def initialize | ||
@collection = Hash.new | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# required in "abstract_graph/graph" | ||
|
||
module AbstractGraph | ||
class Graph | ||
|
||
# add a vertex named s to the graph | ||
# p: String s represents the name of the wanted vertex | ||
def add_vertex( s ) | ||
# create the vertex | ||
vertex = Vertex.new s | ||
@vertices.add vertex | ||
self | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ class Graph | |
# public constructor | ||
def initialize | ||
|
||
@vertices = UniqueNameCollection.new | ||
|
||
end | ||
|
||
end | ||
|
40 changes: 40 additions & 0 deletions
40
spec/abstract_graph/composition/uniquenamecollection/add_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'spec_helper' | ||
|
||
module AbstractGraph | ||
module Composition | ||
describe UniqueNameCollection do | ||
|
||
before(:each) do | ||
|
||
@collection = UniqueNameCollection.new | ||
end | ||
|
||
describe "#add(Object)" do | ||
|
||
it "tracks an object that has implemented #name" do | ||
dummy = Dummy.new | ||
dummy.name = "Hello" | ||
@collection.add dummy | ||
end | ||
|
||
it "doesn't allow us to add the same object twice" do | ||
dummy = Dummy.new | ||
dummy.name = "Hello" | ||
@collection.add dummy | ||
expect { @collection.add dummy }.to raise_error | ||
end | ||
|
||
it "doesn't allow two objects to have the same name" do | ||
dummy = Dummy.new | ||
dummy2 = Dummy.new | ||
dummy.name = "Hello" | ||
dummy2.name = "Hello" | ||
@collection.add dummy | ||
expect { @collection.add dummy2 }.to raise_error | ||
end | ||
|
||
end | ||
|
||
end | ||
end | ||
end |
21 changes: 21 additions & 0 deletions
21
spec/abstract_graph/composition/uniquenamecollection/initialize_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'spec_helper' | ||
|
||
module AbstractGraph | ||
module Composition | ||
describe UniqueNameCollection do | ||
|
||
before (:each) do | ||
@collection = UniqueNameCollection.new | ||
end | ||
|
||
describe "#new" do | ||
|
||
it "returns an object of class UniqueNameCollection" do | ||
@collection.should be_an_instance_of(UniqueNameCollection) | ||
end | ||
|
||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
require 'spec_helper' | ||
|
||
module AbstractGraph | ||
module Composition | ||
describe UniqueNameCollection do | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require 'spec_helper' | ||
|
||
module AbstractGraph | ||
describe Graph do | ||
|
||
before :each do | ||
@graph = Graph.new | ||
end | ||
|
||
describe "#add_vertex(String)" do | ||
|
||
it "returns an object of class Graph" do | ||
@graph.add_vertex( "MyVertex" ).should be_an_instance_of(Graph) | ||
end | ||
|
||
it "throws an exception when adding vertex of existing name" do | ||
@graph.add_vertex( "MyVertex" ) | ||
expect { @graph.add_vertex( "MyVertex" ) }.to raise_error | ||
end | ||
|
||
it "allows two different graphs to have the same names" do | ||
graph2 = Graph.new | ||
@graph.add_vertex( "MyVertex" ) | ||
expect { graph2.add_vertex( "MyVertex" ) }.to_not raise_error | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# simple class that implements #name | ||
class Dummy | ||
attr_accessor :name | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
require 'dummy_helper' | ||
require 'rspec' | ||
require 'abstract_graph' | ||
|
||
|