...

Source file src/gonum.org/v1/plot/vg/recorder/recorder_test.go

Documentation: gonum.org/v1/plot/vg/recorder

     1  // Copyright ©2015 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 recorder
     6  
     7  import (
     8  	"image"
     9  	"image/color"
    10  	"strings"
    11  	"testing"
    12  
    13  	"gonum.org/v1/plot/font"
    14  	"gonum.org/v1/plot/vg"
    15  )
    16  
    17  func TestRecorder(t *testing.T) {
    18  	tr := font.Font{Typeface: "Liberation", Variant: "Serif"}
    19  
    20  	var rec Canvas
    21  	rec.Actions = append(rec.Actions, &FillString{Font: tr, Size: 12, Point: vg.Point{X: 0, Y: 10}, String: "Text"})
    22  	rec.Comment("End of preamble")
    23  	rec.Scale(1, 2)
    24  	rec.Rotate(0.72)
    25  	rec.KeepCaller = true
    26  	rec.Stroke(vg.Path{{Type: vg.MoveComp, Pos: vg.Point{X: 3, Y: 4}}})
    27  	rec.Push()
    28  	rec.Pop()
    29  	rec.Translate(vg.Point{X: 3, Y: 4})
    30  	rec.KeepCaller = false
    31  	rec.SetLineWidth(100)
    32  	rec.SetLineDash([]font.Length{2, 5}, 6)
    33  	rec.SetColor(color.RGBA{R: 0x65, G: 0x23, B: 0xf2})
    34  	rec.Fill(vg.Path{{Type: vg.MoveComp, Pos: vg.Point{X: 3, Y: 4}}, {Type: vg.LineComp, Pos: vg.Point{X: 2, Y: 3}}, {Type: vg.CloseComp}})
    35  	rec.DrawImage(
    36  		vg.Rectangle{
    37  			Min: vg.Point{X: 0, Y: 0},
    38  			Max: vg.Point{X: 10, Y: 10},
    39  		},
    40  		img,
    41  	)
    42  	if len(rec.Actions) != len(want) {
    43  		t.Fatalf("unexpected number of actions recorded: got:%d want:%d", len(rec.Actions), len(want))
    44  	}
    45  	for i, a := range rec.Actions {
    46  		if got := a.Call(); !strings.HasSuffix(got, want[i]) {
    47  			t.Errorf("unexpected action:\n\tgot: %#v\n\twant: %#v", got, want[i])
    48  		}
    49  	}
    50  
    51  	var replay Canvas
    52  	err := rec.ReplayOn(&replay)
    53  	if err != nil {
    54  		t.Errorf("unexpected error: %v", err)
    55  	}
    56  	for i, a := range rec.Actions {
    57  		got := replay.Actions[i].Call()
    58  		want := a.Call()
    59  		if !strings.HasSuffix(want, got) {
    60  			t.Errorf("unexpected action:\n\tgot: %#v\n\twant: %#v", got, want)
    61  		}
    62  	}
    63  
    64  	replay.Reset()
    65  	rec.Actions = append(rec.Actions, &FillString{Font: font.Font{Typeface: "Foo"}, Size: 12, Point: vg.Point{X: 0, Y: 10}, String: "Bar"})
    66  	err = rec.ReplayOn(&replay)
    67  	switch {
    68  	case err == nil:
    69  		t.Errorf("expected an error")
    70  	case !strings.HasPrefix(err.Error(), "unknown font: Foo"):
    71  		t.Errorf("unexpected error: %v", err)
    72  	}
    73  }
    74  
    75  var img image.Image = image.NewGray(image.Rect(0, 0, 20, 20))
    76  
    77  var want = []string{
    78  	`FillString("LiberationSerif-Regular", 12, 0, 10, "Text")`,
    79  	`Comment("End of preamble")`,
    80  	`Scale(1, 2)`,
    81  	`Rotate(0.72)`,
    82  	`gonum.org/v1/plot/vg/recorder/recorder_test.go:26 Stroke(vg.Path{vg.PathComp{Type:0, Pos:vg.Point{X:3, Y:4}, Control:[]vg.Point(nil), Radius:0, Start:0, Angle:0}})`,
    83  	`gonum.org/v1/plot/vg/recorder/recorder_test.go:27 Push()`,
    84  	`gonum.org/v1/plot/vg/recorder/recorder_test.go:28 Pop()`,
    85  	`gonum.org/v1/plot/vg/recorder/recorder_test.go:29 Translate(3, 4)`,
    86  	`SetLineWidth(100)`,
    87  	`SetLineDash([]font.Length{2, 5}, 6)`,
    88  	`SetColor(color.RGBA{R:0x65, G:0x23, B:0xf2, A:0x0})`,
    89  	`Fill(vg.Path{vg.PathComp{Type:0, Pos:vg.Point{X:3, Y:4}, Control:[]vg.Point(nil), Radius:0, Start:0, Angle:0}, vg.PathComp{Type:1, Pos:vg.Point{X:2, Y:3}, Control:[]vg.Point(nil), Radius:0, Start:0, Angle:0}, vg.PathComp{Type:4, Pos:vg.Point{X:0, Y:0}, Control:[]vg.Point(nil), Radius:0, Start:0, Angle:0}})`,
    90  	`DrawImage(vg.Rectangle{Min:vg.Point{X:0, Y:0}, Max:vg.Point{X:10, Y:10}}, {image.Rectangle{Min:image.Point{X:0, Y:0}, Max:image.Point{X:20, Y:20}}, IMAGE:iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAAAAACo4kLRAAAAFElEQVR4nGJiwAJGBQeVICAAAP//JBgAKeMueQ8AAAAASUVORK5CYII=})`,
    91  }
    92  

View as plain text