github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/graph/formats/dot/ast/ast_test.go (about) 1 // This file is dual licensed under CC0 and The Gonum License. 2 // 3 // Copyright ©2017 The Gonum Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 // 7 // Copyright ©2017 Robin Eklind. 8 // This file is made available under a Creative Commons CC0 1.0 9 // Universal Public Domain Dedication. 10 11 package ast_test 12 13 import ( 14 "bytes" 15 "io/ioutil" 16 "testing" 17 18 "github.com/jingcheng-WU/gonum/graph/formats/dot" 19 "github.com/jingcheng-WU/gonum/graph/formats/dot/ast" 20 ) 21 22 func TestParseFile(t *testing.T) { 23 golden := []struct { 24 in string 25 out string 26 }{ 27 {in: "../internal/testdata/empty.dot"}, 28 {in: "../internal/testdata/graph.dot"}, 29 {in: "../internal/testdata/digraph.dot"}, 30 {in: "../internal/testdata/strict.dot"}, 31 {in: "../internal/testdata/multi.dot"}, 32 {in: "../internal/testdata/named_graph.dot"}, 33 {in: "../internal/testdata/node_stmt.dot"}, 34 {in: "../internal/testdata/edge_stmt.dot"}, 35 {in: "../internal/testdata/attr_stmt.dot"}, 36 {in: "../internal/testdata/attr.dot"}, 37 { 38 in: "../internal/testdata/subgraph.dot", 39 out: "../internal/testdata/subgraph.golden", 40 }, 41 { 42 in: "../internal/testdata/semi.dot", 43 out: "../internal/testdata/semi.golden", 44 }, 45 { 46 in: "../internal/testdata/empty_attr.dot", 47 out: "../internal/testdata/empty_attr.golden", 48 }, 49 { 50 in: "../internal/testdata/attr_lists.dot", 51 out: "../internal/testdata/attr_lists.golden", 52 }, 53 { 54 in: "../internal/testdata/attr_sep.dot", 55 out: "../internal/testdata/attr_sep.golden", 56 }, 57 {in: "../internal/testdata/subgraph_vertex.dot"}, 58 {in: "../internal/testdata/port.dot"}, 59 } 60 for _, g := range golden { 61 file, err := dot.ParseFile(g.in) 62 if err != nil { 63 t.Errorf("%q: unable to parse file; %v", g.in, err) 64 continue 65 } 66 // If no output path is specified, the input is already golden. 67 out := g.in 68 if len(g.out) > 0 { 69 out = g.out 70 } 71 buf, err := ioutil.ReadFile(out) 72 if err != nil { 73 t.Errorf("%q: unable to read file; %v", g.in, err) 74 continue 75 } 76 got := file.String() 77 // Remove trailing newline. 78 want := string(bytes.TrimSpace(buf)) 79 if got != want { 80 t.Errorf("%q: graph mismatch; expected %q, got %q", g.in, want, got) 81 } 82 } 83 } 84 85 // Verify that all statements implement the Stmt interface. 86 var ( 87 _ ast.Stmt = (*ast.NodeStmt)(nil) 88 _ ast.Stmt = (*ast.EdgeStmt)(nil) 89 _ ast.Stmt = (*ast.AttrStmt)(nil) 90 _ ast.Stmt = (*ast.Attr)(nil) 91 _ ast.Stmt = (*ast.Subgraph)(nil) 92 ) 93 94 // Verify that all vertices implement the Vertex interface. 95 var ( 96 _ ast.Vertex = (*ast.Node)(nil) 97 _ ast.Vertex = (*ast.Subgraph)(nil) 98 )