forked from nickalie/go-webpbin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dwebp_test.go
85 lines (77 loc) · 1.69 KB
/
dwebp_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
package webpbin
import (
"image/png"
"os"
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/image/webp"
)
func TestVersionDWebP(t *testing.T) {
c := NewDWebP()
r, err := c.Version()
assert.Nil(t, err)
if _, ok := os.LookupEnv("DOCKER_ARM_TEST"); !ok {
assert.Equal(t, "0.6.0", r)
}
}
func TestDecodeReader(t *testing.T) {
c := NewDWebP()
f, err := os.Open("source.webp")
assert.Nil(t, err)
defer f.Close()
c.Input(f)
c.OutputFile("target.png")
img, err := c.Run()
assert.Nil(t, err)
assert.Nil(t, img)
validatePng(t)
}
func TestDecodeFile(t *testing.T) {
c := NewDWebP()
c.InputFile("source.webp")
c.OutputFile("target.png")
img, err := c.Run()
assert.Nil(t, err)
assert.Nil(t, img)
validatePng(t)
}
func TestDecodeImage(t *testing.T) {
c := NewDWebP()
f, err := os.Open("source.webp")
assert.Nil(t, err)
defer f.Close()
imgSource, err := webp.Decode(f)
assert.Nil(t, err)
f.Seek(0, 0)
c.Input(f)
imgTarget, err := c.Run()
assert.Nil(t, err)
assert.NotNil(t, imgTarget)
assert.Equal(t, imgSource.Bounds(), imgTarget.Bounds())
}
func TestDecodeWriter(t *testing.T) {
f, err := os.Create("target.png")
assert.Nil(t, err)
defer f.Close()
c := NewDWebP()
c.InputFile("source.webp")
c.Output(f)
img, err := c.Run()
assert.Nil(t, err)
assert.Nil(t, img)
f.Close()
validatePng(t)
}
func validatePng(t *testing.T) {
defer os.Remove("target.png")
fSource, err := os.Open("source.webp")
assert.Nil(t, err)
imgSource, err := webp.Decode(fSource)
assert.Nil(t, err)
fTarget, err := os.Open("target.png")
assert.Nil(t, err)
defer fTarget.Close()
imgTarget, err := png.Decode(fTarget)
assert.Nil(t, err)
assert.Equal(t, imgSource.Bounds(), imgTarget.Bounds())
}