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

Commit

Permalink
Add attribute matching to Graph methods
Browse files Browse the repository at this point in the history
  • Loading branch information
floriandejonckheere committed Apr 17, 2024
1 parent 272a5ee commit d750d12
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
8 changes: 4 additions & 4 deletions lib/mosaik/graph/graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ def find_edges(from, to)
T.must(T.must(vertices[from]).edges[to])
end

sig { params(from: String, to: String).returns(T.nilable(Edge)) }
def find_edge(from, to)
T.must(vertices[from]).find_edge(to)
sig { params(from: String, to: String, attributes: Attributes).returns(T.nilable(Edge)) }
def find_edge(from, to, attributes = {})
T.must(vertices[from]).find_edge(to, attributes)
end

sig { params(from: String, to: String, attributes: Attributes).returns(Edge) }
def find_or_add_edge(from, to, attributes = {})
find_edge(from, to) || add_edge(from, to, attributes)
find_edge(from, to, attributes) || add_edge(from, to, attributes)
end

sig { params(from: String, to: String, edge: T.nilable(Edge)).void }
Expand Down
72 changes: 68 additions & 4 deletions spec/mosaik/graph/graph_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,38 @@

expect(graph.find_edge("vertex1", "vertex2")).to eq e1
end

context "when attributes are given" do
it "returns the edge matching the attributes entirely" do
graph.add_vertex("vertex1")
graph.add_vertex("vertex2")

graph.add_edge("vertex1", "vertex2")
e1 = graph.add_edge("vertex1", "vertex2", weight: 3)

expect(graph.find_edge("vertex1", "vertex2", weight: 3)).to eq e1
end

it "returns the edge matching the attributes partially" do
graph.add_vertex("vertex1")
graph.add_vertex("vertex2")

graph.add_edge("vertex1", "vertex2")
e1 = graph.add_edge("vertex1", "vertex2", weight: 3, key: "value")

expect(graph.find_edge("vertex1", "vertex2", weight: 3)).to eq e1
end

it "returns nothing when the edge does not have the attributes" do
graph.add_vertex("vertex1")
graph.add_vertex("vertex2")

graph.add_edge("vertex1", "vertex2")
graph.add_edge("vertex1", "vertex2", weight: 3)

expect(graph.find_edge("vertex1", "vertex2", weight: 4)).to be_nil
end
end
end

describe "#find_or_add_edge" do
Expand All @@ -219,11 +251,43 @@
expect(graph.find_or_add_edge("vertex1", "vertex2")).to eq e1
end

it "adds an edge with attributes" do
graph.add_vertex("vertex1")
graph.add_vertex("vertex2")
context "when attributes are given" do
it "adds an edge with attributes" do
graph.add_vertex("vertex1")
graph.add_vertex("vertex2")

expect(graph.find_or_add_edge("vertex1", "vertex2", weight: 3).attributes).to eq weight: 3
end

expect(graph.find_or_add_edge("vertex1", "vertex2", weight: 3).attributes).to eq weight: 3
it "returns the edge matching the attributes entirely" do
graph.add_vertex("vertex1")
graph.add_vertex("vertex2")

graph.add_edge("vertex1", "vertex2")
e1 = graph.add_edge("vertex1", "vertex2", weight: 3)

expect(graph.find_or_add_edge("vertex1", "vertex2", weight: 3)).to eq e1
end

it "returns the edge matching the attributes partially" do
graph.add_vertex("vertex1")
graph.add_vertex("vertex2")

graph.add_edge("vertex1", "vertex2")
e1 = graph.add_edge("vertex1", "vertex2", weight: 3, key: "value")

expect(graph.find_or_add_edge("vertex1", "vertex2", weight: 3)).to eq e1
end

it "returns a new edge when the edge does not have the attributes" do
graph.add_vertex("vertex1")
graph.add_vertex("vertex2")

graph.add_edge("vertex1", "vertex2")
graph.add_edge("vertex1", "vertex2", weight: 3)

expect(graph.find_or_add_edge("vertex1", "vertex2", weight: 4).attributes).to eq weight: 4
end
end
end

Expand Down

0 comments on commit d750d12

Please sign in to comment.