forked from nickalie/go-webpbin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcwebp_test.go
87 lines (76 loc) · 1.85 KB
/
cwebp_test.go
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
package webpbin
import (
"image/jpeg"
"os"
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/image/webp"
)
func init() {
DetectUnsupportedPlatforms()
downloadFile("https://upload.wikimedia.org/wikipedia/commons/e/e3/Avola-Syracuse-Sicilia-Italy_-_Creative_Commons_by_gnuckx_%283858115914%29.jpg", "source.jpg")
downloadFile("https://upload.wikimedia.org/wikipedia/commons/d/d1/Snail_in_Forest_on_Lichtscheid_2.webp", "source.webp")
}
func TestEncodeImage(t *testing.T) {
c := NewCWebP()
f, err := os.Open("source.jpg")
assert.Nil(t, err)
img, err := jpeg.Decode(f)
assert.Nil(t, err)
c.InputImage(img)
c.OutputFile("target.webp")
err = c.Run()
assert.Nil(t, err)
validateWebp(t)
}
func TestEncodeReader(t *testing.T) {
c := NewCWebP()
f, err := os.Open("source.jpg")
assert.Nil(t, err)
c.Input(f)
c.OutputFile("target.webp")
err = c.Run()
assert.Nil(t, err)
validateWebp(t)
}
func TestEncodeFile(t *testing.T) {
c := NewCWebP()
c.InputFile("source.jpg")
c.OutputFile("target.webp")
err := c.Run()
assert.Nil(t, err)
validateWebp(t)
}
func TestEncodeWriter(t *testing.T) {
f, err := os.Create("target.webp")
assert.Nil(t, err)
defer f.Close()
c := NewCWebP()
c.InputFile("source.jpg")
c.Output(f)
err = c.Run()
assert.Nil(t, err)
f.Close()
validateWebp(t)
}
func TestVersionCWebP(t *testing.T) {
c := NewCWebP()
r, err := c.Version()
assert.Nil(t, err)
if _, ok := os.LookupEnv("DOCKER_ARM_TEST"); !ok {
assert.Equal(t, "0.6.0", r)
}
}
func validateWebp(t *testing.T) {
defer os.Remove("target.webp")
fSource, err := os.Open("source.jpg")
assert.Nil(t, err)
imgSource, err := jpeg.Decode(fSource)
assert.Nil(t, err)
fTarget, err := os.Open("target.webp")
assert.Nil(t, err)
defer fTarget.Close()
imgTarget, err := webp.Decode(fTarget)
assert.Nil(t, err)
assert.Equal(t, imgSource.Bounds(), imgTarget.Bounds())
}