depgraph: add Noun function to get a noun
This commit is contained in:
parent
0bf394dfe2
commit
94a11583c2
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue