76 lines
1.8 KiB
Go
76 lines
1.8 KiB
Go
package caption_test
|
|
|
|
import (
|
|
"crypto/sha1"
|
|
"encoding/hex"
|
|
"errors"
|
|
"image"
|
|
"image/color"
|
|
"image/png"
|
|
"testing"
|
|
|
|
"git.gensokyo.uk/yonah/caption"
|
|
"github.com/golang/freetype/truetype"
|
|
)
|
|
|
|
var (
|
|
beforeCheck []func(t *testing.T, name string, got image.Image)
|
|
)
|
|
|
|
func TestRender(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
line *caption.Line
|
|
x, y int
|
|
want string
|
|
wantErr error
|
|
}{
|
|
{"bad font", &caption.Line{Font: []byte{0xfd}},
|
|
0, 0, "", truetype.FormatError("TTF data is too short")},
|
|
{"colors", &caption.Line{Data: []caption.Segment{
|
|
{"Blue", c(0x55, 0xcd, 0xfc)},
|
|
{"Pink", c(0xf7, 0xa8, 0xb8)},
|
|
{"White", color.White},
|
|
{"Pink", c(0xf7, 0xa8, 0xb8)},
|
|
{"Blue", c(0x55, 0xcd, 0xfc)},
|
|
}}, 7 << 6, 1 << 8, "2af4b09361aeb3a9befc79cc0b28dd3874657fa0", nil},
|
|
}
|
|
|
|
var digest []byte
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got := image.NewRGBA64(image.Rectangle{Max: image.Point{X: 1 << 10, Y: 1 << 9}})
|
|
err := tc.line.Render(got, tc.x, tc.y)
|
|
if err != nil {
|
|
if !errors.Is(err, tc.wantErr) {
|
|
t.Errorf("Render: error = %q, want %q",
|
|
err, tc.wantErr)
|
|
}
|
|
return
|
|
}
|
|
|
|
for _, f := range beforeCheck {
|
|
f(t, tc.name, got)
|
|
}
|
|
|
|
t.Run("hash", func(t *testing.T) {
|
|
var wantDigest []byte
|
|
if wantDigest, err = hex.DecodeString(tc.want); err != nil {
|
|
t.Errorf("cannot parse expected hash: %v", err)
|
|
}
|
|
|
|
h := sha1.New()
|
|
if err = (&png.Encoder{CompressionLevel: png.NoCompression}).Encode(h, got); err != nil {
|
|
t.Fatalf("cannot encode png for hashing: %v", err)
|
|
}
|
|
digest := h.Sum(digest[:0])
|
|
if string(digest) != string(wantDigest) {
|
|
t.Errorf("Render: %x, want %x", digest, wantDigest)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
func c(r, g, b uint8) color.Color { return color.NRGBA{R: r, G: g, B: b, A: 0xff} }
|