From 99e4223d9b3baef392d4a6b0b996078f06fcadf7 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Mon, 2 Jul 2012 16:31:19 +0100 Subject: [PATCH] Tidied up the let clauses in edge_spec --- lib/graph.njae.rb | 8 +++++--- lib/graph.njae/edge.rb | 19 +++++++++++++++-- lib/graph.njae/graph.rb | 2 -- lib/graph.njae/vertex.rb | 18 ++++++++++++++-- spec/graph/edge_spec.rb | 43 +++++++++++++++++++++++---------------- spec/graph/vertex_spec.rb | 22 ++++++++++++++++++++ 6 files changed, 85 insertions(+), 27 deletions(-) diff --git a/lib/graph.njae.rb b/lib/graph.njae.rb index ac4e0b3..0fea6d7 100644 --- a/lib/graph.njae.rb +++ b/lib/graph.njae.rb @@ -1,5 +1,7 @@ require 'bundler/setup' -require 'graph.njae/graph' -require 'graph.njae/edge' -require 'graph.njae/vertex' +require 'ostruct' + +require_relative 'graph.njae/graph' +require_relative 'graph.njae/edge' +require_relative 'graph.njae/vertex' diff --git a/lib/graph.njae/edge.rb b/lib/graph.njae/edge.rb index f3efc91..7fd99b7 100644 --- a/lib/graph.njae/edge.rb +++ b/lib/graph.njae/edge.rb @@ -1,5 +1,3 @@ -require 'ostruct' - # A simple graph library module GraphNjae @@ -48,6 +46,23 @@ module GraphNjae def to_s '' end + + def to_dot(opts = {}) + if block_given? + yield self + else + dot = self.connections[0].end.object_id.to_s + " -- " + self.connections[1].end.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 # A connection between an Edge and a Vertex.The connection can have arbitrary attributes, diff --git a/lib/graph.njae/graph.rb b/lib/graph.njae/graph.rb index bdf75b2..37b7720 100644 --- a/lib/graph.njae/graph.rb +++ b/lib/graph.njae/graph.rb @@ -1,5 +1,3 @@ -require 'ostruct' - require 'logger' $log = Logger.new(STDERR) $log.level = Logger::WARN diff --git a/lib/graph.njae/vertex.rb b/lib/graph.njae/vertex.rb index 6e46dcb..4ac903c 100644 --- a/lib/graph.njae/vertex.rb +++ b/lib/graph.njae/vertex.rb @@ -1,5 +1,3 @@ -require 'ostruct' - # A simple graph library module GraphNjae @@ -43,5 +41,21 @@ module GraphNjae '' 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 --git a/spec/graph/edge_spec.rb b/spec/graph/edge_spec.rb index 8fc71ba..bd823bf 100644 --- a/spec/graph/edge_spec.rb +++ b/spec/graph/edge_spec.rb @@ -3,6 +3,10 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') module GraphNjae describe Edge do let (:e) { Edge.new } + 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 @@ -28,8 +32,6 @@ module GraphNjae 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 @@ -45,8 +47,6 @@ module GraphNjae 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) @@ -55,7 +55,6 @@ module GraphNjae 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 @@ -66,8 +65,6 @@ module GraphNjae 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 @@ -76,9 +73,6 @@ module GraphNjae 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 @@ -86,7 +80,6 @@ module GraphNjae 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 @@ -96,8 +89,6 @@ module GraphNjae 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 @@ -106,7 +97,6 @@ module GraphNjae 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 @@ -114,21 +104,38 @@ module GraphNjae 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 + + 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 + #vdot = v1.to_dot :label => :name, :shape => :shape + #vdot.should == "#{v.object_id.to_s} {label = \"vertex\", shape = \"house\"};" + 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 diff --git a/spec/graph/vertex_spec.rb b/spec/graph/vertex_spec.rb index e091289..3689c0b 100644 --- a/spec/graph/vertex_spec.rb +++ b/spec/graph/vertex_spec.rb @@ -242,5 +242,27 @@ module GraphNjae 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 = \"vertex\", shape = \"house\"};" + 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 -- 2.34.1