mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-03-15 18:35:54 -07:00
feat(test): better coverage for vips interface
This commit is contained in:
parent
b065e902ed
commit
2bc5756fad
3 changed files with 87 additions and 39 deletions
|
|
@ -20,10 +20,7 @@ func TestResize(t *testing.T) {
|
||||||
t.Fatal("Image is not jpeg")
|
t.Fatal("Image is not jpeg")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = Write("fixtures/test_out.jpg", newImg)
|
Write("fixtures/test_out.jpg", newImg)
|
||||||
if err != nil {
|
|
||||||
t.Fatal("Cannot save the image")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRotate(t *testing.T) {
|
func TestRotate(t *testing.T) {
|
||||||
|
|
@ -39,10 +36,7 @@ func TestRotate(t *testing.T) {
|
||||||
t.Fatal("Image is not jpeg")
|
t.Fatal("Image is not jpeg")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = Write("fixtures/test_rotate_out.jpg", newImg)
|
Write("fixtures/test_rotate_out.jpg", newImg)
|
||||||
if err != nil {
|
|
||||||
t.Fatal("Cannot save the image")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCorruptedImage(t *testing.T) {
|
func TestCorruptedImage(t *testing.T) {
|
||||||
|
|
@ -58,10 +52,7 @@ func TestCorruptedImage(t *testing.T) {
|
||||||
t.Fatal("Image is not jpeg")
|
t.Fatal("Image is not jpeg")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = Write("fixtures/test_corrupt_out.jpg", newImg)
|
Write("fixtures/test_corrupt_out.jpg", newImg)
|
||||||
if err != nil {
|
|
||||||
t.Fatal("Cannot save the image")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInvalidRotate(t *testing.T) {
|
func TestInvalidRotate(t *testing.T) {
|
||||||
|
|
@ -77,10 +68,7 @@ func TestInvalidRotate(t *testing.T) {
|
||||||
t.Fatal("Image is not jpeg")
|
t.Fatal("Image is not jpeg")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = Write("fixtures/test_invalid_rotate_out.jpg", newImg)
|
Write("fixtures/test_invalid_rotate_out.jpg", newImg)
|
||||||
if err != nil {
|
|
||||||
t.Fatal("Cannot save the image")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConvert(t *testing.T) {
|
func TestConvert(t *testing.T) {
|
||||||
|
|
@ -112,10 +100,7 @@ func TestConvert(t *testing.T) {
|
||||||
t.Fatal("Invalid image size")
|
t.Fatal("Invalid image size")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = Write("fixtures/test_out.png", newImg)
|
Write("fixtures/test_out.png", newImg)
|
||||||
if err != nil {
|
|
||||||
t.Fatal("Cannot save the image")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResizePngWithTransparency(t *testing.T) {
|
func TestResizePngWithTransparency(t *testing.T) {
|
||||||
|
|
@ -147,10 +132,7 @@ func TestResizePngWithTransparency(t *testing.T) {
|
||||||
t.Fatal("Invalid image size")
|
t.Fatal("Invalid image size")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = Write("fixtures/transparent_out.png", newImg)
|
Write("fixtures/transparent_out.png", newImg)
|
||||||
if err != nil {
|
|
||||||
t.Fatal("Cannot save the image")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func runBenchmarkResize(file string, o Options, b *testing.B) {
|
func runBenchmarkResize(file string, o Options, b *testing.B) {
|
||||||
|
|
|
||||||
1
vips.h
1
vips.h
|
|
@ -243,3 +243,4 @@ vips_watermark(VipsImage *in, VipsImage **out, watermarkTextOptions *to, waterma
|
||||||
g_object_unref(base);
|
g_object_unref(base);
|
||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
95
vips_test.go
95
vips_test.go
|
|
@ -18,30 +18,18 @@ func TestVipsRead(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
img, _ := os.Open(path.Join("fixtures", file.name))
|
image, imageType, _ := vipsRead(readImage(file.name))
|
||||||
buf, _ := ioutil.ReadAll(img)
|
|
||||||
defer img.Close()
|
|
||||||
|
|
||||||
image, imageType, _ := vipsRead(buf)
|
|
||||||
if image == nil {
|
if image == nil {
|
||||||
t.Fatal("Empty image")
|
t.Fatal("Empty image")
|
||||||
}
|
}
|
||||||
if imageType != file.expected {
|
if imageType != file.expected {
|
||||||
t.Fatal("Empty image")
|
t.Fatal("Invalid image type")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestVipsSave(t *testing.T) {
|
func TestVipsSave(t *testing.T) {
|
||||||
img, _ := os.Open(path.Join("fixtures", "test.jpg"))
|
image, _, _ := vipsRead(readImage("test.jpg"))
|
||||||
buf, _ := ioutil.ReadAll(img)
|
|
||||||
defer img.Close()
|
|
||||||
|
|
||||||
image, _, _ := vipsRead(buf)
|
|
||||||
if image == nil {
|
|
||||||
t.Fatal("Empty image")
|
|
||||||
}
|
|
||||||
|
|
||||||
options := vipsSaveOptions{Quality: 95, Type: JPEG}
|
options := vipsSaveOptions{Quality: 95, Type: JPEG}
|
||||||
|
|
||||||
buf, err := vipsSave(image, options)
|
buf, err := vipsSave(image, options)
|
||||||
|
|
@ -52,3 +40,80 @@ func TestVipsSave(t *testing.T) {
|
||||||
t.Fatal("Empty image")
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue