Merged in SimpleCov from master#
authorNeil Smith <neil.github@njae.me.uk>
Wed, 18 Jul 2012 08:35:50 +0000 (09:35 +0100)
committerNeil Smith <neil.github@njae.me.uk>
Wed, 18 Jul 2012 08:35:50 +0000 (09:35 +0100)
1  2 
lib/graph.njae/vertex.rb
spec/graph/edge_spec.rb
spec/graph/vertex_spec.rb

diff --combined lib/graph.njae/vertex.rb
index 4ac903c2bea7f9424ab34ecb9c6a8b42a57cab88,aca3fa9aea23baccb74156c6fe8ce29fd3d17477..def0b66d79a81ff2b0348655d1459cab1720aefe
@@@ -1,3 -1,5 +1,3 @@@
 -require 'ostruct'
 -
  # A simple graph library
  
  module GraphNjae
      end
      
      def to_s
-       '<V: ' + self.name + '>'
+       '<V: ' + self.name.to_s + '>'
      end
      
 +    def to_dot(opts = {})
 +      if block_given?
 +        yield self
 +      else
 +        dot = self.object_id.to_s
 +        if opts.size > 0
 +          dot << ' {'
 +          dot << opts.keys.map { |k|
 +            (k.to_s + ' = "' + self.instance_eval(opts[k].to_s).to_s) + '"'
 +                        }.join(', ')
 +          dot << '}'
 +        end
 +        dot << ';'
 +      end
 +    end
 +    
    end
  end
