...

Source file src/gonum.org/v1/plot/vg/vg_test.go

Documentation: gonum.org/v1/plot/vg

     1  // Copyright ©2015 The Gonum 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 vg_test
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"log"
    11  	"os"
    12  	"path/filepath"
    13  	"strings"
    14  	"testing"
    15  
    16  	"gonum.org/v1/plot"
    17  	"gonum.org/v1/plot/cmpimg"
    18  	"gonum.org/v1/plot/plotter"
    19  	"gonum.org/v1/plot/vg"
    20  )
    21  
    22  // TestLineWidth tests output against test images generated by
    23  // running tests with -tag good.
    24  func TestLineWidth(t *testing.T) {
    25  	formats := []string{
    26  		// TODO: Add logic to cope with run to run eps differences.
    27  		"pdf",
    28  		"svg",
    29  		"png",
    30  		"tiff",
    31  		"jpg",
    32  	}
    33  
    34  	const (
    35  		width  = 100
    36  		height = 100
    37  	)
    38  
    39  	for _, w := range []vg.Length{-1, 0, 1} {
    40  		for _, typ := range formats {
    41  			p, err := lines(w)
    42  			if err != nil {
    43  				log.Fatalf("failed to create plot for %v:%s: %v", w, typ, err)
    44  			}
    45  
    46  			c, err := p.WriterTo(width, height, typ)
    47  			if err != nil {
    48  				t.Fatalf("failed to render plot for %v:%s: %v", w, typ, err)
    49  			}
    50  
    51  			var buf bytes.Buffer
    52  			if _, err = c.WriteTo(&buf); err != nil {
    53  				t.Fatalf("failed to write plot for %v:%s: %v", w, typ, err)
    54  			}
    55  
    56  			name := filepath.Join(".", "testdata", fmt.Sprintf("width_%v_golden.%s", w, typ))
    57  
    58  			// Recreate Golden images.
    59  			if *cmpimg.GenerateTestData {
    60  				err = p.Save(width, height, name)
    61  				if err != nil {
    62  					log.Fatalf("failed to save %q: %v", name, err)
    63  				}
    64  			}
    65  
    66  			want, err := os.ReadFile(name)
    67  			if err != nil {
    68  				t.Fatalf("failed to read test image [%s]: %v\n", name, err)
    69  			}
    70  
    71  			ok, err := cmpimg.Equal(typ, buf.Bytes(), want)
    72  			if err != nil {
    73  				t.Fatalf("failed to run cmpimg test [%s]: %v\n", name, err)
    74  			}
    75  
    76  			if !ok {
    77  				got := strings.Replace(name, "_golden.", ".", 1)
    78  				err = os.WriteFile(got, buf.Bytes(), 0644)
    79  				if err != nil {
    80  					t.Errorf("could not write plot image %q: %+v", got, err)
    81  				}
    82  				t.Errorf("image mismatch for %v:%s", w, typ)
    83  			}
    84  		}
    85  	}
    86  }
    87  
    88  func lines(w vg.Length) (*plot.Plot, error) {
    89  	p := plot.New()
    90  	pts := plotter.XYs{
    91  		{X: 0, Y: 0}, {X: 0, Y: 1},
    92  		{X: 1, Y: 0}, {X: 1, Y: 1},
    93  	}
    94  	line, err := plotter.NewLine(pts)
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  	line.Width = w
    99  	p.Add(line)
   100  	p.X.Label.Text = "X label"
   101  	p.Y.Label.Text = "Y label"
   102  
   103  	return p, nil
   104  }
   105  
   106  func TestParseLength(t *testing.T) {
   107  	for _, table := range []struct {
   108  		str  string
   109  		want vg.Length
   110  		err  error
   111  	}{
   112  		{
   113  			str:  "42.2cm",
   114  			want: 42.2 * vg.Centimeter,
   115  		},
   116  		{
   117  			str:  "42.2mm",
   118  			want: 42.2 * vg.Millimeter,
   119  		},
   120  		{
   121  			str:  "42.2in",
   122  			want: 42.2 * vg.Inch,
   123  		},
   124  		{
   125  			str:  "42.2pt",
   126  			want: 42.2,
   127  		},
   128  		{
   129  			str:  "42.2",
   130  			want: 42.2,
   131  		},
   132  		{
   133  			str: "999bottles",
   134  			err: fmt.Errorf(`strconv.ParseFloat: parsing "999bottles": invalid syntax`),
   135  		},
   136  		{
   137  			str:  "42inch",
   138  			want: 42 * vg.Inch,
   139  			err:  fmt.Errorf(`strconv.ParseFloat: parsing "42inch": invalid syntax`),
   140  		},
   141  	} {
   142  		v, err := vg.ParseLength(table.str)
   143  		if table.err != nil {
   144  			if err == nil {
   145  				t.Errorf("%s: expected an error (%v)\n",
   146  					table.str, table.err,
   147  				)
   148  			}
   149  			if table.err.Error() != err.Error() {
   150  				t.Errorf("%s: got error=%q. want=%q\n",
   151  					table.str, err.Error(), table.err.Error(),
   152  				)
   153  			}
   154  			continue
   155  		}
   156  		if err != nil {
   157  			t.Errorf("error setting flag.Value %q: %v\n",
   158  				table.str,
   159  				err,
   160  			)
   161  		}
   162  		if v != table.want {
   163  			t.Errorf("%s: incorrect value. got %v, want %v\n",
   164  				table.str,
   165  				float64(v), float64(table.want),
   166  			)
   167  		}
   168  	}
   169  }
   170  func TestInMemoryCanvas(t *testing.T) {
   171  	cmpimg.CheckPlot(Example_inMemoryCanvas, t, "sine.png")
   172  }
   173  
   174  func TestWriterToCanvas(t *testing.T) {
   175  	cmpimg.CheckPlot(Example_writerToCanvas, t, "cosine.png")
   176  }
   177  

View as plain text