From 94a11583c24659a429df0aabb12dc647d7b4bf69 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 25 Jun 2014 11:28:00 -0700 Subject: [PATCH] depgraph: add Noun function to get a noun --- depgraph/graph.go | 11 +++++++++++ depgraph/graph_test.go | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/depgraph/graph.go b/depgraph/graph.go index ab980ccbc8..a7d92403eb 100644 --- a/depgraph/graph.go +++ b/depgraph/graph.go @@ -109,6 +109,17 @@ func (g *Graph) CheckConstraints() error { return nil } +// Noun returns the noun with the given name, or nil if it cannot be found. +func (g *Graph) Noun(name string) *Noun { + for _, n := range g.Nouns { + if n.Name == name { + return n + } + } + + return nil +} + // String generates a little ASCII string of the graph, useful in // debugging output. func (g *Graph) String() string { diff --git a/depgraph/graph_test.go b/depgraph/graph_test.go index 1251cfd86a..a6dc60a1f2 100644 --- a/depgraph/graph_test.go +++ b/depgraph/graph_test.go @@ -55,6 +55,28 @@ func NounMapToList(m map[string]*Noun) []*Noun { return list } +func TestGraph_Noun(t *testing.T) { + nodes := ParseNouns(`a -> b +a -> c +b -> d +b -> e +c -> d +c -> e`) + + g := &Graph{ + Name: "Test", + Nouns: NounMapToList(nodes), + } + + n := g.Noun("a") + if n == nil { + t.Fatal("should not be nil") + } + if n.Name != "a" { + t.Fatalf("bad: %#v", n) + } +} + func TestGraph_String(t *testing.T) { nodes := ParseNouns(`a -> b a -> c