diff --combined spec/graph/edge_spec.rb
index 938890fcb251f1e333a5305968fd2d2f391d2df6,f4be16b5e3787353e08e5261133d47f83cc06edb..23537560ba40be5ce0ef39d277710951b8f43081
@@@ -3,35 -3,43 +3,45 @@@ require File.expand_path(File.dirname(_
  module GraphNjae
    describe Edge do
      let (:e) { Edge.new }
 -    describe "#initialize" do
 -      it "creates an empty edge" do
 +    let(:v1) {Vertex.new :name => :v1}
 +    let(:v2) {Vertex.new :name => :v2}
 +    let(:v3) {Vertex.new :name => :v3}
 +
 +    describe '#initialize' do
 +      it 'creates an empty edge' do
          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
 +      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.value2.should == 'value2'
          e.value3.should == :v3
          e.value4.should be_nil
        end
      end # #initialize
      
 -    describe "adds attribues" do
 -      it "adds then reports arbitrary attributes" do
 +    describe 'adds attribues' do
 +      it 'adds then reports arbitrary attributes' do
          e.score = 15
          e.score.should == 15
        end
      end # adds attributes
      
-     describe '#<<' do
-       it 'adds a new vertex to an edge (with a connection)' do
+     describe "#to_s" do
+       it "returns the string form of an edge" do
+         v1 = Vertex.new :name => :v1
+         v2 = Vertex.new :name => :v2
+         e.type = :test
+         e << v1 << v2
+         e.to_s.should == '<E: test [<V: v1>, <V: v2>] >'
+       end
+     end
+     
+     describe "#<<" do
+       it "adds a new vertex to an edge (with a connection)" do
          e.connections.should be_empty
 -        v1 = Vertex.new :name => :v1
 -        v2 = Vertex.new :name => :v2
          e << v1
          e.should have(1).connections
          e.should have(1).vertices
          v2.edges.should include(e)
        end
        
 -      it "adds several vertices to an edge" do
 +      it 'adds several vertices to an edge' do
          e.connections.should be_empty
 -        v1 = Vertex.new :name => :v1
 -        v2 = Vertex.new :name => :v2
          e << v1 << v2
          e.vertices.should include(v1)
          e.vertices.should include(v2)
          e.should have(2).vertices
        end
  
 -      it "adds a self-loop" do
 +      it 'adds a self-loop' do
          e.connections.should be_empty
 -        v1 = Vertex.new
          e << v1 << v1
          e.vertices.should include(v1)
          e.should have(2).vertices
        end
      end # #<<
      
 -    describe "connection_at" do
 -      it "returns the connection that links to a vertex" do
 +    describe 'connection_at' do
 +      it 'returns the connection that links to a vertex' do
          e.connections.should be_empty
 -        v1 = Vertex.new :name => :v1
 -        v2 = Vertex.new :name => :v2
          e << v1 << v2
          
          e.connection_at(v1).end.should be v1
          e.connection_at(v2).end.should be v2
        end
        
 -      it "returns nil if there is no connection to that vertex" do
 +      it 'returns nil if there is no connection to that vertex' do
          e.connections.should be_empty
 -        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
        end
        
 -      it "returns the vertex for a self-loop" do
 +      it 'returns the vertex for a self-loop' do
          e.connections.should be_empty
 -        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
 +    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
 +      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
 +      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
 +      it 'returns nil if it cannot return something sensible' do
          e.other_end(v1).should be_nil
          e << v1
          e.other_end(v1).should be_nil
        end
      end # other_end
 +    
 +    describe '#to_dot' do
 +      it 'describes an edge in dot notation' do
 +        e << v1 << v2
 +        e.to_dot.should == "#{v1.object_id.to_s} -- #{v2.object_id.to_s};"
 +      end
 +      
 +      it 'describes an edge in dot notation, using given attributes' do
 +        e << v1 << v2
 +        e.name = 'Edge name'
 +        edot = e.to_dot :label => :name
 +        edot.should == "#{v1.object_id.to_s} -- #{v2.object_id.to_s} {label = \"#{e.name}\"};"
 +      end
 +      
 +      it 'describes an edge in dot notation, given a block' do
 +        e << v1 << v2
 +        e.to_dot {|e| e.object_id.to_s}.should == e.object_id.to_s
 +      end
 +    end # dot
    end # Edge
    
    describe Connection do
      let (:c) {Connection.new }
      
 -    describe "adds attribues" do
 -      it "adds then reports arbitrary attributes" do
 +    describe 'adds attribues' do
 +      it 'adds then reports arbitrary attributes' do
          c.score = 15
          c.score.should == 15
        end
index 9d109db3aced26dfdb072fdea7eed4ca9439985b,33b9e0313ea4d360a8cfad9168f464fb2a2a29d7..ca9f7eb715687d7ee3ec69e16463c3f9327f4342
@@@ -4,30 -4,38 +4,38 @@@ module GraphNja
    describe Vertex do
      let (:v) { Vertex.new }
      
 -    describe "#initialize" do
 -      it "creates an empty vertex" do
 +    describe '#initialize' do
 +      it 'creates an empty vertex' do
          v = Vertex.new
          v.edges.should be_empty
        end
        
 -      it "creates a vertex with some parameters" do
 -        v = Vertex.new :value1 => 1, :value2 => "value2", :value3 => :v3
 +      it 'creates a vertex with some parameters' do
 +        v = Vertex.new :value1 => 1, :value2 => 'value2', :value3 => :v3
          v.value1.should == 1
 -        v.value2.should == "value2"
 +        v.value2.should == 'value2'
          v.value3.should == :v3
          v.value4.should be_nil
        end
      end # #initialize
      
 -    describe "adds attribues" do
 -      it "adds then reports arbitrary attributes" do
 +    describe 'adds attribues' do
 +      it 'adds then reports arbitrary attributes' do
          v.score = 15
          v.score.should == 15
        end
      end # adds attributes
+     describe "#to_s" do
+       it "returns the string form of a vertex" do
+         v.name = :v1
+         v.to_s.should == '<V: v1>'
+       end
+     end
      
 -    describe "#<<" do
 -      it "adds a single edge between vertices" do
 +    describe '#<<' do
 +      it 'adds a single edge between vertices' do
          v.neighbours.should be_empty
          v.edges.should be_empty
          
@@@ -49,7 -57,7 +57,7 @@@
          v1.neighbours.should_not include(v1)
        end
  
 -      it "adds a single edge as a self-loop" do
 +      it 'adds a single edge as a self-loop' do
          v.neighbours.should be_empty
          v.edges.should be_empty
          
@@@ -61,8 -69,8 +69,8 @@@
        end
      end # #<<
      
 -    describe "connect" do
 -      it "connects two vertices" do
 +    describe 'connect' do
 +      it 'connects two vertices' do
          v1 = Vertex.new
          v1.id = :v1 # Need this to ensure that v != v1
  
          e.should have(2).connections
        end
  
 -      it "connects two vertices by an edge with attributes" do
 +      it 'connects two vertices by an edge with attributes' do
          v1 = Vertex.new
          v1.id = :v1
          e = v.connect(v1, {:type => :edge_type})
          e.type.should == :edge_type
        end
        
 -      it "creates a self-connection" do
 +      it 'creates a self-connection' do
          e = v.connect v
          
          v.should have(1).neighbours
          e.should have(2).connections
        end
  
 -      it "creates a self-connection with an edge with attributes" do
 +      it 'creates a self-connection with an edge with attributes' do
          e = v.connect(v, {:type => :edge_type})
          e.type.should == :edge_type
        end
  
      end # #connect
      
 -    describe "#neighbours" do
 -      it "finds neighbours of a self loop" do
 +    describe '#neighbours' do
 +      it 'finds neighbours of a self loop' do
          v << v
          v.should have(1).neighbours
          v.neighbours.should include v
        end
        
 -      it "finds neighbours on an edge" do
 +      it 'finds neighbours on an edge' do
          v1 = Vertex.new
          v << v1
          v.should have(1).neighbours
          v1.neighbours.should include v
        end
        
 -      it "finds neighbours with multiple edges" do
 +      it 'finds neighbours with multiple edges' do
          v1 = Vertex.new
          v1.id = :v1
          v << v1
          v1.neighbours.should_not include v1
        end
        
 -      it "finds neighbours with multiple self loops" do
 +      it 'finds neighbours with multiple self loops' do
          v << v << v << v
          v.should have(3).neighbours
          v.neighbours.should include v
          v.neighbours.uniq.length.should == 1
        end
        
 -      it "finds neighbours with all sorts of edges" do
 +      it 'finds neighbours with all sorts of edges' do
          v1 = Vertex.new ; v1.id = :v1
          v2 = Vertex.new ; v2.id = :v2
          v3 = Vertex.new ; v3.id = :v3
          v3.neighbours.should include v2
        end
        
 -      it "finds neighbours in graphs with several vertices" do
 +      it 'finds neighbours in graphs with several vertices' do
          v1 = Vertex.new ; v1.id = :v1
          v2 = Vertex.new ; v2.id = :v2
          v3 = Vertex.new ; v3.id = :v3
          v4.neighbours.should_not include v4
        end
        
 -      it "finds neighbours with multiple edges between vertices" do
 +      it 'finds neighbours with multiple edges between vertices' do
          v1 = Vertex.new ; v1.id = :v1
          v2 = Vertex.new ; v2.id = :v2
          v3 = Vertex.new ; v3.id = :v3
          v3.neighbours.uniq.length.should == 1
        end
      end # #neighbours
 +    
 +    describe '#to_dot' do
 +      it 'describes a vertex in dot notation' do
 +        v = Vertex.new
 +        v.to_dot.should == "#{v.object_id.to_s};"
 +      end
 +
 +      it 'describes a vertex in dot notation, using given attributes' do
 +        v = Vertex.new
 +        v.name = 'vertex'
 +        v.shape = 'house'
 +        vdot = v.to_dot :label => :name, :shape => :shape
 +        vdot.should == "#{v.object_id.to_s} {label = \"#{v.name}\", shape = \"#{v.shape}\"};"
 +      end
 +      
 +      it 'describes a vertex using a block' do
 +        v = Vertex.new
 +        v.field1 = 'f1'
 +        vdot = v.to_dot {|v| v.field1 + ';'}
 +        vdot.should == "#{v.field1};"
 +      end
 +    end # #to_dot
    end
  end