github.com/JimmyHuang454/JLS-go@v0.0.0-20230831150107-90d536585ba0/internal/dag/parse_test.go (about) 1 // Copyright 2022 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package dag 6 7 import ( 8 "reflect" 9 "strings" 10 "testing" 11 ) 12 13 const diamond = ` 14 NONE < a < b, c < d; 15 ` 16 17 func mustParse(t *testing.T, dag string) *Graph { 18 t.Helper() 19 g, err := Parse(dag) 20 if err != nil { 21 t.Fatal(err) 22 } 23 return g 24 } 25 26 func wantEdges(t *testing.T, g *Graph, edges string) { 27 t.Helper() 28 29 wantEdges := strings.Fields(edges) 30 wantEdgeMap := make(map[string]bool) 31 for _, e := range wantEdges { 32 wantEdgeMap[e] = true 33 } 34 35 for _, n1 := range g.Nodes { 36 for _, n2 := range g.Nodes { 37 got := g.HasEdge(n1, n2) 38 want := wantEdgeMap[n1+"->"+n2] 39 if got && want { 40 t.Logf("%s->%s", n1, n2) 41 } else if got && !want { 42 t.Errorf("%s->%s present but not expected", n1, n2) 43 } else if want && !got { 44 t.Errorf("%s->%s missing but expected", n1, n2) 45 } 46 } 47 } 48 } 49 50 func TestParse(t *testing.T) { 51 // Basic smoke test for graph parsing. 52 g := mustParse(t, diamond) 53 54 wantNodes := strings.Fields("a b c d") 55 if !reflect.DeepEqual(wantNodes, g.Nodes) { 56 t.Fatalf("want nodes %v, got %v", wantNodes, g.Nodes) 57 } 58 59 // Parse returns the transitive closure, so it adds d->a. 60 wantEdges(t, g, "b->a c->a d->a d->b d->c") 61 }