Skip to content

Commit

Permalink
plot,vg/...: make testdata regeneration easier
Browse files Browse the repository at this point in the history
  • Loading branch information
kortschak committed Nov 3, 2020
1 parent cad3ccd commit de78615
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 187 deletions.
218 changes: 31 additions & 187 deletions plot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@ import (
"bytes"
"fmt"
"image/color"
"io/ioutil"
"math"
"reflect"
"testing"
"time"

"gonum.org/v1/plot"
"gonum.org/v1/plot/cmpimg"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
"gonum.org/v1/plot/vg/draw"
"gonum.org/v1/plot/vg/recorder"
"gonum.org/v1/plot/vg/vgimg"
)

func TestLegendAlignment(t *testing.T) {
const fontSize = 10.189054726368159 // This font size gives an entry height of 10.
font, err := vg.MakeFont(plot.DefaultFont, fontSize)
font, err := vg.MakeFont(plot.DefaultFont, 10)
if err != nil {
t.Fatalf("failed to create font: %v", err)
}
Expand All @@ -33,206 +34,49 @@ func TestLegendAlignment(t *testing.T) {
Handler: plot.DefaultTextHandler,
},
}
for _, n := range []string{"A", "B", "C", "D"} {
for i, n := range []string{"A", "B", "C", "D"} {
b, err := plotter.NewBarChart(plotter.Values{0}, 1)
if err != nil {
t.Fatalf("failed to create bar chart %q: %v", n, err)
}
b.Color = color.Gray{byte(i+1)*64 - 1}
l.Add(n, b)
}

var r recorder.Canvas
c := draw.NewCanvas(&r, 100, 100)
l.Draw(draw.Canvas{
Canvas: c.Canvas,
Rectangle: vg.Rectangle{
Min: vg.Point{X: 0, Y: 0},
Max: vg.Point{X: 100, Y: 100},
},
})

got := r.Actions
c := vgimg.PngCanvas{Canvas: vgimg.New(5*vg.Centimeter, 5*vg.Centimeter)}
l.Draw(draw.New(c))
var buf bytes.Buffer
if _, err = c.WriteTo(&buf); err != nil {
t.Fatal(err)
}

// want is a snapshot of the actions for the code above when the
// graphical output has been visually confirmed to be correct for
// the bar charts example show in gonum/plot#25.
want := []recorder.Action{
&recorder.SetColor{
Color: color.Gray16{},
},
&recorder.Fill{
Path: vg.Path{
{Type: vg.MoveComp, Pos: vg.Point{X: 80, Y: 30}},
{Type: vg.LineComp, Pos: vg.Point{X: 80, Y: 40}},
{Type: vg.LineComp, Pos: vg.Point{X: 100, Y: 40}},
{Type: vg.LineComp, Pos: vg.Point{X: 100, Y: 30}},
{Type: vg.CloseComp},
},
},
&recorder.SetColor{
Color: color.Gray16{},
},
&recorder.SetLineWidth{
Width: 1,
},
&recorder.SetLineDash{},
&recorder.Stroke{
Path: vg.Path{
{Type: vg.MoveComp, Pos: vg.Point{X: 80, Y: 30}},
{Type: vg.LineComp, Pos: vg.Point{X: 80, Y: 40}},
{Type: vg.LineComp, Pos: vg.Point{X: 100, Y: 40}},
{Type: vg.LineComp, Pos: vg.Point{X: 100, Y: 30}},
{Type: vg.LineComp, Pos: vg.Point{X: 80, Y: 30}},
},
},
&recorder.SetColor{},
&recorder.FillString{
Font: string("Times-Roman"),
Size: fontSize,
Point: vg.Point{X: 70.09452736318407, Y: 31.733830845771145},
String: "A",
},
&recorder.SetColor{
Color: color.Gray16{},
},
&recorder.Fill{
Path: vg.Path{
{Type: vg.MoveComp, Pos: vg.Point{X: 80, Y: 20}},
{Type: vg.LineComp, Pos: vg.Point{X: 80, Y: 30}},
{Type: vg.LineComp, Pos: vg.Point{X: 100, Y: 30}},
{Type: vg.LineComp, Pos: vg.Point{X: 100, Y: 20}},
{Type: vg.CloseComp},
},
},
&recorder.SetColor{
Color: color.Gray16{},
},
&recorder.SetLineWidth{
Width: 1,
},
&recorder.SetLineDash{},
&recorder.Stroke{
Path: vg.Path{
{Type: vg.MoveComp, Pos: vg.Point{X: 80, Y: 20}},
{Type: vg.LineComp, Pos: vg.Point{X: 80, Y: 30}},
{Type: vg.LineComp, Pos: vg.Point{X: 100, Y: 30}},
{Type: vg.LineComp, Pos: vg.Point{X: 100, Y: 20}},
{Type: vg.LineComp, Pos: vg.Point{X: 80, Y: 20}},
},
},
&recorder.SetColor{},
&recorder.FillString{
Font: string("Times-Roman"),
Size: fontSize,
Point: vg.Point{X: 70.65671641791045, Y: 21.733830845771145},
String: "B",
},
&recorder.SetColor{
Color: color.Gray16{
Y: uint16(0),
},
},
&recorder.Fill{
Path: vg.Path{
{Type: vg.MoveComp, Pos: vg.Point{X: 80, Y: 10}},
{Type: vg.LineComp, Pos: vg.Point{X: 80, Y: 20}},
{Type: vg.LineComp, Pos: vg.Point{X: 100, Y: 20}},
{Type: vg.LineComp, Pos: vg.Point{X: 100, Y: 10}},
{Type: vg.CloseComp},
},
},
&recorder.SetColor{
Color: color.Gray16{},
},
&recorder.SetLineWidth{
Width: 1,
},
&recorder.SetLineDash{},
&recorder.Stroke{
Path: vg.Path{
{Type: vg.MoveComp, Pos: vg.Point{X: 80, Y: 10}},
{Type: vg.LineComp, Pos: vg.Point{X: 80, Y: 20}},
{Type: vg.LineComp, Pos: vg.Point{X: 100, Y: 20}},
{Type: vg.LineComp, Pos: vg.Point{X: 100, Y: 10}},
{Type: vg.LineComp, Pos: vg.Point{X: 80, Y: 10}},
},
},
&recorder.SetColor{},
&recorder.FillString{
Font: string("Times-Roman"),
Size: fontSize,
Point: vg.Point{X: 70.65671641791045, Y: 11.733830845771145},
String: "C",
},
&recorder.SetColor{
Color: color.Gray16{},
},
&recorder.Fill{
Path: vg.Path{
{Type: vg.MoveComp, Pos: vg.Point{X: 80, Y: 0}},
{Type: vg.LineComp, Pos: vg.Point{X: 80, Y: 10}},
{Type: vg.LineComp, Pos: vg.Point{X: 100, Y: 10}},
{Type: vg.LineComp, Pos: vg.Point{X: 100, Y: 0}},
{Type: vg.CloseComp},
},
},
&recorder.SetColor{
Color: color.Gray16{},
},
&recorder.SetLineWidth{
Width: 1,
},
&recorder.SetLineDash{},
&recorder.Stroke{
Path: vg.Path{
{Type: vg.MoveComp, Pos: vg.Point{X: 80, Y: 0}},
{Type: vg.LineComp, Pos: vg.Point{X: 80, Y: 10}},
{Type: vg.LineComp, Pos: vg.Point{X: 100, Y: 10}},
{Type: vg.LineComp, Pos: vg.Point{X: 100, Y: 0}},
{Type: vg.LineComp, Pos: vg.Point{X: 80, Y: 0}},
},
},
&recorder.SetColor{},
&recorder.FillString{
Font: string("Times-Roman"),
Size: fontSize,
Point: vg.Point{X: 70.09452736318407, Y: 1.7338308457711449},
String: "D",
},
if *cmpimg.GenerateTestData {
// Recreate Golden images and exit.
err = ioutil.WriteFile("testdata/legendAlignment_golden.png", buf.Bytes(), 0o644)
if err != nil {
t.Fatal(err)
}
return
}

if !reflect.DeepEqual(got, want) {
t.Errorf("unexpected legend actions:\ngot:\n%s\nwant:\n%s", formatActions(got), formatActions(want))
t.Errorf("First diff:\n%s", printFirstDiff(got, want))
err = ioutil.WriteFile("testdata/legendAlignment.png", buf.Bytes(), 0o644)
if err != nil {
t.Fatal(err)
}
}

func formatActions(actions []recorder.Action) string {
var buf bytes.Buffer
for _, a := range actions {
fmt.Fprintf(&buf, "\t%s\n", a.Call())
want, err := ioutil.ReadFile("testdata/legendAlignment_golden.png")
if err != nil {
t.Fatal(err)
}
return buf.String()
}

// printFirstDiff prints the first line that is different between two actions.
func printFirstDiff(got, want []recorder.Action) string {
var buf bytes.Buffer
for i, g := range got {
if i >= len(want) {
fmt.Fprintf(&buf, "line %d:\n\tgot: %s\n\twant is empty", i, g.Call())
break
}
w := want[i]
if w.Call() != g.Call() {
fmt.Fprintf(&buf, "line %d:\n\tgot: %s\n\twant: %s", i, g.Call(), w.Call())
break
}
ok, err := cmpimg.Equal("png", buf.Bytes(), want)
if err != nil {
t.Fatal(err)
}
if len(want) > len(got) {
fmt.Fprintf(&buf, "line %d:\n\twant: %s\n\tgot is empty", len(got), want[len(got)].Call())
if !ok {
t.Fatalf("images differ")
}
return buf.String()

}

func TestIssue514(t *testing.T) {
Expand Down
Binary file added testdata/legendAlignment_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions vg/vgimg/vgimg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ func TestIssue540(t *testing.T) {
p.Add(lines, points)
p.Add(plotter.NewGrid())

if *cmpimg.GenerateTestData {
// Recreate Golden images and exit.
err = p.Save(100, 100, "testdata/issue540_golden.png")
if err != nil {
t.Fatal(err)
}
return
}

err = p.Save(100, 100, "testdata/issue540.png")
if err != nil {
t.Fatal(err)
Expand Down
33 changes: 33 additions & 0 deletions vg/vgpdf/vgpdf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ func TestEmbedFonts(t *testing.T) {
t.Fatalf("could not write canvas: %v", err)
}

if *cmpimg.GenerateTestData {
// Recreate Golden images and exit.
err = ioutil.WriteFile(tc.name, buf.Bytes(), 0o644)
if err != nil {
t.Fatal(err)
}
return
}

want, err := ioutil.ReadFile(tc.name)
if err != nil {
t.Fatalf("failed to read golden plot: %v", err)
Expand Down Expand Up @@ -95,6 +104,21 @@ func TestArc(t *testing.T) {
c.EmbedFonts(false)
p.Draw(draw.New(c))

if *cmpimg.GenerateTestData {
// Recreate Golden images and exit.
f, err := os.Create("testdata/arc_golden.pdf")
if err != nil {
t.Fatal(err)
}
defer f.Close()

_, err = c.WriteTo(f)
if err != nil {
t.Fatalf("could not write canvas: %v", err)
}
return
}

f, err := os.Create("testdata/arc.pdf")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -160,6 +184,15 @@ func TestIssue540(t *testing.T) {
p.Add(lines, points)
p.Add(plotter.NewGrid())

if *cmpimg.GenerateTestData {
// Recreate Golden images and exit.
err = p.Save(100, 100, "testdata/issue540_golden.pdf")
if err != nil {
t.Fatal(err)
}
return
}

err = p.Save(100, 100, "testdata/issue540.pdf")
if err != nil {
t.Fatal(err)
Expand Down
9 changes: 9 additions & 0 deletions vg/vgsvg/vgsvg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ func TestHtmlEscape(t *testing.T) {
t.Fatal(err)
}

if *cmpimg.GenerateTestData {
// Recreate Golden images and exit.
err = ioutil.WriteFile("testdata/scatter_line_golden.svg", b.Bytes(), 0o644)
if err != nil {
t.Fatal(err)
}
return
}

want, err := ioutil.ReadFile("testdata/scatter_line_golden.svg")
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit de78615

Please sign in to comment.