Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Allow adding multiple edges
Browse files Browse the repository at this point in the history
  • Loading branch information
floriandejonckheere committed Mar 31, 2024
1 parent 274fe68 commit db2cb34
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/mosaik/graph/vertex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class Vertex

def initialize(id, attributes = {})
@id = id
@edges = {}
@edges = Hash.new { |h, k| h[k] = [] }
@attributes = attributes
end

def add_edge(to, **attributes)
edges[to] = Edge.new(attributes)
edges[to] << Edge.new(attributes)
end

def remove_edge(id)
Expand Down
10 changes: 6 additions & 4 deletions spec/mosaik/graph/graph_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@
graph.add_directed_edge("vertex1", "vertex2", key: "value")

expect(graph.find_vertex("vertex1").edges.keys).to eq ["vertex2"]
expect(graph.find_vertex("vertex1").edges["vertex2"].attributes).to eq key: "value"
expect(graph.find_vertex("vertex1").edges["vertex2"].first.attributes).to eq key: "value"
end

it "does not add a directed edge twice" do
it "adds a directed edge twice" do
graph.add_vertex("vertex1")
graph.add_vertex("vertex2")
graph.add_directed_edge("vertex1", "vertex2")
graph.add_directed_edge("vertex1", "vertex2")

expect(graph.find_vertex("vertex1").edges.keys).to eq ["vertex2"]
expect(graph.find_vertex("vertex1").edges["vertex2"].size).to eq 2
end
end

Expand All @@ -60,16 +61,17 @@
graph.add_undirected_edge("vertex1", "vertex2", key: "value")

expect(graph.find_vertex("vertex1").edges.keys).to eq ["vertex2"]
expect(graph.find_vertex("vertex1").edges["vertex2"].attributes).to eq key: "value"
expect(graph.find_vertex("vertex1").edges["vertex2"].first.attributes).to eq key: "value"
end

it "does not add an undirected edge twice" do
it "adds an undirected edge twice" do
graph.add_vertex("vertex1")
graph.add_vertex("vertex2")
graph.add_undirected_edge("vertex1", "vertex2")
graph.add_undirected_edge("vertex1", "vertex2")

expect(graph.find_vertex("vertex1").edges.keys).to eq ["vertex2"]
expect(graph.find_vertex("vertex1").edges["vertex2"].size).to eq 2
end
end

Expand Down
5 changes: 3 additions & 2 deletions spec/mosaik/graph/vertex_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
vertex.add_edge("child", key: "value")

expect(vertex.edges.keys).to eq ["child"]
expect(vertex.edges["child"].attributes).to eq key: "value"
expect(vertex.edges["child"].first.attributes).to eq key: "value"
end

it "does not add an edge twice" do
it "adds an edge twice" do
vertex.add_edge("child")
vertex.add_edge("child")

expect(vertex.edges.keys).to eq ["child"]
expect(vertex.edges["child"].size).to eq 2
end
end
end

0 comments on commit db2cb34

Please sign in to comment.