diff --git a/resize_test.go b/resize_test.go index 4fcc091..cb8dfe3 100644 --- a/resize_test.go +++ b/resize_test.go @@ -20,10 +20,7 @@ func TestResize(t *testing.T) { t.Fatal("Image is not jpeg") } - err = Write("fixtures/test_out.jpg", newImg) - if err != nil { - t.Fatal("Cannot save the image") - } + Write("fixtures/test_out.jpg", newImg) } func TestRotate(t *testing.T) { @@ -39,10 +36,7 @@ func TestRotate(t *testing.T) { t.Fatal("Image is not jpeg") } - err = Write("fixtures/test_rotate_out.jpg", newImg) - if err != nil { - t.Fatal("Cannot save the image") - } + Write("fixtures/test_rotate_out.jpg", newImg) } func TestCorruptedImage(t *testing.T) { @@ -58,10 +52,7 @@ func TestCorruptedImage(t *testing.T) { t.Fatal("Image is not jpeg") } - err = Write("fixtures/test_corrupt_out.jpg", newImg) - if err != nil { - t.Fatal("Cannot save the image") - } + Write("fixtures/test_corrupt_out.jpg", newImg) } func TestInvalidRotate(t *testing.T) { @@ -77,10 +68,7 @@ func TestInvalidRotate(t *testing.T) { t.Fatal("Image is not jpeg") } - err = Write("fixtures/test_invalid_rotate_out.jpg", newImg) - if err != nil { - t.Fatal("Cannot save the image") - } + Write("fixtures/test_invalid_rotate_out.jpg", newImg) } func TestConvert(t *testing.T) { @@ -112,10 +100,7 @@ func TestConvert(t *testing.T) { t.Fatal("Invalid image size") } - err = Write("fixtures/test_out.png", newImg) - if err != nil { - t.Fatal("Cannot save the image") - } + Write("fixtures/test_out.png", newImg) } func TestResizePngWithTransparency(t *testing.T) { @@ -147,10 +132,7 @@ func TestResizePngWithTransparency(t *testing.T) { t.Fatal("Invalid image size") } - err = Write("fixtures/transparent_out.png", newImg) - if err != nil { - t.Fatal("Cannot save the image") - } + Write("fixtures/transparent_out.png", newImg) } func runBenchmarkResize(file string, o Options, b *testing.B) { diff --git a/vips.h b/vips.h index 2537cae..23b9613 100644 --- a/vips.h +++ b/vips.h @@ -243,3 +243,4 @@ vips_watermark(VipsImage *in, VipsImage **out, watermarkTextOptions *to, waterma g_object_unref(base); return 0; }; + diff --git a/vips_test.go b/vips_test.go index 533e810..5eab2c3 100644 --- a/vips_test.go +++ b/vips_test.go @@ -18,37 +18,102 @@ 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(readImage("test.jpg")) + options := vipsSaveOptions{Quality: 95, Type: JPEG} - image, _, _ := vipsRead(buf) - if image == nil { + buf, err := vipsSave(image, options) + if err != nil { + t.Fatal("Cannot save the image") + } + if len(buf) == 0 { t.Fatal("Empty image") } +} - options := vipsSaveOptions{Quality: 95, Type: JPEG} +func TestVipsRotate(t *testing.T) { + image, _, _ := vipsRead(readImage("test.jpg")) - buf, err := vipsSave(image, options) + 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 +}