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