Skip to content

Commit 159cd04

Browse files
Nils Wogatzkysbinet
authored andcommitted
plot: separate test examples from regular tests
* plot: separate test examples from regular tests according to https://blog.golang.org/examples Fixes #545.
1 parent 9aa8614 commit 159cd04

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+2974
-2661
lines changed

align_test.go

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -5,94 +5,11 @@
55
package plot_test
66

77
import (
8-
"math"
9-
"os"
108
"testing"
119

12-
"gonum.org/v1/plot"
1310
"gonum.org/v1/plot/cmpimg"
14-
"gonum.org/v1/plot/vg"
15-
"gonum.org/v1/plot/vg/draw"
16-
"gonum.org/v1/plot/vg/vgimg"
1711
)
1812

19-
func ExampleAlign() {
20-
const rows, cols = 4, 3
21-
plots := make([][]*plot.Plot, rows)
22-
for j := 0; j < rows; j++ {
23-
plots[j] = make([]*plot.Plot, cols)
24-
for i := 0; i < cols; i++ {
25-
if i == 0 && j == 2 {
26-
// This shows what happens when there are nil plots.
27-
continue
28-
}
29-
30-
p, err := plot.New()
31-
if err != nil {
32-
panic(err)
33-
}
34-
35-
if j == 0 && i == 2 {
36-
// This shows what happens when the axis padding
37-
// is different among plots.
38-
p.X.Padding, p.Y.Padding = 0, 0
39-
}
40-
41-
if j == 1 && i == 1 {
42-
// To test the Align function, we make the axis labels
43-
// on one of the plots stick out.
44-
p.Y.Max = 1e9
45-
p.X.Max = 1e9
46-
p.X.Tick.Label.Rotation = math.Pi / 2
47-
p.X.Tick.Label.XAlign = draw.XRight
48-
p.X.Tick.Label.YAlign = draw.YCenter
49-
p.X.Tick.Label.Font.Size = 8
50-
p.Y.Tick.Label.Font.Size = 8
51-
} else {
52-
p.Y.Max = 1e9
53-
p.X.Max = 1e9
54-
p.X.Tick.Label.Font.Size = 1
55-
p.Y.Tick.Label.Font.Size = 1
56-
}
57-
58-
plots[j][i] = p
59-
}
60-
}
61-
62-
img := vgimg.New(vg.Points(150), vg.Points(175))
63-
dc := draw.New(img)
64-
65-
t := draw.Tiles{
66-
Rows: rows,
67-
Cols: cols,
68-
PadX: vg.Millimeter,
69-
PadY: vg.Millimeter,
70-
PadTop: vg.Points(2),
71-
PadBottom: vg.Points(2),
72-
PadLeft: vg.Points(2),
73-
PadRight: vg.Points(2),
74-
}
75-
76-
canvases := plot.Align(plots, t, dc)
77-
for j := 0; j < rows; j++ {
78-
for i := 0; i < cols; i++ {
79-
if plots[j][i] != nil {
80-
plots[j][i].Draw(canvases[j][i])
81-
}
82-
}
83-
}
84-
85-
w, err := os.Create("testdata/align.png")
86-
if err != nil {
87-
panic(err)
88-
}
89-
90-
png := vgimg.PngCanvas{Canvas: img}
91-
if _, err := png.WriteTo(w); err != nil {
92-
panic(err)
93-
}
94-
}
95-
9613
func TestAlign(t *testing.T) {
9714
cmpimg.CheckPlot(ExampleAlign, t, "align.png")
9815
}

