go-hep.org/x/hep@v0.38.1/hplot/hplot_test.go (about)

     1  // Copyright ©2016 The go-hep 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 hplot_test
     6  
     7  import (
     8  	"os"
     9  	"path"
    10  	"reflect"
    11  	"runtime"
    12  	"testing"
    13  
    14  	"go-hep.org/x/hep/hplot"
    15  	"gonum.org/v1/plot/cmpimg"
    16  	"gonum.org/v1/plot/vg"
    17  )
    18  
    19  type chkplotFunc func(ExampleFunc func(), t *testing.T, filenames ...string)
    20  
    21  func checkPlot(f chkplotFunc) chkplotFunc {
    22  	return func(ex func(), t *testing.T, filenames ...string) {
    23  		t.Helper()
    24  		f(ex, t, filenames...)
    25  		if t.Failed() {
    26  			return
    27  		}
    28  		for _, fname := range filenames {
    29  			_ = os.Remove(path.Join("testdata", fname))
    30  		}
    31  	}
    32  }
    33  
    34  func TestSubPlot(t *testing.T) {
    35  	checkPlot(cmpimg.CheckPlot)(Example_subplot, t, "sub_plot.png")
    36  }
    37  
    38  func TestLatexPlot(t *testing.T) {
    39  	if runtime.GOOS == "darwin" {
    40  		t.Skipf("ignore test b/c of darwin+Mac-silicon")
    41  	}
    42  	Example_latexplot()
    43  	ref, err := os.ReadFile("testdata/latex_plot_golden.tex")
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  	chk, err := os.ReadFile("testdata/latex_plot.tex")
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	if !reflect.DeepEqual(ref, chk) {
    52  		t.Fatal("files testdata/latex_plot{,_golden}.tex differ\n")
    53  	}
    54  	os.Remove("testdata/latex_plot.tex")
    55  }
    56  
    57  func TestShow(t *testing.T) {
    58  	p := hplot.New()
    59  	p.Title.Text = "title"
    60  	p.X.Label.Text = "x"
    61  	p.Y.Label.Text = "y"
    62  
    63  	for _, tc := range []struct {
    64  		w, h   vg.Length
    65  		format string
    66  	}{
    67  		{-1, -1, ""},
    68  		{-1, -1, "png"},
    69  		{-1, -1, "jpg"},
    70  		{-1, -1, "pdf"},
    71  		{-1, -1, "eps"},
    72  		{-1, -1, "tex"},
    73  		{-1, 10 * vg.Centimeter, ""},
    74  		{10 * vg.Centimeter, -1, ""},
    75  	} {
    76  		t.Run(tc.format, func(t *testing.T) {
    77  			_, err := hplot.Show(p, tc.w, tc.h, tc.format)
    78  			if err != nil {
    79  				t.Fatalf("%+v", err)
    80  			}
    81  		})
    82  	}
    83  }