From 1bd42608db15715f695d7dbaa95e6bf9429bcc2b Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Wed, 18 Jul 2012 10:42:36 +0100 Subject: [PATCH] Got #to_dot working with blocks --- lib/graph.njae/graph.rb | 4 ++-- spec/graph/graph_spec.rb | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/graph.njae/graph.rb b/lib/graph.njae/graph.rb index f3d0032..eb2ddc6 100644 --- a/lib/graph.njae/graph.rb +++ b/lib/graph.njae/graph.rb @@ -46,7 +46,7 @@ module GraphNjae if vertex_block.nil? dot << v.to_dot(vertex_args) else - dot << v.do_dot(&vertex_block) + dot << v.to_dot(&vertex_block) end dot << "\n" end @@ -54,7 +54,7 @@ module GraphNjae if edge_block.nil? dot << e.to_dot(edge_args) else - dot << e.do_dot(&lambda {edge_block}) + dot << e.to_dot(&edge_block) end dot << "\n" end diff --git a/spec/graph/graph_spec.rb b/spec/graph/graph_spec.rb index 7da0bb9..88724a4 100644 --- a/spec/graph/graph_spec.rb +++ b/spec/graph/graph_spec.rb @@ -131,10 +131,27 @@ module GraphNjae v1 = Vertex.new(:name => :v1) v2 = Vertex.new(:name => :v2) g.connect(v1, v2, :type => :edge_type) - gdot = g.to_dot(:vertex_block => lambda {|v| v.name}) + gdot = g.to_dot(:vertex_block => lambda {|v| v.name.to_s + ';'}) gdot.should == "graph {\n#{v1.name};\n#{v2.name};\n#{v1.object_id.to_s} -- #{v2.object_id.to_s};\n}" end + it "gives the dot notation of a simple graph, with vertex and edge attributes" do + v1 = Vertex.new(:name => :v1) + v2 = Vertex.new(:name => :v2) + g.connect(v1, v2, :type => :edge_type) + gdot = g.to_dot(:edge_args => {:label => :type}, :vertex_args => {:label => :name}) + gdot.should == "graph {\n#{v1.object_id.to_s} {label = \"#{v1.name}\"};\n#{v2.object_id.to_s} {label = \"#{v2.name}\"};\n#{v1.object_id.to_s} -- #{v2.object_id.to_s} {label = \"#{g.edges[0].type}\"};\n}" + end + + it "gives the dot notation of a simple graph, with vertices and edges generated by a block" do + v1 = Vertex.new(:name => :v1) + v2 = Vertex.new(:name => :v2) + g.connect(v1, v2, :type => :edge_type) + gdot = g.to_dot(:vertex_block => lambda {|v| v.name.to_s + ';'}, + :edge_block => lambda {|e| "#{e.vertices[0].name.to_s} -- #{e.vertices[1].name.to_s} {label = \"#{e.type.to_s}\"};"}) + gdot.should == "graph {\n#{v1.name};\n#{v2.name};\n#{v1.name} -- #{v2.name} {label = \"#{g.edges[0].type}\"};\n}" + end + end # #to_dot describe 'product' do -- 2.34.1