example_align_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright ©2017 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 plot_test
6+
7+
import (
8+
"math"
9+
"os"
10+
11+
"gonum.org/v1/plot"
12+
"gonum.org/v1/plot/vg"
13+
"gonum.org/v1/plot/vg/draw"
14+
"gonum.org/v1/plot/vg/vgimg"
15+
)
16+
17+
func ExampleAlign() {
18+
const rows, cols = 4, 3
19+
plots := make([][]*plot.Plot, rows)
20+
for j := 0; j < rows; j++ {
21+
plots[j] = make([]*plot.Plot, cols)
22+
for i := 0; i < cols; i++ {
23+
if i == 0 && j == 2 {
24+
// This shows what happens when there are nil plots.
25+
continue
26+
}
27+
28+
p, err := plot.New()
29+
if err != nil {
30+
panic(err)
31+
}
32+
33+
if j == 0 && i == 2 {
34+
// This shows what happens when the axis padding
35+
// is different among plots.
36+
p.X.Padding, p.Y.Padding = 0, 0
37+
}
38+
39+
if j == 1 && i == 1 {
40+
// To test the Align function, we make the axis labels
41+
// on one of the plots stick out.
42+
p.Y.Max = 1e9
43+
p.X.Max = 1e9
44+
p.X.Tick.Label.Rotation = math.Pi / 2
45+
p.X.Tick.Label.XAlign = draw.XRight
46+
p.X.Tick.Label.YAlign = draw.YCenter
47+
p.X.Tick.Label.Font.Size = 8
48+
p.Y.Tick.Label.Font.Size = 8
49+
} else {
50+
p.Y.Max = 1e9
51+
p.X.Max = 1e9
52+
p.X.Tick.Label.Font.Size = 1
53+
p.Y.Tick.Label.Font.Size = 1
54+
}
55+
56+
plots[j][i] = p
57+
}
58+
}
59+
60+
img := vgimg.New(vg.Points(150), vg.Points(175))
61+
dc := draw.New(img)
62+
63+
t := draw.Tiles{
64+
Rows: rows,
65+
Cols: cols,
66+
PadX: vg.Millimeter,
67+
PadY: vg.Millimeter,
68+
PadTop: vg.Points(2),
69+
PadBottom: vg.Points(2),
70+
PadLeft: vg.Points(2),
71+
PadRight: vg.Points(2),
72+
}
73+
74+
canvases := plot.Align(plots, t, dc)
75+
for j := 0; j < rows; j++ {
76+
for i := 0; i < cols; i++ {
77+
if plots[j][i] != nil {
78+
plots[j][i].Draw(canvases[j][i])
79+
}
80+
}
81+
}
82+
83+
w, err := os.Create("testdata/align.png")
84+
if err != nil {
85+
panic(err)
86+
}
87+
defer w.Close()
88+
png := vgimg.PngCanvas{Canvas: img}
89+
if _, err := png.WriteTo(w); err != nil {
90+
panic(err)
91+
}
92+
}

example_legend_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright ©2018 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 plot_test
6+
7+
import (
8+
"image/color"
9+
"os"
10+
11+
"gonum.org/v1/plot"
12+
"gonum.org/v1/plot/vg"
13+
"gonum.org/v1/plot/vg/draw"
14+
"gonum.org/v1/plot/vg/vgimg"
15+
)
16+
17+
type exampleThumbnailer struct {
18+
color.Color
19+
}
20+
21+
// Thumbnail fulfills the plot.Thumbnailer interface.
22+
func (et exampleThumbnailer) Thumbnail(c *draw.Canvas) {
23+
pts := []vg.Point{
24+
{X: c.Min.X, Y: c.Min.Y},
25+
{X: c.Min.X, Y: c.Max.Y},
26+
{X: c.Max.X, Y: c.Max.Y},
27+
{X: c.Max.X, Y: c.Min.Y},
28+
}
29+
poly := c.ClipPolygonY(pts)
30+
c.FillPolygon(et.Color, poly)
31+
32+
pts = append(pts, vg.Point{X: c.Min.X, Y: c.Min.Y})
33+
outline := c.ClipLinesY(pts)
34+
c.StrokeLines(draw.LineStyle{
35+
Color: color.Black,
36+
Width: vg.Points(1),
37+
}, outline...)
38+
}
39+
40+
// This example creates a some standalone legends with borders around them.
41+
func ExampleLegend_standalone() {
42+
c := vgimg.New(vg.Points(120), vg.Points(100))
43+
dc := draw.New(c)
44+
45+
// These example thumbnailers could be replaced with any of Plotters
46+
// in the plotter subpackage.
47+
red := exampleThumbnailer{Color: color.NRGBA{R: 255, A: 255}}
48+
green := exampleThumbnailer{Color: color.NRGBA{G: 255, A: 255}}
49+
blue := exampleThumbnailer{Color: color.NRGBA{B: 255, A: 255}}
50+
51+
l, err := plot.NewLegend()
52+
if err != nil {
53+
panic(err)
54+
}
55+
l.Add("red", red)
56+
l.Add("green", green)
57+
l.Add("blue", blue)
58+
l.Padding = vg.Millimeter
59+
60+
// purpleRectangle draws a purple rectangle around the given Legend.
61+
purpleRectangle := func(l plot.Legend) {
62+
r := l.Rectangle(dc)
63+
dc.StrokeLines(draw.LineStyle{
64+
Color: color.NRGBA{R: 255, B: 255, A: 255},
65+
Width: vg.Points(1),
66+
}, []vg.Point{
67+
{X: r.Min.X, Y: r.Min.Y}, {X: r.Min.X, Y: r.Max.Y}, {X: r.Max.X, Y: r.Max.Y},
68+
{X: r.Max.X, Y: r.Min.Y}, {X: r.Min.X, Y: r.Min.Y},
69+
})
70+
}
71+
72+
l.Draw(dc)
73+
purpleRectangle(l)
74+
75+
l.Left = true
76+
l.Draw(dc)
77+
purpleRectangle(l)
78+
79+
l.Top = true
80+
l.Draw(dc)
81+
purpleRectangle(l)
82+
83+
l.Left = false
84+
l.Draw(dc)
85+
purpleRectangle(l)
86+
87+
w, err := os.Create("testdata/legend_standalone.png")
88+
if err != nil {
89+
panic(err)
90+
}
91+
defer w.Close()
92+
png := vgimg.PngCanvas{Canvas: c}
93+
if _, err := png.WriteTo(w); err != nil {
94+
panic(err)
95+
}
96+
}

