Added hash parameter to graph initialization
[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 describe "#initialize" do
7 it "creates an empty edge" do
8 e = Edge.new
9 e.connections.should be_empty
10 end
11
12 it "creates an edge with some parameters" do
13 e = Edge.new :value1 => 1, :value2 => "value2", :value3 => :v3
14 e.value1.should == 1
15 e.value2.should == "value2"
16 e.value3.should == :v3
17 e.value4.should be_nil
18 end
19
20 end # #initialize
21
22 describe "adds attribues" do
23 it "adds then reports arbitrary attributes" do
24 e.score = 15
25 e.score.should == 15
26 end
27 end # adds attributes
28
29 describe "#<<" do
30 it "adds a new vertex to an edge (with a connection)" do
31 e.connections.should be_empty
32 v1 = Vertex.new
33 v2 = Vertex.new
34 e << v1
35 e.should have(1).connections
36 e.should have(1).vertices
37 e.vertices.should include(v1)
38 e << v2
39 e.should have(2).connections
40 e.should have(2).vertices
41 e.vertices.should include(v1)
42 e.vertices.should include(v2)
43 end
44
45 it "adds several vertices to an edge" do
46 e.connections.should be_empty
47 v1 = Vertex.new
48 v2 = Vertex.new
49 e << v1 << v2
50 e.vertices.should include(v1)
51 e.vertices.should include(v2)
52 e.should have(2).vertices
53 end
54
55 it "adds a self-loop" do
56 e.connections.should be_empty
57 v1 = Vertex.new
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 v1 = Vertex.new
69 v2 = Vertex.new
70 e << v1 << v2
71
72 e.connection_at(v1).end.should be v1
73 e.connection_at(v2).end.should be v2
74 end
75
76 it "returns nil if there is no connection to that vertex" do
77 e.connections.should be_empty
78 v1 = Vertex.new
79 v2 = Vertex.new
80 v3 = Vertex.new
81 e << v1 << v2
82
83 e.connection_at(v3).should be nil
84 end
85
86 it "returns the vertex for a self-loop" do
87 e.connections.should be_empty
88 v1 = Vertex.new
89 e << v1 << v1
90
91 e.connection_at(v1).end.should be v1
92 end
93
94
95 end # #connection_at
96 end # Edge
97
98 describe Connection do
99 let (:c) {Connection.new }
100
101 describe "adds attribues" do
102 it "adds then reports arbitrary attributes" do
103 c.score = 15
104 c.score.should == 15
105 end
106 end # adds attributes
107 end # Connection
108
109 end