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

     1  // Copyright ©2020 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
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  )
    11  
    12  func TestSave(t *testing.T) {
    13  	p := New()
    14  	p.Title.Text = "my title"
    15  	p.X.Label.Text = "x"
    16  	p.Y.Label.Text = "y"
    17  
    18  	for _, tc := range []struct {
    19  		name  string
    20  		files []string
    21  		want  error
    22  	}{
    23  		{
    24  			name:  "empty-fnames",
    25  			files: []string{},
    26  			want:  fmt.Errorf(`hplot: need at least 1 file name`),
    27  		},
    28  		{
    29  			name:  "invalid-format",
    30  			files: []string{"invalid-format"},
    31  			want:  fmt.Errorf(`hplot: could not save plot: hplot: could not create canvas: unsupported format: ""`),
    32  		},
    33  		{
    34  			name:  "unknown-format",
    35  			files: []string{"file.txt"},
    36  			want:  fmt.Errorf(`hplot: could not save plot: hplot: could not create canvas: unsupported format: "txt"`),
    37  		},
    38  	} {
    39  		t.Run(tc.name, func(t *testing.T) {
    40  			err := Save(p, -1, -1, tc.files...)
    41  			if err == nil {
    42  				t.Fatalf("expected an error")
    43  			}
    44  
    45  			if got, want := err.Error(), tc.want.Error(); got != want {
    46  				t.Fatalf("invalid error:\ngot= %v\nwant=%v", got, want)
    47  			}
    48  		})
    49  	}
    50  }