legend_test.go

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -5,98 +5,11 @@
55
package plot_test
66

77
import (
8-
"image/color"
9-
"os"
108
"testing"
119

12-
"gonum.org/v1/plot"
1310
"gonum.org/v1/plot/cmpimg"
14-
"gonum.org/v1/plot/vg"
15-
"gonum.org/v1/plot/vg/draw"
16-
"gonum.org/v1/plot/vg/vgimg"
1711
)
1812

19-
type exampleThumbnailer struct {
20-
color.Color
21-
}
22-
23-
// Thumbnail fulfills the plot.Thumbnailer interface.
24-
func (et exampleThumbnailer) Thumbnail(c *draw.Canvas) {
25-
pts := []vg.Point{
26-
{c.Min.X, c.Min.Y},
27-
{c.Min.X, c.Max.Y},
28-
{c.Max.X, c.Max.Y},
29-
{c.Max.X, c.Min.Y},
30-
}
31-
poly := c.ClipPolygonY(pts)
32-
c.FillPolygon(et.Color, poly)
33-
34-
pts = append(pts, vg.Point{X: c.Min.X, Y: c.Min.Y})
35-
outline := c.ClipLinesY(pts)
36-
c.StrokeLines(draw.LineStyle{
37-
Color: color.Black,
38-
Width: vg.Points(1),
39-
}, outline...)
40-
}
41-
42-
// This example creates a some standalone legends with borders around them.
43-
func ExampleLegend_standalone() {
44-
c := vgimg.New(vg.Points(120), vg.Points(100))
45-
dc := draw.New(c)
46-
47-
// These example thumbnailers could be replaced with any of Plotters
48-
// in the plotter subpackage.
49-
red := exampleThumbnailer{Color: color.NRGBA{R: 255, A: 255}}
50-
green := exampleThumbnailer{Color: color.NRGBA{G: 255, A: 255}}
51-
blue := exampleThumbnailer{Color: color.NRGBA{B: 255, A: 255}}
52-
53-
l, err := plot.NewLegend()
54-
if err != nil {
55-
panic(err)
56-
}
57-
l.Add("red", red)
58-
l.Add("green", green)
59-
l.Add("blue", blue)
60-
l.Padding = vg.Millimeter
61-
62-
// purpleRectangle draws a purple rectangle around the given Legend.
63-
purpleRectangle := func(l plot.Legend) {
64-
r := l.Rectangle(dc)
65-
dc.StrokeLines(draw.LineStyle{
66-
Color: color.NRGBA{R: 255, B: 255, A: 255},
67-
Width: vg.Points(1),
68-
}, []vg.Point{
69-
{r.Min.X, r.Min.Y}, {r.Min.X, r.Max.Y}, {r.Max.X, r.Max.Y},
70-
{r.Max.X, r.Min.Y}, {r.Min.X, r.Min.Y},
71-
})
72-
}
73-
74-
l.Draw(dc)
75-
purpleRectangle(l)
76-
77-
l.Left = true
78-
l.Draw(dc)
79-
purpleRectangle(l)
80-
81-
l.Top = true
82-
l.Draw(dc)
83-
purpleRectangle(l)
84-
85-
l.Left = false
86-
l.Draw(dc)
87-
purpleRectangle(l)
88-
89-
w, err := os.Create("testdata/legend_standalone.png")
90-
if err != nil {
91-
panic(err)
92-
}
93-
94-
png := vgimg.PngCanvas{Canvas: c}
95-
if _, err := png.WriteTo(w); err != nil {
96-
panic(err)
97-
}
98-
}
99-
10013
func TestLegend_standalone(t *testing.T) {
10114
cmpimg.CheckPlot(ExampleLegend_standalone, t, "legend_standalone.png")
10215
}

0 commit comments

Comments
 (0)