# Add a Vertex or Edge to the graph.
def <<(other)
- if other.class == Vertex
+ if other.class.ancestors.include? Vertex
self.vertices << other
- elsif
+ elsif other.class.ancestors.include? Edge
self.edges << other
end
self
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
module GraphNjae
+
+ class SVertex < Vertex
+ end
+
+ class SEdge < Edge
+ end
+
describe Graph do
let (:g) { Graph.new }
describe "#initialize" do
g.edges.should include(e1)
g.edges.should include(e2)
end
+
+ it "adds a subclass of Vertex" do
+ g.vertices.should be_empty
+ v1 = SVertex.new
+ v2 = SVertex.new
+ g << v1 << v2
+ g.should have(2).vertices
+ g.vertices.should include(v1)
+ g.vertices.should include(v2)
+ end
end # #<<
describe "connect" do