forked from ariefsuharsono/imaginary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_test.go
73 lines (64 loc) · 1.64 KB
/
image_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
package main
import (
"io/ioutil"
"testing"
)
func TestImageResize(t *testing.T) {
opts := ImageOptions{Width: 300, Height: 300}
buf, _ := ioutil.ReadAll(readFile("imaginary.jpg"))
img, err := Resize(buf, opts)
if err != nil {
t.Errorf("Cannot process image: %s", err)
}
if img.Mime != "image/jpeg" {
t.Error("Invalid image MIME type")
}
if assertSize(img.Body, opts.Width, opts.Height) != nil {
t.Errorf("Invalid image size, expected: %dx%d", opts.Width, opts.Height)
}
}
func TestImageFit(t *testing.T) {
opts := ImageOptions{Width: 300, Height: 300}
buf, _ := ioutil.ReadAll(readFile("imaginary.jpg"))
img, err := Fit(buf, opts)
if err != nil {
t.Errorf("Cannot process image: %s", err)
}
if img.Mime != "image/jpeg" {
t.Error("Invalid image MIME type")
}
// 550x740 -> 222x300
if assertSize(img.Body, 222, 300) != nil {
t.Errorf("Invalid image size, expected: %dx%d", opts.Width, opts.Height)
}
}
func TestImagePipelineOperations(t *testing.T) {
width, height := 300, 260
operations := PipelineOperations{
PipelineOperation{
Name: "crop",
Params: map[string]interface{}{
"width": width,
"height": height,
},
},
PipelineOperation{
Name: "convert",
Params: map[string]interface{}{
"type": "webp",
},
},
}
opts := ImageOptions{Operations: operations}
buf, _ := ioutil.ReadAll(readFile("imaginary.jpg"))
img, err := Pipeline(buf, opts)
if err != nil {
t.Errorf("Cannot process image: %s", err)
}
if img.Mime != "image/webp" {
t.Error("Invalid image MIME type")
}
if assertSize(img.Body, width, height) != nil {
t.Errorf("Invalid image size, expected: %dx%d", width, height)
}
}