feat(test): better coverage for vips interface

This commit is contained in:
Tomas Aparicio 2015-04-11 14:19:15 +02:00
parent b065e902ed
commit 2bc5756fad
3 changed files with 87 additions and 39 deletions

View file

@ -18,30 +18,18 @@ func TestVipsRead(t *testing.T) {
}
for _, file := range files {
img, _ := os.Open(path.Join("fixtures", file.name))
buf, _ := ioutil.ReadAll(img)
defer img.Close()
image, imageType, _ := vipsRead(buf)
image, imageType, _ := vipsRead(readImage(file.name))
if image == nil {
t.Fatal("Empty image")
}
if imageType != file.expected {
t.Fatal("Empty image")
t.Fatal("Invalid image type")
}
}
}
func TestVipsSave(t *testing.T) {
img, _ := os.Open(path.Join("fixtures", "test.jpg"))
buf, _ := ioutil.ReadAll(img)
defer img.Close()
image, _, _ := vipsRead(buf)
if image == nil {
t.Fatal("Empty image")
}
image, _, _ := vipsRead(readImage("test.jpg"))
options := vipsSaveOptions{Quality: 95, Type: JPEG}
buf, err := vipsSave(image, options)
@ -52,3 +40,80 @@ func TestVipsSave(t *testing.T) {
t.Fatal("Empty image")
}
}
func TestVipsRotate(t *testing.T) {
image, _, _ := vipsRead(readImage("test.jpg"))
newImg, err := vipsRotate(image, D90)
if err != nil {
t.Fatal("Cannot save the image")
}
buf, _ := vipsSave(newImg, vipsSaveOptions{Quality: 95})
if len(buf) == 0 {
t.Fatal("Empty image")
}
}
func TestVipsZoom(t *testing.T) {
image, _, _ := vipsRead(readImage("test.jpg"))
newImg, err := vipsRotate(image, D90)
if err != nil {
t.Fatal("Cannot save the image")
}
buf, _ := vipsSave(newImg, vipsSaveOptions{Quality: 95})
if len(buf) == 0 {
t.Fatal("Empty image")
}
}
func TestVipsWatermark(t *testing.T) {
image, _, _ := vipsRead(readImage("test.jpg"))
watermark := Watermark{
Text: "Copy me if you can",
Font: "sans bold 12",
Opacity: 0.5,
Width: 200,
DPI: 100,
Margin: 100,
Background: Color{255, 255, 255},
}
newImg, err := vipsWatermark(image, watermark)
if err != nil {
t.Errorf("Cannot add watermark: %s", err)
}
buf, _ := vipsSave(newImg, vipsSaveOptions{Quality: 95})
if len(buf) == 0 {
t.Fatal("Empty image")
}
}
func TestVipsImageType(t *testing.T) {
imgType := vipsImageType(readImage("test.jpg"))
if imgType != JPEG {
t.Fatal("Invalid image type")
}
}
func TestVipsMemory(t *testing.T) {
mem := VipsMemory()
if mem.Memory < 1024 {
t.Fatal("Invalid memory")
}
if mem.Allocations == 0 {
t.Fatal("Invalid memory allocations")
}
}
func readImage(file string) []byte {
img, _ := os.Open(path.Join("fixtures", file))
buf, _ := ioutil.ReadAll(img)
defer img.Close()
return buf
}