End of the day
[graph.njae.git] / spec / graph / edge_spec.rb
index 8bd1f9abf17d9826ac530e0d460fec5c77caf79d..8fc71ba76c5bfa42b7ee3843e32d144c3a111e5a 100644 (file)
@@ -1,6 +1,6 @@
 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
 
-module Graph
+module GraphNjae
   describe Edge do
     let (:e) { Edge.new }
     describe "#initialize" do
@@ -8,6 +8,14 @@ module Graph
         e = Edge.new
         e.connections.should be_empty
       end
+      
+      it "creates an edge with some parameters" do
+        e = Edge.new :value1 => 1, :value2 => "value2", :value3 => :v3
+        e.value1.should == 1
+        e.value2.should == "value2"
+        e.value3.should == :v3
+        e.value4.should be_nil
+      end
     end # #initialize
     
     describe "adds attribues" do
@@ -20,23 +28,25 @@ module Graph
     describe "#<<" do
       it "adds a new vertex to an edge (with a connection)" do
         e.connections.should be_empty
-        v1 = Vertex.new
-        v2 = Vertex.new
+        v1 = Vertex.new :name => :v1
+        v2 = Vertex.new :name => :v2
         e << v1
         e.should have(1).connections
         e.should have(1).vertices
         e.vertices.should include(v1)
+        v1.edges.should include(e)
         e << v2
         e.should have(2).connections
         e.should have(2).vertices
         e.vertices.should include(v1)
         e.vertices.should include(v2)
+        v2.edges.should include(e)
       end
       
       it "adds several vertices to an edge" do
         e.connections.should be_empty
-        v1 = Vertex.new
-        v2 = Vertex.new
+        v1 = Vertex.new :name => :v1
+        v2 = Vertex.new :name => :v2
         e << v1 << v2
         e.vertices.should include(v1)
         e.vertices.should include(v2)
@@ -56,8 +66,8 @@ module Graph
     describe "connection_at" do
       it "returns the connection that links to a vertex" do
         e.connections.should be_empty
-        v1 = Vertex.new
-        v2 = Vertex.new
+        v1 = Vertex.new :name => :v1
+        v2 = Vertex.new :name => :v2
         e << v1 << v2
         
         e.connection_at(v1).end.should be v1
@@ -66,9 +76,9 @@ module Graph
       
       it "returns nil if there is no connection to that vertex" do
         e.connections.should be_empty
-        v1 = Vertex.new
-        v2 = Vertex.new
-        v3 = Vertex.new
+        v1 = Vertex.new :name => :v1
+        v2 = Vertex.new :name => :v2
+        v3 = Vertex.new :name => :v3
         e << v1 << v2
         
         e.connection_at(v3).should be nil
@@ -76,14 +86,49 @@ module Graph
       
       it "returns the vertex for a self-loop" do
         e.connections.should be_empty
-        v1 = Vertex.new
+        v1 = Vertex.new :name => :v1
         e << v1 << v1
         
         e.connection_at(v1).end.should be v1
       end
-
-    
     end # #connection_at
+    
+    describe "other_end" do
+      it "returns the vertex at the other end of the given one" do
+        e.connections.should be_empty
+        v1 = Vertex.new :name => :v1
+        v2 = Vertex.new :name => :v2
+        e << v1 << v2
+        
+        e.other_end(v1).should be v2
+        e.other_end(v2).should be v1
+      end
+      
+      it "returns the same vertex in a self-loop" do
+        e.connections.should be_empty
+        v1 = Vertex.new :name => :v1
+        e << v1 << v1
+        
+        e.other_end(v1).should be v1
+      end
+      
+      it "returns one of the connected edges if given a vertex not connected to it" do
+        e.connections.should be_empty
+        v1 = Vertex.new :name => :v1
+        v2 = Vertex.new :name => :v2
+        v3 = Vertex.new :name => :v3
+        e << v1 << v2
+        
+        [v1, v2].should include e.other_end(v3)
+      end 
+      
+      it "returns nil if it can't return something sensible" do
+        v1 = Vertex.new :name => :v1
+        e.other_end(v1).should be_nil
+        e << v1
+        e.other_end(v1).should be_nil
+      end
+    end # other_end
   end # Edge
   
   describe Connection do