bd823bfd919ab882fed7d7267710413018b648e6
[graph.njae.git] / spec / graph / edge_spec.rb
1 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
3 module GraphNjae
4 describe Edge do
5 let (:e) { Edge.new }
6 let(:v1) {Vertex.new :name => :v1}
7 let(:v2) {Vertex.new :name => :v2}
8 let(:v3) {Vertex.new :name => :v3}
9
10 describe "#initialize" do
11 it "creates an empty edge" do
12 e = Edge.new
13 e.connections.should be_empty
14 end
15
16 it "creates an edge with some parameters" do
17 e = Edge.new :value1 => 1, :value2 => "value2", :value3 => :v3
18 e.value1.should == 1
19 e.value2.should == "value2"
20 e.value3.should == :v3
21 e.value4.should be_nil
22 end
23 end # #initialize
24
25 describe "adds attribues" do
26 it "adds then reports arbitrary attributes" do
27 e.score = 15
28 e.score.should == 15
29 end
30 end # adds attributes
31
32 describe "#<<" do
33 it "adds a new vertex to an edge (with a connection)" do
34 e.connections.should be_empty
35 e << v1
36 e.should have(1).connections
37 e.should have(1).vertices
38 e.vertices.should include(v1)
39 v1.edges.should include(e)
40 e << v2
41 e.should have(2).connections
42 e.should have(2).vertices
43 e.vertices.should include(v1)
44 e.vertices.should include(v2)
45 v2.edges.should include(e)
46 end
47
48 it "adds several vertices to an edge" do
49 e.connections.should be_empty
50 e << v1 << v2
51 e.vertices.should include(v1)
52 e.vertices.should include(v2)
53 e.should have(2).vertices
54 end
55
56 it "adds a self-loop" do
57 e.connections.should be_empty
58 e << v1 << v1
59 e.vertices.should include(v1)
60 e.should have(2).vertices
61 e.vertices.uniq.length.should == 1
62 end
63 end # #<<
64
65 describe "connection_at" do
66 it "returns the connection that links to a vertex" do
67 e.connections.should be_empty
68 e << v1 << v2
69
70 e.connection_at(v1).end.should be v1
71 e.connection_at(v2).end.should be v2
72 end
73
74 it "returns nil if there is no connection to that vertex" do
75 e.connections.should be_empty
76 e << v1 << v2
77
78 e.connection_at(v3).should be nil
79 end
80
81 it "returns the vertex for a self-loop" do
82 e.connections.should be_empty
83 e << v1 << v1
84
85 e.connection_at(v1).end.should be v1
86 end
87 end # #connection_at
88
89 describe "other_end" do
90 it "returns the vertex at the other end of the given one" do
91 e.connections.should be_empty
92 e << v1 << v2
93
94 e.other_end(v1).should be v2
95 e.other_end(v2).should be v1
96 end
97
98 it "returns the same vertex in a self-loop" do
99 e.connections.should be_empty
100 e << v1 << v1
101
102 e.other_end(v1).should be v1
103 end
104
105 it "returns one of the connected edges if given a vertex not connected to it" do
106 e.connections.should be_empty
107 e << v1 << v2
108
109 [v1, v2].should include e.other_end(v3)
110 end
111
112 it "returns nil if it can't return something sensible" do
113 e.other_end(v1).should be_nil
114 e << v1
115 e.other_end(v1).should be_nil
116 end
117 end # other_end
118
119 describe "#to_dot" do
120 it "describes an edge in dot notation" do
121 e << v1 << v2
122
123 e.to_dot.should == "#{v1.object_id.to_s} -- #{v2.object_id.to_s};"
124 end
125
126 it "describes an edge in dot notation, using given attributes" do
127 e << v1 << v2
128 #vdot = v1.to_dot :label => :name, :shape => :shape
129 #vdot.should == "#{v.object_id.to_s} {label = \"vertex\", shape = \"house\"};"
130 end
131
132
133 it "describes an edge in dot notation, given a block" do
134 e << v1 << v2
135
136 e.to_dot {|e| e.object_id.to_s}.should == e.object_id.to_s
137 end
138 end # dot
139 end # Edge
140
141 describe Connection do
142 let (:c) {Connection.new }
143
144 describe "adds attribues" do
145 it "adds then reports arbitrary attributes" do
146 c.score = 15
147 c.score.should == 15
148 end
149 end # adds attributes
150 end # Connection
151
152 end