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