-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgoshot_test.go
More file actions
112 lines (98 loc) · 2.83 KB
/
Copy pathgoshot_test.go
File metadata and controls
112 lines (98 loc) · 2.83 KB
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
package goshot_test
import (
"image"
"image/color"
"strings"
"testing"
"github.com/watzon/goshot"
"github.com/watzon/goshot/background"
"github.com/watzon/goshot/chrome"
"github.com/watzon/goshot/code"
"github.com/watzon/goshot/term"
)
// The fake key is assembled at runtime so secret scanners don't flag
// this file; it still matches the redaction patterns when rendered.
var sample = `package main
import "fmt"
func main() {
apiKey := "` + "sk_live_" + strings.Repeat("x4", 16) + `"
fmt.Println("hello", apiKey)
}
`
func TestFullPipeline(t *testing.T) {
img, err := goshot.New().
WithContent(code.New(sample).WithLanguage("go").WithMinWidth(400)).
WithChrome(chrome.Mac().WithTitle("main.go").Dark()).
WithBackground(background.Solid(color.RGBA{40, 42, 54, 255}).WithPadding(40).WithCornerRadius(8)).
Image()
if err != nil {
t.Fatal(err)
}
b := img.Bounds()
// content >= 400 wide, plus 2x40 padding
if b.Dx() < 480 {
t.Errorf("image too narrow: %d", b.Dx())
}
if b.Dy() < 100 {
t.Errorf("image too short: %d", b.Dy())
}
}
func TestEmptyCanvas(t *testing.T) {
if _, err := goshot.New().Image(); err == nil {
t.Fatal("expected error for empty canvas")
}
}
func TestGradientAndShadow(t *testing.T) {
content := imageContent{image.NewRGBA(image.Rect(0, 0, 100, 50))}
bg := background.Gradient(background.LinearGradient,
background.Stop{Color: color.RGBA{255, 0, 0, 255}, Position: 0},
background.Stop{Color: color.RGBA{0, 0, 255, 255}, Position: 1},
).WithAngle(45).WithPadding(30).WithShadow(background.NewShadow())
img, err := goshot.New().WithContent(content).WithBackground(bg).Image()
if err != nil {
t.Fatal(err)
}
if img.Bounds().Dx() <= 160 { // 100 + 2x30 padding + shadow margin
t.Errorf("shadow margin missing: width %d", img.Bounds().Dx())
}
}
func TestTerminalRender(t *testing.T) {
out := []byte("\x1b[1;32mPASS\x1b[0m ok\n\x1b[31mred\x1b[0m\n")
img, err := term.New(out).WithTheme("dracula").WithAutoSize().Render()
if err != nil {
t.Fatal(err)
}
if img.Bounds().Empty() {
t.Fatal("empty terminal image")
}
}
func TestRedactionBlock(t *testing.T) {
img, err := code.New(sample).
WithLanguage("go").
WithRedaction(code.NewRedaction()).
Render()
if err != nil {
t.Fatal(err)
}
if img.Bounds().Empty() {
t.Fatal("empty image")
}
}
func TestChromeVariants(t *testing.T) {
content := image.NewRGBA(image.Rect(0, 0, 300, 100))
for _, name := range chrome.Names() {
c, ok := chrome.Named(name)
if !ok {
t.Fatalf("chrome %q not found", name)
}
img, err := c.WithTitle("title").Render(content)
if err != nil {
t.Fatalf("%s: %v", name, err)
}
if img.Bounds().Dy() < 100 {
t.Errorf("%s: missing title bar, height %d", name, img.Bounds().Dy())
}
}
}
type imageContent struct{ img image.Image }
func (c imageContent) Render() (image.Image, error) { return c.img, nil }