depgraph: add Noun function to get a noun

This commit is contained in:
Mitchell Hashimoto 2014-06-25 11:28:00 -07:00
parent 0bf394dfe2
commit 94a11583c2
2 changed files with 33 additions and 0 deletions

View File

@ -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 {

View File

@ -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