github.com/gopherd/gonum@v0.0.4/graph/formats/dot/internal/lexer/lexer_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 lexer_test 12 13 import ( 14 "archive/zip" 15 "bytes" 16 "os" 17 "testing" 18 19 "github.com/gopherd/gonum/graph/formats/dot" 20 ) 21 22 func TestParseFile(t *testing.T) { 23 golden := []struct { 24 in string 25 out string 26 }{ 27 { 28 in: "testdata/tokens.dot", 29 out: "testdata/tokens.golden", 30 }, 31 } 32 for _, g := range golden { 33 file, err := dot.ParseFile(g.in) 34 if err != nil { 35 t.Errorf("%q: unable to parse file; %v", g.in, err) 36 continue 37 } 38 // If no output path is specified, the input is already golden. 39 out := g.in 40 if len(g.out) > 0 { 41 out = g.out 42 } 43 buf, err := os.ReadFile(out) 44 if err != nil { 45 t.Errorf("%q: unable to read file; %v", g.in, err) 46 continue 47 } 48 got := file.String() 49 // Remove trailing newline. 50 want := string(bytes.TrimSpace(buf)) 51 if got != want { 52 t.Errorf("%q: graph mismatch; expected %q, got %q", g.in, want, got) 53 } 54 } 55 } 56 57 func TestParseFuzz(t *testing.T) { 58 r, err := zip.OpenReader("../../fuzz/corpus.zip") 59 if err != nil { 60 if os.IsNotExist(err) { 61 t.Skip("no corpus") 62 } 63 t.Fatalf("failed to open corpus: %v", err) 64 } 65 defer r.Close() 66 67 for _, f := range r.File { 68 rc, err := f.Open() 69 if err != nil { 70 t.Fatalf("failed to open %q: %v", f.Name, err) 71 } 72 func() { 73 defer func() { 74 p := recover() 75 if p != nil { 76 t.Errorf("unexpected panic parsing %q: %v", f.Name, p) 77 } 78 }() 79 80 _, err = dot.Parse(rc) 81 if err != nil { 82 t.Errorf("unexpected error parsing %q: %v", f.Name, err) 83 } 84 }() 85 rc.Close() 86 } 87 }